-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay_func.py
88 lines (72 loc) · 2.65 KB
/
play_func.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
from __future__ import print_function, division
from ui import *
import numpy as np
from deap import base, creator, tools, algorithms
import multiprocessing
from collections import Sequence
from itertools import repeat
import random
def eval_mcts(params):
args = {'rules': False, 'azmode': False, 'version': False, 'mode': None, 'resume': False}
game = Game(**args)
max_depth, max_iters = params[0], params[1]
sco, tim, num = game.loop(strategy='mcts', delay=0, max_depth=max_depth, max_iters=max_iters)
print(max_depth, ", ", max_iters, ", ", num, ", ", round(tim, 3), ", ", sco)
return sco,
def two_params(icls, dl, dh, rl, rh):
return icls([random.randint(dl, dh), random.randint(rl, rh)])
def mutGaussianInt(individual, mu, sigma, indpb):
size = len(individual)
if not isinstance(mu, Sequence):
mu = repeat(mu, size)
elif len(mu) < size:
raise IndexError("mu must be at least the size of individual: %d < %d" % (len(mu), size))
if not isinstance(sigma, Sequence):
sigma = repeat(sigma, size)
elif len(sigma) < size:
raise IndexError("sigma must be at least the size of individual: %d < %d" % (len(sigma), size))
for i, m, s in zip(xrange(size), mu, sigma):
if random.random() < indpb:
new_ind = individual[i] + int(round(random.gauss(m, s)))
if new_ind < 1:
individual[i] = 1
else:
individual[i] = new_ind
return individual,
def simple():
pop = toolbox.population(n=pop_sz)
hof = tools.HallOfFame(1000)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("min", np.min)
stats.register("max", np.max)
pop, logbook = algorithms.eaSimple(pop, toolbox,
cxpb=cxpb, mutpb=mutpb, ngen=ngen,
stats=stats, halloffame=hof, verbose=False)
return pop, logbook, hof
# PARALLEL!
def parallel_simple(toolbox, pop_sz, cxpb, mutpb, ngen, n_jobs=4):
# Process Pool of 4 workers
print("Beginning parallel processing")
pool = multiprocessing.Pool(processes=n_jobs)
toolbox.register("map", pool.map)
history = tools.History()
# Decorate the variation operators
toolbox.decorate("mate", history.decorator)
toolbox.decorate("mutate", history.decorator)
# Create the population and populate the history
pop = toolbox.population(n=pop_sz)
history.update(pop)
# create a hall of fame record
hof = tools.HallOfFame(1000)
# intiialize stats
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("std", np.std)
stats.register("min", np.min)
stats.register("max", np.max)
pop, logbook = algorithms.eaSimple(pop, toolbox,
cxpb=cxpb, mutpb=mutpb, ngen=ngen,
stats=stats, halloffame=hof, verbose=False)
pool.close()
return pop, logbook, hof, history