-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgym.py
74 lines (57 loc) · 1.92 KB
/
gym.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
import gymnasium as gym
def select_action(action_selection_code):
if action_selection_code == 0:
action = env.action_space.sample() #random action
elif action_selection_code == 1:
action = 2 #push to the right
elif action_selection_code == 2:
#two rules
if observation[0] > -0.53 and observation[1] > 0:
action = 2
elif observation[0] < -0.53 and observation[1] < 0:
action = 0
else:
action = 1
elif action_selection_code == 3:
#four rules
if observation[0] > -0.53 and observation[1] > 0:
action = 2
elif observation[0] < -0.53 and observation[1] < 0:
action = 0
elif observation[0] < -0.53 and observation[1] > 0:
action = 2
elif observation[0] > -0.53 and observation[1] < 0:
action = 0
else:
action = 1
else:
action = 1
return action
print("Starting Mountain Car")
# env = gym.make("MountainCar-v0")
env = gym.make("MountainCar-v0", render_mode="human")
observation, info = env.reset()
iter = 200
agents = 100
approaches = 4
steps = np.zeros((agents, approaches))
for k in range(approaches):
for j in range(agents):
for i in range(iter):
action = select_action(k) #0: random action, 1: push to the right, 2: two rules, 3: four rules
observation, reward, terminated, truncated, info = env.step(action)
print(observation)
if terminated or truncated:
observation, info = env.reset()
break
print("Finished in %i steps"%i)
steps[j][k] = i
bp = plt.boxplot(steps, showmeans=True)
plt.title("Number of steps needed")
plt.xlabel("Method")
plt.ylabel("Actions")
plt.xticks(np.arange(1,5), ('Random', 'Always right', 'Two rules', 'Four rules'))
plt.show()
print("Closing environment")
env.close()