# AttributeError: 'EnvContext' object has no attribute is\_directed

**URL:** <https://discuss.ray.io/t/attributeerror-envcontext-object-has-no-attribute-is-directed/8829>\
**Category:** RLlib\
**Created:** [December 31, 2022, 11:24pm UTC](https://discuss.ray.io/t/attributeerror-envcontext-object-has-no-attribute-is-directed/8829 "2022-12-31T23:24:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![IncompleteOmega](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/incompleteomega/32/3636_2.png) [@IncompleteOmega](https://discuss.ray.io/u/IncompleteOmega)\
**Post date:** [December 31, 2022, 11:24pm UTC](https://discuss.ray.io/t/attributeerror-envcontext-object-has-no-attribute-is-directed/8829/1 "2022-12-31T23:24:56Z")

</div>

The context for this post is the same as this one: [https://discuss.ray.io/t/registering-custom-env-that-passes-an-argument-in-1-13-0/8783](https://discuss.ray.io/t/registering-custom-env-that-passes-an-argument-in-1-13-0/8783). The code has been modified to look like the following:

```auto
def env_creator(env_config):
    return ind_set(env_config)

tune.register_env("myenv", env_creator)

G = nx.dodecahedral_graph()

if __name__ == " __main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--num-workers", default=6, type=int)
    parser.add_argument("--training-iteration", default=100, type=int) # change depending on graph size
    parser.add_argument("--ray-num-cpus", default=7, type=int)
    args = parser.parse_args()
    ray.init(num_cpus=args.ray_num_cpus)

    ModelCatalog.register_custom_model("dense_model", DenseModel)

    tune.run(
        "contrib/AlphaZero",
        stop={"training_iteration": args.training_iteration},
        max_failures=0,
        config={
            "env": "myenv",
            "env_config": {"graph": G},
            "num_workers": args.num_workers,
            "rollout_fragment_length": 10,
            "train_batch_size": 50,
            "sgd_minibatch_size": 8,
            "lr": 1e-4,
            "num_sgd_iter": 1,
            "mcts_config": {
                "puct_coefficient": 1.5,
                "num_simulations": 5,
                "temperature": 1.0,
                "dirichlet_epsilon": 0.20,
                "dirichlet_noise": 0.03,
                "argmax_tree_policy": False,
                "add_dirichlet_noise": True,
            },
            "ranked_rewards": {
                "enable": True,
            },
            "model": {
                "custom_model": "dense_model",
            },
        },
    )

```

Now, I get the error

```auto
(RolloutWorker pid=8796) File "/home/IncompleteOmega/.local/lib/python3.8/site-packages/networkx/utils/decorators.py", line 86, in _not_implemented_for
(RolloutWorker pid=8796) dval is None or dval == g.is_directed()
(RolloutWorker pid=8796) AttributeError: 'EnvContext' object has no attribute 'is_directed'

```

Note that I am using the networkx library (imported as nx). I suspect this is an issue with the way I have written the code and not the networkx library because of two reasons:

1. My Networkx Module is updated to the newest version
2. When I separately run `G = nx.dodecahedral_graph()` and then ask `G.is_directed()`, the result is the expected `False` – so this is functional in the correct way.

It would be great if someone could let me know how I can modify my code to get around this.

---

<div class="post-metadata">

**Author:** ![mannyv](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mannyv/32/606_2.png) [@mannyv](https://discuss.ray.io/u/mannyv)\
**Post date:** [January 1, 2023, 1:59pm UTC](https://discuss.ray.io/t/attributeerror-envcontext-object-has-no-attribute-is-directed/8829/2 "2023-01-01T13:59:27Z")

</div>

Hi @IncompleteOmega,

Just a quick check on this line,

```auto
return ind_set(env_config)

```

The env\_config is an EnvContext object and is a subclass for dict.

Somewhere in ind\_set you are pulling out the graph right?

```auto
graph_variable =env_config["graph"]

```

Alternatively if you do not want to pass in the EnvContext object you could call ind\_set like this.

```auto
return ind_set(env_config["graph"]) 

```

---

<div class="post-metadata">

**Author:** ![IncompleteOmega](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/incompleteomega/32/3636_2.png) [@IncompleteOmega](https://discuss.ray.io/u/IncompleteOmega)\
**Post date:** [January 2, 2023, 2:26am UTC](https://discuss.ray.io/t/attributeerror-envcontext-object-has-no-attribute-is-directed/8829/3 "2023-01-02T02:26:12Z")

</div>

Yes in the **init** function of ind\_set, an argument taken as input is graph.

---

<div class="post-metadata">

**Author:** ![IncompleteOmega](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/incompleteomega/32/3636_2.png) [@IncompleteOmega](https://discuss.ray.io/u/IncompleteOmega)\
**Post date:** [January 2, 2023, 4:13am UTC](https://discuss.ray.io/t/attributeerror-envcontext-object-has-no-attribute-is-directed/8829/4 "2023-01-02T04:13:29Z")

</div>

I believe it might be some sort of type error. When I changed the syntax to `return ind_set(env_config["graph"])`, I get a type error with the environment (the environment is fully functional though). The graph is supposed to be of the type `nx.Graph()`. Is there an issue with assigning `"env_config": {"graph": G}` because of this? Is it interpreting G as something else/can it not understand the `nx.Graph()` type?

---

<div class="post-metadata">

**Author:** ![mannyv](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mannyv/32/606_2.png) [@mannyv](https://discuss.ray.io/u/mannyv)\
**Post date:** [January 2, 2023, 3:05pm UTC](https://discuss.ray.io/t/attributeerror-envcontext-object-has-no-attribute-is-directed/8829/5 "2023-01-02T15:05:12Z")

</div>

@IncompleteOmega,

Hard to know what is going on without the full error.

---

<div class="post-metadata">

**Author:** ![IncompleteOmega](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/incompleteomega/32/3636_2.png) [@IncompleteOmega](https://discuss.ray.io/u/IncompleteOmega)\
**Post date:** [January 2, 2023, 7:40pm UTC](https://discuss.ray.io/t/attributeerror-envcontext-object-has-no-attribute-is-directed/8829/6 "2023-01-02T19:40:52Z")

</div>

The exact error for when I edit my code to use your version is the following:

```auto
ValueError: The observation collected from env.reset() was not contained within your env's observation space. Its possible that There was a type mismatch, or that one of the sub-observations was out of bounds: 

 reset_obs: None

 env.observation_space: Discrete(20)

 reset_obs's dtype: <class 'NoneType'>

 env.observation_space's dtype: int64

```

The observation space of my environment is a discrete object with size as the number of vertices in the graph. For the dodecahedral\_graph I am using, this is 20. This is because the observation of a state is the vertices that are currently selected.

---

<div class="post-metadata">

**Author:** ![mannyv](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mannyv/32/606_2.png) [@mannyv](https://discuss.ray.io/u/mannyv)\
**Post date:** [January 2, 2023, 7:44pm UTC](https://discuss.ray.io/t/attributeerror-envcontext-object-has-no-attribute-is-directed/8829/7 "2023-01-02T19:44:10Z")

</div>

That error is saying that you either did not return a value in reset or the value you returned is None.

---

<div class="post-metadata">

**Author:** ![IncompleteOmega](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/incompleteomega/32/3636_2.png) [@IncompleteOmega](https://discuss.ray.io/u/IncompleteOmega)\
**Post date:** [January 2, 2023, 7:50pm UTC](https://discuss.ray.io/t/attributeerror-envcontext-object-has-no-attribute-is-directed/8829/8 "2023-01-02T19:50:15Z")

</div>

Yeah, I’m setting `self.state = None` in the reset function. Oops. Let me fix this issue. Thanks for your help.
