-
Notifications
You must be signed in to change notification settings - Fork 1
/
neuralnetwork.cpp
98 lines (79 loc) · 1.84 KB
/
neuralnetwork.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
#include <iostream>
#include <math.h>
#include <time.h>
#include <vector>
using namespace std;
float getRand(){//Float from 0 to 1.
return ((float)rand())/RAND_MAX;
}
float norm(float a){
if(a>1)return 1.0;
if(a<0)return 0.0;
return a;
}
class Connection;
class Neuron{
public:
float val;
float nextVal;
bool isConstant;
vector<Connection> cons;
Neuron(){}
Neuron(vector<Neuron> n){
isConstant=false;
val=getRand();
nextVal=0;
cons.resize(n.size());
for(int i=0;i<n.size();i++)
cons=new Connection(n[i]);
}
void next(float score){
for(int i=0;i<cons.size();i++)
cons[i].next(score);
nextVal=0;
float totalWeight=0;
for(int i=0;i<cons.size();i++){
nextVal+=cons.from.val*cons.weight;
totalWeight+=cons.weight;
}
nextVal/=totalWeight;
}
void nextFinish(){
val=nextVal;
nextVal=0;
}
};
class Connection{
public:
Neuron from;
float weight;
//how good it "has been"?
Connection(){}
Connection(Neuron f){
from=f;
weight=getRand();
}
void next(float score){//score also from 0 to 1.
weight=norm(weight+(getRand()-0.5)*(1-score));
//Changes more if it's a bad score. - but then it does that on EVERY thing.
}
};
int main(){
srand(time(NULL));
vector<Neuron> neurons;
float a=getRand(),b=getRand();
int n=10;
neurons.resize(n);
for(int i=0;i<n;i++)
neurons[i]=new Neuron(neurons);
while(true){
neurons[0].isConstant=neurons[1].isConstant=true;
neurons[0].val=a;neurons[1].val=b;
float score=neurons[3].val-a-b;
score*=score;
for(int i=0;i<neurons.size();i++)neurons[i].next(score);
for(int i=0;i<neurons.size();i++)neurons[i].nextFinish();
}
//How many "updates" of the brain will be allowed? Huh... the brain is even more complex than this model - it works in the temporal dimension too.
//Maybe when it indicates with one more neuron that the answer is "ready".
}