-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathmy_random_agent.py
54 lines (39 loc) · 1.46 KB
/
my_random_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import argparse
import gymnasium as gym
import numpy as np
## random agent
class RandomAgent(object):
def __init__(self, action_space):
self.action_space = action_space
def act(self, observation, reward, done):
return self.action_space.sample()
class BiasedAgent(object):
def __init__(self, action_space):
self.action_space = action_space
self.action_always = self.action_space.sample()
def act(self, observation, reward, done):
return self.action_always
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--display', action='store_true')
parser.add_argument('game', nargs="?", default="CartPole-v0")
args = parser.parse_args()
env = gym.make(args.game, render_mode='human')
num_episodes = 20
num_maxstep = 100
agent_id = 1
if agent_id == 1:
agent = RandomAgent(env.action_space)
elif agent_id == 2:
agent = BiasedAgent(env.action_space)
reward = 0
done = False
for i_episode in range(num_episodes):
observation, _ = env.reset()
for t in range(num_maxstep):
env.render()
action = agent.act(observation, reward, done)
observation, reward, terminated, truncated, info = env.step(action)
done = np.logical_or(terminated, truncated)
print('episode {}-step {}, taking action {}, observation {}'.format(i_episode, t, action, observation))
env.close()