-
Notifications
You must be signed in to change notification settings - Fork 1
/
trainer.py
216 lines (188 loc) · 9.04 KB
/
trainer.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
208
209
210
211
212
213
214
215
216
import datetime
import os
from os import path
import sys
import getopt
import math
import subprocess
import time
BATCH_SIZE = 10
game_sim_cmd = ["java", "--add-opens", "java.base/java.lang=ALL-UNNAMED", "-Dnotimeout=\"true\"", "-jar", "ea-2022-keep-off-the-grass-1.0-SNAPSHOT.jar", "\"python", "Boss-easy.pyc\"", "local"]
def help():
print("Usage: trainer.py (-s <num_simulations> | -t <time_of_training>) -f <file_suffix> -i <ai_python_script> [-d]")
sys.exit(2)
def train(debug, ai_script, num_simulations, num_seconds, file_suffix):
print("AI TRAINER STARTED")
print("")
global BATCH_SIZE
_num_simulations = int(num_simulations)
_num_seconds = int(num_seconds)
training_command = ["python", ai_script, "-t", "-f", file_suffix]
start_seconds = time.time()
list_times = []
total_avg_reward = 0
batch_avg_reward = 0
n = 0
first_batch_score = 0
last_batch_score = 0
print("BATCH 0")
print("---------")
if _num_simulations > 0:
for i in range(_num_simulations):
r = 1.0 - math.sqrt(float(i)) / math.sqrt(float(_num_simulations))
command_words = ["\"python", ai_script]
if debug: command_words.append("-d")
command_words.append("-l")
command_words.append("-r")
command_words.append(str(r))
command_words.append("-f")
command_words.append(file_suffix + "\"")
local_game_sim_cmd = game_sim_cmd.copy()
for j in range(len(command_words)):
local_game_sim_cmd.insert(6 + j, command_words[j])
#run simulation
print("\tTesting/playing...", end='\r')
list_time_start = time.time()
simulation_output = subprocess.run(local_game_sim_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
list_times.append(time.time() - list_time_start)
reward_file = open("reward_log" + file_suffix + ".txt", "r")
reward = int(reward_file.readline())
reward_file.close()
batch_avg_reward += reward
total_avg_reward += reward
if simulation_output.returncode != 0:
print("")
print("Something errored during testing/playing!")
print(simulation_output.stderr)
sys.exit(1)
elif i % BATCH_SIZE == BATCH_SIZE - 1 and i > 0: # Gather data
batch_avg_reward /= BATCH_SIZE
if i == BATCH_SIZE - 1: first_batch_score = batch_avg_reward
last_batch_score = batch_avg_reward
avg_time = 0
for curr_time in list_times:
avg_time += curr_time
avg_time /= len(list_times)
# Train the batch
print("\tTesting/playing... \t\tDone!")
#print("\tTraining...", end='\r')
#start_training_time = time.time()
#simulation_output = subprocess.run(training_command, text=True)
#if simulation_output.returncode != 0:
# print("")
# print("Something errored during training!")
# print(simulation_output.stderr)
# sys.exit(1)
#print("\tTraining... \t\t\tDone!")
# Print out info
print("\n\tStats for simulations " + str(i - (BATCH_SIZE - 1)) + "-" + str(i))
print("\t--------------------------------------------")
print("\tLatest learning rate scalar: \t" + str(r))
print("\tBatch avg score: \t\t" + str(batch_avg_reward))
print("\tElapsed time: \t\t\t" + str(datetime.timedelta(seconds=time.time() - start_seconds)))
print("\tEstimated time remaining: \t" + str(datetime.timedelta(seconds=(avg_time * (_num_simulations - (i + 1)))))) # + (time.time() - start_training_time)))))
print("\t--------------------------------------------")
print("\t")
if i <= _num_simulations - BATCH_SIZE:
print("BATCH " + str((i + 1) // BATCH_SIZE))
print("---------")
list_times.clear()
batch_avg_reward = 0
else:
end_seconds = start_seconds + _num_seconds
while time.time() < end_seconds:
r = 1.0 - math.sqrt(float(time.time() - start_seconds)) / math.sqrt(float(_num_seconds))
command_words = ["\"python", ai_script]
if debug: command_words.append("-d")
command_words.append("-l")
command_words.append("-r")
command_words.append(str(r))
command_words.append("-f")
command_words.append(file_suffix + "\"")
local_game_sim_cmd = game_sim_cmd.copy()
for i in range(len(command_words)):
local_game_sim_cmd.insert(6 + i, command_words[i])
#run simulation
print("\tTesting/playing...", end='\r')
simulation_output = subprocess.run(local_game_sim_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
reward_file = open("reward_log" + file_suffix + ".txt", "r")
reward = int(reward_file.readline())
reward_file.close()
batch_avg_reward += reward
total_avg_reward += reward
if simulation_output.returncode != 0:
print("")
print("Something errored during testing/playing!")
print(simulation_output.stderr)
sys.exit(1)
elif n % BATCH_SIZE == BATCH_SIZE - 1 and n > 0: # Gather data
batch_avg_reward /= BATCH_SIZE
if n == BATCH_SIZE - 1: first_batch_score = batch_avg_reward
last_batch_score = batch_avg_reward
# Train the batch
print("\tTesting/playing... \t\tDone!")
#print("\tTraining...", end='\r')
#simulation_output = subprocess.run(training_command, text=True)
#if simulation_output.returncode != 0:
# print("")
# print("Something errored during training!")
# print(simulation_output.stderr)
# sys.exit(1)
#print("\tTraining... \t\t\tDone!")
# Print out info
print("\n\tStats for simulations " + str(n - (BATCH_SIZE - 1)) + "-" + str(n))
print("\t--------------------------------------------")
print("\tLatest learning rate scalar: \t" + str(r))
print("\tBatch avg score: \t\t" + str(batch_avg_reward))
print("\tElapsed time: \t\t\t" + str(datetime.timedelta(seconds=time.time() - start_seconds)))
print("\tTime remaining: \t\t" + str(datetime.timedelta(seconds=end_seconds - time.time())))
print("\t--------------------------------------------")
print("\t")
if time.time() < end_seconds - 5:
print("BATCH " + str((n + 1) // BATCH_SIZE))
print("---------")
batch_avg_reward = 0
n += 1
os.remove("reward_log" + file_suffix + ".txt")
print("\n ________________________________")
print("|\tAI TRAINER FINISHED \t |")
print("|Time taken: \t " + str(datetime.timedelta(seconds=time.time() - start_seconds)) + "|")
print("|# of simulations: \t\t" + str(max(_num_simulations, n)) + "|")
print("|Delta avg score: \t\t" + str(last_batch_score - first_batch_score) + "|")
print("|Avg score: \t\t " + str(float(total_avg_reward) / max(n, _num_simulations)) + "|")
print(" --------------------------------")
if path.exists("transitions/HQ_sum_transitions" + file_suffix + ".txt"):
os.remove("transitions/HQ_sum_transitions" + file_suffix + ".txt")
if path.exists("transitions/HQ_transitions" + file_suffix + ".tns"):
os.remove("transitions/HQ_transitions" + file_suffix + ".tns")
if path.exists("transitions/robot_sum_transitions" + file_suffix + ".txt"):
os.remove("transitions/robot_sum_transitions" + file_suffix + ".txt")
if path.exists("transitions/robot_transitions" + file_suffix + ".tns"):
os.remove("transitions/robot_transitions" + file_suffix + ".tns")
def main(argv):
opts = []
try:
opts, args = getopt.getopt(argv, "hs:t:f:i:d")
except getopt.GetoptError:
help()
file_suffix = ""
debug_mode = False
ai_script = ""
num_simulations = 0
total_seconds = 0
for opt, arg in opts:
if opt == "-f":
file_suffix = arg
if opt == "-h":
help()
if opt == "-d":
debug_mode = True
if opt == "-i":
ai_script = arg
if opt == "-s":
num_simulations = arg
elif opt == "-t":
total_seconds = arg
train(debug_mode, ai_script, num_simulations, total_seconds, file_suffix)
if __name__ == "__main__":
main(sys.argv[1:])