-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeno.java
More file actions
139 lines (124 loc) · 3.11 KB
/
Copy pathGeno.java
File metadata and controls
139 lines (124 loc) · 3.11 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
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Geno {
public static final String TARGET = "EVOLUTION SIMULATOR";
//not necessary to use Character type
private static final char[] MY_FULL_LIST = new char[]{ 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', ' ', '-', '\''};
private static Random myRandom = new Random();
public double myMutationRate; // mutation rate means that whether a gene will mutate or not
private List<Character> gene;
//constructor1
public Geno(double theMutationRate) {
myMutationRate = theMutationRate;
gene = new ArrayList<Character>();
gene.add('A');
}
//constructor2
public Geno(Geno thegene) {
myMutationRate = thegene.myMutationRate;
gene = new ArrayList<Character>();
gene.addAll(thegene.gene);
}
//mutate
public void mutate() {
//add
double r1 = myRandom.nextDouble();
if (r1 <= myMutationRate) {
addRandomChar();
}
//delete
double r2 = myRandom.nextDouble();
if (r2 <= myMutationRate) {
deleteRandomChar();
}
//replace a certain character
for(int i = 0; i < gene.size(); i++) {
double r3 = myRandom.nextDouble();
if(r3 <= myMutationRate) {
replaceRandomChar(i);
}
}
}
//implementation of three mutations
//add
private void addRandomChar() {
int charIndex = myRandom.nextInt(29);
int index = myRandom.nextInt(gene.size() +1);
if (index < gene.size()) {
gene.add(index, MY_FULL_LIST[charIndex]);
} else {
gene.add(MY_FULL_LIST[charIndex]);
}
}
//delete
private void deleteRandomChar() {
if (gene.size() >= 2) {
int charIndex = myRandom.nextInt(gene.size());
gene.remove(charIndex);
}
}
//replace
private void replaceRandomChar(int theIndex) {
//not necessary to check the theIndex out the boundary or not
int charIndex = myRandom.nextInt(29);
gene.set(theIndex, MY_FULL_LIST[charIndex]);
}
//crossover
//will update the current gene
public void crossover(Geno other) {
List<Character> otherGene = new ArrayList<Character>();
otherGene.addAll(other.gene);
List<Character> newGene = new ArrayList<Character>();
int i = 0;
while(true){
int r = myRandom.nextInt(2);
if(r == 0){
if(i < gene.size())
newGene.add(gene.get(i));
else
break;
}else{
if(i < otherGene.size())
newGene.add(otherGene.get(i));
else
break;
}
i++;
}
this.gene.clear();
this.gene.addAll(newGene);
}
//fitness
public Integer fitness() {
int n = gene.size();
int m = TARGET.length();
int l = Math.max(n, m);
int f = Math.abs(m - n);
for(int i = 0; i < l; i++){
if (i < gene.size()
&& i < TARGET.length()) {
if (gene.get(i) != TARGET.charAt(i)) {
f++;
}
} else {
f++;
}
}
return f;
}
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("(\"");
for(Character c: gene) {
stringBuilder.append(c);
}
stringBuilder.append("\", ");
stringBuilder.append(fitness());
stringBuilder.append(")");
return stringBuilder.toString();
}
}