-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenome.py
242 lines (164 loc) · 6.33 KB
/
Genome.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
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
from DataStructures.RandomHashSet import *
from Constants import *
from ConnectionGene import *
from Calculator import *
import random
import math
class Genome:
connections = RandomHashSet()
nodes = RandomHashSet()
neat = ''
calculator = ''
#TODO get rid of calculator in genome
def generateCalculator(self):
self.calculator = Calculator(self)
def calculate(self,foo):
if(self.calculator!=''):
return self.calculator.calculate(foo)
else:
return null
def distance(self, g2):
g1 = self
#indexes
iG1=0
iG2=0
disjoint=0
excess = 0
weightDiff = 0
similar = 0
lastInnovation1=0
lastInnovation2=0
if(g1.getConnections().getSize()!=0):
lastInnovation1 = g1.getConnections().get(g1.getConnections.size-1).getInnovationNum()
if(g2.getConnections().getSize()!=0):
lastInnovation2 = g2.getConnections().get(g2.getConnections.size-1).getInnovationNum()
if(lastInnovation1<lastInnovation2):
ge= g1
g1 = g2
g2 = ge
while((iG1 < g1.getConnections().getSize()) and (iG2 < g2.getConnections().getSize())):
gene1 = g1.getConnections().get(iG1)
gene2 = g2.getConnections().get(iG2)
#inovation numbers
inn1 = gene1.getInnovationNum()
inn2 = gene2.getInnovationNum()
#similar
if(inn1==inn2):
iG1=iG1+1
iG2=iG2+1
similar = similar +1
weightDiff += math.abs(gene1.getWeight()-gene2.getWeight())
#disjoints
if(inn1>inn2):
iG2=iG2+1
disjoint=disjoint+1
if(inn1<inn2):
iG1=iG1+1
disjoint=disjoint+1
if(similar!=0):
weightDiff = weightDiff/similar
excess = g1.getConnections().getSize() - iG1
N = max(g1.getConnections().getSize(), g2.getConnections().getSize())
if(N<20):
N=1
return ((c1 * disjoint/N)+(c2 * excess/N)+ (c3 * excess/N) )
def __init__(self, nNeat):
#print(self.connections.List is self.nodes.List)
self.neat = nNeat
def crossOver(self, g1, g2):
#g1 = self
childGenome = g1.neat.emptyGenome()
#indexes
iG1=0
iG2=0
while((iG1 < g1.getConnections().getSize()) and (iG2 < g2.getConnections().getSize())):
gene1 = g1.getConnections().get(iG1)
gene2 = g2.getConnections().get(iG2)
#inovation numbers
inn1 = gene1.getInnovationNum()
inn2 = gene2.getInnovationNum()
if(inn1==inn2):
choice = random.choice((gene1,gene2))
#TODO change back to single param and make single param def in neat.py
#childGenome.getConnections().add(g1.neat.getConnection(choice))
childGenome.getConnections().add(g1.neat.getConnection(choice.getOrigin(), choice.getTarget()))
iG1=iG1+1
iG2=iG2+1
if(inn1>inn2):
iG2=iG2+1
if(inn1<inn2):
iG1=iG1+1
childGenome.getConnections().add(g1.neat.getConnection(gene1.getOrigin(), gene1.getTarget()))
while(iG1< g1.getConnections().getSize()):
gene1 = g1.getConnections().get(IG1)
childGenome.getConnections().add(g1.neat.getConnection(gene1.getOrigin(), gene1.getTarget()))
iG1=iG1+1
for CG in childGenome.getConnections().List:
childGenome.getNodes().add(CG.getOrigin())
childGenome.getNodes().add(CG.getTarget())
return childGenome
def mutate(self):
if(PROBABILITY_MUTATE_LINK>random.random()):
self.mutateLink()
if(PROBABILITY_MUTATE_NODE>random.random()):
self.mutateNode()
if(PROBABILITY_MUTATE_TOGGLE_LINK>random.random()):
self.mutateToggleLink()
if(PROBABILITY_MUTATE_WEIGHT_SHIFT>random.random()):
self.mutateWeightShift()
if(PROBABILITY_MUTATE_WEIGHT_RANDOM>random.random()):
self.mutateWeightRandom()
def mutateLink(self):
for a in range(0, MUTATION_LINK_ATTEMPS):
nodeA = self.getNodes().getRandom()
nodeB = self.getNodes().getRandom()
con = None
if(nodeA.getX()==nodeB.getX()):
continue
if(nodeA.getX()<nodeB.getX()):
con = ConnectionGene(nodeA,nodeB)
else:
con = ConnectionGene(nodeB,nodeA)
if(self.getConnections().contains(con)):
continue
con = self.neat.getConnection(con.getOrigin(), con.getTarget())
con.setWeight((random.random() * 2-1)*WEIGHT_RANDOM_STRENGTH)
self.connections.addSorted(con)
break
def mutateNode(self):
con = self.getConnections().getRandom()
if(con== None):
return
origin = con.getOrigin()
target = con.getTarget()
middle = self.neat.getNode()
signo = 1
if ( random.random()>0.5):
signo=-1
middle.setX((origin.getX()+target.getX())/2)
middle.setY(((origin.getY()+target.getY())/2)+(signo*(random.random()*0.075)))
con1 = self.neat.getConnection(origin, middle)
con2 = self.neat.getConnection(middle, target)
con1.setWeight(1)
con2.setWeight(con.getWeight())
con2.setEnabled(con.isEnabled())
self.nodes.add(middle)
self.connections.deleteObj(con)
self.connections.add(con1)
self.connections.add(con2)
def mutateToggleLink(self):
nCon = self.getConnections().getRandom()
if(nCon != None):
nCon.toggle()
def mutateWeightShift(self):
nCon = self.getConnections().getRandom()
if(nCon != None):
nCon.setWeight(nCon.getWeight()+(random.random()*2-1)*WEIGHT_SHIFT_STRENGTH)
def mutateWeightRandom(self):
nCon = self.getConnections().getRandom()
if(nCon != None):
nCon.setWeight((random.random()*2-1)*WEIGHT_RANDOM_STRENGTH)
def getConnections(self):
return self.connections
def getNodes(self):
return self.nodes