forked from intelligent-systems-course/planet-wars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nova-train.py
212 lines (158 loc) · 6 KB
/
nova-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
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
import argparse
import multiprocessing
import multiprocessing.pool
import sys
import random
from sklearn.linear_model import LogisticRegression
from sklearn.externals import joblib
from api import State, util
from bots.ml.ml import features
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
colors = {
'SUCCESS': GREEN,
'INFO': BLUE,
'WARN': YELLOW,
'FAIL': RED
}
args = None
NOTIFY_AMOUNT = 50
def main():
pool = multiprocessing.Pool(processes=args.parallelism)
bots = []
for id, botname in enumerate(args.players):
bots.append(util.load_player(botname))
matches = len(bots) * args.matches * len(args.planets)
log("Training against {} Bots, {} Maps, {} Matches".format(len(bots), len(args.planets), matches))
data, target = [], []
try:
i = 0
for ret in pool.imap_unordered(execute, gen_rounds(bots)):
i += 1
(bid, mid), winner, state_vectors, (map_size, seed) = ret
if winner == 1:
result = 'won'
elif winner == 2:
result = 'lost'
else:
result = 'draw'
data += state_vectors
target += [result] * len(state_vectors)
log("({}:{} | {}:{}): {}".format(bid, mid, map_size, seed, result), lvl=1)
if i % NOTIFY_AMOUNT == 0:
log("Finished {}/{} matches ({:.2f})%.".format(i, matches, (float(i) / matches * 100)))
except KeyboardInterrupt:
log("Tournament interrupted by user", type="FAIL")
pool.terminate()
pool.join()
sys.exit(1)
pool.close()
pool.join()
log("All games finished", type="SUCCESS")
generate_model(data, target)
# If you wish to use a different model, this
# is where to edit
def generate_model(data, target):
log("Training logistic regression model", lvl=1)
learner = LogisticRegression()
model = learner.fit(data, target)
log("Checking class imbalance", lvl=1)
count = {}
for str in target:
if str not in count:
count[str] = 0
count[str] += 1
log("Instances per class: {}".format(count))
joblib.dump(model, args.model)
log("Done", type="SUCCESS")
def gen_rounds(bots):
for bid, bot in enumerate(bots):
for map_id, map_size in enumerate(args.planets):
for i in range(args.matches):
mid = map_id * args.matches + i
seed = random.randint(0, 100000)
yield ((bid, mid), bot, (map_size, seed, args.max_turns, args.asym))
def execute(params):
ids, bot, (map_size, seed, max_turns, asym) = params
state, _ = State.generate(map_size, seed, symmetric=not asym)
state_vectors = []
i = 0
while not state.finished() and i <= max_turns:
state_vectors.append(features(state))
move = bot.get_move(state)
state = state.next(move)
i += 1
winner = state.winner()
return ids, winner, state_vectors, (map_size, seed)
# following from Python cookbook, #475186
def has_colours(stream):
if not hasattr(stream, "isatty"):
return False
if not stream.isatty():
return False # auto color only on TTYs
try:
import curses
curses.setupterm()
return curses.tigetnum("colors") > 2
except:
# guess false in case of error
return False
def log(s, type='INFO', lvl=0):
color = WHITE
if type in colors:
color = colors[type]
if args.verbose >= lvl:
sys.stdout.write("[")
printout("%07s" % type, color)
sys.stdout.write("] %s\n" % s)
def printout(text, colour=WHITE):
if args.color:
seq = "\x1b[1;%dm" % (30 + colour) + text + "\x1b[0m"
sys.stdout.write(seq)
else:
sys.stdout.write(text)
def optparse():
global args
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-c', '--color', action='store_true', dest='color',
help="force color output")
parser.add_argument('-n', '--no-color', action='store_false', dest='color',
help="force disable color output")
parser.add_argument("-p", "--num-planets",
dest="planets",
help="List of map sizes to use",
type=int, nargs='*',
default=[6])
parser.add_argument("-m", "--num-matches",
dest="matches",
help="Amount of matches played per map size",
type=int, default=1000)
parser.add_argument("-t", "--max-time",
dest="max_time",
help="Maximum amount of time allowed per turn in seconds",
type=float, default=5)
parser.add_argument("-T", "--max-turns",
dest="max_turns",
help="Maximum amount of turns per game",
type=int, default=100)
parser.add_argument("model",
help="Output file for model",
type=str, default="./bots/ml/model.pkl")
parser.add_argument("players",
metavar="player",
help="Players for the game",
type=str, nargs='+')
parser.add_argument("-P", "--pool-size",
dest="parallelism",
help="Pool size for parallelism. Do not use unless you know what you are doing",
type=int, default=multiprocessing.cpu_count())
parser.add_argument("-v", "--verbose",
action="count", default=0,
help="Show more output")
parser.add_argument("-a", "--asym", dest="asym",
help="Whether to start with an asymmetric state.",
action="store_true")
parser.set_defaults(color=has_colours(sys.stdout))
args = parser.parse_args()
if __name__ == "__main__":
optparse()
main()