/
sinTest.cpp
64 lines (54 loc) · 1.53 KB
/
sinTest.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
// copyright (C) 2001-2011 by Patrick Hanevold
#include "sinTest.h"
#include "net.h"
#include "gui/screen.h"
#include "gui/graph.h"
#include "gui/grid.h"
#include "population.h"
SinTest::SinTest() : Genetic(2,400,8,1){
}
// set calibration factors of a neural net
// input: net = a neural net from the population
void SinTest::calibrate(Net *net){
net->setCalibration(-1,1);
}
// evaluate and stimulate a neural net. Todo?: Futuristic punishment/reward
// inputs: net = neural net to evaluate
void SinTest::evaluate(Net *net){
int i=rand()%1000;
for(int n=0; n<8; n++) net->setInput(n,double(i)/1000.0);
net->run();
stimulate(net,-fabs(sin(double(i*3)/1000.0)-*net->getOutput()));
}
bool SinTest::criteria(Net *net){
if(net->getAge()<60) return false; // keep youngsters out of the score board
return true;
}
// start evolution of neural networks!
void SinTest::go(){
Screen scr(640,512);
Graph<double> score;
Graph<int> age;
Graph<double> sample;
Graph<double> sample2;
Grid grid(&scr);
grid.attach(&score);
grid.attach(&sample);
grid.attach(&age);
grid.attach(&sample2);
while(true){
for(int n=0; n<10; n++) nextGeneration();
sample.clear();
sample2.clear();
Net *net = getPopulation(0)->getIndividual(0);
for(int i=0; i<100; i++){
for(int n=0; n<8; n++) net->setInput(n,double(i)/100.0);
net->run();
sample.add(*net->getOutput());
sample2.add(sin(double(i*3)/100.0));
}
score.add(getBoardScore());
age.add(getBoardAge());
scr.render(Rect(0,0,640,512));
}
}