-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenetic_Algorithm.pde
More file actions
129 lines (123 loc) · 3.09 KB
/
Genetic_Algorithm.pde
File metadata and controls
129 lines (123 loc) · 3.09 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
final int populationSize= 500;
final float mutationRate= 0.01;
final int panelHeight= 100;
int height0;
int lifeTime;
int lifeCycle= 0;
final int gridScale= 10;
final int targetSize= 40;
int cols, rows;
Population population;
Obstacle target;
ArrayList<Obstacle> obstacles;
boolean debug= false;
Obstacle current= null;
void settings() {
size(1080, 640+panelHeight, P2D);
}
void setup() {
height0= height-panelHeight;
cols= width/gridScale;
rows= height0/gridScale;
lifeTime= width/3;
population= new Population(populationSize);
target= new Obstacle(width*.7, height0/2-targetSize/2, targetSize, targetSize);
obstacles= new ArrayList<Obstacle>();
textSize(15);
}
void draw() {
stroke(0);
fill(255);
rect(0, 0, width, height0);
for (Obstacle obstacle : obstacles) {
obstacle.display(100);
}
target.display(#00FF00);
if (lifeCycle < lifeTime) {
if (lifeCycle==lifeTime/2) {
for (Ant a : population.population) {
a.middlePos= a.pos.copy();
}
}
population.live(obstacles);
lifeCycle++;
} else {
lifeCycle= 0;
population.calcFitness();
population.generate();
}
if (current!=null) current.display(50);
showPanel();
}
void showPanel() {
fill(255);
rect(0, height0, width, panelHeight);
fill(0);
stroke(0);
textAlign(LEFT);
line(width*.5, height0, width*.5, height);
String text;
text=
"Population size : " + populationSize + '\n' +
"Generation : " + population.generation + '\n' +
"Mutation rate : " + mutationRate*pow(1 + population.failures/10, 1.1);
text(text, 20, height0+10, width*.5, panelHeight-20);
text=
"Time : " + lifeCycle + '\n' +
"Last Arrive Time : " + (population.lastTime==lifeTime? "Did not Arrived" : population.lastTime) + '\n' +
"Last success rate : " + (float) population.lastWinners*100/populationSize + " %";
text(text, width*.5 + 20, height0+10, width*.5, panelHeight-20);
}
void keyPressed() {
if (key=='d') {
debug= !debug;
}
}
boolean movingTarget= false;
void mousePressed() {
if (mouseY>height0) return;
if (target.contains(new PVector(mouseX, mouseY))) {
movingTarget= true;
target.x= mouseX - target.w/2;
target.y= mouseY - target.h/2;
target.center.x= mouseX;
target.center.y= mouseY;
return;
}
if (mouseButton==RIGHT) {
for (Obstacle ob : obstacles) {
if (ob.contains(new PVector(mouseX, mouseY))) {
obstacles.remove(ob);
break;
}
}
} else current= new Obstacle(mouseX, mouseY, 0, 0);
}
void mouseDragged() {
if (movingTarget && mouseY<height0) {
target.x= mouseX - target.w/2;
target.y= mouseY - target.h/2;
target.center.x= mouseX;
target.center.y= mouseY;
return;
}
if (current==null) return;
if (mouseButton==RIGHT) return;
current.w= mouseX-current.x;
current.h= mouseY-current.y;
}
void mouseReleased() {
movingTarget= false;
if (current==null) return;
if (mouseButton==RIGHT) return;
if (current.w<0) {
current.x+=current.w;
current.w*=-1;
}
if (current.h<0) {
current.y+=current.h;
current.h*=-1;
}
obstacles.add(current);
current= null;
}