-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebGui.py
More file actions
176 lines (139 loc) · 5.39 KB
/
Copy pathWebGui.py
File metadata and controls
176 lines (139 loc) · 5.39 KB
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
import json
from threading import Thread
import web
from beer.BeerEA import *
from flatland.FlatlandEA import *
parentStrat = [
('Fitness Proportionate', Strategies.fitness),
('Sigma Scaling', Strategies.sigma),
('Boltzmann Selection', Strategies.boltz),
('Rank Selection', Strategies.rank),
('Tournament Selection', Strategies.tournament),
('Uniform Selection', Strategies.uniform),
]
adultStrat = [
"Generation Replacement",
"Generation Mixing"
]
defaults = {
'generations': 100,
'adultStrategy': 1,
'parentStrategy': 3, #Rank sel, see array over
'childPool': 100,
'adultPool': 50,
'parentPool': 30,
'mutation': [-0.2, 0.3, 0.0, 1.0],
'crossover': [0.3, 0.2, 0.0, 1.0],
'rank': [0.5, 1.5],
'tournament': [5, 0.1],
'flatland': 0,
'beeragent':0
}
urls = (
'/', 'index',
'/settings', 'settings',
'/flatland', 'flatland_web',
'/beeragent', 'beeragent_web',
'/start', 'starter',
'/progress', 'progress'
)
app = web.application(urls, globals())
render = web.template.render('templates/', base='layout')
web.template.Template.globals['render'] = web.template.render('templates/')
web.template.Template.globals['str'] = str
web.template.Template.globals['adultStrat'] = adultStrat
web.template.Template.globals['parentStrat'] = parentStrat
def setup_ea(input):
EA.max_generations = int(input.generations)
EA.adult_pool_size = int(input.adultPool)
EA.child_pool_size = int(input.childPool)
EA.parent_pool_size = int(input.parentPool)
EA.adult_selection_mode = int(input.adultStrategy)
EA.parent_selection_strategy = parentStrat[int(input.parentStrategy)][1]
EA.crossover_rate = NormDist(float(input.crossover[0]), float(input.crossover[1]),
float(input.crossover[2]), float(input.crossover[3]))
EA.mutation_rate = NormDist(float(input.mutation[0]), float(input.mutation[1]),
float(input.mutation[2]), float(input.mutation[3]))
Strategies.rank_min = float(input.rank[0])
Strategies.rank_max = float(input.rank[1])
Strategies.tournament_k = int(input.tournament[0])
Strategies.tournament_e = float(input.tournament[1])
FlatlandEA.dynamic = input.has_key("flatland")
Beer.avoid_objects = input.has_key("beeragent")
return "T"
current_ea = None
ea_thread = None
current_game = None
def start_ea(ea):
print("startingEA")
ea.run()
class index:
def GET(self):
return render.index()
class settings:
def GET(self):
i = web.input()
defaults['game'] = i.game
return render.settings(defaults, False)
class starter:
def GET(self):
global ea_thread, current_ea, current_game
if current_ea:
current_ea.current_generation = EA.max_generations +1
i = web.input(tournament=[], rank=[], mutation=[], crossover=[])
setup_ea(i)
current_game = i.game
current_ea = FlatlandEA() if i.game == "flatland" else BeerEA()
print current_ea.parent_selection_strategy
current_ea.fitness_goal = 100
ea_thread = Thread(target=start_ea, args=[current_ea])
ea_thread.start()
web.header('Content-Type', 'application/json')
return json.dumps({'start': True})
class progress:
def GET(self):
global ea_thread, current_ea, current_game
web.header('Content-Type', 'application/json')
complete = current_ea.current_generation == EA.max_generations or current_ea.best_individual.fitness > current_ea.fitness_goal
res = {
'complete': complete
}
input = web.input()
if complete or input.has_key('requestupdate'):
res['means'] = current_ea.means
res['sds'] = current_ea.sds
res['bests'] = current_ea.bests
res['similarity'] = current_ea.best_similarities
res['fitness'] = current_ea.best_individual.fitness
network = {}
for key,n in current_ea.best_individual.ann.neurons.items():
network[key] = {
}
for iKey,inp in n.inputs.items():
network[key][iKey] = inp.weight
res['net'] = network
if current_game == 'beeragent':
res['game'] = current_ea.best_history.states
else:
res['game'] ={
'maps':[flatland.map.tolist() for flatland in current_ea.best_maps],
'botPos':[list(flatland.agent_pos) for flatland in current_ea.best_maps],
'hists':[flatland.history for flatland in current_ea.best_maps],
'botDir':[flatland.agent_direction.val.tolist() for flatland in current_ea.best_maps],
'mapRes':[[flatland.food_gathered,flatland.poisoned] for flatland in current_ea.best_maps]
}
else:
best_fitness = current_ea.best_individual.fitness if current_ea.best_individual else 0
res = {
'm': "Generation: %d of %d Best:%.4f" % (current_ea.current_generation, EA.max_generations, best_fitness)}
return json.dumps(res)
class flatland_web:
def GET(self):
defaults['game'] = "flatland"
return render.flatland(defaults)
class beeragent_web:
def GET(self):
defaults['game'] = "beeragent"
return render.beeragent(defaults)
if __name__ == "__main__":
app.run()