/
reproduce.go
47 lines (42 loc) · 1.02 KB
/
reproduce.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
package evolve
import (
"math/rand"
)
var reproduceRand *rand.Rand
/*
ReproduceSeed seeds the shared randomness source for Reproduce invocations. If
not invoked, the shared source will be initialized with 1, just like math/rand's
shared source.
*/
func ReproduceSeed(seed int64) {
reproduceRand = rand.New(rand.NewSource(seed))
}
/*
Reproduce takes a *Genome and returns a new *Genome with a single point
mutation, using a shared source for randomness.
*/
func Reproduce(parent *Genome) *Genome {
if reproduceRand == nil {
ReproduceSeed(1)
}
return ReproduceRand(parent, reproduceRand)
}
/*
ReproduceRand takes a *Genome and returns a new *Genome with a single point
mutation, using the passed *Rand for randomness.
*/
func ReproduceRand(parent *Genome, rand *rand.Rand) *Genome {
if len(*parent) == 0 {
panic(InvalidGenomeError(*parent))
}
child := make(Genome, len(*parent))
copy(child, *parent)
i := rand.Intn(len(child))
pos := rand.Float32() < 0.5
if pos {
child[i] += 1
} else {
child[i] -= 1
}
return &child
}