-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.js
More file actions
135 lines (132 loc) · 3.68 KB
/
Copy pathBot.js
File metadata and controls
135 lines (132 loc) · 3.68 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
class BasicBot {
constructor(world, cols){
this.world = world;
this.cols = cols;
this.birb = new Birb(world.getHeight(), cols);
this.birb.setWallCollisionHandler(this.onCollision.bind(this));
this.birb.setColumnCollisionHandler(this.onCollision.bind(this));
this.mIsAlive = true;
this.neuralNet = new NeuralNetwork(5, 3, 1);
this.fitness = 0;
this.frame = 0;
this.nearestHoleY = 0;
}
onCollision(){
this.mIsAlive = false;
this.fitness = this.birb.getDistance() - this.nearestHoleY / 2;
console.log('BasicBot.onCollision, fitness:' + tostr(this.fitness) + ', distance:'
+ tostr(this.birb.getDistance()) + ', nearestHoleY:' + tostr(this.nearestHoleY));
this.birb.resetDistance();
}
isAlive(){
return this.mIsAlive;
}
setWeights(data){
this.neuralNet.setWeights(data);
}
getFitness(){
return this.fitness;
}
getNeuralNet(){
return this.neuralNet;
}
reset(){
this.mIsAlive = true;
}
update(gameSpeed){
if (this.mIsAlive){
this.frame++;
this.birb.update(gameSpeed);
let nearestColumn = this.birb.getNearestColumn();
this.nearestHoleY = Math.abs(nearestColumn[1]);
nearestColumn = [nearestColumn[0] / 300, nearestColumn[1] / 50];
let data = nearestColumn.concat([this.birb.getY() / 300, this.birb.vel / 10, gameSpeed / 10]);
let output = this.neuralNet.forwardPropagation(data);
if (this.world.isDebugging()){
this.neuralNet.showWeights();
console.log('data:', data, ', output:', output);
}
if (output[0] > 0.5)
this.birb.jump();
if (output[0] > 2)
console.log('SOMETHING WEIRD, OUTPUT[0]:' + output[0]);
return output[0] > 0.5;
}
}
draw(g, color){
if (this.mIsAlive){
this.birb.draw(g, color);
}
}
}
class BotSA extends BasicBot {
constructor(world, cols){
super(world, cols);
this.annealing = new SimulatedAnnealing(this.getNeuralNet().getAmountOfWeights(), 5, 0.998, true);
}
onCollision(){
super.onCollision();
this.annealing.updateState(this.getFitness() / 100);
this.setWeights(this.annealing.generateState());
}
}
class BotRANDOM extends BasicBot {
constructor(world, cols){
super(world, cols);
let numberOfWeights = this.getNeuralNet().getAmountOfWeights();
this.randomData = [];
for (let i = 0; i < numberOfWeights; i++)
this.randomData.push(Math.random() * 20 - 10);
}
onCollision(){
super.onCollision();
for (let i = 0; i < this.randomData.length; i++)
this.randomData[i] = Math.random() * 20 - 10;
this.setWeights(this.randomData);
}
}
class BotGA {
constructor(world, cols){
this.cols = cols;
this.world = world;
this.epoch = 0;
this.bots = [];
let population = 50;
for (let i = 0; i < population; i++){
this.bots.push(new BasicBot(world, cols));
}
this.ga = new RCGA(population, 0.3);
this.ga.addFeature(-10, 10, this.bots[0].getNeuralNet().getAmountOfWeights());
}
isAlive(){
for (let bot of this.bots)
if (bot.isAlive())
return true;
return false;
}
update(gameSpeed){
for (let bot of this.bots)
bot.update(gameSpeed);
}
reset(){
for (let i = 0; i < this.bots.length; i++){
this.bots[i].reset();
this.ga.setFitness(i, this.bots[i].getFitness());
this.bots[i].setWeights(this.ga.getCreature(i));
}
this.ga.newEpoch();
}
getNeuralNet(){
for (let bot of this.bots)
if (bot.isAlive())
return bot.getNeuralNet();
return this.bots[0].getNeuralNet();
}
getFitness(){
return this.ga.getFittestFitness();
}
draw(g, color){
for (let bot of this.bots)
bot.draw(g, color);
}
}