-
Notifications
You must be signed in to change notification settings - Fork 0
/
MountainCar_q1Design.py
44 lines (37 loc) · 1.04 KB
/
MountainCar_q1Design.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
# Experiment on linear model with policy to maximize momentum
from matplotlib import pyplot as plt
import gym
import numpy as np
from keras import Sequential
from keras.layers import Dense
env = gym.make('MountainCar-v0')
ns = env.observation_space.shape[0]
na = env.action_space.n
model = Sequential([
Dense(na, input_shape=(ns,))
])
model.set_weights([np.array([[0, 0, 0], [1, 2, 3]]), np.array([0, 0, 0])])
test_size = 200
rewards = 0
x = []
y = []
for _ in range(test_size):
s = env.reset()
x.append(s[0])
i = 0
while True:
# env.render()
q_values = model.predict(np.array([s]))
s, r, done, _ = env.step(np.argmax(q_values == q_values.max()))
rewards += r
i += 1
if done:
print("solved for {} iterations".format(i))
break
y.append(-i)
print("The average reward of {} episodes is {}".format(test_size, rewards / test_size))
plt.title("MountainCar Designed Linear Model")
plt.ylabel("Episode reward")
plt.xlabel("Initial location")
plt.scatter(x, y)
plt.show()