-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSettings.py
207 lines (180 loc) · 5.42 KB
/
Settings.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Purpose: Script containing Settings for the Model
#
# Info: Change the Parameters at the top of the scrip to change how the Agent interacts
#
# Developed as part of the Software Agents Course at City University
#
# Dev: Dan Dixey and Enrico Lopedoto
#
# Updated: 10/3/2016
#
import numpy as np
import os
import json
model_version = 3
# Case 1 - Default Evaluation
case_one = dict(trials=200,
completed=500,
crashed=-100,
open=5,
alpha=0.65,
epsilon=0.75,
gamma=0.7,
nb_actions=5,
model=model_version,
epsilon_decay=0.9,
epsilon_action=6000,
change_values=[])
# Case 2 - Change Gamma values
case_two = dict(trials=200,
completed=500,
crashed=-100,
open=5,
alpha=0.65,
epsilon=0.75,
gamma=np.arange(0.1, 1.1, 0.1),
nb_actions=5,
model=model_version,
epsilon_decay=0.9,
epsilon_action=6000,
change_values=['gamma'])
# Case 3 - Change Learning Rates
case_three = dict(trials=200,
completed=500,
crashed=-100,
open=5,
alpha=np.arange(0.1, 1.1, 0.1),
epsilon=0.75,
gamma=0.7,
nb_actions=5,
model=model_version,
epsilon_decay=0.9,
epsilon_action=6000,
change_values=['alpha'])
# Case 4 - different policies (epsilon)
case_four = dict(trials=200,
completed=500,
crashed=-100,
open=5,
alpha=0.65,
epsilon=np.arange(0.1, 1.1, 0.1),
gamma=0.7,
nb_actions=5,
model=model_version,
epsilon_decay=0.9,
epsilon_action=6000,
change_values=['epsilon'])
# Case 5 - different Reward functions
case_five = dict(trials=200,
completed=np.arange(10, 500, 50),
crashed=np.arange(-10, -110, -10),
open=np.arange(0, 10),
alpha=0.65,
epsilon=0.75,
gamma=0.7,
nb_actions=5,
model=model_version,
epsilon_decay=0.9,
epsilon_action=6000,
lambda_td=0.5,
change_values=['completed',
'crashed',
'open'])
# Case Dictionary
case_lookup = dict(case_one=case_one,
case_two=case_two,
case_three=case_three,
case_four=case_four,
case_five=case_five)
def save_results(case, settings, results):
"""
Save all results to a JSON file
:param case: str
:param settings: dict
:param results: list
:return: None
"""
f = open(
os.path.join(
os.getcwd(),
'Results',
case,
'Model{}'.format(
settings['model']) +
'.json'),
'w').write(
json.dumps(results))
def load_results(directory, model):
"""
Loading the Settings File
:param directory: str
:param model: int
:return: dict
"""
return json.loads(
open(directory + '/Model{}.json'.format(model), 'r').read())
def check_files(settings, case, value_iter):
"""
In case the Train File Stops...
:param settings: dict
:param case: str
:param value_iter: int
:return: Boolean
"""
name = 'model_{}_case_{}_iter_{}'.format(
settings['model'],
case.split('_')[1],
value_iter)
path = os.path.join(os.getcwd(), 'Results', case) + \
'/Model{}'.format(settings['model']) + '.json'
results_file = os.path.isfile(path)
# Depending on Model No. Check if Model Memory is Saved
if settings['model'] < 3:
path = os.path.join(
os.getcwd(), 'Model/NN_Model/', name + '.pkl')
model_saved = os.path.isfile(path)
else:
path = os.path.join(
os.getcwd(),
'Model/NN_Model/',
name + '_weights.h5')
model_saved = os.path.isfile(path)
if results_file and model_saved:
continue_on = True
else:
continue_on = False
return continue_on, name
def get_indicies(data, ind=0):
"""
Get the number of Iterations Required for Dictionary
:param data: dict
:param ind: int
:return: tuple(int, dict)
"""
if len(data['change_values']) > 0:
return 10, get_settings(data)
else:
return 1, data
# Get New Dictionary values
def get_settings(dictionary=None, ind=0):
"""
Get Next value in dictionary
:param dictionary: dict
:param ind: int
:return: dict
"""
new_dict = dictionary.copy()
for each_value in dictionary['change_values']:
new_dict[each_value] = dictionary[each_value][ind]
return new_dict
results = dict(time_chart=[],
final_location=[],
best_test=[],
q_plot=[],
model_names=[],
q_matrix=[],
paths=[])
t_array = [] # Storing Time to Complete
f_array = [] # Storing Final Locations
b_array = [] # Storing Full Control
path = []