-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunitEvolution.py
More file actions
executable file
·316 lines (249 loc) · 9.33 KB
/
unitEvolution.py
File metadata and controls
executable file
·316 lines (249 loc) · 9.33 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/python
from pygene.gene import FloatGene
#from pygene.organism import GenomeSplitOrganism
from pygene.organism import Organism
from pygene.population import Population
from runGame import Game;
from economy import Economy;
from soldiers import *;
import gamemap;
from output import Output, ConsoleOutput;
from multiprocessing import Pool
import numpy
import time
from foragers import App
class ProbGene(FloatGene):
"""
Gene which represents the numbers used in our organism
"""
# genes get randomly generated within this range
randMin = 0.00
randMax = 1.00
# probability of mutation
mutProb = 0.10
# degree of mutation
mutAmt = 0.10
def runGame(g):
return g.run()
class SoldierOrganism(Organism):
"""
Implements the organism which tries
to survive in a game
"""
genome = {'riskinessSoldierClass':ProbGene, 'riskinessTrapClass':ProbGene,
'riskinessTowerClass': ProbGene, 'curiosity':ProbGene, 'groupSpirit':ProbGene,
'fasting':ProbGene, 'greed':ProbGene, 'spontaneity':ProbGene, 'repetition':ProbGene};
economy = None
gameMap = None
def __init__(self, **kw):
try:
#print "Loading soldier class"
SoldierOrganism.soldierClass = kw.get("soldierClass")
del kw["soldierClass"]
#print "Done"
except:
print "Reverting to default class since no class was given for soldier organism."
SoldierOrganism.soldierClass = "SoldierClass"
super(SoldierOrganism, self).__init__(**self.genome)
def getGameMap(self, bReset = False):
if self.gameMap is None or bReset:
economy = self.getEconomy();
self.gameMap = gamemap.GameMap(economy, 10, 10);
return self.gameMap
def getEconomy(self, bReset = False):
if self.economy is None or bReset:
self.economy = Economy(5000);
return self.economy
def getSoldier(self, economy, gameMap):
s = eval('%s(economy, gameMap)'%(self.soldierClass));
s.riskiness = {"SoldierClass" : self['riskinessSoldierClass'],
"TrapClass" : self['riskinessTrapClass'],
"TowerClass" : self['riskinessTowerClass'],
};
s.curiosity = self['curiosity'];
s.groupSpirit = self['groupSpirit'];
s.fasting = self['fasting'];
s.greed = self['greed'];
s.spontaneity = self['spontaneity'];
s.repetition = self['repetition'];
return s;
# TODO: To use???
def loadFromSoldier(self, sSoldier):
self['riskinessSoldierClass'] = sSoldier.riskiness["SoldierClass"]
self['riskinessTrapClass'] = sSoldier.riskiness["TrapClass"]
self['riskinessTowerClass'] = sSoldier.riskiness["TowerClass"];
self['curiosity'] = sSoldier.curiosity
self['groupSpirit'] = sSoldier.groupSpirit
self['fasting'] = sSoldier.fasting
self['greed'] = sSoldier.greed
self['spontaneity'] = sSoldier.spontaneity
self['repetition'] = sSoldier.repetition
return self;
def getHelper(self, economy, gameMap):
s = SoldierClass(economy, gameMap);
return s;
# submit fitness calculation to worker process
def prepare_fitness(self):
self.NUMBER_OF_GAMES = 20;
self.NUMBER_OF_SOLDIERS = 1;
self.NUMBER_OF_HELPERS = 3;
self.results = []
for iGameCnt in range(self.NUMBER_OF_GAMES):
# Init messaging
output = Output();
# Init army
# Set colors
sAttackerColor = "white";
# Init economy and map
economy = self.getEconomy(False);
gameMap = self.getGameMap(True);
# Get army
army = [self.getSoldier(economy, gameMap) for x in range(self.NUMBER_OF_SOLDIERS)];
army += [self.getHelper(economy, gameMap) for x in range(self.NUMBER_OF_HELPERS)];
for curSoldier in army: curSoldier.color = sAttackerColor;
# Init game
g = Game(economy, gameMap, army, output, 0.0);
self.results.append(pool.apply_async(runGame, [g]))
# DEBUG LINES
#print len(self.results)
def fitness(self):
"""
Implements the 'fitness function' for this species.
Organisms try to evolve to minimise this function's value
"""
try:
return self.cachedFitness
except:
pass
scores = [];
for curRes in self.results:
scores += [float(curRes.get())]
# DEBUG LINES
#print curRes.get()
# DEBUG LINES
#raw_input()
# Save avg score
self.avgScore = numpy.percentile(scores, 0.33);
# Init economy and map
economy = self.getEconomy(False);
gameMap = self.getGameMap(True);
basePrice = self.NUMBER_OF_SOLDIERS * self.getEconomy(False).cost(self.getSoldier(economy, gameMap));
# Calc evaluation (lower is better)
self.cachedFitness = (1000.0 + 5.0 * basePrice) / (self.avgScore + 1.0)
return self.cachedFitness;
def __repr__(self):
sPhenotype = ";".join([x + ":" + ("%5.3f"%(self[x])) for x in self.genome.keys()]);
return "%s <fitness=%4.2f (or score %4.2f)> %s" % (self.soldierClass,
self.fitness(), self.avgScore,
sPhenotype
)
class SoldierPopulation(Population):
def __init__(self, *a, **kw):
# DEBUG LINES
#print str(kw.get("species"))
super(SoldierPopulation, self).__init__(species=kw.get("species"))
#print "Done"
initPopulation = 10
#species = SoldierOrganism
# cull to this many children after each generation
childCull = 10
# number of children to create after each generation
childCount = 10
# number of random new orgs to add each generation, default 0
numNewOrganisms = 10
def runEvolutionFor(sSoldier):
# Globals
global pool
pool = Pool(processes=4)
sSoldierType = type(sSoldier).__name__
sSoldier
# Create custom organism
class cLocalOrganism(SoldierOrganism):
def __init__(self, **kw):
super(cLocalOrganism, self).__init__(soldierClass=sSoldierType, **kw);
pop = SoldierPopulation((),species=cLocalOrganism)
#pop.setSpecies('localOrganism')
# create a new population, with randomly created members
maxGens = 10;
import time
lastTime = time.time()
try:
generations = 0
while True:
# execute a generation
pop.gen()
generations += 1
# and dump it out
#print [("%.2f %.2f" % (o['x1'], o['x2'])) for o in pop.organisms]
best = pop.organisms[0]
# DEBUG LINES
print("Fitness %4.2f (%s)" % (best.get_fitness(), str(best)))
print "Generation running time %d secs"%(time.time() - lastTime)
lastTime = time.time()
if best.get_fitness() < 0.10 or generations >= maxGens:
break
except KeyboardInterrupt:
pass
print("Executed", generations, "generations")
print("on species ", str(pop.species.soldierClass))
print("best soldier ", str(best.getSoldier(None, None)))
print("with genome ",str(best))
# now a func to run the population
def main():
# Globals
global pool
pool = Pool(processes=4)
# Init calculator pool
sSoldierClass = 'BarbarianClass'
print "Starting run for " + sSoldierClass
# Create custom organism
class cLocalOrganism(SoldierOrganism):
def __init__(self, **kw):
super(cLocalOrganism, self).__init__(soldierClass='BarbarianClass', **kw)
pop = SoldierPopulation((),species=cLocalOrganism)
#pop.setSpecies('localOrganism')
# create a new population, with randomly created members
maxGens = 10;
lastTime = time.time()
try:
generations = 0
while True:
# execute a generation
pop.gen()
generations += 1
# and dump it out
#print [("%.2f %.2f" % (o['x1'], o['x2'])) for o in pop.organisms]
best = pop.organisms[0]
print("fitness=%4.2f %s" % (best.get_fitness(), str(best)))
print "Generation running time %d secs"%(time.time() - lastTime)
lastTime = time.time()
if best.get_fitness() < 0.10 or generations >= maxGens:
break
except KeyboardInterrupt:
pass
print("Executed", generations, "generations")
print("on species ", str(pop.species.soldierClass))
print("best soldier ", str(best.getSoldier(None, None)))
print("with genome ",str(best))
print "Running emulation..."
# Init economy and map
economy = best.getEconomy(False);
gameMap = best.getGameMap(True);
output = ConsoleOutput();
# Get army
army = [best.getSoldier(economy, gameMap) for x in range(best.NUMBER_OF_SOLDIERS)];
army += [best.getHelper(economy, gameMap) for x in range(best.NUMBER_OF_HELPERS)];
# Set colors
sAttackerColor = (255, 255, 255);
for curSoldier in army: curSoldier.color = sAttackerColor;
# Init game
#g = Game(economy, gameMap, army, output, 0.1);
output = Output() # Redirect to NULL output
theApp = App(economy, gameMap, army, output)
theApp.on_execute()
resScore = theApp.finalScore
print "Final score:" + str(resScore)
print "Expected:" + str(best)
output.saveToFile("logBest.txt")
if __name__ == '__main__':
main()