-
Notifications
You must be signed in to change notification settings - Fork 0
/
MountainCarV2.py
187 lines (129 loc) · 5.68 KB
/
MountainCarV2.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import gym
from keras import models
from keras import layers
from keras.optimizers import Adam
from collections import deque
import random
import numpy as np
#############################
#if you want to use GPU to boost, use these code.
# import tensorflow as tf
# import keras
# config = tf.ConfigProto( device_count = {'GPU': 2 , 'CPU': 1} )
# sess = tf.Session(config=config)
# keras.backend.set_session(sess)
#############################
class MountainCarTrain:
def __init__(self,env):
self.env=env
self.gamma=0.99
self.epsilon = 1
self.epsilon_decay = 0.05
self.epsilon_min=0.01
self.learingRate=0.001
self.replayBuffer=deque(maxlen=20000)
self.trainNetwork=self.createNetwork()
self.episodeNum=400
self.iterationNum=201 #max is 200
self.numPickFromBuffer=32
self.targetNetwork=self.createNetwork()
self.targetNetwork.set_weights(self.trainNetwork.get_weights())
def createNetwork(self):
model = models.Sequential()
state_shape = self.env.observation_space.shape
model.add(layers.Dense(24, activation='relu', input_shape=state_shape))
model.add(layers.Dense(48, activation='relu'))
model.add(layers.Dense(self.env.action_space.n,activation='linear'))
# model.compile(optimizer=optimizers.RMSprop(lr=self.learingRate), loss=losses.mean_squared_error)
model.compile(loss='mse', optimizer=Adam(lr=self.learingRate))
return model
def getBestAction(self,state):
self.epsilon = max(self.epsilon_min, self.epsilon)
if np.random.rand(1) < self.epsilon:
action = np.random.randint(0, 3)
else:
action=np.argmax(self.trainNetwork.predict(state)[0])
return action
def trainFromBuffer_Boost(self):
if len(self.replayBuffer) < self.numPickFromBuffer:
return
samples = random.sample(self.replayBuffer,self.numPickFromBuffer)
npsamples = np.array(samples)
states_temp, actions_temp, rewards_temp, newstates_temp, dones_temp = np.hsplit(npsamples, 5)
states = np.concatenate((np.squeeze(states_temp[:])), axis = 0)
rewards = rewards_temp.reshape(self.numPickFromBuffer,).astype(float)
targets = self.trainNetwork.predict(states)
newstates = np.concatenate(np.concatenate(newstates_temp))
dones = np.concatenate(dones_temp).astype(bool)
notdones = ~dones
notdones = notdones.astype(float)
dones = dones.astype(float)
Q_futures = self.targetNetwork.predict(newstates).max(axis = 1)
targets[(np.arange(self.numPickFromBuffer), actions_temp.reshape(self.numPickFromBuffer,).astype(int))] = rewards * dones + (rewards + Q_futures * self.gamma)*notdones
self.trainNetwork.fit(states, targets, epochs=1, verbose=0)
def trainFromBuffer(self):
if len(self.replayBuffer) < self.numPickFromBuffer:
return
samples = random.sample(self.replayBuffer,self.numPickFromBuffer)
states = []
newStates=[]
for sample in samples:
state, action, reward, new_state, done = sample
states.append(state)
newStates.append(new_state)
newArray = np.array(states)
states = newArray.reshape(self.numPickFromBuffer, 2)
newArray2 = np.array(newStates)
newStates = newArray2.reshape(self.numPickFromBuffer, 2)
targets = self.trainNetwork.predict(states)
new_state_targets=self.targetNetwork.predict(newStates)
i=0
for sample in samples:
state, action, reward, new_state, done = sample
target = targets[i]
if done:
target[action] = reward
else:
Q_future = max(new_state_targets[i])
target[action] = reward + Q_future * self.gamma
i+=1
self.trainNetwork.fit(states, targets, epochs=1, verbose=0)
def orginalTry(self,currentState,eps):
rewardSum = 0
max_position=-99
for i in range(self.iterationNum):
bestAction = self.getBestAction(currentState)
#show the animation every 50 eps
if eps%50==0:
env.render()
new_state, reward, done, _ = env.step(bestAction)
new_state = new_state.reshape(1, 2)
# # Keep track of max position
if new_state[0][0] > max_position:
max_position = new_state[0][0]
# # Adjust reward for task completion
if new_state[0][0] >= 0.5:
reward += 10
self.replayBuffer.append([currentState, bestAction, reward, new_state, done])
#Or you can use self.trainFromBuffer_Boost(), it is a matrix wise version for boosting
self.trainFromBuffer()
rewardSum += reward
currentState = new_state
if done:
break
if i >= 199:
print("Failed to finish task in epsoide {}".format(eps))
else:
print("Success in epsoide {}, used {} iterations!".format(eps, i))
self.trainNetwork.save('./trainNetworkInEPS{}.h5'.format(eps))
#Sync
self.targetNetwork.set_weights(self.trainNetwork.get_weights())
print("now epsilon is {}, the reward is {} maxPosition is {}".format(max(self.epsilon_min, self.epsilon), rewardSum,max_position))
self.epsilon -= self.epsilon_decay
def start(self):
for eps in range(self.episodeNum):
currentState=env.reset().reshape(1,2)
self.orginalTry(currentState, eps)
env = gym.make('MountainCar-v0')
dqn=MountainCarTrain(env=env)
dqn.start()