-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsketch.js
More file actions
148 lines (119 loc) · 2.91 KB
/
sketch.js
File metadata and controls
148 lines (119 loc) · 2.91 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
// José Bezerra - 21/07/2018
// josebezerraneto@outlook.com
var rockets;
var target;
var lifespan = 200;
var count = 0;
var maxPop = 50;
var mutRate = 0.01;
var lifespanP;
var genP;
var inTargetP;
var avgFitP;
let pressed, done = false;
var mxBegin, myBegin, mxEnd, myEnd;
function setup() {
createCanvas(1000, 500);
rockets = new Population();
target = createVector(width / 2, 40);
lifespanP = createP();
genP = createP();
inTargetP = createP();
HTMLStuffs();
}
function draw() {
background(0);
rockets.show();
if (count >= lifespan) {
rockets.calcFitness();
rockets.naturalSelection();
rockets.maxPop = maxPop;
count = 0;
}
fill(255);
ellipse(target.x, target.y, 20, 20);
count++;
displayInfos();
//draw obstacles
if (pressed && !done) {
fill(0, 0, 255, 180);
stroke(255);
rect(mxBegin, myBegin, mouseX - mxBegin, mouseY - myBegin);
}
if (!pressed && done) {
done = false;
}
if (!pressed && !done) {
fill(0, 0, 255);
stroke(255);
strokeWeight(2);
rect(mxBegin, myBegin, mxEnd - mxBegin, myEnd - myBegin);
}
}
// Started Interaction
function mousePressed() {
mxEnd = 0;
myEnd = 0;
mxBegin = mouseX;
myBegin = mouseY;
pressed = true;
}
// Stopped Interaction
function mouseReleased() {
mxEnd = mouseX;
myEnd = mouseY;
pressed = false;
done = true;
}
function displayInfos() {
lifespanP.html(count + "<br>");
var textP = "";
textP += "Rockets in Target: " + rockets.inTarget + "<br><br>";
textP += "Generation: " + rockets.generations + "<br>";
textP += "population size: " + maxPop + "<br>";
textP += "Mutation rate: " + mutRate + "<br>";
textP += "Lifespan: " + lifespan;
genP.html(textP);
}
function HTMLStuffs() {
createElement("h2", "Change Parameters")
createP("here are some values you can play with")
// Change Pop Size
createElement("h3", "Change Population Size")
popMinus = createButton("-");
popPlus = createButton('+');
popPlus.mousePressed(plusPopSize);
popMinus.mousePressed(minusPopSize);
// Change Mut Rate
createElement("h3", "Change Mutation Rate")
mutMinus = createButton("-");
mutPlus = createButton('+');
mutPlus.mousePressed(plusMutRate);
mutMinus.mousePressed(minusMutRate);
// Change Lifespan
createElement("h3", "Change Lifespan")
popMinus = createButton("-");
popPlus = createButton('+');
popPlus.mousePressed(plusLifespan);
popMinus.mousePressed(minusLifespan);
}
function plusPopSize() {
maxPop += 10;
}
function minusPopSize() {
if (maxPop - 10 > 0) {
maxPop -= 10;
}
}
function plusMutRate() {
mutRate += 0.01;
}
function minusMutRate() {
mutRate -= 0.01;
}
function plusLifespan() {
lifespan += 50;
}
function minusLifespan() {
lifespan -= 50;
}