-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhraseRunner.java
More file actions
118 lines (105 loc) · 3.34 KB
/
PhraseRunner.java
File metadata and controls
118 lines (105 loc) · 3.34 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
public class PhraseRunner {
private static EvolvingPhrase targetPhrase;
private static EvolvingPhrase[] population;
private static int populationFitness;
private static final int POP_SIZE = 300;
private static final int NUM_GENERATIONS = 2000;
public static void main(String[] args) {
String target = "to be or not to be that is the question";
targetPhrase = new EvolvingPhrase(target);
population = new EvolvingPhrase[POP_SIZE];
for (int i = 0; i < population.length; i++) {
population[i] = new EvolvingPhrase(target.length());
}
// initial population
System.out.println("Initial population");
for (EvolvingPhrase ep : population) {
System.out.println(ep);
}
populationFitness = getPopulationFitness();
//System.out.println("Total fitness: "+populationFitness);
for (int gen = 0; gen < NUM_GENERATIONS; gen++) {
createNextGeneration();
//System.out.println("Generation "+gen+"\tTotal fitness: "+populationFitness);
}
System.out.println("Final population");
int correctCount = 0;
for (EvolvingPhrase ep : population) {
System.out.println(ep);
if (ep.toString().equals(target))
correctCount++;
}
System.out.println("Optimal member count " + correctCount);
}
/**
* Sums the number of correct DNA matches across the entire population.
*
* @return
*/
private static int getPopulationFitness() {
int sum = 0;
for (EvolvingPhrase ep : population) {
sum += ep.countNumMatches(targetPhrase);
}
return sum;
}
private static void createNextGeneration() {
populationFitness = getPopulationFitness();
EvolvingPhrase[] nextPop = new EvolvingPhrase[POP_SIZE];
for (int i = 0; i < POP_SIZE; i++) {
EvolvingPhrase a = getRandomPartner();
EvolvingPhrase b = getRandomPartner();
nextPop[i] = a.produceOffspring(b);
nextPop[i].potentiallyMutate(.0005);
}
population = nextPop;
}
private static void createNextGeneration2() {
EvolvingPhrase[] pool = getMatingPool();
EvolvingPhrase[] nextPop = new EvolvingPhrase[POP_SIZE];
for (int i = 0; i<POP_SIZE; i++) {
EvolvingPhrase a = getRandomPartner2(pool);
EvolvingPhrase b = getRandomPartner2(pool);
nextPop[i] = a.produceOffspring(b);
nextPop[i].potentiallyMutate(.0005);
}
}
private static EvolvingPhrase getRandomPartner2(
EvolvingPhrase[] pool) {
int randIndex = (int) (Math.random()*pool.length);
return pool[randIndex];
}
/**
* Select at random a single member of the current population. If the member is
* "fit enough" to mate, return its reference. If not, try again.
*
* @param population
* @return
*/
private static EvolvingPhrase getRandomPartner() {
while (true) {
int selectedIndex = (int) (Math.random() * population.length);
double qualifying = Math.random();
if (qualifying < population[selectedIndex].countNumMatches(targetPhrase)/(double) populationFitness) {
return population[selectedIndex];
}
}
}
/**
* Creates a pool where each member occurs the number of times equal to its fitness
* @return
*/
private static EvolvingPhrase[] getMatingPool() {
int totalFitness = getPopulationFitness();
EvolvingPhrase[] pool = new EvolvingPhrase[totalFitness];
int index = 0;
for (EvolvingPhrase ep: population) {
int numOccurrences = ep.countNumMatches(targetPhrase);
for (int i = 0; i<numOccurrences; i++) {
pool[index] = ep;
index++;
}
}
return pool;
}
}