forked from karpathy/scriptsbots
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMLPBrain.cpp
More file actions
311 lines (263 loc) · 7.22 KB
/
Copy pathMLPBrain.cpp
File metadata and controls
311 lines (263 loc) · 7.22 KB
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
#include "MLPBrain.h"
using namespace std;
MLPBox::MLPBox()
{
w.resize(CONNS,0);
id.resize(CONNS,0);
type.resize(CONNS,0);
//constructor
for (int i=0;i<CONNS;i++) {
w[i]= randf(-5,5);
if(randf(0,1)<conf::BRAIN_DEADCONNS) w[i]=0; //we might want to simulate brain development over time, so set some conns to zero weight
id[i]= randi(0,BRAINSIZE);
if (randf(0,1)<conf::BRAIN_DIRECTINPUT) id[i]= randi(0,Input::INPUT_SIZE); //connect a portion of the brain directly to input.
type[i]= 0;
if(randf(0,1)<conf::BRAIN_CHANGECONNS) type[i] = 1; //some conns can be change sensitive synapses
if(randf(0,1)<conf::BRAIN_MEMCONN) type[i] = 2; //some conns can be memory synapses
}
seed= 0;
kp= randf(0.01,1);
gw= randf(-2,2);
bias= randf(-3,3);
out= 0;
oldout= 0;
target= 0;
}
MLPBrain::MLPBrain()
{
//constructor
// do not OMP!!!
for (int i=0;i<BRAINSIZE;i++) {
MLPBox a; //make a random box and copy it over
boxes.push_back(a);
}
//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
for (int j=0; j<(int)boxes.size(); j++){
MLPBox* abox= &boxes[j];
if (j<Input::INPUT_SIZE) { //take first few boxes and set their out to in[]. (no need to do these separately, since thay are first)
abox->out= in[j];
} else { //then do a dynamics tick and set all targets
float acc=abox->bias;
for (int k=0;k<CONNS;k++) {
int idx=abox->id[k];
int type = abox->type[k];
float val= boxes[idx].out;
if(type==2){ //switch conn
if(val>0.5){
break;
continue;
}
continue;
}
if(type==1){ //change sensitive conn
val-= boxes[idx].oldout;
val*=10;
}
acc+= val*abox->w[k];
}
acc*= abox->gw;
//put through sigmoid
acc= 1.0/(1.0+exp(-acc));
abox->target= cap(acc);
}
}
for (int j=0; j<(int)boxes.size(); j++){
MLPBox* abox= &boxes[j];
//back up current out for each box
abox->oldout = abox->out;
//make all boxes go a bit toward target
if (j>=Input::INPUT_SIZE) abox->out+= (abox->target-abox->out)*abox->kp;
}
//finally set out[] to the last few boxes output
for (int j=0;j<Output::OUTPUT_SIZE;j++) {
//jump has different responce because we've made it into a change sensitive output
if (j==Output::JUMP) out[j]= cap(10*(boxes[BRAINSIZE-1-j].out-boxes[BRAINSIZE-1-j].oldout));
else out[j]= boxes[BRAINSIZE-1-j].out;
}
}
float MLPBrain::getActivity()
{
float sum= 0;
for (int j=0; j<(int)boxes.size(); j++){
MLPBox* abox= &boxes[j];
sum+= fabs(abox->out - abox->oldout);
}
return sum/BRAINSIZE;
}
void MLPBrain::initMutate(float MR, float MR2)
{
//for mutations which may occur at conception
for (int j=0; j<(int)boxes.size(); j++){
MLPBox* abox= &boxes[j];
if (randf(0,1)<MR/50) {
//randomize synapse type
int rc= randi(0, CONNS);
abox->type[rc] = randi(0,2);
// a2.mutations.push_back("synapse type randomized\n");
abox->seed= 0;
}
if (randf(0,1)<MR/40) {
//copy box
int k= randi(0,BRAINSIZE);
if(k!=j) {
abox->type= boxes[k].type;
abox->id= boxes[k].id;
abox->bias= boxes[k].bias;
abox->kp= boxes[k].kp;
abox->type= boxes[k].type;
abox->w= boxes[k].w;
// a2.mutations.push_back("box coppied\n");
abox->seed= 0;
}
}
if (randf(0,1)<MR/20) {
//randomize connection
int rc= randi(0, CONNS);
int ri= randi(0,BRAINSIZE);
abox->id[rc]= ri;
// a2.mutations.push_back("connection randomized\n");
abox->seed= 0;
}
if (randf(0,1)<MR/10) {
//swap two input sources
int rc1= randi(0, CONNS);
int rc2= randi(0, CONNS);
int temp= abox->id[rc1];
abox->id[rc1]= abox->id[rc2];
abox->id[rc2]= temp;
// a2.mutations.push_back("inputs swapped\n");
abox->seed= 0;
}
// more likely changes here
if (randf(0,1)<MR/2) {
//jiggle global weight
abox->gw+= randn(0, MR2);
if (abox->gw<0) abox->gw=0;
// a2.mutations.push_back("global weight jiggled\n");
// abox->seed= 0;
}
if (randf(0,1)<MR) {
//jiggle bias
abox->bias+= randn(0, MR2);
// a2.mutations.push_back("bias jiggled\n");
// abox->seed= 0;
}
if (randf(0,1)<MR) {
//jiggle dampening
abox->kp+= randn(0, MR2);
if (abox->kp<0.01) abox->kp=0.01;
if (abox->kp>1) abox->kp=1;
// a2.mutations.push_back("kp jiggled\n");
// abox->seed= 0;
}
if (randf(0,1)<MR) {
//jiggle weight
int rc= randi(0, CONNS);
abox->w[rc]+= randn(0, MR2);
// a2.mutations.push_back("weight jiggled\n");
// abox->seed= 0;
}
}
}
void MLPBrain::liveMutate(float MR, float MR2, vector<float>& out)
{
//for mutations which may occur while the bot is live
int j= randi(0,BRAINSIZE);
MLPBox* abox= &boxes[j];
if (randf(0,1)<MR/30) {
//"neurons that fire together, wire together"
int rc= randi(0, CONNS);
int b= -1;
while (b<=-1){
b-= 1;
int rb= randi(0,BRAINSIZE);
if (abs(boxes[rb].oldout-abox->out)<=0.01) b= rb;
if (b<=-100) break;
}
if (b>=0){
abox->id[rc]= b;
// a2.mutations.push_back("connection Hebb'ed\n");
abox->seed= 0;
}
}
if (randf(0,1)<MR/20) {
//stimulate box weight
float stim= out[Output::STIMULANT];
if(stim>0.5){
for (int k=0;k<CONNS;k++) {
//modify weights based on matching old output and new input, if stimulant is active
float val= boxes[abox->id[k]].out;
abox->w[k]+= conf::LEARNRATE*stim*(abox->oldout-(1-val));
}
}
// a2.mutations.push_back("weight stimulated\n");
// abox->seed= 0;
}
if (randf(0,1)<MR/10) {
//jiggle bias
abox->bias+= randn(0, MR2);
// a2.mutations.push_back("bias jiggled\n");
// abox->seed= 0;
}
if (randf(0,1)<MR/10) {
//jiggle dampening
abox->kp+= randn(0, MR2);
if (abox->kp<0.01) abox->kp=0.01;
if (abox->kp>1) abox->kp=1;
// a2.mutations.push_back("kp jiggled\n");
// abox->seed= 0;
}
}
MLPBrain MLPBrain::crossover(const MLPBrain& other)
{
MLPBrain newbrain(*this);
#pragma omp parallel for
for (int i=0; i<(int)newbrain.boxes.size(); i++){
int s1= this->boxes[i].seed;
int s2= other.boxes[i].seed;
//function which offers pobability of which parent to use, based on relative seed counters
float threshold= ((s1-s2)/(1+abs(s1-s2))+1)/2;
if(randf(0,1)<threshold){
newbrain.boxes[i].bias= this->boxes[i].bias;
newbrain.boxes[i].gw= this->boxes[i].gw;
newbrain.boxes[i].kp= this->boxes[i].kp;
newbrain.boxes[i].seed= this->boxes[i].seed + 1;
// this->boxes[i].seed += 1; //reward the copied box
for (int j=0; j<CONNS; j++){
newbrain.boxes[i].id[j] = this->boxes[i].id[j];
newbrain.boxes[i].w[j] = this->boxes[i].w[j];
newbrain.boxes[i].type[j] = this->boxes[i].type[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;
newbrain.boxes[i].seed= other.boxes[i].seed + 1;
// other.boxes[i].seed += 1;
for (int j=0; j<CONNS; j++){
newbrain.boxes[i].id[j] = other.boxes[i].id[j];
newbrain.boxes[i].w[j] = other.boxes[i].w[j];
newbrain.boxes[i].type[j] = other.boxes[i].type[j];
}
}
}
return newbrain;
}