forked from karpathy/scriptsbots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLPBrain.cpp
179 lines (145 loc) · 4.4 KB
/
MLPBrain.cpp
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
#include "MLPBrain.h"
using namespace std;
MLPBox::MLPBox()
{
w.resize(CONNS,0);
id.resize(CONNS,0);
//constructor
for (int i=0;i<CONNS;i++) {
w[i]= randf(-3,3);
if(randf(0,1)<0.5) w[i]=0; //make brains sparse
id[i]= randi(0,BRAINSIZE);
if (randf(0,1)<0.2) id[i]= randi(0,INPUTSIZE); //20% of the brain AT LEAST should connect to input.
}
// kp= randf(0.1,1);
kp=1;
gw= randf(0,5);
bias= randf(-1,1);
out=0;
target=0;
}
MLPBrain::MLPBrain()
{
//constructor
for (int i=0;i<BRAINSIZE;i++) {
MLPBox a; //make a random box and copy it over
boxes.push_back(a);
boxes[i].out= a.out;
boxes[i].target= a.target;
boxes[i].kp= a.kp;
boxes[i].gw= a.gw;
boxes[i].bias= a.bias;
for (int j=0;j<CONNS;j++) {
boxes[i].w[j]= a.w[j];
boxes[i].id[j]= a.id[j];
if (i<BRAINSIZE/2) {
boxes[i].id[j]= randi(0,INPUTSIZE);
}
}
}
//do other initializations
init();
}
MLPBrain::MLPBrain(const MLPBrain& other)
{
boxes = other.boxes;
}
MLPBrain& MLPBrain::operator=(const MLPBrain& other)
{
if( this != &other )
boxes = other.boxes;
return *this;
}
void MLPBrain::init()
{
}
void MLPBrain::tick(vector< float >& in, vector< float >& out)
{
//do a single tick of the brain
//take first few boxes and set their out to in[].
for (int i=0;i<INPUTSIZE;i++) {
boxes[i].out= in[i];
}
//then do a dynamics tick and set all targets
for (int i=INPUTSIZE;i<BRAINSIZE;i++) {
MLPBox* abox= &boxes[i];
float acc=0;
for (int j=0;j<CONNS;j++) {
int idx=abox->id[j];
float val= boxes[idx].out;
acc= acc + val*abox->w[j];
}
acc*= abox->gw;
acc+= abox->bias;
//put through sigmoid
acc= 1.0/(1.0+exp(-acc));
abox->target= acc;
}
//make all boxes go a bit toward target
for (int i=INPUTSIZE;i<BRAINSIZE;i++) {
MLPBox* abox= &boxes[i];
abox->out =abox->out + (abox->target-abox->out)*abox->kp;
}
//finally set out[] to the last few boxes output
for (int i=0;i<OUTPUTSIZE;i++) {
out[i]= boxes[BRAINSIZE-1-i].out;
}
}
void MLPBrain::mutate(float MR, float MR2)
{
for (int j=0;j<BRAINSIZE;j++) {
if (randf(0,1)<MR*3) {
boxes[j].bias+= randn(0, MR2);
// a2.mutations.push_back("bias jiggled\n");
}
if (randf(0,1)<MR*3) {
boxes[j].kp+= randn(0, MR2);
if (boxes[j].kp<0.01) boxes[j].kp=0.01;
if (boxes[j].kp>1) boxes[j].kp=1;
// a2.mutations.push_back("kp jiggled\n");
}
if (randf(0,1)<MR*3) {
boxes[j].gw+= randn(0, MR2);
if (boxes[j].gw<0) boxes[j].gw=0;
// a2.mutations.push_back("kp jiggled\n");
}
if (randf(0,1)<MR*3) {
int rc= randi(0, CONNS);
boxes[j].w[rc]+= randn(0, MR2);
// a2.mutations.push_back("weight jiggled\n");
}
//more unlikely changes here
if (randf(0,1)<MR*3) {
int rc= randi(0, CONNS);
int ri= randi(0,BRAINSIZE);
boxes[j].id[rc]= ri;
// a2.mutations.push_back("connectivity changed\n");
}
}
}
MLPBrain MLPBrain::crossover(const MLPBrain& other)
{
//this could be made faster by returning a pointer
//instead of returning by value
MLPBrain newbrain(*this);
for (int i=0;i<newbrain.boxes.size(); i++) {
if(randf(0,1)<0.5){
newbrain.boxes[i].bias= this->boxes[i].bias;
newbrain.boxes[i].gw= this->boxes[i].gw;
newbrain.boxes[i].kp= this->boxes[i].kp;
for (int j=0;j<newbrain.boxes[i].id.size();j++) {
newbrain.boxes[i].id[j] = this->boxes[i].id[j];
newbrain.boxes[i].w[j] = this->boxes[i].w[j];
}
} else {
newbrain.boxes[i].bias= other.boxes[i].bias;
newbrain.boxes[i].gw= other.boxes[i].gw;
newbrain.boxes[i].kp= other.boxes[i].kp;
for (int j=0;j<newbrain.boxes[i].id.size();j++) {
newbrain.boxes[i].id[j] = other.boxes[i].id[j];
newbrain.boxes[i].w[j] = other.boxes[i].w[j];
}
}
}
return newbrain;
}