This repository has been archived by the owner on Aug 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ga.go
235 lines (222 loc) · 6.77 KB
/
ga.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
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
package gago
import (
"errors"
"log"
"math"
"math/rand"
"sort"
"time"
)
// A GA contains population which themselves contain individuals.
type GA struct {
// Required fields
NewGenome NewGenome `json:"-"`
NPops int `json:"-"` // Number of Populations
PopSize int `json:"-"` // Number of Individuls per Population
Model Model `json:"-"`
// Optional fields
NBest int `json:"-"` // Length of HallOfFame
Migrator Migrator `json:"-"`
MigFrequency int `json:"-"` // Frequency at which migrations occur
Speciator Speciator `json:"-"`
Logger *log.Logger `json:"-"`
Callback func(ga *GA) `json:"-"`
RNG *rand.Rand `json:"-"`
ParallelEval bool `json:"-"`
// Fields generated at runtime
Populations Populations `json:"populations"`
HallOfFame Individuals `json:"hall_of_fame"` // Sorted best Individuals ever encountered
Age time.Duration `json:"duration"` // Duration during which the GA has been evolved
Generations int `json:"generations"` // Number of generations the GA has been evolved
}
// Validate the parameters of a GA to ensure it will run correctly; some
// settings or combination of settings may be incoherent during runtime.
func (ga GA) Validate() error {
// Check the NewGenome presence
if ga.NewGenome == nil {
return errors.New("NewGenome cannot be nil")
}
// Check the number of populations is higher than 0
if ga.NPops < 1 {
return errors.New("NPops should be higher than 0")
}
// Check the number of individuals per population is higher than 0
if ga.PopSize < 1 {
return errors.New("PopSize should be higher than 0")
}
// Check the model presence
if ga.Model == nil {
return errors.New("Model cannot be nil")
}
// Check the model is valid
var modelErr = ga.Model.Validate()
if modelErr != nil {
return modelErr
}
// Check the migration frequency if a Migrator has been provided
if ga.Migrator != nil && ga.MigFrequency < 1 {
return errors.New("MigFrequency should be strictly higher than 0")
}
// Check the speciator is valid if it has been provided
if ga.Speciator != nil {
if specErr := ga.Speciator.Validate(); specErr != nil {
return specErr
}
}
// No error
return nil
}
// Find the best current Individual in each population and then compare the best
// overall Individual to the current best Individual. The Individuals in each
// population are expected to be sorted.
func updateHallOfFame(hof Individuals, indis Individuals) {
var k = len(hof)
// Start by finding the current best Individual
for _, indi := range indis[:min(k, len(indis))] {
// Find if and where the Individual should fit in the hall of fame
var (
f = func(i int) bool { return indi.Fitness < hof[i].Fitness }
i = sort.Search(k, f)
)
if i < k {
// Shift the hall of fame to the right
copy(hof[i+1:], hof[i:])
// Insert the new Individual
hof[i] = indi
}
}
}
// Initialized indicates if the GA has been initialized or not.
func (ga GA) Initialized() bool {
if len(ga.Populations) != ga.NPops {
return false
}
return true
}
// Initialize each population in the GA and assign an initial fitness to each
// individual in each population. Running Initialize after running Evolve will
// reset the GA entirely.
func (ga *GA) Initialize() {
// Check the NBest field
if ga.NBest < 1 {
ga.NBest = 1
}
// Initialize the random number generator if it hasn't been set
if ga.RNG == nil {
ga.RNG = rand.New(rand.NewSource(time.Now().UnixNano()))
}
ga.Populations = make([]Population, ga.NPops)
for i := range ga.Populations {
// Generate a Population
ga.Populations[i] = newPopulation(ga.PopSize, ga.NewGenome, ga.RNG)
// Evaluate its Individuals
ga.Populations[i].Individuals.Evaluate(ga.ParallelEval)
// Sort it's Individuals
ga.Populations[i].Individuals.SortByFitness()
// Log current statistics if a logger has been provided
if ga.Logger != nil {
ga.Populations[i].Log(ga.Logger)
}
}
// Initialize HallOfFame
ga.HallOfFame = make(Individuals, ga.NBest)
for i := range ga.HallOfFame {
ga.HallOfFame[i] = Individual{Fitness: math.Inf(1)}
}
for _, pop := range ga.Populations {
updateHallOfFame(ga.HallOfFame, pop.Individuals)
}
// Execute the callback if it has been set
if ga.Callback != nil {
ga.Callback(ga)
}
}
// Evolve each population in the GA. The population level operations are done
// in parallel with a wait group. After all the population operations have been
// run, the GA level operations are run.
func (ga *GA) Evolve() error {
var start = time.Now()
ga.Generations++
// Check the GA has been initialized
if !ga.Initialized() {
return errors.New("The GA has not been initialized")
}
// Migrate the individuals between the populations if there are at least 2
// Populations and that there is a migrator and that the migration frequency
// divides the generation count
if len(ga.Populations) > 1 && ga.Migrator != nil && ga.Generations%ga.MigFrequency == 0 {
ga.Migrator.Apply(ga.Populations, ga.RNG)
}
var f = func(pop *Population) error {
var err error
// Apply speciation if a positive number of species has been specified
if ga.Speciator != nil {
err = pop.speciateEvolveMerge(ga.Speciator, ga.Model)
if err != nil {
return err
}
} else {
// Else apply the evolution model to the entire population
err = ga.Model.Apply(pop)
if err != nil {
return err
}
}
// Evaluate and sort
pop.Individuals.Evaluate(ga.ParallelEval)
pop.Individuals.SortByFitness()
pop.Age += time.Since(start)
pop.Generations++
// Log current statistics if a logger has been provided
if ga.Logger != nil {
pop.Log(ga.Logger)
}
return err
}
var err = ga.Populations.Apply(f, true)
if err != nil {
return err
}
// Update HallOfFame
for _, pop := range ga.Populations {
updateHallOfFame(ga.HallOfFame, pop.Individuals)
}
// Execute the callback if it has been set
if ga.Callback != nil {
ga.Callback(ga)
}
ga.Age += time.Since(start)
// No error
return nil
}
func (pop *Population) speciateEvolveMerge(spec Speciator, model Model) error {
var (
species, err = spec.Apply(pop.Individuals, pop.RNG)
pops = make([]Population, len(species))
)
if err != nil {
return err
}
// Create a subpopulation from each specie so that the evolution Model can
// be applied to it.
for i, specie := range species {
pops[i] = Population{
Individuals: specie,
Age: pop.Age,
Generations: pop.Generations,
ID: randString(len(pop.ID), pop.RNG),
RNG: pop.RNG,
}
err = model.Apply(&pops[i])
if err != nil {
return err
}
}
// Merge each species back into the original population
var i int
for _, subpop := range pops {
copy(pop.Individuals[i:i+len(subpop.Individuals)], subpop.Individuals)
i += len(subpop.Individuals)
}
return nil
}