This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChromo.java
221 lines (168 loc) · 6.78 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
/******************************************************************************
* 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 String 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 = "";
for (int i=0; i<Parameters.numGenes; i++){
for (int j=0; j<Parameters.geneSize; j++){
randnum = Search.r.nextDouble();
if (randnum > 0.5) geneBit = '0';
else geneBit = '1';
this.chromo = chromo + geneBit;
}
}
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(){
String mutChromo = "";
char x;
switch (Parameters.mutationType){
case 1: // Replace with new random number
for (int j=0; j<(Parameters.geneSize * Parameters.numGenes); j++){
x = this.chromo.charAt(j);
randnum = Search.r.nextDouble();
if (randnum < Parameters.mutationRate){
if (x == '1') x = '0';
else x = '1';
}
mutChromo = mutChromo + x;
}
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;
int k = 0;
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
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
xoverPoint1 = 1 + (int)(Search.r.nextDouble() * (Parameters.numGenes * Parameters.geneSize-1));
// Create child chromosome from parental material
child1.chromo = parent1.chromo.substring(0,xoverPoint1) + parent2.chromo.substring(xoverPoint1);
child2.chromo = parent2.chromo.substring(0,xoverPoint1) + parent1.chromo.substring(xoverPoint1);
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){
targetA.chromo = sourceB.chromo;
targetA.rawFitness = sourceB.rawFitness;
targetA.sclFitness = sourceB.sclFitness;
targetA.proFitness = sourceB.proFitness;
return;
}
} // End of Chromo.java ******************************************************