-
Notifications
You must be signed in to change notification settings - Fork 5
/
Car.pde
235 lines (225 loc) · 6.92 KB
/
Car.pde
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
public class Car {
//physics attributes
public boolean dead = false;
private boolean accelerating = false, turningLeft = false, turningRight = false, decelerating = false;
public PVector pos;
public PVector vel;
public float drag = 0.96;
public float angle = PI;
public float angularVelocity = 0;
public float angularDrag = 0.9;
public float power = 0.10;
public float turnSpeed = 0.01;
public float braking = 0.95;
//Neural attributes
public NNetwork neuralNetwork;
private float[][] proximity;
//Genetic attributes
public int proximitySensorLength = 200;
private int species;
public boolean isBest = false;
public float fitness = 0;
public float mutationRate = 0.01;
private int previousMarkerIndex = -1;
public Car(int x, int y, int species) {
pos = new PVector(x, y);
vel = new PVector(0, 0);
this.species = species;
proximity = new float[3][1];
neuralNetwork = new NNetwork();
switch(species) {
case 1: // 1%
mutationRate = 0.01;
break;
case 2:// 3%
mutationRate = 0.03;
break;
case 3:// 8%
mutationRate = 0.08;
break;
case 4:// 20%
mutationRate = 0.20;
break;
case 5:// 100%
mutationRate = 1.0;
break;
}
}
public void updateCarState() {
if (!dead) {
if (!testing) {
updateMarkerStatus();
}
drawSensors();
setControls();
if (accelerating) {
PVector delta = PVector.fromAngle(angle);
delta.mult(power);
vel.add(delta);
} else if (decelerating) {
vel.mult(braking);
}
if (turningLeft) {
angularVelocity += turnSpeed;
}
if (turningRight) {
angularVelocity -= turnSpeed;
}
pos.add(vel);
vel.mult(drag);
angle += angularVelocity;
angularVelocity *= angularDrag;
//if controlled by neural network and colliding with walls then die
if (notOnTrack()) {
dead = true;
}
}
}
private void updateMarkerStatus() {
for (int i = 0; i < markers.size(); i++) {
Marker currentMarker = markers.get(i);
if (currentMarker.colliding(this) && currentMarker.index != previousMarkerIndex) {
if (currentMarker.index == previousMarkerIndex + 1) {
fitness = currentMarker.score * currentMarker.score;
ga.activity++;
if (fitness > 1000) {
ga.timeoutLimit = 20;
}
if (fitness > 4000) {
ga.timeoutLimit = 30;
}
previousMarkerIndex = currentMarker.index;
} else {
if (previousMarkerIndex == 21 && currentMarker.index == 0) {
fitness = 1000000;
ga.activity++;
}
dead = true;
println("DIED " + species);
}
return;
}
}
}
//check for points near the periphery of the car that lie in the wall for collision detection
private boolean notOnTrack() {
int index0 = (int)pos.x + 7 + (int)pos.y * mapWidth;
int index1 = (int)pos.x - 7 + (int)pos.y * mapWidth;
int index2 = (int)pos.x + (int)(pos.y + 7) * mapWidth;
int index3 = (int)pos.x + (int)(pos.y - 7) * mapWidth;
boolean condition0 = index0 < 0 || index1 < 0 || index2 < 0 || index3 < 0;
boolean condition1 = index0 >= myMap.pixels.length || index1 >= myMap.pixels.length || index2 >= myMap.pixels.length || index3 >= myMap.pixels.length;
final int blackPixelValue = -16777216;
if (!condition0 && !condition1) {
if (myMap.pixels[index0] == blackPixelValue || myMap.pixels[index1] == blackPixelValue || myMap.pixels[index2] == blackPixelValue || myMap.pixels[index3] == blackPixelValue) {
return true;
}
} else {
return true;
}
return false;
}
//setControls sets the inputs to car based on the neural networks output in the not manual mode
private void setControls() {
float[][] directions = neuralNetwork.feedForward(proximity);
//println(directions[0][0] + "," + directions[1][0] + "," + directions[2][0] + "," + directions[3][0]);
if (directions[0][0] >= 0.5) {
accelerating = true;
} else {
accelerating = false;
}
if (directions[1][0] >= 0.5) {
turningRight = true;
} else {
turningRight = false;
}
if (directions[2][0] >= 0.5) {
decelerating = true;
} else {
decelerating = false;
}
if (directions[3][0] >= 0.5) {
turningLeft = true;
} else {
turningLeft = false;
}
}
//findDistance iterates over unit distances in the heading passed to it until it finds a wall or reaches the sensor end.
private void findDistance(PVector heading, int index) {
PVector posCopy = this.pos.copy();
heading.setMag(1);
for (int i = 0; i < proximitySensorLength; i++) {
//updating the posCopy vector to point a unit further in the heading direction passed
posCopy.add(heading);
//checking if the head of the posCopy vector lies in a wall
if (myMap.pixels[(int)posCopy.x + ((int)posCopy.y) * mapWidth] == -16777216) {
if (debugMode) {
fill(255, 0, 0);
ellipse(posCopy.x, posCopy.y, 5, 5);
}
//setting the proximity value to the iteration number aka distance in the heading direction
proximity[index][0] = i / (float)proximitySensorLength;
return;
}
}
proximity[index][0] = 1.0;
}
//drawSensors uses cars angle heading to draw proximity sensors and pass arguments to the proximity findDistance function.
private void drawSensors() {
PVector heading = PVector.fromAngle(angle - PI / 6);
heading.mult(proximitySensorLength);
stroke(0, 0, 0, 100);
//drawing for the -30 degree sensor
if (debugMode) {
line(pos.x, pos.y, pos.x + heading.x, pos.y + heading.y);
}
findDistance(heading.copy(), 0);
//drawing for the +30 degree sensor
heading.rotate(PI / 3);
if (debugMode) {
line(pos.x, pos.y, pos.x + heading.x, pos.y + heading.y);
}
findDistance(heading.copy(), 2);
//drawing for the 0 degree sensor
heading.rotate(-PI / 6);
if (debugMode) {
line(pos.x, pos.y, pos.x + heading.x, pos.y + heading.y);
}
findDistance(heading.copy(), 1);
//println(proximity[0][0] + ", " + proximity[1][0] + ", " + proximity[2][0]);
}
public void renderCar() {
if (!(!debugMode && dead)) {
stroke(0);
pushMatrix();
switch(species) {
case 1:// 1%
fill(255, 255, 0, 150);
break;
case 2:// 3%
fill(0, 0, 130, 150);
break;
case 3:
// 8%
fill(0, 255, 255, 150);
break;
case 4:
// 20%
fill(0, 0, 255, 150);
break;
case 5:
// 100%
fill(255, 0, 0, 150);
break;
}
if (isBest) {
//println("Best Reporting " + previousMarkerIndex);
fill(0, 255, 0, 150);
}
translate(pos.x, pos.y);
rotate(angle);
rect(0, 0, 20, 10);
popMatrix();
}
}
}