-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomUnitEvolution.py
More file actions
85 lines (69 loc) · 2.68 KB
/
randomUnitEvolution.py
File metadata and controls
85 lines (69 loc) · 2.68 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
import random
import copy
from runGame import Game
from output import Output
from economy import Economy
from gamemap import GameMap
class RandomUnitEvolution():
def __init__(self):
pass
def evolveEachUnit(self, economy, gameMap, army):
scores = []
output = Output() # Dummy output
newArmy = copy.deepcopy(army)
# For each unit
i = 1
for unitIdx in range(len(newArmy)):
# Get the i-th unit of the army
unitToOptimize = newArmy[unitIdx]
print (i)
bestUnit = copy.deepcopy(unitToOptimize)
bestScore = 0
for i in range(1, 100):
# Create an instance of our army, based on the original army, which we will use in a given game
fodderArmy = copy.deepcopy(newArmy)
# Create a (changeable) map, based on the original map
changeableMap = copy.deepcopy(gameMap)
# create a "trained"/improved version of the unit we optimize, on the changeable map
alteredUnit = self.getImprovedClone(unitToOptimize, changeableMap)
# Update the fodder army with the altered unit
fodderArmy[unitIdx] = alteredUnit
self.updateArmyWithCurrentMap(fodderArmy, changeableMap);
# We create a new game, with the changeable map
g = Game(economy, changeableMap, fodderArmy, output, 0.0);
score = self.runGame(g)
if score > bestScore:
bestScore = score
bestUnit = alteredUnit
print(bestScore)
#print ("Run %d"%(i))
# Update the army
newArmy[unitIdx] = bestUnit
scores.append((bestScore,bestUnit))
i = i + 1
scores.sort(reverse=True)
return scores
def updateArmyWithCurrentMap(self, fodderArmy, newMap):
for unit in fodderArmy:
unit.setMap(newMap)
return fodderArmy
def runGame(self, game):
evaluationScore = game.run();
return evaluationScore;
def getImprovedClone(self, unit, mapToAssign):
randomNumber = random.random();
# Creates a copy of the unit
resUnit = copy.deepcopy(unit)
# Update traits
resUnit.strategy.curiosity = randomNumber
# Update map
resUnit.setMap(mapToAssign)
return resUnit
def main():
economy = Economy()
output = Output()
curmap = GameMap(economy)
myevol = RandomUnitEvolution()
print (myevol.evolveEachUnit(economy, curmap, Game.selectArmy(economy, curmap, "white", output)))
if __name__ == '__main__':
main()