-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment.go
280 lines (239 loc) · 7.17 KB
/
experiment.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright (c) 2015, Brian Hummer (brian@redq.me)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package neat
import (
"bytes"
"encoding/json"
"fmt"
"sort"
. "github.com/rqme/errors"
)
type ExperimentSettings interface {
Iterations() int
Traits() Traits
FitnessType() FitnessType
ExperimentName() string
}
// Experiment provides the definition of how to solve the problem using NEAT
type Experiment struct {
ExperimentSettings
ctx Context
// State
population Population `neat:"state"`
cache map[int]Phenome
best Genome
iteration int
stopped bool
}
func (e *Experiment) SetContext(x Context) error {
e.ctx = x
e.ctx.State()["population"] = &e.population
return nil
}
func (e Experiment) Context() Context { return e.ctx }
func (e Experiment) Population() Population { return e.population }
func (e Experiment) Stopped() bool { return e.stopped }
func (e Experiment) Iteration() int { return e.iteration }
// String returns a description of the experiment
func (e Experiment) String() string {
return fmt.Sprintf("Experiment %s at iteration %d has best genome %d with fitness %f", e.ExperimentName(), e.iteration, e.best.ID, e.best.Fitness)
}
// Runs a configured experiment. If restoring, including just the configuration, this must be done
// prior to calling Run.
func Run(e *Experiment) error {
// Ensure this is a valid experiment
if e.Iterations() < 1 {
return fmt.Errorf("Invalid value for Iterations: %d", e.Iterations())
}
// Iterate the experiment
for e.iteration = 0; e.iteration < e.Iterations(); e.iteration++ {
//fmt.Println("iteration", e.iteration, "best", e.best.Fitness)
// Reset the innovation history
//e.mrk.Reset()
// Advance the population
if err := advance(e); err != nil {
return fmt.Errorf("Could not advance the population: %v", err)
}
// Update the phenome cache
if err := updateCache(e); err != nil {
return fmt.Errorf("Couuld not update cache in the experiment: %v", err)
}
// Evaluate the population
if stop, err := search(e); err != nil {
return fmt.Errorf("Error evaluating the population: %v", err)
} else if stop {
e.stopped = true
break
}
}
// Take one last archive and return
if err := e.ctx.Visualizer().Visualize(e.population); err != nil {
return fmt.Errorf("Could not visualize the experiment for the last time: %v", err)
}
if err := e.ctx.Archiver().Archive(e.ctx); err != nil {
return fmt.Errorf("Could not take last archive of experiment: %v", err)
}
return nil
}
// Advances the experiment to the next generation
func advance(e *Experiment) error {
curr := e.population
next, err := e.ctx.Generator().Generate(curr)
if err != nil {
return err
}
if next.Generation > curr.Generation {
if err = e.ctx.Visualizer().Visualize(e.population); err != nil {
return err
}
if err = e.ctx.Archiver().Archive(e.ctx); err != nil {
return err
}
if err = updateSettings(e, e.best); err != nil {
return err
}
}
e.population = next
return nil
}
// Update the settings based on the traits of a genome
func updateSettings(e *Experiment, g Genome) error {
cnt := 0
b := bytes.NewBufferString("{")
for t, trait := range e.Traits() {
if trait.IsSetting {
if cnt > 0 {
b.WriteString(",\n")
}
b.WriteString(fmt.Sprintf(`"%s": %f`, trait.Name, g.Traits[t]))
cnt += 1
}
}
b.WriteString("\n}")
enc := json.NewEncoder(b)
return enc.Encode(&e.ctx)
}
// Updates the cache of phenomes
func updateCache(e *Experiment) (err error) {
var old map[int]Phenome
if len(e.cache) == 0 {
old = make(map[int]Phenome, 0)
} else {
old = e.cache
}
e.cache = make(map[int]Phenome, len(e.population.Genomes))
errs := new(Errors)
pc := make(chan Phenome)
cnt := 0
for _, g := range e.population.Genomes {
if p, ok := old[g.ID]; ok {
e.cache[g.ID] = p
} else {
cnt += 1
go func(g Genome) {
p, err := e.ctx.Decoder().Decode(g)
if err != nil {
errs.Add(fmt.Errorf("Unable to decode genome [%d]: %v", g.ID, err))
}
pc <- p
}(g)
}
}
for i := 0; i < cnt; i++ {
p := <-pc
if p != nil {
e.cache[p.ID()] = p
}
}
return errs.Err()
}
// Searches the population and updates the genomes' fitness
func search(e *Experiment) (stop bool, err error) {
// Map the genomes for convenience
m := make(map[int]int, len(e.population.Genomes))
for i, g := range e.population.Genomes {
m[g.ID] = i
}
// Perform the search
phenomes := make([]Phenome, 0, len(e.cache))
for _, p := range e.cache {
phenomes = append(phenomes, p)
}
for _, h := range []interface{}{e.ctx.Searcher(), e.ctx.Evaluator()} {
if ph, ok := h.(Phenomable); ok {
if err = ph.SetPhenomes(phenomes); err != nil {
return
}
}
if sh, ok := h.(Setupable); ok {
if err = sh.Setup(); err != nil {
return
}
}
}
var rs Results
if rs, err = e.ctx.Searcher().Search(phenomes); err != nil {
return
}
for _, h := range []interface{}{e.ctx.Evaluator(), e.ctx.Searcher()} {
if th, ok := h.(Takedownable); ok {
if err = th.Takedown(); err != nil {
return
}
}
}
// Update the fitnesses
var best Genome
errs := new(Errors)
// := make([]float64, len(e.population.Genomes))
// TODO: make this concurrent
for _, r := range rs {
i := m[r.ID()]
if err = r.Err(); err != nil {
errs.Add(fmt.Errorf("Error updating fitness for genome [%d]: %v", r.ID(), r.Err()))
}
e.population.Genomes[i].Fitness = r.Fitness()
if imp, ok := r.(Improvable); ok {
e.population.Genomes[i].Improvement = imp.Improvement()
} else {
e.population.Genomes[i].Improvement = e.population.Genomes[i].Fitness
}
//fit[i] = e.population.Genomes[i].Fitness
if e.population.Genomes[i].Fitness > best.Fitness {
best = e.population.Genomes[i]
}
stop = stop || r.Stop()
}
// Update the best genome
if errs.Err() == nil {
if e.FitnessType() == Absolute {
if best.Fitness > e.best.Fitness {
e.best = best
}
} else {
e.best = best
}
}
// Leave the genomes sorted by their fitness descending
sort.Sort(sort.Reverse(e.population.Genomes))
return stop, errs.Err()
}