/
Population.pde
174 lines (146 loc) · 4.68 KB
/
Population.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
//*****************************************************
// Natural Selection
// Jack Kalish - ITP Spring 2012
//*****************************************************
// Interactive Selection
// http://www.genarts.com/karl/papers/siggraph91.html
// Daniel Shiffman <http://www.shiffman.net>
// A class to describe a population of recursive trees structures
class Population {
int MAX; //population maximum
float mutationRate; //mutation rate
Drawing[] population; //arraylist to hold the current population
DNA[] genotypes;
ArrayList darwin; //ArrayList which we will use for our "mating pool"
int generations; //number of generations
int _id;
Drawing _healthiestChild;
Drawing mom;
float rotX, rotY, rotZ = 0;
//*INITIALIZE THE POPULATION*//
Population(float m, int num, boolean randomize) {
mutationRate = m;
MAX = num;
population = new Drawing[MAX];
darwin = new ArrayList();
generations = 0;
for (int i = 0; i < population.length; i++) {
population[i] = new Drawing(new DNA(randomize), width/2, height/2);
}
}
//display all faces
void display(int id) {
// println("Population display: "+id);
_id = id;
pushMatrix();
translate(width/2, height/2);
rotateX(rotX);
rotateY(rotY);
rotateZ(rotZ);
population[id].render();
popMatrix();
rotX+=.01;
rotY+=.03;
rotZ+=.02;
if (rotX >= 2*PI) {
rotX = 0;
}
if (rotY >= 2*PI) {
rotY = 0;
}
if (rotZ >= 2*PI) {
rotZ = 0;
}
}
//generate a mating pool
void naturalSelection() {
_healthiestChild = population[0];
for (int i = 0; i < population.length; i++) {
if (population[i].getFitness() > _healthiestChild.getFitness()) {
_healthiestChild = population[i];
}
}
}
void naturalSelectionSexual() {
//clear the ArrayList
darwin.clear();
//Calculate total fitness of whole population
float totalFitness = getTotalFitness();
//Calculate *normalized* fitness for each member of the population
//based on normalized fitness, each member will get added to the mating pool a certain number of times a la roulette wheel
//a higher fitness = more entries to mating pool = more likely to be picked as a parent
//a lower fitness = fewer entries to mating pool = less likely to be picked as a parent
for (int i = 0; i < population.length; i++) {
float fitnessNormal = population[i].getFitness() / totalFitness;
int n = (int) (fitnessNormal * 1000.0f);
//print(n + " ");
for (int j = 0; j < n; j++) {
darwin.add(population[i]);
}
}
//println();
//println("----------------
}
//*CREATE A NEW GENERATION**//
void generate() {
//add first member of next generation as an exact clone of the mother?
//CHANGE THIS TO CHOOSE ONLY THE FITTEST, (no mating)
for (int i = 0; i < population.length; i++) {
// println("make child: "+ i);
mom = _healthiestChild;
//get their genes
DNA momgenes = mom.getGenes();
//mutate their genes
DNA child = new DNA(momgenes.getMutatedDNA(mutationRate));
//fill the new population with the new child
population[i] = new Drawing(child, width/2, height/2);
}
generations++;
}
void generateSexual() {
//add first member of next generation as an exact clone of the mother?
//CHANGE THIS TO CHOOSE ONLY THE FITTEST, (no mating)
for (int i = 0; i < population.length; i++) {
// println("make child: "+ i);
mom = _healthiestChild;
//get their genes
DNA momgenes = mom.getGenes();
//mutate their genes
DNA child = new DNA(momgenes.getMutatedDNA(mutationRate));
//fill the new population with the new child
population[i] = new Drawing(child, width/2, height/2);
}
generations++;
}
//make new set of children from the same parent
void regenerate(){
for (int i = 0; i < population.length; i++) {
//get their genes
DNA momgenes = mom.getGenes();
//mutate their genes
DNA child = new DNA(momgenes.getMutatedDNA(mutationRate));
//fill the new population with the new child
population[i] = new Drawing(child, width/2, height/2);
}
}
float[] getMomDNA() {
return mom.getGenes().getDNA();
}
void scoreCurrent(int m) {
population[_id].score(m);
}
int getGenerations() {
return generations;
}
//compute total fitness for the population
float getTotalFitness() {
float total = 0;
for (int i = 0; i < population.length; i++) {
total += population[i].getFitness();
}
return total;
}
Drawing getChildAt(int i) {
return population[i];
}
}