-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpopulation.go
217 lines (191 loc) · 4.87 KB
/
population.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
package evoli
import (
"errors"
"sort"
)
// Population contains the current set of solutions seen as Individual
type Population interface {
sort.Interface
Sort()
Cap() int
SetCap(int)
Add(...Individual)
Get(int) Individual
RemoveAt(int)
Remove(...Individual)
Replace(int, Individual)
Min() Individual
Max() Individual
Has(...Individual) bool
IndexOf(Individual) (int, error)
Each(func(item Individual) bool)
Slice() []Individual
New(cap int) Population
Close()
}
var (
// ErrCapacity -
ErrCapacity = errors.New("ErrCapacity - capacity must be >= 1")
// ErrIndexOutOfBounds -
ErrIndexOutOfBounds = errors.New("ErrIndexOutOfBounds")
// ErrNotFound -
ErrNotFound = errors.New("ErrNotFound")
)
// population is a set of individuals in population genetics.
type population []Individual
// NewPopulation is population constructor
func NewPopulation(capacity int) Population {
if capacity < 1 {
panic(ErrCapacity)
}
pop := population(make([]Individual, 0, capacity))
return &pop
}
// Len returns the current livings count of a population
func (pop *population) Len() int {
return len(*pop)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (pop *population) Less(i, j int) bool {
indivi := pop.Get(i)
indivj := pop.Get(j)
return indivi.Fitness() >= indivj.Fitness()
}
// Swap swaps the elements with indexes i and j.
func (pop *population) Swap(i, j int) {
if i != j && i >= 0 && j >= 0 && i < pop.Len() && j < pop.Len() {
tmp := (*pop)[i]
(*pop)[i] = (*pop)[j]
(*pop)[j] = tmp
}
}
// Sort sort the population
func (pop *population) Sort() {
sort.Sort(pop)
}
// Cap returns the population capacity
func (pop *population) Cap() int {
return cap(*pop)
}
// SetCap set the resize the population capacity
func (pop *population) SetCap(newCap int) {
if newCap < 0 {
panic(ErrCapacity)
}
currentCap := pop.Cap()
if newCap != currentCap {
tmp := *pop
switch {
case newCap < currentCap:
tmp = (*pop)[0:newCap]
*pop = make([]Individual, newCap)
case newCap > currentCap:
*pop = make([]Individual, pop.Len(), newCap)
}
copy(tmp, *pop)
}
}
// Add adds an individual to a population. If the populagtion has already reached its capacity, capacity is incremented.
func (pop *population) Add(indiv ...Individual) {
*pop = append(*pop, indiv...)
}
// Get returns the individual at index i
func (pop *population) Get(i int) Individual {
err := pop.checkIndex(i)
if err != nil {
panic(err)
}
return (*pop)[i]
}
// RemoveAt removesthe individual at index i without preserving order
func (pop *population) RemoveAt(i int) {
err := pop.checkIndex(i)
if err != nil {
panic(err)
}
popLen := pop.Len()
(*pop)[i] = (*pop)[popLen-1]
(*pop)[popLen-1] = nil
*pop = (*pop)[:popLen-1]
}
// Remove removes all given individuals without preserving order
func (pop *population) Remove(individuals ...Individual) {
for _, indiv := range individuals {
i, err := pop.IndexOf(indiv)
if err == nil {
pop.RemoveAt(i)
}
}
}
// Replace replaces and returns the individual at index i by the substitute
func (pop *population) Replace(i int, substitute Individual) {
err := pop.checkIndex(i)
if err != nil {
panic(err)
}
(*pop)[i] = substitute
}
// Min returns the least Resilent individual
func (pop *population) Min() Individual {
return pop.extremum(false)
}
// Max returns the most Resilent individual
func (pop *population) Max() Individual {
return pop.extremum(true)
}
func (pop *population) extremum(greaterThan bool) Individual {
extremum := pop.Get(0)
length := pop.Len()
for i := 1; i < length; i++ {
indiv := pop.Get(i)
if (greaterThan && indiv.Fitness() > extremum.Fitness()) || (!greaterThan && indiv.Fitness() < extremum.Fitness()) {
extremum = indiv
}
}
return extremum
}
// Has return true if the specified individual is in the population
func (pop *population) Has(individuals ...Individual) bool {
has := true
for _, indiv := range individuals {
_, err := pop.IndexOf(indiv)
has = has && err == nil
}
return has
}
// IndexOf returns the inde of the specified individual if it exists
func (pop *population) IndexOf(indiv Individual) (int, error) {
for i, current := range *pop {
if current.Equal(indiv) {
return i, nil
}
}
return -1, ErrNotFound
}
// Each traverse the population and execute given callback on each individual. Stops if the callbak return false.
func (pop *population) Each(f func(indiv Individual) bool) {
for _, individual := range *pop {
resume := f(individual)
if !resume {
break
}
}
}
// Slice returns the population as []Individual
func (pop *population) Slice() []Individual {
return *pop
}
func (pop *population) New(cap int) Population {
return NewPopulation(cap)
}
func (pop *population) checkIndex(i int) error {
if i < 0 || i >= pop.Len() {
return ErrIndexOutOfBounds
}
return nil
}
func (pop *population) Close() {
*pop = (*pop)[:0]
*pop = nil
}