This repository has been archived by the owner on Dec 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgenome_new.go
197 lines (167 loc) · 3.67 KB
/
genome_new.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
package neat
import (
"math/rand"
)
type NodeType int
const (
Sensor NodeType = iota
Output
Hidden
)
type ActivationFunc int
const (
Linear ActivationFunc = iota
Sigmoid
Tanh
ReLU
Sine
Gaussian
)
type NodeGene struct {
nodeId int
nodeType NodeType
activation ActivationFunc
}
func NewNodeGene(nodeId int, nodeType NodeType, activation ActivationFunc) *NodeGene {
return &NodeGene{
nodeId: nodeId,
nodeType: nodeType,
activation: activation,
}
}
func (n *NodeGene) Id() int {
return n.nodeId
}
func (n *NodeGene) Type() NodeType {
return n.nodeType
}
func (n *NodeGene) Activation() ActivationFunc {
return n.activation
}
func (n *NodeGene) String() string {
str := fmt.Sprintf("Node(%d, ", n.nodeId)
// node type
switch n.nodeType {
case Sensor:
str += "Sensor, "
case Output:
str += "Output, "
case Hidden:
str += "Hidden, "
}
// activation function
switch n.activation {
case Linear:
str += "Linear"
case Sigmoid:
str += "Sigmoid"
case Tanh:
str += "Tanh"
case ReLU:
str += "ReLU"
case Sine:
str += "Sine"
case Gaussian:
str += "Gaussian"
}
str += ")"
return str
}
type ConnectionGene struct {
innovId int // innovation number
src *NodeGene // source node
dst *NodeGene // destination node
weight float64 // connection weight
expressed bool // connection expression
}
func NewConnectionGene(innovID int, src, dst *NodeGene) *ConnectionGene {
return &ConnectionGene{
innovID: innovID,
src: src,
dst: dst,
weight: randWeight(),
expressed: true,
}
}
func randWeight() float64 {
return rand.NormFloat64() * 3.0
}
func (c *ConnectionGene) Id() int {
return c.innovId
}
func (c *ConnectionGene) Src() *NodeGene {
return c.src
}
func (c *ConnectionGene) Dst() *NodeGene {
return c.dst
}
func (c *ConnectionGene) Weight() float64 {
return c.weight
}
func (c *ConnectionGene) Expressed() bool {
return c.expressed
}
func (c *ConnectionGene) Enable() {
c.expressed = true
}
func (c *ConnectionGene) Disable() {
c.expressed = false
}
func (c *ConnectionGene) String() string {
srcId := c.src.Id()
dstId := c.dst.Id()
if c.expressed {
return fmt.Sprintf("[%d]--{%f}-->[%d]", srcId, c.weight, dstId)
}
return fmt.Sprintf("[%d]--/ /-->[%d]", srcId, dstId)
}
type Genome struct {
id int
nodeGenes []*NodeGene
connectionGenes []*ConnectionGene
}
func NewGenome(id int) *Genome {
return &Genome{
id: id,
nodeGenes: make([]*NodeGene),
connectionGenes: make([]*ConnectionGene),
}
}
func (g *Genome) String() string {
numNodes := len(g.nodeGenes)
numConnections := len(g.conncectionGenes)
strs := make([]string, 0, numNodes+numConnections)
for i := 0; i < numNodes; i++ {
strs = append(strs, g.nodeGenes[i].String())
}
for i := 0; i < numConnections; i++ {
strs = append(strs, g.connectionGenes[i].String())
}
return strings.Join(strs, "\n")
}
func (g *Genome) NodeGenes() []*NodeGene {
return g.nodeGenes
}
func (g *Genome) ConnenctionGenes() []*ConnectionGene {
return g.connectionGenes
}
// PushNode creates and appends a new node gene to this genome.
func (g *Genome) Push(nodeType NodeType, activation ActivationFunc) *NodeGene {
nodeId := len(g.nodeGenes) // its new index is its ID
node := NewNodeGene(nodeId, nodeType, activation)
g.nodeGene = append(g.nodeGene, node)
return node
}
// Connect
func (g *Genome) Connect(srcId, dstId int) error {
if outOfBounds(srcId) || outOfBounds(dstId) {
return errors.New("")
}
}
// helper function that checks if the argument node ID is within the range.
func (g *Genome) outOfBounds(nodeId int) bool {
if 0 > nodeId || len(g.nodeGenes) <= nodeId {
return false
}
return true
}