Background:
In the study of teacher Guo Xian's reinforcement learning in simple terms: introduction to principles, in the last step of writing the environment for robots to find gold coins, there was such a problem: AttributeError: 'GridEnv' object has no attribute 'unwrapped'
The following are error reports:
1 Traceback (most recent call last): 2 File "Z:\DQN\02\ending.py", line 7, in <module> 3 env = gym.make('GridWorld-v0') 4 File "Z:\Anaconda3\envs\my_env\lib\site-packages\gym\envs\registration.py", line 235, in make 5 return registry.make(id, **kwargs) 6 File "Z:\Anaconda3\envs\my_env\lib\site-packages\gym\envs\registration.py", line 129, in make 7 env = spec.make(**kwargs) 8 File "Z:\Anaconda3\envs\my_env\lib\site-packages\gym\envs\registration.py", line 95, in make 9 env.unwrapped.spec = spec 10 AttributeError: 'GridEnv' object has no attribute 'unwrapped' 11 12 Process finished with exit code 1
How to solve:
Let's first talk about how I solved it, and then talk about the way to solve it
The problem is
1 class GridEnv:
At the definition of this class, change it to
1 class GridEnv(gym.Env):
That's it
Idea:
First of all, there is no object 'unwrapped' in the 'GridEnv' class. python here has not been used for a long time and many grammars are forgotten.
The first reaction to seeing this problem is to look at the code of the error location, registration Line 95 of Py defines the make function (line 22 here)
1 def make(self, **kwargs): 2 """Instantiates an instance of the environment with appropriate kwargs""" 3 if self.entry_point is None: 4 raise error.Error( 5 "Attempting to make deprecated env {}. (HINT: is there a newer registered version of this env?)".format( 6 self.id 7 ) 8 ) 9 10 _kwargs = self._kwargs.copy() 11 _kwargs.update(kwargs) 12 13 if callable(self.entry_point): 14 env = self.entry_point(**_kwargs) 15 else: 16 cls = load(self.entry_point) 17 env = cls(**_kwargs) 18 19 # Make the environment aware of which spec it came from. 20 spec = copy.deepcopy(self) 21 spec._kwargs = _kwargs 22 env.unwrapped.spec = spec#This is line 95 23 if env.spec.max_episode_steps is not None: 24 from gym.wrappers.time_limit import TimeLimit 25 26 env = TimeLimit(env, max_episode_steps=env.spec.max_episode_steps) 27 else: 28 if self.order_enforce: 29 from gym.wrappers.order_enforcing import OrderEnforcing 30 31 env = OrderEnforcing(env) 32 return env
From the top to the bottom, you can see that this is the first time that the make function calls env Unwrapped, which is the first time the whole program calls env. So the question is, does env not have this object or does it have other problems? So I went to see the definition of Env:
1 @property 2 def unwrapped(self): 3 return self.env.unwrapped
In more than 300 lines of code, I found this. env has the unwrapped object, so it's not this problem. Where can I find other problems? At an impasse.
An idea, there are other sample code, go to see how other environments are written, don't you know? Then I found this
1 class CartPoleEnv(gym.Env):
It turned out that my definition was wrong. After slapping myself hard, I ended the debug. Although there were some small bugs, they were solved quickly.
Postscript:
In addition to this bug, there are some problems in configuring the environment, but after searching enough web pages, we finally found a solution. The main summary is as follows:
1. The reference materials in the book (GitHub) must be read, and you will soon know the problem.
2. All steps should follow the rules. It's best not to skip steps or watch them in series with other tutorials. Sooner or later, there will be problems.
3. There are plenty of Internet solution resources. You can try to find the answer on StackOverflow (a very important inspiration is from StackOverflow)
After looking at the internal implementation of some things in gym, I feel that my skills have increased greatly. You can also try the internal implementation of some things next time.