This repository has been archived by the owner on Aug 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup_test.go
103 lines (82 loc) · 2.02 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package gago
import (
"errors"
"log"
"math"
"math/rand"
"os"
"time"
)
var (
ga = GA{
NewGenome: 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
)
// newRand returns a new random number generator with a random seed.
func newRand() *rand.Rand {
return rand.New(rand.NewSource(time.Now().UnixNano()))
}
func init() {
ga.Initialize()
for i := 0; i < nbrGenerations; i++ {
ga.Evolve()
}
}
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) {
CrossUniformFloat64(X, Y.(Vector), rng)
}
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 }
// Runtime error model
type ModRuntimeError struct{}
func (mod ModRuntimeError) Apply(pop *Population) error { return errors.New("") }
func (mod ModRuntimeError) Validate() error { return nil }
// Runtime error speciator
type SpecRuntimeError struct{}
func (spec SpecRuntimeError) Apply(indis Individuals, rng *rand.Rand) ([]Individuals, error) {
return []Individuals{indis, indis}, errors.New("")
}
func (spec SpecRuntimeError) Validate() error { return nil }