-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
86 lines (68 loc) · 2.2 KB
/
training.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
#!/usr/bin/python
#!/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 SoldierOrganismConverter:
@staticmethod
def getOrganismForSoldier(sSoldier, eEconomy):
pass
def applyGenomeToSoldier(genome, sSoldier):
pass
class TrainingSession:
def __init__(self, economy, army, building):
self.economy = economy
self.army = army
self.building = building
self.curGround = self.building.trainingGround
self.curMap = gamemap.GameMap(self.building.mapSize, self.building.mapSize, curGround["traps"],
curGround["treasure"], curGround["soldiers"], curGround["towers"])
def train(self):
aResArmy = []
# For each soldier in army
for sSoldier in self.army:
# apply evolution
# get organism
# sCurOrganism = SoldierOrganismConverter.getOrganismForSoldier(sSoldier, economy)
# call GA to evolve on map
# apply training
# for each improvement
for sKey in self.building.improvement.keys():
# if improvement is about abilities
if sKey == "abilities":
# if roll is below threshold
if random.random() < self.building.improvement[sKey]:
# give ability
sSoldier.abilities = set(sSoldier.abilities) + set(random.choice(self.building.newAbilities))
# else
else:
# if roll is below threshold
if random.random() < self.building.improvement[sKey]:
# increase stat
eval("sSoldier." + sKey + " *= 1.05")
# add to result army
aResArmy += [SoldierOrganismConverter.applyGenomeToSoldier(sSoldier)]
return aResArmy