This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
118 lines (89 loc) · 3.96 KB
/
train.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
import os
import sys
import threading
import random
from itertools import chain
from shutil import copyfile
import hlt_client.hlt_client.compare_bots as compare
path_to_binary = "/Users/philippmatthes/desktop/halite-java/halite"
path_to_bot = "MyBot"
run_bot_command = "java " + path_to_bot
map_height = 160
map_width = 240
tries = 25
randomization = 0.2
number_of_threads = 8
winners = []
def load_properties(filepath, sep='=', comment_char='#'):
"""
Read the file passed as parameter as a properties file.
"""
props = {}
with open(filepath, "rt") as f:
for line in f:
l = line.strip()
if l and not l.startswith(comment_char):
key_value = l.split(sep)
key = key_value[0].strip()
value = sep.join(key_value[1:]).strip().strip('"')
props[key] = value
return props
def task():
winners.append(compare.play_games(path_to_binary, map_width, map_height,
[run_bot_command, run_bot_command, run_bot_command, run_bot_command], tries))
def generate_randomized_values():
for botNumber in range(3):
properties = load_properties("bot" + str(botNumber) + ".properties")
f = open("bot" + str(botNumber) + ".properties", "w")
f.write("OBSTACLE_AVOIDANCE_DISTANCE=" +
str((float(properties["OBSTACLE_AVOIDANCE_DISTANCE"])) + random.uniform(-randomization, randomization)))
f.write("\n")
f.write("BOUNDING_FACTOR=" +
str((float(properties["BOUNDING_FACTOR"])) + random.uniform(-randomization, randomization)))
f.write("\n")
f.write("POSITION_TEND_FACTOR=" +
str((float(properties["POSITION_TEND_FACTOR"])) + random.uniform(-randomization, randomization)))
f.write("\n")
f.write("SEPERATION_FACTOR=" +
str((float(properties["SEPERATION_FACTOR"])) + random.uniform(-randomization, randomization)))
f.write("\n")
f.write("REGROUPING_RADIUS=" +
str((float(properties["REGROUPING_RADIUS"])) + random.uniform(-randomization, randomization)))
f.write("\n")
f.write("OBSTACLE_AVOIDANCE_FACTOR=" +
str((float(properties["OBSTACLE_AVOIDANCE_FACTOR"])) + random.uniform(-randomization, randomization)))
f.write("\n")
f.write("SEPERATION_DISTANCE=" +
str((float(properties["SEPERATION_DISTANCE"])) + random.uniform(-randomization, randomization)))
f.write("\n")
f.write("REGROUPING_FACTOR=" +
str((float(properties["REGROUPING_FACTOR"])) + random.uniform(-randomization, randomization)))
f.write("\n")
f.close()
def most_common(lst):
return max(set(lst), key=lst.count)
if __name__ == "__main__":
training_round = 0
os.system("javac MyBot.java")
while True:
print("Launching training round: "+str(training_round))
print("Running with "+str(number_of_threads)+" threads and training "+str(tries)+" rounds.")
generate_randomized_values();
threads = []
for thread_number in range(number_of_threads):
threads.append(threading.Thread(target=task))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
chained_winners_as_strings = list(chain.from_iterable(winners))
chained_winners_as_numbers = []
for winner in chained_winners_as_strings:
chained_winners_as_numbers.append(int(winner.replace(",", "").replace("#","")))
leading_bot = most_common(chained_winners_as_numbers)
for botNumber in range(3):
if leading_bot != botNumber:
copyfile("bot" + str(leading_bot) + ".properties", "bot" + str(botNumber) + ".properties")
print("Bot "+str(leading_bot)+" won the competition. He will now overtake.")
os.system("find . -name \*.hlt -delete; find . -name \*.log -delete")
training_round += 1;