-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpool.go
161 lines (146 loc) · 3.58 KB
/
pool.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
package evoli
import (
"math/rand"
"sync"
)
var (
// ErrPoolEvaluater - all evolutions of a pool must share the same evaluater operator
ErrPoolEvaluater = "ErrPoolEvaluater - all evolution of a pool must share the same evaluater operator"
)
// Pool - solve one problem with different algorithms(with different pros/cons)
// It also make sense to have a pool running multiple instances of the same algorithm asynchronously with smaller populations.
type Pool interface {
Add(Evolution)
Delete(Evolution)
Has(Evolution) bool
Alpha() Individual
Individuals() []Individual
Populations() []Population
Evolutions() []Evolution
Shuffle()
Next() error
NextAsync() error
}
type pool struct {
evaluater Evaluater
evolutions []Evolution
}
// NewPool - creates a Pool
func NewPool(length int) Pool {
return &pool{nil, make([]Evolution, 0, length)}
}
func (p *pool) Add(e Evolution) {
switch p.evaluater {
case nil:
p.evaluater = e.Evaluater()
case e.Evaluater():
break
default:
panic(ErrPoolEvaluater)
}
p.evolutions = append(p.evolutions, e)
}
func (p *pool) Delete(e Evolution) {
length := len(p.evolutions)
for i := range p.evolutions {
if p.evolutions[i] == e {
p.evolutions[i] = p.evolutions[length-1]
p.evolutions[length-1] = nil
p.evolutions = p.evolutions[:length-1]
break
}
}
if len(p.evolutions) == 0 {
p.evaluater = nil
}
}
func (p *pool) Has(e Evolution) bool {
for i := range p.evolutions {
if p.evolutions[i] == e {
return true
}
}
return false
}
func (p *pool) Evolutions() []Evolution {
return p.evolutions
}
func (p *pool) Populations() []Population {
populations := make([]Population, 0, len(p.evolutions))
for _, e := range p.evolutions {
populations = append(populations, e.Population())
}
return populations
}
func (p *pool) Individuals() []Individual {
individualsLen := 0
for _, e := range p.evolutions {
individualsLen += e.Population().Len()
}
individuals := make([]Individual, 0, individualsLen)
for _, e := range p.evolutions {
individuals = append(individuals, e.Population().Slice()...)
}
return individuals
}
func (p *pool) Alpha() Individual {
var alpha Individual
for _, e := range p.evolutions {
outsider := e.Alpha()
if alpha == nil || alpha.Fitness() < outsider.Fitness() {
alpha = outsider
}
}
return alpha
}
func (p *pool) Shuffle() {
tmpPopulations := make([]Population, 0, len(p.evolutions))
nextPopulations := make([]Population, 0, len(p.evolutions))
for _, e := range p.evolutions {
pop := e.Population()
capacity := pop.Cap()
newPop := pop.New(capacity)
tmpPopulations = append(tmpPopulations, newPop)
}
individuals := p.Individuals()
for _, indiv := range individuals {
tmpPopulationsLen := len(tmpPopulations)
i := rand.Intn(tmpPopulationsLen)
tmpPopulations[i].Add(indiv)
if tmpPopulations[i].Len() == tmpPopulations[i].Cap() {
nextPopulations = append(nextPopulations, tmpPopulations[i])
tmpPopulations[i] = tmpPopulations[tmpPopulationsLen-1]
tmpPopulations[tmpPopulationsLen-1] = nil
tmpPopulations = tmpPopulations[:tmpPopulationsLen-1]
}
}
for i := range p.evolutions {
p.evolutions[i].SetPopulation(nextPopulations[i])
}
}
func (p *pool) Next() error {
for _, e := range p.evolutions {
err := e.Next()
if err != nil {
return err
}
}
return nil
}
func (p *pool) NextAsync() error {
evolutionsLen := len(p.evolutions)
wg := sync.WaitGroup{}
wg.Add(evolutionsLen)
var bubbledErr error
for _, e := range p.evolutions {
go func(e Evolution) {
err := e.Next()
if err != nil {
bubbledErr = err
}
wg.Done()
}(e)
}
wg.Wait()
return bubbledErr
}