forked from asuiu/cartpole
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcartpole_NN.py
More file actions
132 lines (107 loc) · 4.66 KB
/
Copy pathcartpole_NN.py
File metadata and controls
132 lines (107 loc) · 4.66 KB
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
import random
from collections import deque
from typing import Tuple
import gym
import numpy as np
from keras.layers import Dense
from keras.models import Sequential
from keras.optimizers import Adam
from scores import score_logger
"""
Vanilla Multi Layer Perceptron version.
The below configuration starts converging to solution after ~7 runs with STOCHASTIC_TRAIN=True
"""
ENV_NAME = "CartPole-v1"
GAMMA = 0.95
LEARNING_RATE = 0.001
MEMORY_SIZE = 384
BATCH_SIZE = 20
Q_LEARN_RATE = 0.999
# Setting next parameter to True would force training of the network on the whole random-sample batch every step/action
# When on True - it converges faster to solution, with less runs, but consuming more resources
TRAIN_EACH_STEP = True
# Stochastic training - ie train with batches of size 1, thus offering faster feedback loop of predicted Q-values
# Convergence is significantly faster and starts converging after 7 runs
# As higher the BATCH_SIZE, as higher might be difference in conv speed offered by this, but slowing-down the training
STOCHASTIC_TRAIN = True
EXPLORATION_MAX = 1.0
EXPLORATION_MIN = 0.01
EXPLORATION_DECAY = 0.93
PLOT_REFRESH = 10
class DQNSolver:
def __init__(self, observation_space, action_space):
self.exploration_rate = EXPLORATION_MAX
self.action_space = action_space
self.memory = deque(maxlen=MEMORY_SIZE)
self.isFit = False
self.model = Sequential()
self.model.add(Dense(24, input_shape=(observation_space,), activation="relu"))
self.model.add(Dense(24, activation="relu"))
self.model.add(Dense(24, activation="relu"))
self.model.add(Dense(self.action_space, activation="linear"))
self.model.compile(loss="mse", optimizer=Adam(lr=LEARNING_RATE))
def remember(self, state, action, q_val, reward, next_state, done):
self.memory.append((state, action, q_val, reward, next_state, done))
def act(self, state) -> Tuple[int, float]:
if np.random.rand() < self.exploration_rate:
action = random.randrange(self.action_space)
return action, 0.
q_values = self.model.predict(state)
return np.argmax(q_values[0]), np.max(q_values[0])
def experience_replay_new(self):
if self.isFit:
self.exploration_rate *= EXPLORATION_DECAY
self.exploration_rate = max(EXPLORATION_MIN, self.exploration_rate)
if len(self.memory) < BATCH_SIZE:
return
batch = random.sample(self.memory, BATCH_SIZE)
if not self.isFit:
self.model.fit(batch[0][0], np.array([np.zeros((self.action_space,))]), verbose=0)
X, Y = [], []
for state, action, cur_q, reward, state_next, terminal in batch:
if terminal:
new_q = reward
else:
predicts = self.model.predict(state_next)
max_expected_q = np.amax(predicts[0])
new_q = (cur_q * (1 - Q_LEARN_RATE)) + (Q_LEARN_RATE * (reward + GAMMA * max_expected_q))
y_q_values = self.model.predict(state)
y_q_values[0][action] = new_q
if STOCHASTIC_TRAIN:
self.model.fit(state, y_q_values, steps_per_epoch=1, verbose=0)
else:
X.append(state[0])
Y.append(y_q_values[0])
if not STOCHASTIC_TRAIN:
self.model.fit(np.array(X), np.array(Y), batch_size=len(X), verbose=0)
if not TRAIN_EACH_STEP:
self.memory = deque(maxlen=MEMORY_SIZE)
self.isFit = True
def cartpole():
env = gym.make(ENV_NAME)
sl = score_logger.ScoreLogger(ENV_NAME)
observation_space = env.observation_space.shape[0]
action_space = env.action_space.n
dqn_solver = DQNSolver(observation_space, action_space)
run = 0
while True:
run += 1
state = env.reset()
state = np.reshape(state, [1, observation_space])
step = 0
while True:
step += 1
# env.render()
action, cur_q = dqn_solver.act(state)
state_next, reward, terminal, info = env.step(action)
reward = reward if not terminal else -reward
state_next = np.reshape(state_next, [1, observation_space])
dqn_solver.remember(state, action, cur_q, reward, state_next, terminal)
state = state_next
if terminal:
print("Run: " + str(run) + ", exploration: " + str(dqn_solver.exploration_rate) + ", score: " + str(step))
sl.add_score(step, run, dqn_solver.exploration_rate, len(dqn_solver.memory), refresh=run % PLOT_REFRESH == 0)
break
dqn_solver.experience_replay_new()
if __name__ == "__main__":
cartpole()