Repository navigation
Expand file tree
/
Copy pathnetwork.cpp
More file actions
679 lines (534 loc) · 18.9 KB
/
Copy pathnetwork.cpp
File metadata and controls
679 lines (534 loc) · 18.9 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#include "network.h"
#include <iostream>
#include <sstream>
#include <cstring>
using namespace NEAT;
Network::Network(std::vector<NNode*> in,std::vector<NNode*> out,std::vector<NNode*> all,int netid) {
inputs=in;
outputs=out;
all_nodes=all;
name=0; //Defaults to no name ..NOTE: TRYING TO PRINT AN EMPTY NAME CAN CAUSE A CRASH
numnodes=-1;
numlinks=-1;
net_id=netid;
adaptable=false;
}
Network::Network(std::vector<NNode*> in,std::vector<NNode*> out,std::vector<NNode*> all,int netid, bool adaptval) {
inputs=in;
outputs=out;
all_nodes=all;
name=0; //Defaults to no name ..NOTE: TRYING TO PRINT AN EMPTY NAME CAN CAUSE A CRASH
numnodes=-1;
numlinks=-1;
net_id=netid;
adaptable=adaptval;
}
Network::Network(int netid) {
name=0; //Defaults to no name
numnodes=-1;
numlinks=-1;
net_id=netid;
adaptable=false;
}
Network::Network(int netid, bool adaptval) {
name=0; //Defaults to no name
numnodes=-1;
numlinks=-1;
net_id=netid;
adaptable=adaptval;
}
Network::Network(const Network& network)
{
std::vector<NNode*>::const_iterator curnode;
// Copy all the inputs
for(curnode = network.inputs.begin(); curnode != network.inputs.end(); ++curnode) {
NNode* n = new NNode(**curnode);
inputs.push_back(n);
all_nodes.push_back(n);
}
// Copy all the outputs
for(curnode = network.outputs.begin(); curnode != network.outputs.end(); ++curnode) {
NNode* n = new NNode(**curnode);
outputs.push_back(n);
all_nodes.push_back(n);
}
if(network.name)
name = strdup(network.name);
else
name = 0;
numnodes = network.numnodes;
numlinks = network.numlinks;
net_id = network.net_id;
adaptable = network.adaptable;
}
Network::~Network() {
if (name!=0)
delete [] name;
destroy(); // Kill off all the nodes and links
}
// Puts the network back into an initial state
void Network::flush() {
std::vector<NNode*>::iterator curnode;
for(curnode=outputs.begin();curnode!=outputs.end();++curnode) {
(*curnode)->flushback();
}
}
// Debugger: Checks network state
void Network::flush_check() {
std::vector<NNode*>::iterator curnode;
std::vector<NNode*>::iterator location;
std::vector<NNode*> seenlist; //List of nodes not to doublecount
for(curnode=outputs.begin();curnode!=outputs.end();++curnode) {
location= std::find(seenlist.begin(),seenlist.end(),(*curnode));
if (location==seenlist.end()) {
seenlist.push_back(*curnode);
(*curnode)->flushback_check(seenlist);
}
}
}
// If all output are not active then return true
bool Network::outputsoff() {
std::vector<NNode*>::iterator curnode;
for(curnode=outputs.begin();curnode!=outputs.end();++curnode) {
if (((*curnode)->activation_count)==0) return true;
}
return false;
}
// Print the connections weights to a file separated by only carriage returns
void Network::print_links_tofile(char *filename) {
std::vector<NNode*>::iterator curnode;
std::vector<Link*>::iterator curlink;
std::ofstream oFile(filename);
//Make sure it worked
//if (!oFile) {
// cerr<<"Can't open "<<filename<<" for output"<<endl;
//return 0;
//}
for(curnode=all_nodes.begin();curnode!=all_nodes.end();++curnode) {
if (((*curnode)->type)!=SENSOR) {
for(curlink=((*curnode)->incoming).begin(); curlink!=((*curnode)->incoming).end(); ++curlink) {
oFile << (*curlink)->in_node->node_id << " -> " <<( *curlink)->out_node->node_id << " : " << (*curlink)->weight << std::endl;
} // end for loop on links
} //end if
} //end for loop on nodes
oFile.close();
} //print_links_tofile
// Activates the net such that all outputs are active
// Returns true on success;
bool Network::activate() {
std::vector<NNode*>::iterator curnode;
std::vector<Link*>::iterator curlink;
double add_amount; //For adding to the activesum
bool onetime; //Make sure we at least activate once
int abortcount=0; //Used in case the output is somehow truncated from the network
//cout<<"Activating network: "<<this->genotype<<endl;
//Keep activating until all the outputs have become active
//(This only happens on the first activation, because after that they
// are always active)
onetime=false;
while(outputsoff()||!onetime) {
++abortcount;
if (abortcount==20) {
return false;
//cout<<"Inputs disconnected from output!"<<endl;
}
//std::cout<<"Outputs are off"<<std::endl;
// For each node, compute the sum of its incoming activation
for(curnode=all_nodes.begin();curnode!=all_nodes.end();++curnode) {
//Ignore SENSORS
//cout<<"On node "<<(*curnode)->node_id<<endl;
if (((*curnode)->type)!=SENSOR) {
(*curnode)->activesum=0;
(*curnode)->active_flag=false; //This will tell us if it has any active inputs
// For each incoming connection, add the activity from the connection to the activesum
for(curlink=((*curnode)->incoming).begin();curlink!=((*curnode)->incoming).end();++curlink) {
//Handle possible time delays
if (!((*curlink)->time_delay)) {
add_amount=((*curlink)->weight)*(((*curlink)->in_node)->get_active_out());
if ((((*curlink)->in_node)->active_flag)||
(((*curlink)->in_node)->type==SENSOR)) (*curnode)->active_flag=true;
(*curnode)->activesum+=add_amount;
//std::cout<<"Node "<<(*curnode)->node_id<<" adding "<<add_amount<<" from node "<<((*curlink)->in_node)->node_id<<std::endl;
}
else {
//Input over a time delayed connection
add_amount=((*curlink)->weight)*(((*curlink)->in_node)->get_active_out_td());
(*curnode)->activesum+=add_amount;
}
} //End for over incoming links
} //End if (((*curnode)->type)!=SENSOR)
} //End for over all nodes
// Now activate all the non-sensor nodes off their incoming activation
for(curnode=all_nodes.begin();curnode!=all_nodes.end();++curnode) {
if (((*curnode)->type)!=SENSOR) {
//Only activate if some active input came in
if ((*curnode)->active_flag) {
//cout<<"Activating "<<(*curnode)->node_id<<" with "<<(*curnode)->activesum<<": ";
//Keep a memory of activations for potential time delayed connections
(*curnode)->last_activation2=(*curnode)->last_activation;
(*curnode)->last_activation=(*curnode)->activation;
//If the node is being overrided from outside,
//stick in the override value
if ((*curnode)->overridden()) {
//Set activation to the override value and turn off override
(*curnode)->activate_override();
}
else {
//Now run the net activation through an activation function
if ((*curnode)->ftype==SIGMOID)
(*curnode)->activation=NEAT::fsigmoid((*curnode)->activesum,4.924273,2.4621365); //Sigmoidal activation- see comments under fsigmoid
}
//cout<<(*curnode)->activation<<endl;
//Increment the activation_count
//First activation cannot be from nothing!!
(*curnode)->activation_count++;
}
}
}
onetime=true;
}
if (adaptable) {
//std::cout << "ADAPTING" << std:endl;
// ADAPTATION: Adapt weights based on activations
for(curnode=all_nodes.begin();curnode!=all_nodes.end();++curnode) {
//Ignore SENSORS
//cout<<"On node "<<(*curnode)->node_id<<endl;
if (((*curnode)->type)!=SENSOR) {
// For each incoming connection, perform adaptation based on the trait of the connection
for(curlink=((*curnode)->incoming).begin();curlink!=((*curnode)->incoming).end();++curlink) {
if (((*curlink)->trait_id==2)||
((*curlink)->trait_id==3)||
((*curlink)->trait_id==4)) {
//In the recurrent case we must take the last activation of the input for calculating hebbian changes
if ((*curlink)->is_recurrent) {
(*curlink)->weight=
hebbian((*curlink)->weight,maxweight,
(*curlink)->in_node->last_activation,
(*curlink)->out_node->get_active_out(),
(*curlink)->params[0],(*curlink)->params[1],
(*curlink)->params[2]);
}
else { //non-recurrent case
(*curlink)->weight=
hebbian((*curlink)->weight,maxweight,
(*curlink)->in_node->get_active_out(),
(*curlink)->out_node->get_active_out(),
(*curlink)->params[0],(*curlink)->params[1],
(*curlink)->params[2]);
}
}
}
}
}
} //end if (adaptable)
return true;
}
// THIS WAS NOT USED IN THE FINAL VERSION, AND NOT FULLY IMPLEMENTED,
// BUT IT SHOWS HOW SOMETHING LIKE THIS COULD BE INITIATED
// Note that checking networks for loops in general in not necessary
// and therefore I stopped writing this function
// Check Network for loops. Return true if its ok, false if there is a loop.
//bool Network::integrity() {
// std::vector<NNode*>::iterator curnode;
// std::vector<std::vector<NNode*>*> paths;
// int count;
// std::vector<NNode*> *newpath;
// std::vector<std::vector<NNode*>*>::iterator curpath;
// for(curnode=outputs.begin();curnode!=outputs.end();++curnode) {
// newpath=new std::vector<NNode*>();
// paths.push_back(newpath);
// if (!((*curnode)->integrity(newpath))) return false;
// }
//Delete the paths now that we are done
// curpath=paths.begin();
// for(count=0;count<paths.size();count++) {
// delete (*curpath);
// curpath++;
// }
// return true;
//}
// Prints the values of its outputs
void Network::show_activation() {
std::vector<NNode*>::iterator curnode;
int count;
//if (name!=0)
// cout<<"Network "<<name<<" with id "<<net_id<<" outputs: (";
//else cout<<"Network id "<<net_id<<" outputs: (";
count=1;
for(curnode=outputs.begin();curnode!=outputs.end();++curnode) {
//cout<<"[Output #"<<count<<": "<<(*curnode)<<"] ";
count++;
}
//cout<<")"<<endl;
}
void Network::show_input() {
std::vector<NNode*>::iterator curnode;
int count;
//if (name!=0)
// cout<<"Network "<<name<<" with id "<<net_id<<" inputs: (";
//else cout<<"Network id "<<net_id<<" outputs: (";
count=1;
for(curnode=inputs.begin();curnode!=inputs.end();++curnode) {
//cout<<"[Input #"<<count<<": "<<(*curnode)<<"] ";
count++;
}
//cout<<")"<<endl;
}
// Add an input
void Network::add_input(NNode *in_node) {
inputs.push_back(in_node);
}
// Add an output
void Network::add_output(NNode *out_node) {
outputs.push_back(out_node);
}
// Takes an array of sensor values and loads it into SENSOR inputs ONLY
void Network::load_sensors(double *sensvals) {
//int counter=0; //counter to move through array
std::vector<NNode*>::iterator sensPtr;
for(sensPtr=inputs.begin();sensPtr!=inputs.end();++sensPtr) {
//only load values into SENSORS (not BIASes)
if (((*sensPtr)->type)==SENSOR) {
(*sensPtr)->sensor_load(*sensvals);
sensvals++;
}
}
}
void Network::load_sensors(const std::vector<float> &sensvals) {
//int counter=0; //counter to move through array
std::vector<NNode*>::iterator sensPtr;
std::vector<float>::const_iterator valPtr;
for(valPtr = sensvals.begin(), sensPtr = inputs.begin(); sensPtr != inputs.end() && valPtr != sensvals.end(); ++sensPtr, ++valPtr) {
//only load values into SENSORS (not BIASes)
if (((*sensPtr)->type)==SENSOR) {
(*sensPtr)->sensor_load(*valPtr);
//sensvals++;
}
}
}
// Takes and array of output activations and OVERRIDES
// the outputs' actual activations with these values (for adaptation)
void Network::override_outputs(double* outvals) {
std::vector<NNode*>::iterator outPtr;
for(outPtr=outputs.begin();outPtr!=outputs.end();++outPtr) {
(*outPtr)->override_output(*outvals);
outvals++;
}
}
void Network::give_name(char *newname) {
char *temp;
char *temp2;
temp=new char[strlen(newname)+1];
strcpy(temp,newname);
if (name==0) name=temp;
else {
temp2=name;
delete temp2;
name=temp;
}
}
// The following two methods recurse through a network from outputs
// down in order to count the number of nodes and links in the network.
// This can be useful for debugging genotype->phenotype spawning
// (to make sure their counts correspond)
int Network::nodecount() {
int counter=0;
std::vector<NNode*>::iterator curnode;
std::vector<NNode*>::iterator location;
std::vector<NNode*> seenlist; //List of nodes not to doublecount
for(curnode=outputs.begin();curnode!=outputs.end();++curnode) {
location = std::find(seenlist.begin(),seenlist.end(),(*curnode));
if (location==seenlist.end()) {
counter++;
seenlist.push_back(*curnode);
nodecounthelper((*curnode),counter,seenlist);
}
}
numnodes=counter;
return counter;
}
void Network::nodecounthelper(NNode *curnode,int &counter,std::vector<NNode*> &seenlist) {
std::vector<Link*> innodes=curnode->incoming;
std::vector<Link*>::iterator curlink;
std::vector<NNode*>::iterator location;
if (!((curnode->type)==SENSOR)) {
for(curlink=innodes.begin();curlink!=innodes.end();++curlink) {
location= std::find(seenlist.begin(),seenlist.end(),((*curlink)->in_node));
if (location==seenlist.end()) {
counter++;
seenlist.push_back((*curlink)->in_node);
nodecounthelper((*curlink)->in_node,counter,seenlist);
}
}
}
}
int Network::linkcount() {
int counter=0;
std::vector<NNode*>::iterator curnode;
std::vector<NNode*> seenlist; //List of nodes not to doublecount
for(curnode=outputs.begin();curnode!=outputs.end();++curnode) {
linkcounthelper((*curnode),counter,seenlist);
}
numlinks=counter;
return counter;
}
void Network::linkcounthelper(NNode *curnode,int &counter,std::vector<NNode*> &seenlist) {
std::vector<Link*> inlinks=curnode->incoming;
std::vector<Link*>::iterator curlink;
std::vector<NNode*>::iterator location;
location = std::find(seenlist.begin(),seenlist.end(),curnode);
if ((!((curnode->type)==SENSOR))&&(location==seenlist.end())) {
seenlist.push_back(curnode);
for(curlink=inlinks.begin();curlink!=inlinks.end();++curlink) {
counter++;
linkcounthelper((*curlink)->in_node,counter,seenlist);
}
}
}
// Destroy will find every node in the network and subsequently
// delete them one by one. Since deleting a node deletes its incoming
// links, all nodes and links associated with a network will be destructed
// Note: Traits are parts of genomes and not networks, so they are not
// deleted here
void Network::destroy() {
std::vector<NNode*>::iterator curnode;
std::vector<NNode*>::iterator location;
std::vector<NNode*> seenlist; //List of nodes not to doublecount
// Erase all nodes from all_nodes list
for(curnode=all_nodes.begin();curnode!=all_nodes.end();++curnode) {
delete (*curnode);
}
// -----------------------------------
// OLD WAY-the old way collected the nodes together and then deleted them
//for(curnode=outputs.begin();curnode!=outputs.end();++curnode) {
//cout<<seenstd::vector<<endl;
//cout<<curnode<<endl;
//cout<<curnode->node_id<<endl;
// location=find(seenlist.begin(),seenlist.end(),(*curnode));
// if (location==seenlist.end()) {
// seenlist.push_back(*curnode);
// destroy_helper((*curnode),seenlist);
// }
//}
//Now destroy the seenlist, which is all the NNodes in the network
//for(curnode=seenlist.begin();curnode!=seenlist.end();++curnode) {
// delete (*curnode);
//}
}
void Network::destroy_helper(NNode *curnode,std::vector<NNode*> &seenlist) {
std::vector<Link*> innodes=curnode->incoming;
std::vector<Link*>::iterator curlink;
std::vector<NNode*>::iterator location;
if (!((curnode->type)==SENSOR)) {
for(curlink=innodes.begin();curlink!=innodes.end();++curlink) {
location = std::find(seenlist.begin(),seenlist.end(),((*curlink)->in_node));
if (location==seenlist.end()) {
seenlist.push_back((*curlink)->in_node);
destroy_helper((*curlink)->in_node,seenlist);
}
}
}
}
// This checks a POTENTIAL link between a potential in_node and potential out_node to see if it must be recurrent
bool Network::is_recur(NNode *potin_node,NNode *potout_node,int &count,int thresh) {
std::vector<Link*>::iterator curlink;
++count; //Count the node as visited
if (count>thresh) {
//cout<<"returning false"<<endl;
return true; //Short out the whole thing- loop detected
}
if (potin_node==potout_node) return true;
else {
//Check back on all links...
for(curlink=(potin_node->incoming).begin();curlink!=(potin_node->incoming).end();curlink++) {
//But skip links that are already recurrent
//(We want to check back through the forward flow of signals only
if (!((*curlink)->is_recurrent)) {
if (is_recur((*curlink)->in_node,potout_node,count,thresh)) return true;
}
}
return false;
}
}
bool Network::is_recur2(NNode *potin_node,NNode *potout_node,int &count, int thresh) {
NNode* curnode;
std::vector<NNode*> seenlist; //List of nodes not to doublecount
curnode=potin_node; //start at out node, if we find a path that lands at in_node, then this link will be recurrent
seenlist.push_back(curnode);
return is_rec_helper(curnode,potout_node,seenlist);
}
bool Network::is_rec_helper(NNode* curnode, NNode* find_node,std::vector<NNode*> &seenlist)
{
std::vector<Link*> innodes=curnode->incoming;
std::vector<Link*>::iterator curlink;
std::vector<NNode*>::iterator location;
if (!((curnode->type)==SENSOR)) {
for(curlink=innodes.begin();curlink!=innodes.end();++curlink) {
if((*curlink)->in_node==find_node)
return true;
location= std::find(seenlist.begin(),seenlist.end(),((*curlink)->in_node));
if (location==seenlist.end()) {
seenlist.push_back((*curlink)->in_node);
if(is_rec_helper((*curlink)->in_node,find_node,seenlist))
return true;
}
}
}
return false;
}
/*
int Network::nodecount() {
int counter=0;
std::vector<NNode*>::iterator curnode;
std::vector<NNode*>::iterator location;
std::vector<NNode*> seenlist; //List of nodes not to doublecount
for(curnode=outputs.begin();curnode!=outputs.end();++curnode) {
location = std::find(seenlist.begin(),seenlist.end(),(*curnode));
if (location==seenlist.end()) {
counter++;
seenlist.push_back(*curnode);
nodecounthelper((*curnode),counter,seenlist);
}
}
numnodes=counter;
return counter;
}
void Network::nodecounthelper(NNode *curnode,int &counter,std::vector<NNode*> &seenlist) {
std::vector<Link*> innodes=curnode->incoming;
std::vector<Link*>::iterator curlink;
std::vector<NNode*>::iterator location;
if (!((curnode->type)==SENSOR)) {
for(curlink=innodes.begin();curlink!=innodes.end();++curlink) {
location= std::find(seenlist.begin(),seenlist.end(),((*curlink)->in_node));
if (location==seenlist.end()) {
counter++;
seenlist.push_back((*curlink)->in_node);
nodecounthelper((*curlink)->in_node,counter,seenlist);
}
}
}
}
*/
int Network::input_start() {
input_iter=inputs.begin();
return 1;
}
int Network::load_in(double d) {
(*input_iter)->sensor_load(d);
input_iter++;
if (input_iter==inputs.end()) return 0;
else return 1;
}
//Find the maximum number of neurons between an ouput and an input
int Network::max_depth() {
std::vector<NNode*>::iterator curoutput; //The current output we are looking at
int cur_depth; //The depth of the current node
int max=0; //The max depth
int count=0; //a way to prevent infinite loops
int thresh=(all_nodes.size())*(all_nodes.size());
for(curoutput=outputs.begin();curoutput!=outputs.end();curoutput++) {
cur_depth=(*curoutput)->depth(0,this,count,thresh);
if (cur_depth>max) max=cur_depth;
}
return max;
}