-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_controller.py
More file actions
304 lines (251 loc) · 11.5 KB
/
game_controller.py
File metadata and controls
304 lines (251 loc) · 11.5 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import os
import sys
import time
import random
import string
import datetime
import torch as T
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.animation as animation
from domain import Domain
from database import DataBase
from models import DQNAgent, DDQNAgent
from util import create_random_domain, save_results_data
class GameController:
def __init__(self, domain_pr, ql_pr, dqn_pr, output_folder):
self.domain_params = domain_pr
self.ql_params = ql_pr
self.dqn_params = dqn_pr
self.output_folder = output_folder
self._create_output_folder()
self._initiate_domain()
self._initiate_agent()
self.db = DataBase()
self.db.initialize(output_folder, 'state_action.db')
def _create_output_folder(self):
cwd = os.getcwd()
if not os.path.isdir(os.path.join(cwd,self.output_folder)):
os.mkdir(os.path.join(cwd,self.output_folder))
def _initiate_domain(self):
if self.domain_params.get('use_available_domain',None):
self.domain = Domain(self.domain_params['domain_shape'],
self.domain_params['domain_type'], self.domain_params["domain_number"])
self.domain.generate_domain_name()
stat = self.domain.load_domain(self.output_folder, self.domain_params)
self.new_agent_loc = self.domain_params['new_agent_loc']
if stat:
print("Using available domain ...")
return
else:
print("Domain is not available")
print("Creating a new random domain ... ")
self.domain = create_random_domain(self.domain_params['domain_shape'],
self.domain_params['domain_type'],
self.domain_params['num_wall'],
self.domain_params['num_storage'],
self.domain_params['num_gold'],
self.domain_params["domain_number"])
self.save_domain()
self.domain.plot_domain(self.output_folder, self.output_folder)
self.new_agent_loc = self.domain_params['new_agent_loc']
def _initiate_agent(self):
gm = self.ql_params['gamma']
ep_max = self.ql_params['epsilon_max']
ep_min = self.ql_params['epsilon_min']
ep_dec = self.ql_params['epsilon_dec']
input_dims = self.domain.get_state().shape
n_actions = self.domain.n_actions()
mem_s = self.dqn_params['mem_size']
batch_s = self.dqn_params['batch_size']
algo = self.dqn_params['algorithm']
env_name = self.domain.domain_name
lr = self.dqn_params['learning_rate']
replace_every = self.dqn_params['replace_network_every']
domain_type = self.domain_params['domain_type']
# type of agent:
# DQN: Sequential Deep Qlearning
# DDQN: Sequential Double Deep Qlearning
if algo == "DQN":
self.agent = DQNAgent(gamma=gm, epsilon=ep_max, lr=lr,
input_dims=input_dims, n_actions=n_actions,
mem_size=mem_s, batch_size=batch_s, domain_type=domain_type,
eps_min=ep_min, eps_dec=ep_dec, replace= replace_every,
algo=algo, env_name=env_name,
chkpt_dir=self.output_folder)
elif algo == "DDQN":
self.agent = DDQNAgent(gamma=gm, epsilon=ep_max, lr=lr,
input_dims=input_dims, n_actions=n_actions,
mem_size=mem_s, batch_size=batch_s, domain_type=domain_type,
eps_min=ep_min, eps_dec=ep_dec, replace= replace_every,
algo=algo, env_name=env_name,
chkpt_dir=self.output_folder)
if self.dqn_params.get('load_pretrained_agent',None):
if self.dqn_params.get('start_from', None):
self.agent.load_models(self.dqn_params['start_from'])
return
self.agent.load_models()
def save_agent(self):
self.agent.save_models()
def load_agent(self):
self.agent.load_models()
def save_domain(self):
self.domain.save_domain(self.output_folder,
self.domain_params)
def show_parameters(self):
print("Domain parameters:")
for i, val in self.domain_params.items():
print(i,":\t",val)
print("\nQlearning parameters:")
for i, val in self.ql_params.items():
print(i,":\t",val)
print("\nDeep Neural Network parameters:")
for i, val in self.dqn_params.items():
print(i,":\t",val)
def print_domain(self):
pass
def train(self):
best_score = -np.inf
scores = []
eps_history = []
figure_number = 1
if self.dqn_params["load_pretrained_agent"]:
pass
n_games = self.ql_params['n_game']
k = 1
episods=[]
start_time = time.time()
for i in range(n_games):
done = False
score = 0
self.domain.reset(new_start_loc=self.new_agent_loc)
observation = self.domain.get_state()
jj = 1
episods.append(k)
k = k + 1
while not done:
action = self.agent.choose_action(T.unsqueeze(T.tensor(observation, dtype=T.float32), dim=0))
# print("This is action ===> ", action)
new_observation, reward, done = self.domain.step(action)
# print("========OOOO=========")
# print(new_observation, reward, done)
# print(self.domain.total_golds)
# print("========OOOO=========")
if done:
print("Terminated at: ",jj,"-->",done)
# new_observation = mydomain.get_state()
score += reward
# if not load_checkpoint:
self.agent.store_transition(observation, action, reward, new_observation, int(done))
self.agent.learn()
# my_agent.remember(observation, action, reward,new_observation, done)
observation = new_observation
# mydomain.plot_domain(True)
# print(reward)
# print("Observation ====================")
# print(observation)
# print("================================")
# print('xx')
# if jj % 5 == 0 and i % 50 == 0 and plotit:
# # print (f"jj: {jj} and i: {i}")
# current_state = mydomain.get_state()
# with T.no_grad():
# tmp_probs = my_agent.q_next.forward(current_state)
# a = tmp_probs.max(dim=0)[0]
# save_figure(mydomain.plot_domain(False),domain_shape,tmp_probs,a,mydomain.actions,figure_number)
# figure_number += 1
# plotit = False
jj += 1
if jj > self.ql_params['stop_game_after']:
jj = 0
break
# print("Score ==> ", score)
# print(f"Action: {mydomain.actions[a]}, reward: {reward}")
# if i > 1000:
# im = plt.imshow(mydomain.plot_domain(False), interpolation=None)
# ims.append([im])
# if i> 1000:
# ani = animation.ArtistAnimation(fig, ims, interval=200, blit=True)
# ani.save(f'animation_test_{k}.gif', writer='imagemagick', fps=4)
# k = k+1
eps_history.append(self.agent.epsilon)
scores.append(score)
ave_score = np.mean(scores[max(0,i-100):(i+1)])
best_score = max(best_score, score)
print(f'episode: {i},'
f' agent_loc: ({self.domain.original_domain_location[1][0][0]},'
f'{self.domain.original_domain_location[1][0][1]}),'
f' epsilon: {self.agent.epsilon}, score: {ave_score},(max: {best_score})')
if i % 100 == 0 and i > 0:
self.agent.save_models()
end_time = time.time()
learning_time = end_time - start_time
fig = plt.figure()
ax = fig.add_subplot(111, label="1")
ax2 = fig.add_subplot(111, label="2", frame_on=False)
ax.plot(episods, eps_history, color="C0")
ax.set_xlabel("Training Steps", color="C0")
ax.set_ylabel("Epsilon", color="C0")
ax.tick_params(axis='x', colors="C0")
ax.tick_params(axis='y', colors="C0")
ax2.plot(episods, scores, color="C1")
ax2.axes.get_xaxis().set_visible(False)
ax2.yaxis.tick_right()
ax2.set_ylabel('Score', color="C1")
ax2.yaxis.set_label_position('right')
ax2.tick_params(axis='y', colors="C1")
# plt.plot(scores)
# plt.show()
fig.savefig(os.path.join(os.getcwd(),self.output_folder+f"/learning_score_{self.domain_params['domain_number']}.pdf"))
save_results_data(episods,eps_history,scores,best_score,learning_time,self.output_folder,self.domain.domain_name)
@staticmethod
def generate_uid():
""" generates 16 chars random combination from string and numbers"""
char_list = string.ascii_uppercase + string.digits
return ''.join(random.choice(char_list) for _ in range(16))
def generate_animation_of_trained_model(self, random_agent_loc=False):
# connect to database
self.db.connect_to_db()
# generate random number to track
random_number = self.generate_uid()
done = False
self.domain.reset(new_start_loc=random_agent_loc)
total_reward = 0
fig = plt.figure(101, figsize=(6, 6))
ax = plt.gca()
ax.set_xticks(np.arange(0.5, self.domain.ncols, 1))
ax.set_yticks(np.arange(0.5, self.domain.nrows, 1))
ax.set_xticklabels([])
ax.set_yticklabels([])
ims = []
j = 1
current_state = self.domain.get_state()
while not done:
# current state
# predict next state
if self.domain_params['domain_type']=='2D':
# current_state = T.unsqueeze(current_state,0)
current_state_expand = current_state.copy()
current_state_expand = np.expand_dims(current_state_expand,axis=0)
action = self.agent.predict_next_move(current_state_expand)
else:
action = self.agent.predict_next_move(current_state)
self.db.enter_value_db(random_number, current_state, action)
# take that action
new_observation, reward, done = self.domain.step(action)
current_state = new_observation
# print(done)
# reward, done = self.domain.action(self.domain.actions[a])
total_reward += reward
im = ax.imshow(self.domain.plot_domain(False), interpolation=None)
ims.append([im])
j = j + 1
if j>400:
break
print(f'Total reward: {total_reward:0.4f}')
ani = animation.ArtistAnimation(fig, ims, interval=200, blit=True)
# timestamp = datetime.datetime.now().strftime("%Y%m%d%I%M%S")
ani.save(f'{self.output_folder}/animation_{self.domain_params["domain_number"]}_{random_number}.gif', writer='imagemagick', fps=4)
print("Done with storing data into database.")
self.db.close_db_connection()