forked from yaricom/goNEAT
/
genome_reader.go
437 lines (397 loc) · 11.5 KB
/
genome_reader.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package genetics
import (
"io"
"fmt"
"bufio"
"errors"
"strings"
"strconv"
"gopkg.in/yaml.v2"
"github.com/spf13/cast"
"github.com/elmware/goNEAT/neat"
"github.com/elmware/goNEAT/neat/utils"
"github.com/elmware/goNEAT/neat/network"
)
// The interface to define genome reader
type GenomeReader interface {
// Reads one Genome record
Read() (*Genome, error)
}
// Creates reader for Genome data with specified encoding format.
func NewGenomeReader(r io.Reader, encoding GenomeEncoding) (GenomeReader, error) {
switch encoding {
case PlainGenomeEncoding:
return &plainGenomeReader{r: bufio.NewReader(r)}, nil
case YAMLGenomeEncoding:
return &yamlGenomeReader{r: bufio.NewReader(r)}, nil
default:
return nil, ErrUnsupportedGenomeEncoding
}
}
// A PlainGenomeReader reads genome data from plain text file.
type plainGenomeReader struct {
r *bufio.Reader
}
func (pgr *plainGenomeReader) Read() (*Genome, error) {
gnome := Genome{
Traits:make([]*neat.Trait, 0),
Nodes:make([]*network.NNode, 0),
Genes:make([]*Gene, 0),
}
var g_id int
// Loop until file is finished, parsing each line
scanner := bufio.NewScanner(pgr.r)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
parts := strings.SplitN(line, " ", 2)
if len(parts) < 2 {
return nil, errors.New(fmt.Sprintf("Line: [%s] can not be split when reading Genome", line))
}
lr := strings.NewReader(parts[1])
switch parts[0] {
case "trait":
// Read a Trait
new_trait, err := readPlainTrait(lr)
if err != nil {
return nil, err
}
// check that trait ID is unique
if prev_trait := traitWithId(new_trait.Id, gnome.Traits); prev_trait != nil {
return nil, errors.New(
fmt.Sprintf("Trait ID: %d is not unique", new_trait.Id))
}
gnome.Traits = append(gnome.Traits, new_trait)
case "node":
// Read a Network Node
new_node, err := readPlainNetworkNode(lr, gnome.Traits)
if err != nil {
return nil, err
}
// check that node ID is unique
if prev_node := nodeWithId(new_node.Id, gnome.Nodes); prev_node != nil {
return nil, errors.New(
fmt.Sprintf("Node ID: %d is not unique", new_node.Id))
}
gnome.Nodes = append(gnome.Nodes, new_node)
case "gene":
// Read a Gene
new_gene, err := readPlainConnectionGene(lr, gnome.Traits, gnome.Nodes)
if err != nil {
return nil, err
}
gnome.Genes = append(gnome.Genes, new_gene)
case "genomeend":
// Read Genome ID
_, err := fmt.Fscanf(lr, "%d", &g_id)
if err != nil {
return nil, err
}
// save genome ID
gnome.Id = g_id
case "/*":
// read all comments and print it
neat.InfoLog(line)
}
}
return &gnome, nil
}
// The method to read Trait in plain text format
func readPlainTrait(r io.Reader) (*neat.Trait, error) {
nt := neat.NewTrait()
_, err := fmt.Fscanf(r, "%d ", &nt.Id)
if err == nil {
for i := 0; i < neat.Num_trait_params; i++ {
if _, err = fmt.Fscanf(r, "%g ", &nt.Params[i]); err != nil {
return nil, err
}
}
}
return nt, err
}
// Read a Network Node from specified Reader in plain text format
// and applies corresponding trait to it from a list of traits provided
func readPlainNetworkNode(r io.Reader, traits []*neat.Trait) (*network.NNode, error) {
n := network.NewNetworkNode()
buf_r := bufio.NewReader(r)
line, _, err := buf_r.ReadLine()
if err != nil {
return nil, err
}
parts := strings.Split(string(line), " ")
if len(parts) < 4 {
return nil, errors.New(fmt.Sprintf("node line is too short: %d (%s)", len(parts), parts))
}
if n_Id, err := strconv.ParseInt(parts[0], 10, 32); err != nil {
return nil, err
} else {
n.Id = int(n_Id)
}
if trait_id, err := strconv.ParseInt(parts[1], 10, 32); err != nil {
return nil, err
} else {
n.Trait = traitWithId(int(trait_id), traits)
}
if n_NeuronType, err := strconv.ParseInt(parts[3], 10, 32); err != nil {
return nil, err
} else {
n.NeuronType = network.NodeNeuronType(n_NeuronType)
}
if len(parts) == 5 {
n.ActivationType, err = utils.NodeActivators.ActivationTypeFromName(parts[4])
}
return n, err
}
// Reads Gene from reader in plain text format
func readPlainConnectionGene(r io.Reader, traits []*neat.Trait, nodes []*network.NNode) (*Gene, error) {
var traitId, inNodeId, outNodeId int
var inov_num int64
var weight, mut_num float64
var recurrent, enabled bool
_, err := fmt.Fscanf(r, "%d %d %d %g %t %d %g %t ",
&traitId, &inNodeId, &outNodeId, &weight, &recurrent, &inov_num, &mut_num, &enabled)
if err != nil {
return nil, err
}
trait := traitWithId(traitId, traits)
var inNode, outNode *network.NNode
for _, np := range nodes {
if np.Id == inNodeId {
inNode = np
}
if np.Id == outNodeId {
outNode = np
}
}
if trait != nil {
return newGene(network.NewLinkWithTrait(trait, weight, inNode, outNode, recurrent), inov_num, mut_num, enabled), nil
} else {
return newGene(network.NewLink(weight, inNode, outNode, recurrent), inov_num, mut_num, enabled), nil
}
}
// A YAMLGenomeReader reads genome data from YAML encoded text file
type yamlGenomeReader struct {
r *bufio.Reader
}
func (ygr *yamlGenomeReader) Read() (*Genome, error) {
m := make(map[interface{}]interface{})
dec := yaml.NewDecoder(ygr.r)
err := dec.Decode(&m)
if err != nil {
return nil, err
}
gm, ok := m["genome"].(map[interface{}]interface{})
if ok == false {
return nil, errors.New("failed to parse YAML configuration")
}
// read Genome
gen_id, err := cast.ToIntE(gm["id"])
if err != nil {
return nil, err
}
gnome := &Genome{
Id:gen_id,
Traits:make([]*neat.Trait, 0),
Nodes:make([]*network.NNode, 0),
Genes:make([]*Gene, 0),
ControlGenes:make([]*MIMOControlGene, 0),
}
// read traits
traits := gm["traits"].([]interface{})
for _, tr := range traits {
trait, err := readTrait(tr.(map[interface{}]interface{}))
if err != nil {
return nil, err
}
// check that trait ID is unique
if prev_trait := traitWithId(trait.Id, gnome.Traits); prev_trait != nil {
return nil, errors.New(
fmt.Sprintf("Trait ID: %d is not unique", trait.Id))
}
gnome.Traits = append(gnome.Traits, trait)
}
// read nodes
nodes := gm["nodes"].([]interface{})
for _, nd := range nodes {
node, err := readNNode(nd.(map[interface{}]interface{}), gnome.Traits)
if err != nil {
return nil, err
}
// check that node ID is unique
if prev_node := nodeWithId(node.Id, gnome.Nodes); prev_node != nil {
return nil, errors.New(
fmt.Sprintf("Node ID: %d is not unique", node.Id))
}
gnome.Nodes = append(gnome.Nodes, node)
}
// read Genes
genes := gm["genes"].([]interface{})
for _, g := range genes {
gene, err := readGene(g.(map[interface{}]interface{}), gnome.Traits, gnome.Nodes)
if err != nil {
return nil, err
}
gnome.Genes = append(gnome.Genes, gene)
}
// read MIMO control genes
mimoGenes := gm["modules"]
if mimoGenes != nil {
for _, mg := range mimoGenes.([]interface{}) {
mGene, err := readMIMOControlGene(mg.(map[interface{}]interface{}), gnome.Traits, gnome.Nodes)
if err != nil {
return nil, err
}
// check that control node ID is unique
if prev_node := nodeWithId(mGene.ControlNode.Id, gnome.Nodes); prev_node != nil {
return nil, errors.New(
fmt.Sprintf("Control node ID: %d is not unique", mGene.ControlNode.Id))
}
gnome.ControlGenes = append(gnome.ControlGenes, mGene)
}
}
return gnome, nil
}
// Reads gene configuration
func readGene(conf map[interface{}]interface{}, traits []*neat.Trait, nodes []*network.NNode) (*Gene, error) {
traitId := conf["trait_id"].(int)
inNodeId := conf["src_id"].(int)
outNodeId := conf["tgt_id"].(int)
inov_num, err := cast.ToInt64E(conf["innov_num"])
if err != nil {
return nil, err
}
weight, err := cast.ToFloat64E(conf["weight"])
if err != nil {
return nil, err
}
mut_num, err := cast.ToFloat64E(conf["mut_num"])
if err != nil {
return nil, err
}
recurrent, err := cast.ToBoolE(conf["recurrent"])
if err != nil {
return nil, err
}
enabled, err := cast.ToBoolE(conf["enabled"])
if err != nil {
return nil, err
}
trait := traitWithId(traitId, traits)
var inNode, outNode *network.NNode
for _, np := range nodes {
if np.Id == inNodeId {
inNode = np
}
if np.Id == outNodeId {
outNode = np
}
}
if trait != nil {
return newGene(network.NewLinkWithTrait(trait, weight, inNode, outNode, recurrent), inov_num, mut_num, enabled), nil
} else {
return newGene(network.NewLink(weight, inNode, outNode, recurrent), inov_num, mut_num, enabled), nil
}
}
// Reads MIMOControlGene configuration
func readMIMOControlGene(conf map[interface{}]interface{}, traits []*neat.Trait, nodes []*network.NNode) (gene *MIMOControlGene, err error) {
// read control node parameters
control_node := network.NewNetworkNode()
control_node.Id = conf["id"].(int)
control_node.NeuronType = network.HiddenNeuron
// set activation function
activation := conf["activation"].(string)
control_node.ActivationType, err = utils.NodeActivators.ActivationTypeFromName(activation)
if err != nil {
return nil, err
}
// set associated Trait
traitId := conf["trait_id"].(int)
trait := traitWithId(traitId, traits)
control_node.Trait = trait
// read MIMO gene parameters
inov_num, err := cast.ToInt64E(conf["innov_num"])
if err != nil {
return nil, err
}
mut_num, err := cast.ToFloat64E(conf["mut_num"])
if err != nil {
return nil, err
}
enabled, err := cast.ToBoolE(conf["enabled"])
if err != nil {
return nil, err
}
// read input links
inNodes, err := cast.ToSliceE(conf["inputs"])
if err != nil {
return nil, err
}
control_node.Incoming = make([]*network.Link, len(inNodes))
for i, mn := range inNodes {
n := mn.(map[interface{}]interface{})
node_id, err := cast.ToIntE(n["id"])
if err != nil {
return nil, err
}
node := nodeWithId(node_id, nodes)
if node != nil {
control_node.Incoming[i] = network.NewLink(1.0, node, control_node, false)
} else {
return nil, errors.New(fmt.Sprintf("no MIMO input node with id: %d can be found for module: %d",
node_id, control_node.Id))
}
}
// read output links
outNodes, err := cast.ToSliceE(conf["outputs"])
if err != nil {
return nil, err
}
control_node.Outgoing = make([]*network.Link, len(outNodes))
for i, mn := range outNodes {
n := mn.(map[interface{}]interface{})
node_id, err := cast.ToIntE(n["id"])
if err != nil {
return nil, err
}
node := nodeWithId(node_id, nodes)
if node != nil {
control_node.Outgoing[i] = network.NewLink(1.0, control_node, node, false)
} else {
return nil, errors.New(fmt.Sprintf("no MIMO output node with id: %d can be found for module: %d",
node_id, control_node.Id))
}
}
// build gene
gene = NewMIMOGene(control_node, inov_num, mut_num, enabled)
return gene, nil
}
// Reads NNode configuration
func readNNode(conf map[interface{}]interface{}, traits []*neat.Trait) (*network.NNode, error) {
nd := network.NewNetworkNode()
nd.Id = conf["id"].(int)
trait_id := conf["trait_id"].(int)
nd.Trait = traitWithId(trait_id, traits)
type_name := conf["type"].(string)
var err error
nd.NeuronType, err = network.NeuronTypeByName(type_name)
if err != nil {
return nil, err
}
activation := conf["activation"].(string)
nd.ActivationType, err = utils.NodeActivators.ActivationTypeFromName(activation)
return nd, err
}
// Reads Trait configuration
func readTrait(conf map[interface{}]interface{}) (*neat.Trait, error) {
nt := neat.NewTrait()
nt.Id = conf["id"].(int)
params_c := cast.ToSlice(conf["params"])
var err error
for i, p := range params_c {
nt.Params[i], err = cast.ToFloat64E(p)
if err != nil {
return nil, err
}
}
return nt, nil
}