-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNN.py
More file actions
178 lines (133 loc) · 5.08 KB
/
Copy pathNN.py
File metadata and controls
178 lines (133 loc) · 5.08 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
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
import gym
from gym import Env
import numpy as np
import random
from collections import deque
import time
import os
from collections import defaultdict
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import InputLayer
from keras.layers import Dense
mm_position = []
mm_reward = []
position_queue = []
reward_queue = []
def moving_average(a, n):
ret = np.cumsum(a, dtype=float)
ret[n:] = ret[n:] - ret[:-n]
return ret[n - 1:] / n
def get_model(input_shape, actions):
model = Sequential()
model.add(Dense(48, activation='relu', input_shape=input_shape))
model.add(Dense(24, activation='relu'))
model.add(Dense(actions, activation='linear'))
model.compile(loss='mse', optimizer='adam')
return model
def main():
env = gym.make('MountainCar-v0', render_mode="human")
input_shape = env.observation_space.shape
actions = env.action_space.n
EXP_MAX_SIZE = 5000
BATCH_SIZE = EXP_MAX_SIZE//10
experience = deque([],EXP_MAX_SIZE)
model = None
if not os.path.exists("C:/Users/gizzi/OneDrive/Desktop/ReinforcementLearning/model"):
print('Training...')
model = get_model(input_shape, actions)
n_episodes = 1000
start_epsilon = 0.8
final_epsilon = 0.05
eps = start_epsilon
discount_factor = 0.9
for episode in range(n_episodes):
observation = env.reset()
observation = observation[0]
mm_position.append(observation[0])
done = False
print('episode :' + '' + str(episode))
while not done:
action = -1
temp = np.random.random()
if temp < eps:
action = np.random.randint(0, env.action_space.n)
else:
input = np.array(observation.reshape(1,2), dtype = np.float32)
out = model.predict_on_batch(input)
action = np.argmax(out[0])
new_observation, reward, terminated, truncated, info = env.step(action)
mm_position.append(new_observation[0])
done = terminated or truncated
if new_observation[0] >= 0.5:
reward = 100
if new_observation[0] - observation[0] > 0 and action == 2:
reward = reward + 1
if new_observation[0] - observation[0] < 0 and action == 0:
reward = reward + 1
mm_reward.append(reward)
if len(experience) >= EXP_MAX_SIZE:
experience.popleft()
item = np.array([np.array(observation), action, new_observation, reward], dtype=object)
experience.append(item)
observation = new_observation
if done:
if len(experience) >= BATCH_SIZE and (episode+1) % 10 == 0:
batch = random.sample(experience, BATCH_SIZE)
t1 = list()
t2 = list()
for e in batch:
obs = e[0]
act = e[1]
new_obs = e[2]
rew = e[3]
input = np.array(new_obs.reshape(1,2), dtype = np.float32)
out = model.predict_on_batch(input)
r = np.max(out[0])
target = rew + discount_factor*r
target_vector = model.predict_on_batch(np.array(obs.reshape(1,2), dtype = np.float32))[0]
target_vector[act] = target
t1.append(obs)
t2.append(target_vector)
model.fit(tf.constant(t1), tf.constant(t2), verbose = 0, validation_split = 0.2)
model.save("C:/Users/gizzi/OneDrive/Desktop/ReinforcementLearning/model")
eps -= 1/200
if eps < final_epsilon:
eps = final_epsilon
print("Episode: {}, epsilon: {}".format(episode, eps))
position_queue = moving_average(mm_position, 4020) #201*20
reward_queue = moving_average(mm_reward, 4000) #200*20
"""
rolling_length = 500
fig, axs = plt.subplots(ncols=2, figsize=(15, 5))
axs[0].set_title("Positions Per Episode (Training)")
episode_position_t = np.convolve(np.array(position_queue).flatten(), np.ones(rolling_length), mode="valid")/rolling_length
axs[0].plot(range(len(episode_position_t)), episode_position_t)
axs[1].set_title("Reward Per Episode (Training)")
episode_reward_t = np.convolve(np.array(reward_queue).flatten(), np.ones(rolling_length), mode="valid")/rolling_length
axs[1].plot(range(len(episode_reward_t)), episode_reward_t)
plt.show()
"""
model = keras.models.load_model("C:/Users/gizzi/OneDrive/Desktop/ReinforcementLearning/model")
observation = env.reset()
observation = observation[0]
print(observation)
done = False
best_observation = observation
while not done :
observation = observation.reshape(1,2)
input = np.array(observation, dtype = np.float32)
out = model.predict_on_batch(input)
action = np.argmax(out[0])
new_observation, reward, terminated, truncated, info = env.step(action)
done = terminated or truncated
observation = new_observation
if observation[0] >= best_observation[0]:
best_observation = observation
env.render()
print(best_observation)
if best_observation[0] >= 0.5:
print('Flag reached')
main()