-
Notifications
You must be signed in to change notification settings - Fork 20
/
genetic_algorithm.go
167 lines (138 loc) · 4.34 KB
/
genetic_algorithm.go
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
package goga
import (
"sync"
"time"
)
// GeneticAlgorithm -
// The main component of goga, holds onto the state of the algorithm -
// * Mater - combining evolved genomes
// * EliteConsumer - an optional class that accepts the 'elite' of each population generation
// * Simulator - a simulation component used to score each genome in each generation
// * BitsetCreate - used to create the initial population of genomes
type GeneticAlgorithm struct {
Mater Mater
EliteConsumer EliteConsumer
Simulator Simulator
Selector Selector
BitsetCreate BitsetCreate
populationSize int
population []Genome
totalFitness int
genomeSimulationChannel chan Genome
exitFunc func(Genome) bool
waitGroup *sync.WaitGroup
parallelSimulations int
}
// NewGeneticAlgorithm returns a new GeneticAlgorithm structure with null implementations of
// EliteConsumer, Mater, Simulator, Selector and BitsetCreate
func NewGeneticAlgorithm() GeneticAlgorithm {
return GeneticAlgorithm{
EliteConsumer: &NullEliteConsumer{},
Mater: &NullMater{},
Simulator: &NullSimulator{},
Selector: &NullSelector{},
BitsetCreate: &NullBitsetCreate{},
}
}
func (ga *GeneticAlgorithm) createPopulation() []Genome {
ret := make([]Genome, ga.populationSize)
for i := 0; i < ga.populationSize; i++ {
ret[i] = NewGenome(ga.BitsetCreate.Go())
}
return ret
}
// Init initialises internal components, sets up the population size
// and number of parallel simulations
func (ga *GeneticAlgorithm) Init(populationSize, parallelSimulations int) {
ga.populationSize = populationSize
ga.population = ga.createPopulation()
ga.parallelSimulations = parallelSimulations
ga.waitGroup = new(sync.WaitGroup)
}
func (ga *GeneticAlgorithm) beginSimulation() {
ga.Simulator.OnBeginSimulation()
ga.totalFitness = 0
ga.genomeSimulationChannel = make(chan Genome)
// todo: make configurable
for i := 0; i < ga.parallelSimulations; i++ {
go func(genomeSimulationChannel chan Genome,
waitGroup *sync.WaitGroup, simulator Simulator) {
for genome := range genomeSimulationChannel {
defer waitGroup.Done()
simulator.Simulate(genome)
}
}(ga.genomeSimulationChannel, ga.waitGroup, ga.Simulator)
}
ga.waitGroup.Add(ga.populationSize)
}
func (ga *GeneticAlgorithm) onNewGenomeToSimulate(g Genome) {
ga.genomeSimulationChannel <- g
}
func (ga *GeneticAlgorithm) syncSimulatingGenomes() {
close(ga.genomeSimulationChannel)
ga.waitGroup.Wait()
}
func (ga *GeneticAlgorithm) getElite() Genome {
var ret Genome
for i := 0; i < ga.populationSize; i++ {
if ret == nil || ga.population[i].GetFitness() > ret.GetFitness() {
ret = ga.population[i]
}
}
return ret
}
// SimulateUntil simulates a population until 'exitFunc' returns true
// The 'exitFunc' is passed the elite of each population and should return true
// if the elite reaches a certain criteria (e.g. fitness above a certain threshold)
func (ga *GeneticAlgorithm) SimulateUntil(exitFunc func(Genome) bool) bool {
ga.exitFunc = exitFunc
return ga.Simulate()
}
func (ga *GeneticAlgorithm) shouldExit(elite Genome) bool {
if ga.exitFunc == nil {
return ga.Simulator.ExitFunc(elite)
}
return ga.exitFunc(elite)
}
// Simulate runs the genetic algorithm
func (ga *GeneticAlgorithm) Simulate() bool {
if ga.populationSize == 0 {
return false
}
ga.beginSimulation()
for i := 0; i < ga.populationSize; i++ {
ga.onNewGenomeToSimulate(ga.population[i])
}
ga.syncSimulatingGenomes()
ga.Simulator.OnEndSimulation()
for {
elite := ga.getElite()
ga.Mater.OnElite(elite)
ga.EliteConsumer.OnElite(elite)
if ga.shouldExit(elite) {
break
}
time.Sleep(1 * time.Microsecond)
ga.beginSimulation()
newPopulation := ga.createPopulation()
for i := 0; i < ga.populationSize; i += 2 {
g1 := ga.Selector.Go(ga.population, ga.totalFitness)
g2 := ga.Selector.Go(ga.population, ga.totalFitness)
g3, g4 := ga.Mater.Go(g1, g2)
newPopulation[i] = g3
ga.onNewGenomeToSimulate(newPopulation[i])
if (i + 1) < ga.populationSize {
newPopulation[i+1] = g4
ga.onNewGenomeToSimulate(newPopulation[i+1])
}
}
ga.population = newPopulation
ga.syncSimulatingGenomes()
ga.Simulator.OnEndSimulation()
}
return true
}
// GetPopulation returns the population
func (ga *GeneticAlgorithm) GetPopulation() []Genome {
return ga.population
}