-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
56 lines (47 loc) · 1.31 KB
/
code
File metadata and controls
56 lines (47 loc) · 1.31 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
PFont f;
String target;
int popmax;
float mutationRate;
Population population;
void setup() {
size(640, 360);
f = createFont("Courier", 32, true);
target = "To be or not to be.";
popmax = 150;
mutationRate = 0.01;
// Create a populationation with a target phrase, mutation rate, and populationation max
population = new Population(target, mutationRate, popmax);
}
void draw() {
// Generate mating pool
population.naturalSelection();
//Create next generation
population.generate();
// Calculate fitness
population.calcFitness();
displayInfo();
// If we found the target phrase, stop
if (population.finished()) {
println(millis()/1000.0);
noLoop();
}
}
void displayInfo() {
background(255);
// Display current status of populationation
String answer = population.getBest();
textFont(f);
textAlign(LEFT);
fill(0);
textSize(24);
text("Best phrase:",20,30);
textSize(40);
text(answer, 20, 100);
textSize(18);
text("total generations: " + population.getGenerations(), 20, 160);
text("average fitness: " + nf(population.getAverageFitness(), 0, 2), 20, 180);
text("total population: " + popmax, 20, 200);
text("mutation rate: " + int(mutationRate * 100) + "%", 20, 220);
textSize(10);
text("All phrases:\n" + population.allPhrases(), 500, 10);
}