-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetwork.js
180 lines (157 loc) · 4.65 KB
/
NeuralNetwork.js
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
class NeuralNetwork{
constructor(id){
this.id = id;
this.snake = new Snake();
this.totalMoves = 0;
this.score = 0;
this.layers = [];
this.moves = maxMoves;
var inputLayer = new NeuralLayer(noOfInputs);
inputLayer.makeInput(this.snake);
this.layers.push(inputLayer);
this.inputLayer = inputLayer;
for (var i = 0; i < noOfHiddenLayers; i++){
var hiddenLayer = new NeuralLayer(noOfHiddenNeurons);
this.layers.push(hiddenLayer);
}
var outputLayer = new NeuralLayer(noOfOutputs);
this.outputLayer = outputLayer;
this.layers.push(outputLayer);
for (var i = 0; i < this.layers.length - 1; i++){
this.layers[i].connectTo(this.layers[i + 1]);
}
}
setHiddenLayers(layers, neurons){
var inputLayer = new NeuralLayer(noOfInputs);
inputLayer.makeInput(this.snake);
this.layers.push(inputLayer);
this.inputLayer = inputLayer;
for (var i = 0; i < layers; i++){
var hiddenLayer = new NeuralLayer(neurons);
this.layers.push(hiddenLayer);
}
var outputLayer = new NeuralLayer(noOfOutputs);
this.outputLayer = outputLayer;
this.layers.push(outputLayer);
for (var i = 0; i < this.layers.length - 1; i++){
this.layers[i].connectTo(this.layers[i + 1]);
}
}
setID(id){
this.id = id;
}
getID(){
return this.id;
}
runOnce(){
if (!this.snake.alive || this.moves == 0){
this.reset();
resetGame();
return;
}
this.snake.display();
this.snake.fruit.display();
this.snake.update();
if (this.snake.score > this.score){
this.moves = maxMoves;
this.score = this.snake.score;
}
var move = this.getMove();
this.snake.direction = move;
this.moves--;
this.totalMoves++;
this.fitness = this.calcFitness();
}
run(loops){
this.best = 0;
var totalFitness = 0;
for (var i = 0; i < loops; i++){
this.reset();
while (true){
this.snake.update();
if (this.snake.score > this.score){
this.score = this.snake.score;
this.moves = maxMoves;
}
var move = this.getMove();
this.snake.direction = move;
this.moves--;
this.totalMoves++;
if (!this.snake.alive || this.moves == 0){
if (this.calcFitness() > this.best){
this.best = this.calcFitness();
}
totalFitness += this.calcFitness();
break;
}
}
}
var fitness = totalFitness / loops;
this.fitnessFromRun = fitness;
}
calcFitness(){
return (this.totalMoves * movePoints) + (this.score * fruitPoints);
}
reset(){
this.moves = maxMoves;
this.totalMoves = 0;
this.score = 0;
this.snake.reset();
}
getMove(){
var data = this.outputLayer.getData();
this.data = data;
//console.log(data);
var largest = 0;
var index = 0;
for (var i = 0; i < data.length; i++){
if (data[i] > largest){
largest = data[i];
index = i;
}
}
if (index == 0){
return r;
} else if (index == 1){
return d;
} else if (index == 2){
return l;
} else if (index == 3){
return u;
}
}
getWeights(){
var weights = [];
for (var i = 0; i < this.layers.length - 1; i++){
weights[i] = this.layers[i].getWeights();
}
this.weights = weights;
return weights;
}
getBiases(biases){
var biases = [];
for (var i = 1; i < this.layers.length; i++){
biases.push(this.layers[i].getBiases());
}
this.biases = biases;
return biases;
}
getWB(){
var data = [];
data[0] = this.getWeights();
data[1] = this.getBiases();
return data;
}
setWeights(weights){
//console.log("WEIGHTS");
//console.log(weights);
for (var i = 0; i < this.layers.length - 1; i++){
this.layers[i].setWeights(weights[i]);
}
}
setBiases(biases){
for (var i = 1; i < this.layers.length; i++){
this.layers[i].setBiases(biases[i - 1]);
}
}
}