-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chromo.java
276 lines (216 loc) · 8.63 KB
/
Chromo.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/******************************************************************************
* A Teaching GA Developed by Hal Stringer & Annie Wu, UCF
* Version 2, January 18, 2004
*******************************************************************************/
import java.io.*;
import java.util.*;
import java.text.*;
public class Chromo
{
/*******************************************************************************
* INSTANCE VARIABLES *
*******************************************************************************/
public double [] chromo;
public double rawFitness;
public double sclFitness;
public double proFitness;
/*******************************************************************************
* INSTANCE VARIABLES *
*******************************************************************************/
private static double randnum;
/*******************************************************************************
* CONSTRUCTORS *
*******************************************************************************/
public Chromo(){
// Set gene values to a randum sequence of 1's and 0's
char geneBit;
chromo = new double [Parameters.numGenes];
for (int i=0; i<Parameters.numGenes; i++){
randnum = Search.r.nextDouble();
chromo[i] = randnum;
}
this.rawFitness = -1; // Fitness not yet evaluated
this.sclFitness = -1; // Fitness not yet scaled
this.proFitness = -1; // Fitness not yet proportionalized
}
/*******************************************************************************
* MEMBER METHODS *
*******************************************************************************/
// Get Alpha Represenation of a Gene **************************************
// public String getGeneAlpha(int geneID){
// int start = geneID * Parameters.geneSize;
// int end = (geneID+1) * Parameters.geneSize;
// String geneAlpha = this.chromo.substring(start, end);
// return (geneAlpha);
// }
// Get Integer Value of a Gene (Positive or Negative, 2's Compliment) ****
// public int getIntGeneValue(int geneID){
// String geneAlpha = "";
// int geneValue;
// char geneSign;
// char geneBit;
// geneValue = 0;
// geneAlpha = getGeneAlpha(geneID);
// for (int i=Parameters.geneSize-1; i>=1; i--){
// geneBit = geneAlpha.charAt(i);
// if (geneBit == '1') geneValue = geneValue + (int) Math.pow(2.0, Parameters.geneSize-i-1);
// }
// geneSign = geneAlpha.charAt(0);
// if (geneSign == '1') geneValue = geneValue - (int)Math.pow(2.0, Parameters.geneSize-1);
// return (geneValue);
// }
// Get Integer Value of a Gene (Positive only) ****************************
// public int getPosIntGeneValue(int geneID){
// String geneAlpha = "";
// int geneValue;
// char geneBit;
// geneValue = 0;
// geneAlpha = getGeneAlpha(geneID);
// for (int i=Parameters.geneSize-1; i>=0; i--){
// geneBit = geneAlpha.charAt(i);
// if (geneBit == '1') geneValue = geneValue + (int) Math.pow(2.0, Parameters.geneSize-i-1);
// }
// return (geneValue);
// }
// Mutate a Chromosome Based on Mutation Type *****************************
public void doMutation(){
double [] mutChromo = new double[Parameters.numGenes];
switch (Parameters.mutationType){
case 1: // Replace with new random number
// TODO: look up Gaussian mutation
for (int j=0; j<(Parameters.numGenes); j++){
randnum = Search.r.nextDouble();
double change = Search.r.nextDouble() / 10;
boolean negative = Search.r.nextBoolean();
if (randnum < Parameters.mutationRate){
// TODO: implement Gaussian mutation here
if (negative) change *= -1;
mutChromo[j] = Math.min(Math.max(chromo[j] + change, 0),1);
} else {
mutChromo[j] = chromo[j];
}
}
this.chromo = mutChromo;
break;
default:
System.out.println("ERROR - No mutation method selected");
}
}
/*******************************************************************************
* STATIC METHODS *
*******************************************************************************/
// Select a parent for crossover ******************************************
public static int selectParent(){
double rWheel = 0;
int j = 0;
double k = 0.9;
double rand;
switch (Parameters.selectType){
case 1: // Proportional Selection
randnum = Search.r.nextDouble();
for (j=0; j<Parameters.popSize; j++){
rWheel = rWheel + Search.member[j].proFitness;
if (randnum < rWheel) return(j);
}
break;
case 3: // Random Selection
randnum = Search.r.nextDouble();
j = (int) (randnum * Parameters.popSize);
return(j);
case 2: // Tournament Selection
rand = Search.r.nextDouble();
int randComp1 = Search.r.nextInt(Parameters.popSize);
int randComp2 = Search.r.nextInt(Parameters.popSize);
if (rand < k) {
if (Search.member[randComp1].proFitness < Search.member[randComp2].proFitness)
return randComp2;
else
return randComp1;
} else {
if (Search.member[randComp1].proFitness < Search.member[randComp2].proFitness)
return randComp1;
else
return randComp2;
}
case 4: // Rank Selection
//To implement rank selection, scale type must be 3 or 4
if (Parameters.scaleType == 0 || Parameters.scaleType == 1) {
System.out.println("Error - to use rank selection, please change scale type to rank 2 or 3");
return -1;
} else {
//Normal proportional selection
rand = Search.r.nextDouble();
for (j = 0; j < Parameters.popSize; j++) {
rWheel = rWheel + Search.member[Search.memberIndex[j]].proFitness;
if (rand < rWheel)
return Search.memberIndex[j];
}
}
break;
default:
System.out.println("ERROR - No selection method selected");
}
return(-1);
}
// Produce a new child from two parents **********************************
public static void mateParents(int pnum1, int pnum2, Chromo parent1, Chromo parent2, Chromo child1, Chromo child2){
int xoverPoint1;
int xoverPoint2;
switch (Parameters.xoverType){
case 1: // Single Point Crossover
// Select crossover point
// TODO: assert this is correct
xoverPoint1 = (int)(Search.r.nextDouble() * (Parameters.numGenes));
double [] child1Array = new double[Parameters.numGenes];
double [] child2Array = new double[Parameters.numGenes];
for (int i=0; i<Parameters.numGenes; i++){
if(i < xoverPoint1){
// left half of child1 is average of both parents
child1Array[i] = (parent1.chromo[i] + parent2.chromo[i]) / 2;
//child1Array[i] = parent1.chromo[i];
// left half of child2 is the same as parent2
child2Array[i] = parent2.chromo[i];
} else {
// right half of child1 is the same as parent1
child1Array[i] = parent2.chromo[i];
// right half of child2 is the average of both parents
child2Array[i] = (parent1.chromo[i] + parent2.chromo[i]) / 2;
}
}
// Create child chromosome from parental material
child1.chromo = child1Array;
child2.chromo = child2Array;
break;
case 2: // Two Point Crossover
case 3: // Uniform Crossover
default:
System.out.println("ERROR - Bad crossover method selected");
}
// Set fitness values back to zero
child1.rawFitness = -1; // Fitness not yet evaluated
child1.sclFitness = -1; // Fitness not yet scaled
child1.proFitness = -1; // Fitness not yet proportionalized
child2.rawFitness = -1; // Fitness not yet evaluated
child2.sclFitness = -1; // Fitness not yet scaled
child2.proFitness = -1; // Fitness not yet proportionalized
}
// Produce a new child from a single parent ******************************
public static void mateParents(int pnum, Chromo parent, Chromo child){
// Create child chromosome from parental material
child.chromo = parent.chromo;
// Set fitness values back to zero
child.rawFitness = -1; // Fitness not yet evaluated
child.sclFitness = -1; // Fitness not yet scaled
child.proFitness = -1; // Fitness not yet proportionalized
}
// Copy one chromosome to another ***************************************
public static void copyB2A (Chromo targetA, Chromo sourceB){
//Modified by Yu Zou
// targetA.chromo = sourceB.chromo;
System.arraycopy(sourceB.chromo, 0, targetA.chromo, 0, sourceB.chromo.length);
targetA.rawFitness = sourceB.rawFitness;
targetA.sclFitness = sourceB.sclFitness;
targetA.proFitness = sourceB.proFitness;
return;
}
} // End of Chromo.java ******************************************************