-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptP5.js
More file actions
207 lines (166 loc) · 4.84 KB
/
Copy pathscriptP5.js
File metadata and controls
207 lines (166 loc) · 4.84 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//Inspiration from Daniel Shiffman
//Coding Train
var twitPopulation;//
var lifeSpan = 400;//
var count = 0;//
var target;//
var maxforce = 0.2;//
var rx = 100;//
var ry = 150;//
var rw = 200;//
var rh = 10;//
function setup() {
var canvas = createCanvas(windowWidth/2, windowHeight);
canvas.parent('sketch-holder');
twitPopulation = new Population();
target = createVector(width/2, 50);
}
function draw() {
background(0);
twitPopulation.run();
count++;
if (count == lifeSpan) {
twitPopulation.evaluate();
twitPopulation.selection();
count = 0;
}
// fill(255);
// rect(rx, ry, rw, rh);
ellipse(target.x, target.y, 16, 16);
}
function Population() {
this.twitRocket = [];
this.popsize = 15;
this.matingpool = [];
for ( var i = 0; i < this.popsize; i++) {
this.twitRocket[i] = new Rocket(); //
}
//DNA Selection
this.evaluate = function() {
var maxfit = 0;
for (var i = 0; i < this.popsize; i++) {
this.twitRocket[i].calcFitness();
if (this.twitRocket[i].fitness > maxfit) {
maxfit = this.twitRocket[i].fitness;
}
}
for (var i = 0; i < this.popsize; i++) {
this.twitRocket[i].fitness /= maxfit;
}
this.matingpool = [];
for (var i = 0; i < this.popsize; i++) {
var n = this.twitRocket[i].fitness * 100;
for (var j = 0; j < n; j++) {
this.matingpool.push(this.twitRocket[i]);
}
}
}
this.selection = function() {
var newTwitRocket = [];
for (var i = 0; i < this.twitRocket.length; i++) {
var parentA = random(this.matingpool).dna;
var parentB = random(this.matingpool).dna;
var child = parentA.crossover(parentB);
child.mutation();
newTwitRocket[i] = new Rocket(child);
}
this.twitRocket = newTwitRocket;
}
this.run = function() {
for ( var i = 0; i < this.popsize; i++) {
this.twitRocket[i].update();
this.twitRocket[i].show();
}
}
}
function DNA(genes) {
if (genes) {
this.genes = genes;
} else {
this.genes = [];
for (var i = 0; i < lifeSpan; i++) {
this.genes[i] = p5.Vector.random2D();
this.genes[i].setMag(maxforce); // force of vectors
}
}
this.crossover = function(partner) {
var newgenes = [];
var mid = floor(random(this.genes.length));
for (var i = 0; i < this.genes.length; i++) {
if (i > mid) {
newgenes[i] = this.genes[i];
} else {
newgenes[i] = partner.genes[i];
}
}
return new DNA(newgenes);
}
this.mutation = function() {
for (var i = 0; i < this.genes.length; i++) {
if (random(1) < 0.01) {
this.genes[i] = p5.Vector.random2D();
this.genes[i].setMag(maxforce);
}
}
}
}
function Rocket(dna) {
this.pos = createVector(width/2, height);
this.vel = createVector(); // velocity start at 0
this.acc = createVector();
this.completed = false;
this.crashed = false;
if (dna) {
this.dna = dna;
} else {
this.dna = new DNA();
}
this.fitness = 0;
this.applyForce = function(force) {
this.acc.add(force);
}
this.calcFitness = function() {
var d = dist(this.pos.x, this.pos.y, target.x, target.y);
this.fitness = map(d, 0, width, width, 0);
if (this.completed) {
this.fitness *= 10;
}
if (this.crashed) {
this.fitness /= 10;
}
}
this.update = function() {
var d = dist(this.pos.x, this.pos.y, target.x, target.y);
if (d < 10) {
this.completed = true;
this.pos = target.copy();
}
// if (this.pos.x > rx && this.pos.x < rx + rw && this.pos.y > ry && this.pos.y < ry +rh) {
// this.crashed = true;
// }
if(this.pos.x > width || this.pos.x < 0) {
this.crashed = true;
}
if (this.pos.y > height || this.pos.y < 0) {
this.crashed = true;
}
this.applyForce(this.dna.genes[count]);
if (!this.completed && !this.crashed) {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
this.vel.limit(4);
}
}
this.show = function() {
push();
noStroke();
fill(0, 172, 237);
translate(this.pos.x, this.pos.y);
rotate(this.vel.heading());
rectMode(CENTER);
// rect(0, 0, 30, 10); //object structure
ellipse(0, 0, 25, 25);
pop();
}
}