-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvolvingPhrase.java
More file actions
99 lines (90 loc) · 2.51 KB
/
EvolvingPhrase.java
File metadata and controls
99 lines (90 loc) · 2.51 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
public class EvolvingPhrase {
private char[] dna;
private static final String allowedGenes = " abcdefghijklmnopqrstuvwxyz";
/**
* Construct a new phrase of the indicated length. Fill the DNA array with chars
* drawn randomly from the allowed genes provided by geneString
*
* @param n
*/
public EvolvingPhrase(int n) {
dna = new char[n];
randomizeDNA();
}
/**
* Construct a new phrase having as DNA the char sequence of the supplied
* string.
*
* @param s
*/
public EvolvingPhrase(String s) {
dna = s.toCharArray();
}
/**
* Assign each element of the DNA array a char drawn at random from the genes
* allowed by geneString.
*/
private void randomizeDNA() {
for (int i = 0; i < dna.length; i++) {
dna[i] = allowedGenes.charAt(
(int) (Math.random() * allowedGenes.length()));
}
}
public String toString() {
return new String(dna);
}
/**
* Count how many chars in the DNA of this phrase match those in the DNA of the
* comparison phrase.
*
* @param comparison
* a reference collection of DNA
* @return the number of chars in common for each location in the DNA array
*/
public int countNumMatches(EvolvingPhrase comparison) {
int numMatches = 0;
for (int i = 0; i < dna.length; i++) {
if (dna[i] == comparison.dna[i]) {
numMatches++;
}
}
return numMatches*10;
}
/**
* For each char in the DNA, generate a random number; if the random number
* falls below the mutation factor, swap the char at that index with a char
* drawn at random from the geneString.
*
* @param mutationFactor
* a double in the range [0.0, 1.0)
*/
public void potentiallyMutate(double mutationFactor) {
for (int i = 0; i < dna.length; i++) {
if (Math.random() < mutationFactor) {
int randomIndex = (int) (Math.random()
* allowedGenes.length());
dna[i] = allowedGenes.charAt(randomIndex);
}
}
}
/**
* Construct and return a new evolving phrase. Each char in the DNA sequence of
* the offspring should match either the corresponding char of this phrase or of
* the partner phrase, determined randomly.
*
* @param partner
* a phrase acting as a second potential source of DNA
* @return a child with DNA sampled from both parents
*/
public EvolvingPhrase produceOffspring(EvolvingPhrase partner) {
String newDNA = "";
for (int i = 0; i < dna.length; i++) {
if (Math.random() < 0.5) {
newDNA += this.dna[i];
} else {
newDNA += partner.dna[i];
}
}
return new EvolvingPhrase(newDNA);
}
}