forked from MaxHalford/eaopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_test.go
81 lines (67 loc) · 1.46 KB
/
setup_test.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
package gago
import (
"log"
"math"
"math/rand"
"os"
)
var (
ga = GA{
GenomeFactory: NewVector,
NPops: 2,
PopSize: 50,
Model: ModGenerational{
Selector: SelTournament{
NContestants: 3,
},
MutRate: 0.5,
},
Migrator: MigRing{10},
MigFrequency: 3,
Logger: log.New(os.Stdin, "", log.Ldate|log.Ltime),
}
nbrGenerations = 5 // Initial number of generations to enhance
)
func init() {
ga.Initialize()
for i := 0; i < nbrGenerations; i++ {
ga.Enhance()
}
}
type Vector []float64
// Implement the Genome interface
func (X Vector) Evaluate() float64 {
var sum float64
for _, x := range X {
sum += x
}
return sum
}
func (X Vector) Mutate(rng *rand.Rand) {
MutNormalFloat64(X, 0.5, rng)
}
func (X Vector) Crossover(Y Genome, rng *rand.Rand) (Genome, Genome) {
var o1, o2 = CrossUniformFloat64(X, Y.(Vector), rng)
return Vector(o1), Vector(o2)
}
func (X Vector) Clone() Genome {
var XX = make(Vector, len(X))
copy(XX, X)
return XX
}
func NewVector(rng *rand.Rand) Genome {
return Vector(InitUnifFloat64(4, -10, 10, rng))
}
// Minkowski distance with p = 1
func l1Distance(x1, x2 Individual) (dist float64) {
var g1 = x1.Genome.(Vector)
var g2 = x2.Genome.(Vector)
for i := range g1 {
dist += math.Abs(g1[i] - g2[i])
}
return
}
// Identity model
type ModIdentity struct{}
func (mod ModIdentity) Apply(pop *Population) error { return nil }
func (mod ModIdentity) Validate() error { return nil }