-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneat.lua
More file actions
293 lines (264 loc) · 6.9 KB
/
neat.lua
File metadata and controls
293 lines (264 loc) · 6.9 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
function CreatePool()
local pool = {}
pool.species = {}
pool.generation = 0
pool.innovation = Outputs
pool.currentSpecies = 1
pool.currentGenome = 1
pool.currentFrame = 0
pool.maxFitness = 0
return pool
end
function CreateSpecies()
local species = {}
species.topFitness = 0
species.staleness = 0
species.genomes = {}
species.averageFitness = 0
return species
end
function CreateGenome()
local genome = {}
genome.genes = {}
genome.fitness = 0
genome.adjustedFitness = 0
genome.network = {}
genome.maxneuron = 0
genome.globalRank = 0
genome.mutationRates = {}
genome.mutationCount = {}
genome.mutationIter = 0
genome.mutationRates["connections"] = MutateConnectionsChance
genome.mutationCount["connections"] = 0
genome.mutationRates["link"] = LinkMutationChance
genome.mutationCount["link"] = 0
genome.mutationRates["bias"] = BiasMutationChance
genome.mutationCount["bias"] = 0
genome.mutationRates["node"] = NodeMutationChance
genome.mutationCount["node"] = 0
genome.mutationRates["enable"] = EnableMutationChance
genome.mutationCount["enable"] = 0
genome.mutationRates["disable"] = DisableMutationChance
genome.mutationCount["disable"] = 0
genome.mutationRates["step"] = StepSize
return genome
end
function CreateGene()
local gene = {}
gene.input = 0
gene.output = 0
gene.weight = 0.0
gene.enabled = true
gene.innovation = 0
return gene
end
function CopyGene(gene)
local gene2 = CreateGene()
gene2 = DeepCopy(gene)
return gene2
end
function CreateNeuron()
local neuron = {}
neuron.incoming = {}
neuron.value = 0.0
return neuron
end
function BasicGenome()
local genome = CreateGenome()
genome.maxneuron = Inputs
Mutate(genome)
return genome
end
function CopyGenome(genome)
local genome2 = CreateGenome()
for g = 1, #genome.genes do
table.insert(genome2.genes, CopyGene(genome.genes[g]))
end
genome2.maxneuron = genome.maxneuron
genome2.mutationIter = genome.mutationIter
genome2.mutationRates = DeepCopy(genome.mutationRates)
genome2.mutationCount = DeepCopy(genome.mutationCount)
return genome2
end
function GenerateNetwork(genome)
local network = {}
network.neurons = {}
for i = 1, Inputs do
network.neurons[i] = CreateNeuron()
end
for o = 1, Outputs do
network.neurons[MaxNodes + o] = CreateNeuron()
end
table.sort(genome.genes, function (a, b) return (a.output < b.output) end)
for i = 1, #genome.genes do
local gene = genome.genes[i]
if gene.enabled then
if network.neurons[gene.output] == nil then
network.neurons[gene.output] = CreateNeuron()
end
local neuron = network.neurons[gene.output]
table.insert(neuron.incoming, gene)
if network.neurons[gene.input] == nil then
network.neurons[gene.input] = CreateNeuron()
end
end
end
genome.network = network
end
function NewGeneration()
CullSpecies(false) -- Cull the bottom half of each species (reduce to the half best genomes in each species)
RankGlobally()
RemoveStaleSpecies()
RankGlobally()
for s = 1, #Pool.species do
local species = Pool.species[s]
CalculateAverageFitness(species)
end
RemoveWeakSpecies()
local sum = TotalAverageFitness()
local children = {}
for s = 1, #Pool.species do
local species = Pool.species[s]
local breed = math.floor(species.averageFitness / sum * Population) - 1
for i = 1, breed do
table.insert(children, BreedChild(species))
end
end
CullSpecies(true) -- Cull all but the top member of each species (reduce to one genome in each species)
while #children + #Pool.species < Population do
local species = Pool.species[math.random(1, #Pool.species)]
table.insert(children, BreedChild(species))
end
for c = 1, #children do
AddToSpecies(children[c])
end
Pool.generation = Pool.generation + 1
--print("Saving newly generated generation to file...")
--SavePool("new_gen#" .. Pool.generation .. "_" .. forms.gettext(SaveFile))
end
function RandomNeuron(genes, nonInput)
local neurons = {}
if not nonInput then
for i = 1, Inputs do
neurons[i] = true
end
end
for o = 1, Outputs do
neurons[MaxNodes + o] = true
end
for i = 1, #genes do
if (not nonInput) or genes[i].input > Inputs then
neurons[genes[i].input] = true
end
if (not nonInput) or genes[i].output > Inputs then
neurons[genes[i].output] = true
end
end
local n = math.random(1, #neurons + Outputs)
if neurons[n] == nil then
return MaxNodes + n
else
return n
end
return 0
end
function ContainsLink(genes, link)
for i = 1, #genes do
local gene = genes[i]
if gene.input == link.input and gene.output == link.output then
return true
end
end
return false
end
function Disjoint(genes1, genes2)
local i1 = {}
for i = 1, #genes1 do
local gene = genes1[i]
i1[gene.innovation] = true
end
local i2 = {}
for i = 1, #genes2 do
local gene = genes2[i]
i2[gene.innovation] = true
end
local disjointGenes = 0
for i = 1, #genes1 do
local gene = genes1[i]
if not i2[gene.innovation] then
disjointGenes = disjointGenes+1
end
end
for i = 1, #genes2 do
local gene = genes2[i]
if not i1[gene.innovation] then
disjointGenes = disjointGenes+1
end
end
local n = math.max(#genes1, #genes2)
return disjointGenes / n
end
function Weights(genes1, genes2)
local i2 = {}
for i = 1, #genes2 do
local gene = genes2[i]
i2[gene.innovation] = gene
end
local sum = 0
local coincident = 0
for i = 1, #genes1 do
local gene = genes1[i]
if i2[gene.innovation] ~= nil then
local gene2 = i2[gene.innovation]
sum = sum + math.abs(gene.weight - gene2.weight)
coincident = coincident + 1
end
end
return sum / coincident
end
function BreedChild(species)
local child = {}
if Mode["GA"] then
if math.random() < CrossoverChance then
local g1 = species.genomes[math.random(1, #species.genomes)]
local g2 = species.genomes[math.random(1, #species.genomes)]
child = Crossover(g1, g2)
else
local g = species.genomes[math.random(1, #species.genomes)]
child = CopyGenome(g)
end
else
local g = species.genomes[math.random(1, #species.genomes)]
child = CopyGenome(g)
end
Mutate(child)
return child
end
function AddToSpecies(child)
local foundSpecies = false
for s = 1, #Pool.species do
local species = Pool.species[s]
local dd = DeltaDisjoint * Disjoint(child.genes, species.genomes[1].genes)
local dw = DeltaWeights * Weights(child.genes, species.genomes[1].genes)
if not foundSpecies and (dd + dw < DeltaThreshold) then
table.insert(species.genomes, child)
foundSpecies = true
end
end
if not foundSpecies then
local childSpecies = CreateSpecies()
table.insert(childSpecies.genomes, child)
table.insert(Pool.species, childSpecies)
end
end
function NextGenome()
Pool.currentGenome = Pool.currentGenome + 1
if Pool.currentGenome > #Pool.species[Pool.currentSpecies].genomes then
Pool.currentGenome = 1
Pool.currentSpecies = Pool.currentSpecies + 1
if Pool.currentSpecies > #Pool.species then
NewGeneration()
Pool.currentSpecies = 1
end
end
end