-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.go
88 lines (69 loc) · 2.92 KB
/
helpers.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
package evo
// Crosser creates a new child from the parents through crossover (or cloning if there is only one parent). The crosser is not responsible for mutation or for assigning the genome an ID or to a species.
type Crosser interface {
Cross(parents ...Genome) (child Genome, err error)
}
// Evaluator utilises the network provided and returns its fitness (or error) as a result
type Evaluator interface {
Evaluate(Phenome) (Result, error)
}
// Matrix descibes data organised as a matrix. It mimics a subset of the signature of [gonum's mat.Matrix](https://godoc.org/gonum.org/v1/gonum/mat) which allows directly passing matrices from that package as inputs as well as any other type that implements it, such as [sparse](https://godoc.org/github.com/james-bowman/sparse). Network implementations, however, may expect a specific type and throw an error if they cannot convert to the desired type.
type Matrix interface {
// Dims returns the dimensions of a Matrix.
Dims() (r, c int)
// At returns the value of a matrix element at row i, column j.
// It will panic if i or j are out of bounds for the matrix.
At(i, j int) float64
}
// Mutator changes the genome's encoding (nodes, conns, or traits)
type Mutator interface {
Mutate(*Genome) error
}
// Populator provides a popluation from which the experiment will begin
type Populator interface {
Populate() (Population, error)
}
// Seeder provides an unitialised genome from which to construct a new population
type Seeder interface {
Seed() (Genome, error)
}
// Searcher processes each phenome through the evaluator and returns the result
type Searcher interface {
Search(Evaluator, []Phenome) ([]Result, error)
}
// Selector examines a population returns the current genomes who will continue and those that will become parents
type Selector interface {
Select(Population) (continuing []Genome, parents [][]Genome, err error)
}
// Speciator assigns the population's genomes to a species, creating and destroying species as necessary.
type Speciator interface {
Speciate(*Population) error
}
// Transcriber creates the decoded substrate from the encoded one.
type Transcriber interface {
Transcribe(Substrate) (Substrate, error)
}
// Translator creates a new network from defintions contained in the nodes and connections
type Translator interface {
Translate(Substrate) (Network, error)
}
// Mutators collection which acts as a single mutator. Component mutators will be called in order
// until the complexity of the genome changes.
type Mutators []Mutator
// Mutate the genome with the composite mutators
func (m Mutators) Mutate(g *Genome) error {
// Record the starting complexity
n := g.Complexity()
// Iterate the mutators in order
for _, x := range m {
// Use the current mutator on the genome
if err := x.Mutate(g); err != nil {
return err
}
// The complexity has changed so do not continue with the remaining mutations
if g.Complexity() != n {
break
}
}
return nil
}