Play with the environment and visualize the agent behaviour
import gymnasium as gym
render = True # switch if visualize the agent
if render:
env = gym.make('CartPole-v0', render_mode='human')
else:
env = gym.make('CartPole-v0')
env.reset(seed=0)
for _ in range(1000):
env.step(env.action_space.sample()) # take a random action
env.close()
Random play with CartPole-v0
import gymnasium as gym
env = gym.make('CartPole-v0')
for i_episode in range(20):
observation = env.reset()
for t in range(100):
print(observation)
action = env.action_space.sample()
observation, reward, terminated, truncated, info = env.step(action)
done = np.logical_or(terminated, truncated)
env.close()
Example code for random playing (Pong-ram-v0,Acrobot-v1,Breakout-v0)
python my_random_agent.py Pong-ram-v0
Very naive learnable agent playing CartPole-v0 or Acrobot-v1
python my_learning_agent.py CartPole-v0
Playing Pong on CPU (with a great blog). One pretrained model is pong_model_bolei.p(after training 20,000 episodes), which you can load in by replacing save_file in the script.
In line 79, sum([p*(r + prev_v[s_]) lack the gamma (the gamma=1.0 is not affected the result). The right code is sum([p*(r + gamma*prev_v[s_]) in line 70.
Thanks.
Below code in def _draw_grid gives an error (run by python 3.6)
ValueError: Missing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric data
ConversionError: Failed to convert value(s) to axis units: '0'
self.q_texts = [self.ax.text( '0',*self._id_to_position(i)[::-1],
fontsize=11, verticalalignment='center',
horizontalalignment='center') for i in range(12 * 4)]
switch position and '0' could work. Could you please check and correct it?
self.q_texts = [self.ax.text(*self._id_to_position(i)[::-1], '0',
fontsize=11, verticalalignment='center',
horizontalalignment='center') for i in range(12 * 4)]
I have some question about the code in frozenlake_policy_iteration.py. Why is the expression of the value fuction in compute_policy_v (line 52) same as the state-action function in compute_policy_v (line37) ?
And why is the expression of the value function v[s] = sum([p * (r + gamma * prev_v[s_]) for p, s_, r, _ in env.env.P[s][policy_a]]) different from the formula(17) in the slide? It seems that the expression in the code ignore the transition probability P(s'|s,a)?
Hi, I assumed there are some errors with the above two algorithms codes. Basically, they are similar.
In both of them, Professor used "args.batch_size" to update model params every batch_size episodes, this corresponds to what was presented in professor's lecture slide 5. But in the defined function: finish_episode(), G is calculated for every single episode, I guess you might forget to separate rewards in each episode since you also commented in the ac-pong codes and flatten rewards and values you defined for calculation.
If the model is updated every batch_size time, then policy.rewards should append a [] for every episode separately. Hope my understanding is correct.
I am trying to use the code as an example. Well, it is a little bit strange, when I changed the frozen lake env to the deterministic version, e.g. env = gym.make("FrozenLake=v0, is_slippery=False), I found the policy iteration algorithm can't work correctly. I checked the code, it seems nothing wrong. One of the reason might be the insufficient exploration of the agent, however, the env is simple enough and the default iteration numbers are set to 200000. But the problem still can't be solved.