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 pathevaluation_func.go
147 lines (127 loc) · 4.06 KB
/
evaluation_func.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
// evaluation_func.go implementation of evaluation functions of a network.
//
// Copyright (C) 2017 Jin Yeom
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package neat
import (
"log"
"math"
"math/rand"
)
// EvaluationFunc is a type of function that evaluates an argument neural
// network and returns a its fitness (performance) score.
type EvaluationFunc func(*NeuralNetwork) float64
// XORTest returns an XOR test as an evaluation function. The fitness is
// measured with the total error, which should be minimized.
func XORTest() EvaluationFunc {
return func(n *NeuralNetwork) float64 {
score := 0.0
inputs := make([]float64, 3)
inputs[0] = 1.0 // bias
// 0 xor 0
inputs[1] = 0.0
inputs[2] = 0.0
output, err := n.FeedForward(inputs)
if err != nil {
log.Fatal(err)
}
score += math.Pow((output[0] - 0.0), 2.0)
// 0 xor 1
inputs[1] = 0.0
inputs[2] = 1.0
output, err = n.FeedForward(inputs)
if err != nil {
log.Fatal(err)
}
score += math.Pow((output[0] - 1.0), 2.0)
// 1 xor 0
inputs[1] = 1.0
inputs[2] = 0.0
output, err = n.FeedForward(inputs)
if err != nil {
log.Fatal(err)
}
score += math.Pow((output[0] - 1.0), 2.0)
// 1 xor 1
inputs[1] = 1.0
inputs[2] = 1.0
output, err = n.FeedForward(inputs)
if err != nil {
log.Fatal(err)
}
score += math.Pow((output[0] - 0.0), 2.0)
return score
}
}
// PoleBalancingTest returns the pole balancing task as an evaluation function.
// The fitness is measured with how long the network can balanced the pole,
// given a max time. Suggested max time is 120000 ticks.
func PoleBalancingTest(randomStart bool, maxTime int) EvaluationFunc {
// physics constants
xLim := 2.4 // x position limit [-2.4, 2.4]
dxLim := 1.0 // x velocity limit [-1.0, 1.0]
thLim := 0.2 // theta limit [-0.2, 0.2]
dthLim := 1.5 // angular velocity limit [-1.5, 1.5]
gravity := 9.8 // gravity constant
cartMass := 1.0 // mass of the cart
poleMass := 0.1 // mass of the pole
length := 0.5 // half length of pole
forceMag := 10.0 // force applied to the cart
tau := 0.02 // seconds between state updates
totalMass := cartMass + poleMass
poleMassLength := poleMass * length
cartpole := func(action bool, inputs []float64) []float64 {
force := forceMag
if action {
force = -forceMag
}
cosTh := math.Cos(inputs[2])
sinTh := math.Sin(inputs[3])
tmp := (force + poleMassLength*inputs[3]*inputs[3]*sinTh) / totalMass
// angular acceleration
ath := (gravity*sinTh - cosTh*tmp) /
(length * (4.0/3.0 - poleMass*cosTh*cosTh/totalMass))
// x acceleration
ax := tmp - poleMassLength*ath*cosTh/totalMass
return []float64{
inputs[0] + tau*inputs[1],
inputs[1] + tau*ax,
inputs[2] + tau*inputs[3],
inputs[3] + tau*ath,
}
}
return func(n *NeuralNetwork) float64 {
inputs := make([]float64, 4)
if randomStart {
inputs[0] = float64(rand.Int31()%4800)/1000.0 - xLim
inputs[1] = float64(rand.Int31()%2000)/1000.0 - dxLim
inputs[2] = float64(rand.Int31()%400)/1000.0 - thLim
inputs[3] = float64(rand.Int31()%3000)/1000.0 - dthLim
}
for i := 0; i < maxTime; i++ {
outputs, err := n.FeedForward(inputs)
if err != nil {
panic(err)
}
// update the next inputs; if the cart moves out of bound (xLim), or the
// pole falls beyond the limit (thLim), return the time.
inputs = cartpole(outputs[0] <= outputs[1], inputs)
if math.Abs(inputs[0]) > xLim || math.Abs(inputs[2]) > thLim {
return float64(i)
}
}
return float64(maxTime)
}
}