This repository was archived by the owner on Oct 4, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
187 lines (170 loc) · 5.55 KB
/
Copy pathscript.js
File metadata and controls
187 lines (170 loc) · 5.55 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
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
var frameRate = 60;
var ballSize = 50;
var bestScore = Infinity;
var moveDuration = 6;
var maxSpeed = 6;
var window_width = window.innerWidth - 25;
var window_height = window.innerHeight - 25;
var StartPoint = { "x": window_width / 2, "y": window_height - ballSize };
var EndPoint = { "x": window_width / 2, "y": ballSize / 2 };
var timeSinceStart = 0;
var generationTimeInFrames = frameRate * 5; // 5 sec
var generationNumber = 0;
var balls = [];
var bestVectors = {
x: [],
y: [],
}
/**
* This class is following a genetic algorithm
*
* The goal of this experience is to train a ball
* to go from a start point to an end point.
*/
class Ball {
constructor(_bestVectors = null) {
this.size = ballSize;
this.radius = ballSize / 2;
this.x = StartPoint.x;
this.y = StartPoint.y + (ballSize / 2);
this.vectorsX = [];
this.vectorsY = [];
this.score = bestScore;
//load best vectors from last generation
if (_bestVectors != null) {
this.vectorsX = _bestVectors.x;
this.vectorsY = _bestVectors.y;
}
}
Draw(frame) {
// if (frame % moveDuration == 0) {
// alert("gre")
// }
this.UpdateVectors(frame);
this.UpdatePositions(frame);
this.UpdateScore();
fill(255);
ellipse(this.x, this.y, this.size, this.size);
}
UpdatePositions(frame) {
//borders limits
if (this.y + this.radius >= window_height) {
this.y = window_height - this.radius;
}
if (this.x + this.radius >= window_width) {
this.x = window_width - this.radius;
}
//movements
this.x += parseFloat(this.vectorsX[frame]);
this.y += parseFloat(this.vectorsY[frame]);
}
UpdateVectors(frame) {
// randomize new vectors
// first generation case
if (this.vectorsX[frame] == null || this.vectorsY[frame] == null) {
// if a previous vector exists
if (this.vectorsX[frame - 1] && this.vectorsY[frame - 1]) {
this.vectorsX[frame] = this.vectorsX[frame - 1] * random(-1, 1);
this.vectorsY[frame] = this.vectorsY[frame - 1] * random(-1, 1);
} else {
//the really first vector
this.vectorsX[frame] = random(-5, 5);
this.vectorsY[frame] = random(-5, 5);
}
} else {
//~=~=~=~=~=~=~=~=~=~=~=~=
// CONTROL OF SPEED
//~=~=~=~=~=~=~=~=~=~=~=~=
if (this.vectorsX[frame] > 6 || this.vectorsX[frame] < -6) {
if (this.vectorsX[frame] > 6) {
this.vectorsX[frame] = 6;
}
if (this.vectorsX[frame] < -6) {
this.vectorsX[frame] = -6;
}
} else {
//randomizing speed
this.vectorsX[frame] = parseFloat(this.vectorsX[frame] + random(-3, 3));
}
if (this.vectorsY[frame] > 6 || this.vectorsY[frame] < -6) {
if (this.vectorsY[frame] > 6) {
this.vectorsY[frame] = 6;
}
if (this.vectorsY[frame] < -6) {
this.vectorsY[frame] = -6;
}
} else {
this.vectorsY[frame] = parseFloat(this.vectorsY[frame] + random(-3, 3));
}
}
}
UpdateScore() {
var newScore = dist(this.x, this.y, EndPoint.x, EndPoint.y)
if (newScore < this.score) {
this.score = newScore;
}
}
}
function DrawBases() {
//starting base
fill(240, 20, 20);
rect(StartPoint.x - (ballSize / 2), StartPoint.y, ballSize, ballSize);
//end point
fill(20, 240, 20);
rect(EndPoint.x - (ballSize / 2), EndPoint.y - ballSize / 2, ballSize, ballSize);
}
//~=~=~=~=~=~=~=~=~=~=~=~=
// SETUP
//~=~=~=~=~=~=~=~=~=~=~=~=
function setup() {
createCanvas(window_width, window_height);
noStroke();
frameRate(frameRate);
}
//~=~=~=~=~=~=~=~=~=~=~=~=
// DRAW (1 frame)
//~=~=~=~=~=~=~=~=~=~=~=~=
function draw() {
//if it's a new generation, we recreate a ball
if (timeSinceStart == 0) {
for (ball = 0; ball < 1000; ball++) {
balls[ball] = new Ball(bestVectors);
}
generationNumber++;
}
background(0);
DrawBases();
timeSinceStart++;
text("duration: " + timeSinceStart + "\tgeneration: " + generationNumber, 20, 20);
//~=~=~=~=~=~=~=~=~=~=~=~=
// Calculate best genome
//~=~=~=~=~=~=~=~=~=~=~=~=
ScoreArray = [];
balls.forEach(ball => {
ScoreArray.push(parseFloat(ball.score));
});
var maxScoreFound = ScoreArray.reduce(function (a, b) {
return Math.min(a, b);
});
bestScore = maxScoreFound;
var indexOfBest = ScoreArray.indexOf(Math.max(maxScoreFound));
text("vectorX: " + parseInt(balls[indexOfBest].vectorsX[timeSinceStart]) + "\nvectorY: " + parseInt(balls[indexOfBest].vectorsY[timeSinceStart]), 20, 90);
text("bestScore: " + bestScore + "\nbestIndex: " + indexOfBest, 20, 50);
//~=~=~=~=~=~=~=~=~=~=~=~=
// Check for new generation
//~=~=~=~=~=~=~=~=~=~=~=~=
//if delay is not expired
if (timeSinceStart < generationTimeInFrames) {
//ball
balls.forEach(ball => {
ball.Draw(timeSinceStart);
});
} else {
bestVectors.x = balls[indexOfBest].vectorsX;
bestVectors.y = balls[indexOfBest].vectorsY;
timeSinceStart = 0;
}
if (bestScore <= 1) {
alert("You win");
}
}