/
gann.cpp
1282 lines (1106 loc) · 29 KB
/
gann.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
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//##########################################################################
// GANN project
// Copyright (C) 2015 BENOIT-PILVEN Clément / MARTY Damien
//
// 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 2 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, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//##########################################################################
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <stdarg.h>
//#include <SDL/SDL.h>
//#define BIASWHEEL_STATS
double f[5];
double bestEver;
//====================================================
class RandomList {
protected:
int size;
int* array;
public:
RandomList(double max);
virtual ~RandomList();
static double RandAB(double a, double b);
virtual int* GetMixedArray(void);
};
RandomList::RandomList(double max) {
size = max;
array = (int*) malloc(sizeof(int)*size);
for(int i = 0; i< size; i++)
array[i]=i;
}
RandomList::~RandomList() {
free(array);
}
double RandomList::RandAB(double a, double b) {
return ( rand()/(double)RAND_MAX ) * (b-a) + a;
}
int* RandomList::GetMixedArray(void) {
int pickedNumber = 0;
int temp = 0;
for(int i = 0; i< size; i++){
pickedNumber = (int) RandAB(0,(double)size);
// On échange les contenus des cases i et nombre_tire
temp = array[i];
array[i] = array[pickedNumber];
array[pickedNumber] = temp;
}
return array;
}
//====================================================
class objectIO {
public:
virtual double getOutput(double in_date) = 0;
};
//====================================================
class bit: public objectIO {
protected:
double v;
public:
bit(double n);
virtual void setData(double n);
virtual double getOutput(double in_date);
};
bit::bit(double n) {
setData(n);
}
void bit::setData(double n) {
v = n;
}
double bit::getOutput(double in_date) {
return v;
}
//====================================================
class numInput {
protected:
bit **b;
int iSize;
public:
numInput(int n, int sz);
void setData(int n);
bit *getBit(int index);
bit **getBits(void);
};
numInput::numInput(int n, int sz) {
iSize = sz;
b = (bit**) malloc (sizeof(bit*)*sz);
for(int i = 0; i < iSize; i++) {
double v = 0.0;
if((n>>i)&0x01)
v = 1.0;
b[i] = new bit(v);
}
}
void numInput::setData(int n) {
for(int i = 0; i < iSize; i++) {
double v = -1.0;
if((n>>i)&0x01)
v = 1.0;
b[i]->setData(v);
}
}
bit *numInput::getBit(int index) {
if(index<iSize) {
return b[index];
} else {
printf("Fault asked index %d max is %d\n",index,iSize);
}
return NULL;
}
bit **numInput::getBits(void) {
return b;
}
//====================================================
class neuron: public objectIO {
protected:
int sz;
int allSz;
double sum;
double thr;
double *w;
objectIO **x;
double date;
double output;
public:
neuron(objectIO **ios, int sz, double t = 0);
// Network creation stuffs
virtual void checkAllocatedAxones(int s);
virtual void connectObject(objectIO *n, double weight = -99.99);
// Reset a neuron
virtual void randomiseWeight(void);
// Output stuffs
virtual double evaluate(double in_date);
virtual double getOutput(double in_date); // need to be as fast as possible
// Genetics stuffs
virtual int getGeneLen(void);
virtual double *getGenes(void);
virtual void setGenes(double *genes);
// Debug stuffs
virtual void printState(void);
};
neuron::neuron(objectIO **ios, int s, double t) {
// Set all Clean
x = NULL;
w = NULL;
sz = 0;
date = 0.0;
output = 0.0;
// Random Threshold or value from constructor
if(t == 0)
thr = (double)rand()/(double)(RAND_MAX/1.0);
else
thr = t;
// Links to Obj
if((s!=0) && (ios!=NULL)){
// Set Object Links
for(int i = 0; i<s; i++) {
connectObject(ios[i]);
}
}
}
void neuron::checkAllocatedAxones(int s) {
// Always alloc 2 times more memory that we actually need
if(s>allSz) {
allSz = s * 2;
// man tells us if x is NULL realloc(x,sz) = malloc(sz)
x = (objectIO**) realloc(x,sizeof(objectIO*)*allSz);
w = (double*) realloc (w,sizeof(double)*allSz);
}
}
void neuron::connectObject(objectIO *n, double weight) {
// Check if the pointer can handle sz + 1;
checkAllocatedAxones(sz+1);
// Apply the connexion
x[sz] = n;
w[sz] = (weight == -99.99)?(double)rand()/(double)(RAND_MAX/2.0)-1.0:weight;
sz++;
}
void neuron::randomiseWeight(void) {
for(int i = 0; i < sz; i++) {
w[i] = (double)rand()/(double)(RAND_MAX/2.0)-1.0;
}
}
double neuron::evaluate(double in_date) {
sum = 0;
for(int i = 0; i < sz; i++)
sum += w[i]*(x[i]->getOutput(in_date));
date = in_date;
output = 1.0/(1.0+exp(-5*sum));
//output = 1.0/(1.0+exp(-sum));
return output;
}
double neuron::getOutput(double in_date) {
if (in_date <= date)
return output;
else
return evaluate(in_date);
}
int neuron::getGeneLen(void) {
return sz;
}
double *neuron::getGenes(void) {
return w;
}
void neuron::setGenes(double *genes) {
memcpy(w,genes,sz*sizeof(double));
}
void neuron:: printState(void) {
printf("[%1.5f] [%1.5f] [%p] [",thr,sum,w);
for(int i = 0; i < sz; i++)
printf(" %1.5f(%p) ",w[i],x[i]);
printf("]\n");
}
//====================================================
class network {
protected:
int nLayers;
int *nPerLayer;
int genomeSz;
neuron ***neurons;
double networkDate;
public:
// Construtor
network(int nL, int *nPL);
// Input
virtual void *getInputData(void) = 0;
virtual void setInputData(void*) = 0;
// Do
void step();
// Visualize
void print();
// output and error estimation
int output(double *f, int max);
virtual double error(double res) = 0;
// Genome stuffs
int genomeSize(void);
double *extractGenome(bool print);
void setGenome(double* g);
//NEAT stuffs
//void insertNewNeuron(void);
};
network::network(int nL, int *nPL) {
networkDate = 0;
genomeSz = 0;
nLayers = nL;
nPerLayer = (int*) malloc(sizeof(int)*nLayers);
// Apply the number per layers
for(int i = 0; i<nLayers; i++) {
nPerLayer[i] = nPL[i];
}
// Allocate space for layers
neurons = (neuron***) malloc(sizeof(neuron**)*nLayers);
int nPrevLayer = 0;
for(int i = 0; i<nLayers; i++) {
neurons[i] = (neuron**) malloc(sizeof(neuron*)*nPerLayer[i]);
objectIO **list;
if(i == 0) {
list = NULL; // This is the first layer
} else {
list = (objectIO**)neurons[i-1];
}
for(int j = 0; j<nPerLayer[i]; j++) {
neurons[i][j] = new neuron(list,nPrevLayer);
//printf("Created Neuron[%d][%d] %p with nPrevLayer = %d, list %p\n", i, j, neurons[i][j], nPrevLayer, list);
}
nPrevLayer = nPerLayer[i];
}
}
/*
void network::insertNewNeuron(void) {
int targetLayer = (int) RandomList::RandAB(1,nLayers-1);
neurons[targetLayer] = (neuron**) realloc(neurons[targetLayer],sizeof(neuron*)*(nPerLayer[targetLayer]+1));
// neuron birth, with no connexion
neurons[targetLayer][nPerLayer[targetLayer]] = new neuron(0,NULL);
// connexion with upstream layers
int nMaxConnex = 0;
for (int i = 0; i < targetLayer; i++) {
nMaxConnex += nPerLayer[i];
}
int nbConnexions = (int) RandomList::RandAB(1,nMaxConnex);
for (int i = 0; i<nbConnexions ; i++) {
int layer = (int) RandomList::RandAB(0,targetLayer-1);
int neuron = (int) RandomList::RandAB(0,nPerLayer[layer]);
neurons[targetLayer][nPerLayer[targetLayer]]->addConnexion((objectIO*) neurons[layer][neuron]);
}
// connexion with downstream layers
nMaxConnex = 0;
for (int i = targetLayer+1; i < nLayers; i++) {
nMaxConnex += nPerLayer[i];
}
nbConnexions = (int) RandomList::RandAB(1,nMaxConnex);
for (int i = 0; i<nbConnexions ; i++) {
int layer = (int) RandomList::RandAB(targetLayer+1,nLayers);
int neuron = (int) RandomList::RandAB(0,nPerLayer[layer]);
neurons[layer][neuron]->addConnexion((objectIO*) neurons[targetLayer][nPerLayer[targetLayer]]);
}
nPerLayer[targetLayer]++;
}
*/
void network::step(void) {
networkDate++;
for(int i = 0; i<nLayers; i++) {
for(int j = 0; j<nPerLayer[i]; j++) {
neurons[i][j]->evaluate(networkDate);
}
}
}
void network::print(void) {
for(int i = 0; i<nLayers; i++) {
printf("=========l%d=========\n",i);
for(int j = 0; j<nPerLayer[i]; j++) {
neurons[i][j]->printState();
}
}
}
int network::output(double *f, int max) {
networkDate++;
int highestLayer = nLayers-1;
int nNeurons = (nPerLayer[highestLayer]<max)?nPerLayer[highestLayer]:max;
for(int i = 0; i < nNeurons ; i++) {
*(f+i) = neurons[highestLayer][i]->getOutput(networkDate);
}
return nNeurons;
}
int network::genomeSize(void) {
int geneTotalLen = 0;
for(int i = 0; i<nLayers; i++) {
for(int j = 0; j<nPerLayer[i]; j++) {
geneTotalLen += neurons[i][j]->getGeneLen();
}
}
return geneTotalLen;
}
double *network::extractGenome(bool print) {
// iterate over layers
int geneIndex = 0;
int geneTotalLen = genomeSize();
//printf("geneTotalLen %d\n", geneTotalLen);
double *genome = (double*) malloc (sizeof(double)*geneTotalLen);
for(int i = 0; i<nLayers; i++) {
for(int j = 0; j<nPerLayer[i]; j++) {
int l = neurons[i][j]->getGeneLen();
double *g = neurons[i][j]->getGenes();
for(int k = 0; k<l; k++) {
genome[geneIndex] = *(g+k);
if(print)
printf("[%d] %1.4f\n",geneIndex,*(g+k));
geneIndex++;
}
}
}
return genome;
}
void network::setGenome(double* g) {
int index = 0;
for(int i = 0; i<nLayers; i++) {
for(int j = 0; j<nPerLayer[i]; j++) {
int l = neurons[i][j]->getGeneLen();
neurons[i][j]->setGenes(g+index);
index += l;
}
}
}
//====================================================
class booleanOperation: public network {
protected:
int a;
int b;
int intSz;
int mask;
int nInput;
numInput **li;
typedef struct _coupleData {
int a;
int b;
} coupleData;
public:
booleanOperation(int iSz, int nLayer, int *nPerLayer);
void init(int iSz);
virtual void *getInputData(void);
virtual void setInputData(void*);
void setAB(int la, int lb);
void setA(int la);
void setB(int lb);
int getA(void);
int getB(void);
virtual double error(double res);
virtual bool trueResult() = 0;
};
booleanOperation::booleanOperation(int iSz, int nLayer, int *nPerLayer): network(nLayer, nPerLayer) {
nInput = 2;
intSz = iSz;
mask = 0;
for(int i = 0; i< iSz; i++) {
mask <<= 1;
mask += 1;
}
//printf("mask %08x\n", mask);
a = 0;
b = 0;
li = (numInput**) malloc(sizeof(numInput*)*nInput);
li[0] = new numInput(a,intSz);
li[1] = new numInput(b,intSz);
// Here the network is created but none
// of the input are connected so connect
// first layer to our inputs
for(int i = 0; i<intSz; i++) {
for(int j = 0; j<nInput; j++) {
for(int k = 0; k<nPerLayer[0]; k++) {
neurons[0][k]->connectObject(li[j]->getBit(i));
}
}
}
}
void *booleanOperation::getInputData(void) {
coupleData *c = (coupleData*) malloc(sizeof(coupleData));
c->a = rand() & mask;
c->b = rand() & mask;
//printf("new input %d %d\n", c->a, c->b);
return (void*) c;
}
void booleanOperation::setInputData(void* data) {
coupleData *c = (coupleData*) data;
setAB(c->a,c->b);
}
void booleanOperation::setAB(int la, int lb) {
a = la;
b = lb;
li[0]->setData(a);
li[1]->setData(b);
}
void booleanOperation::setA(int la) {
a =la;
li[0]->setData(a);
}
void booleanOperation::setB(int lb) {
b =lb;
li[1]->setData(b);
}
int booleanOperation::getA(void){
return a;
}
int booleanOperation::getB(void){
return b;
}
double booleanOperation::error(double res) {
bool tRes = trueResult();
if(tRes) { // true res is 1
if(res <= 0.5){ // we fail do a big error
return 1.0;
} else { // we do it right make a small error
return 0.0;
}
} else { // true res is 0
if(res <= 0.5){ // we do it right make a small error
return 0.0;
} else { // we fail do a big error
return 1.0;
}
}
}
//====================================================
class AsupB: public booleanOperation {
public:
AsupB(int iSz, int nLayer, int *nPerLayer);
virtual bool trueResult();
};
AsupB::AsupB(int iSz, int nLayer, int *nPerLayer): booleanOperation(iSz,nLayer,nPerLayer) { }
bool AsupB::trueResult() {
return a>b;
}
//====================================================
class AandB: public booleanOperation {
public:
AandB(int nLayer, int *nPerLayer);
virtual bool trueResult();
};
AandB::AandB(int nLayer, int *nPerLayer): booleanOperation(1,nLayer,nPerLayer) { }
bool AandB::trueResult() {
return a & b;
}
//====================================================
class AorB: public booleanOperation {
public:
AorB(int nLayer, int *nPerLayer);
virtual bool trueResult();
};
AorB::AorB(int nLayer, int *nPerLayer): booleanOperation(1,nLayer,nPerLayer) { }
bool AorB::trueResult() {
return a | b;
}
//====================================================
class AxorB: public booleanOperation {
private:
bit bias;
public:
AxorB(int nLayer, int *nPerLayer);
virtual bool trueResult();
};
AxorB::AxorB(int nLayer, int *nPerLayer): booleanOperation(1,nLayer,nPerLayer), bias(1.0) {
neurons[0][0]->connectObject(&bias);
neurons[0][1]->connectObject(&bias);
neurons[1][0]->connectObject(&bias);
}
bool AxorB::trueResult() {
return a ^ b;
}
//====================================================
class AequalB: public booleanOperation {
public:
AequalB(int nLayer, int *nPerLayer);
virtual double error(double res);
virtual bool trueResult() {return false;};
};
AequalB::AequalB(int nLayer, int *nPerLayer): booleanOperation(4,nLayer,nPerLayer) { }
double AequalB::error(double res) {
if(a == b){
//printf("Equal\n");
if((res<=0.6) && (res >= 0.4)) {
//printf("Yeah\n");
return 0.0;
} else {
return 1.0;
}
} else {
return 1.0;
}
}
//====================================================
class biasWheel {
protected:
void **objs; // The objects
double *probs; // The proba
double *normProbs; // The normalised proba
int *indexes; // The indexes of object in intial list
double normProbSum; // The sum of all normalised proba
int szMax; // The max size of bias wheel
int curSz; // The current size of bias wheel
public:
typedef struct _couple {
void *A;
int iA;
double pA;
void *B;
int iB;
double pB;
} couple;
biasWheel(int sz);
virtual ~biasWheel();
virtual void print(void);
virtual void normilize(void);
virtual void addObject(void *obj, double proba, int index = 0);
virtual void elect(void **obj, double *prob, int *ind);
virtual void electCouple(couple *c);
};
biasWheel::biasWheel(int sz) {
curSz = 0;
szMax = sz;
normProbSum = 0;
objs = (void**) malloc (sizeof(void*)*sz);
indexes = (int*) malloc (sizeof(int)*sz);
probs = (double*) malloc (sizeof(double)*sz);
normProbs = (double*) malloc (sizeof(double)*sz);
}
biasWheel::~biasWheel() {
free(objs);
free(indexes);
free(probs);
free(normProbs);
}
void biasWheel::print() {
for(int i = 0; i<curSz; i++) {
printf("%p - %03d - %1.6f - %1.6f\n",objs[i],indexes[i],probs[i],normProbs[i]);
}
}
void biasWheel::normilize(void){
for(int i = 0; i<curSz; i++) {
}
};
void biasWheel::addObject(void *obj, double proba, int index) {
if(curSz<szMax) {
objs[curSz] = obj;
probs[curSz] = (proba);
normProbs[curSz] = (proba)*100;
indexes[curSz] = index;
normProbSum += (proba)*100;
//printf("proba %f\n", proba);
//printf("proba = %f\n",probs[curSz]);
//printf("probaSum = %f\n", normProbSum);
curSz++;
}
}
void biasWheel::elect(void **obj, double *prob, int *index) {
double ind = ((double)rand()/((double)RAND_MAX))*normProbSum;
double d0 = 0;
int i;
//double d1
for(i = 0; i<curSz;i++) {
d0 += normProbs[i];
//printf("d0 %f\n",d0);
//d1 = prob[i+1];
if(ind<=d0) {
break;
}
}
*obj = objs[i];
*prob = probs[i];
*index = indexes[i];
}
void biasWheel::electCouple(couple *c) {
void *a,*b;
int ia,ib;
double pa,pb;
elect(&a,&pa,&ia);
elect(&b,&pb,&ib);
while(b==a) {
elect(&b,&pb,&ib);
}
c->A = a;
c->iA = ia;
c->pA = pa;
c->B = b;
c->iB = ib;
c->pB = pb;
}
//====================================================
// framework to run simulation
//====================================================
class genetics {
protected:
int nNets;
network **nets; // Networks represent the population at a given time
network **childNets;// Chikd represent the new population create after selection
double **errors; // Save the result of a competition at a given time
double *meanErr; // Save the mean error over 1 competition step
int compSize; // Size of the competition (how many time we run the fitting test)
int genIndex; // Generation index of the population
public:
genetics(int nNets, network **networks, network **childnetworks);
void compete(int its);
void select(int chunk, int nbiasWheel, double mutFactor = 0.001, double crossOverFactor = 0.7, int nCpy = 10);
void lovemaking(network *mom, network *dad, network *daughter, network *son, double mutFactor, double crossOverFactor);
void sort(void);
void step(void);
void print(void);
void fit(void);
};
genetics::genetics(int nNetworks, network **networks, network **childnetworks) {
nNets = nNetworks;
nets = networks;
childNets = childnetworks;
errors = (double**) malloc (sizeof(double*)*nNets);
meanErr = (double*) malloc (sizeof(double)*nNets);
compSize = 0;
genIndex = 0;
}
void genetics::compete(int its) {
// allocs
if(its > compSize) {
//printf("Do Allocation for errors\n");
if(compSize == 0) {
for(int i = 0; i<nNets; i++){
errors[i] = (double*) malloc(sizeof(double)*its);
}
} else {
for(int i = 0; i<nNets; i++){
errors[i] = (double*) realloc(errors[i],sizeof(double)*its);
}
}
compSize = its;
}
int track = 0;
int total = (its * nNets);
// competition
memset(meanErr,0, sizeof(double)*nNets);
for(int i = 0; i<its; i++) {
void *data = nets[0]->getInputData();
for(int j = 0; j<nNets; j++) {
nets[j]->setInputData(data);
nets[j]->step();
nets[j]->output(f,1);
double e = nets[j]->error(f[0]);
errors[j][i] = e;
meanErr[j] += e;
}
free(data);
}
// Compute the mean error => short
for(int i = 0; i<nNets; i++) {
meanErr[i] /= its;
//printf("bef- meanErr[%d] %f %d\n",i,meanErr[i],its);
/*
if(meanErr[i] == 0.0) {
printf("Normal???\n");
nets[i]->extractGenome(true);
while(1) {
void *data = nets[i]->getInputData();
nets[i]->setInputData(data);
nets[i]->step();
nets[i]->output(f,1);
double e = nets[i]->error(f[0]);
printf("%d %d %f %f\n",((booleanOperation*)nets[i])->getA(),((booleanOperation*)nets[i])->getB(),f[0],e);
free(data);
}
}
*/
}
}
void genetics::sort(void) {
FILE *f = fopen("meanErr.csv","a+");
// Very naive sorting
// (create temporary arrays)
bool *sorted = (bool*) malloc(sizeof(bool)*nNets);
for(int i = 0; i<nNets; i++) {
sorted[i] = false;
}
// Allocate new table of network
network **sortedNets = (network**) malloc(sizeof(network*)*nNets);
double *sortedMeanErr = (double*) malloc (sizeof(double)*nNets);
for(int i = 0; i<nNets; i++) {
double min = 1.0;
int selIndex = 0;
network *netmin = NULL;
for(int j = 0; j<nNets; j++) {
if(!sorted[j]) {
if(meanErr[j]<=min) {
selIndex = j;
netmin = nets[j];
min = meanErr[j];
}
}
}
sortedNets[i] = netmin;
sortedMeanErr[i] = min;
sorted[selIndex] = true;
}
memcpy(meanErr,sortedMeanErr,sizeof(double)*nNets);
memcpy(nets,sortedNets,sizeof(network *)*nNets);
// Compute population meanError
double popMean = 0;
for(int i = 0; i<nNets; i++) {
popMean += meanErr[i];
fprintf(f,"%1.5f;",meanErr[i]);
//printf("%1.5f ",meanErr[i]);
}
/*
for(int i = 0; i<nNets; i++) {
printf("aft- meanErr[%d] %f\n",i,meanErr[i]);
}
*/
fprintf(f,"\n");
popMean /= nNets;
if(meanErr[0]<bestEver)
bestEver = meanErr[0];
printf("PopMean - %f\t MinErr - %1.6f/%1.6f\n",popMean,meanErr[0],bestEver);
free(sortedNets);
free(sortedMeanErr);
free(sorted);
fclose(f);
}
void genetics::select(int chunk, int nBiasWheel, double mutFactor, double crossOverFactor, int nCpy) {
for(int it = 0; it<nNets; it+=chunk) {
RandomList *rl = new RandomList(nNets);
int* mixedArray = rl->GetMixedArray();
biasWheel *bw = new biasWheel(nBiasWheel);
//printf("===== Selection =====\n");
for(int i = 0; i < nBiasWheel; i++) {
int ind = mixedArray[i];
//printf("%1.5f\n",1-meanErr[ind]);
bw->addObject(nets[ind],1-meanErr[ind],ind);
}
//printf("=====================\n");
//bw->print();
#ifdef BIASWHEEL_STATS
int *selectionIndexes = (int*) malloc (sizeof(int)*nNets);
double *selectionProb = (double*) malloc (sizeof(double)*nNets);
for(int p = 0; p<nNets; p++){
selectionIndexes[p] = 0;
selectionProb[p] = 0.0;
}
#endif
for(int k = 0; k<chunk; k+=2) {
biasWheel::couple c;
bw->electCouple(&c);
//printf("Couple elected %p %p\n",c.A,c.B);
network *mom = (network*)c.A;
network *dad = (network*)c.B;
#ifdef BIASWHEEL_STATS
printf("%03d %03d %1.6f %1.6f\n",c.iA,c.iB,c.pA,c.pB);
selectionIndexes[c.iA]++;
selectionIndexes[c.iB]++;
selectionProb[c.iA] = c.pA;
selectionProb[c.iB] = c.pB;
#endif
lovemaking(mom, dad, childNets[it+k], childNets[it+k+1], mutFactor, crossOverFactor);
}
// TODO Copy the N best elements into child population
mixedArray = rl->GetMixedArray();
for(int i = 0; i<nCpy; i++) {
int ind = mixedArray[i];
double *top = nets[i]->extractGenome(false);
childNets[ind]->setGenome(top);
free(top);
}
// Alternate between child an parents
for(int i = 0; i<nNets; i++){
network *tmp = nets[i];
if(tmp == NULL){
printf("%d #YOLO MAIS YOLO\n",i);
while(1);
}
nets[i] = childNets[i];
childNets[i] = tmp;
}
#ifdef BIASWHEEL_STATS
printf("--BW_STATS-\n");
for(int i = 0; i< nNets; i++) {
if(selectionProb[i] != 0.0)
printf("%03d;%1.5f\n",selectionIndexes[i],selectionProb[i]);
}
free(selectionIndexes);
free(selectionProb);
#endif
delete(bw);
delete(rl);
}
}
void genetics::lovemaking(network *mom, network *dad, network *daughter, network *son, double mutFactor, double crossOverFactor) {
double *genMom = mom->extractGenome(false);
int genMomSz = mom->genomeSize();
double *genDad = dad->extractGenome(false);
int genDadSz = dad->genomeSize();
if(genMomSz != genDadSz) {
int d = 0;
printf("This genetics don't gonna work");
d = 1/d;
}
double *genChildDaughter = (double*) malloc (sizeof(double)*genMomSz);
double *genChildSon = (double*) malloc (sizeof(double)*genMomSz);
bool condom = (RandomList::RandAB(0,1)>=crossOverFactor)?false:true;
if (!condom) {
int margeIn = 1;
int cutPlace = (int) RandomList::RandAB(margeIn,genMomSz-margeIn);
//printf("[%d] Cutting place %d\n",it,cutPlace);
for(int i = 0; i<genMomSz; i++){
if(i<cutPlace) {
genChildDaughter[i] = genMom[i];
genChildSon[i] = genDad[i];
} else {
genChildDaughter[i] = genDad[i];
genChildSon[i] = genMom[i];
}
//printf("Child [%d], %f\n",i,genChild[i]);
}
// Mutate
for(int i = 0; i< genMomSz; i++){