-
Notifications
You must be signed in to change notification settings - Fork 1
/
individuals.go
60 lines (52 loc) · 1.01 KB
/
individuals.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
package eago
import (
"sort"
"sync"
)
type Individuals []Individual
type Individual struct {
Chromosome Genome
Fitness float64
Evaluated bool
}
func (indis Individuals) Evaluate(parallel bool) {
var wg sync.WaitGroup
if !parallel {
for i := range indis {
indis[i].Evaluate()
}
} else {
for i := range indis {
wg.Add(1)
go func(i int) {
defer wg.Done()
indis[i].Evaluate()
}(i)
}
wg.Wait()
}
}
func (indis Individuals) SortByFitness() {
var less = func(i, j int) bool { return indis[i].Fitness < indis[j].Fitness }
sort.Slice(indis, less)
}
func (indis Individuals) Clone() Individuals {
clone := make(Individuals, len(indis))
for i := range indis {
clone[i] = indis[i].Clone()
}
return clone
}
func (indi *Individual) Evaluate() {
if !indi.Evaluated {
indi.Fitness = indi.Chromosome.Fitness()
indi.Evaluated = true
}
}
func (indi Individual) Clone() Individual {
clone := Individual{
Chromosome: indi.Chromosome,
Fitness: indi.Fitness,
}
return clone
}