-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgenetic.go
188 lines (174 loc) · 4.04 KB
/
genetic.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
package evoli
import (
"errors"
"math/rand"
"sync"
)
// genetic is a genetic algorithm implementation
type genetic struct {
evolution
selecter Selecter
SurvivorSize int
crosser Crosser
mutater Mutater
MutationProbability float64
}
var (
// ErrSurvivorSize - survivorSize < 1
ErrSurvivorSize = errors.New("ErrSurvivorSize - survivorSize must be >= 1")
// ErrMutationProb - 0<= mutationProbability <= 1
ErrMutationProb = errors.New("ErrMutationProb - mutation probability must be 0 <= mutationProbability <= 1")
)
// NewGenetic - constructor for Genetic Algorithm
func NewGenetic(pop Population, s Selecter, survivorSize int, c Crosser, m Mutater, mutationProbability float64, e Evaluater) Evolution {
if survivorSize < 1 {
panic(ErrSurvivorSize)
}
if mutationProbability < 0 || mutationProbability > 1 {
panic(ErrMutationProb)
}
return &genetic{newEvolution(pop, e), s, survivorSize, c, m, mutationProbability}
}
// Next takes a population and produce a the new generation of this population
func (g *genetic) Next() error {
err := g.evaluation(g.pop)
if err != nil {
return err
}
survivors, deads, err := g.selecter.Select(g.pop, g.SurvivorSize)
if err != nil {
return err
}
if deads != nil {
deads.Close()
}
offsprings, err := g.crossovers(survivors)
if err != nil {
return err
}
offsprings, err = g.mutations(offsprings)
if err != nil {
return err
}
survivors.Add(offsprings.Slice()...)
g.pop = survivors
return nil
}
func (g *genetic) evaluation(pop Population) error {
var (
length = pop.Len()
wg = sync.WaitGroup{}
bubbledErr error
)
for i := 0; i < length; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
individual := pop.Get(i)
fitness, err := g.evaluater.Evaluate(individual)
if err != nil {
bubbledErr = err
return
}
individual.SetFitness(fitness)
}(i)
}
wg.Wait()
if bubbledErr != nil {
return bubbledErr
}
return nil
}
func (g *genetic) crossovers(pop Population) (Population, error) {
var (
capacity = pop.Cap() - pop.Len()
offsprings = NewPopulation(capacity)
mut sync.Mutex
wg = sync.WaitGroup{}
bubbledErr error
)
for index := 0; index < capacity; index += 2 {
wg.Add(1)
go func(index int) {
defer wg.Done()
var i, j = rand.Intn(pop.Len()), rand.Intn(pop.Len())
if i == j {
switch i {
case pop.Len() - 1:
j = i - 1
default:
j = i + 1
}
}
indiv1, indiv2 := pop.Get(i), pop.Get(j)
child1, child2, err := g.crosser.Cross(indiv1, indiv2)
if err != nil {
bubbledErr = err
return
}
mut.Lock()
offsprings.Add(child1)
if index+1 < capacity {
offsprings.Add(child2)
}
mut.Unlock()
}(index)
}
wg.Wait()
if bubbledErr != nil {
return nil, bubbledErr
}
return offsprings, nil
}
func (g *genetic) mutations(pop Population) (Population, error) {
var (
wg = sync.WaitGroup{}
bubbledErr error
)
for i := 0; i < pop.Len(); i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
indiv := pop.Get(i)
mutant, err := g.mutater.Mutate(indiv, g.MutationProbability)
if err != nil {
bubbledErr = err
return
}
pop.Replace(i, mutant)
}(i)
}
wg.Wait()
if bubbledErr != nil {
return nil, bubbledErr
}
return pop, nil
}
type geneticSync struct {
genetic
sync.RWMutex
}
// NewGeneticSync - constructor for Genetic Algorithm (sync impl)
func NewGeneticSync(pop Population, s Selecter, survivorSize int, c Crosser, m Mutater, mutationProbability float64, e Evaluater) Evolution {
return &geneticSync{*NewGenetic(pop, s, survivorSize, c, m, mutationProbability, e).(*genetic), sync.RWMutex{}}
}
func (s *geneticSync) Next() error {
s.Lock()
defer s.Unlock()
return s.genetic.Next()
}
func (s *geneticSync) Population() Population {
s.RLock()
defer s.RUnlock()
return s.genetic.Population()
}
func (s *geneticSync) SetPopulation(pop Population) {
s.Lock()
defer s.Unlock()
s.genetic.SetPopulation(pop)
}
func (s *geneticSync) Alpha() Individual {
s.RLock()
defer s.RUnlock()
return s.genetic.Alpha()
}