-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.java
424 lines (337 loc) · 13.4 KB
/
Search.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/******************************************************************************
* 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 Search {
/*******************************************************************************
* INSTANCE VARIABLES *
*******************************************************************************/
/*******************************************************************************
* STATIC VARIABLES *
*******************************************************************************/
public static FitnessFunction problem;
public static Chromo[] member;
public static Chromo[] child;
public static Chromo bestOfGenChromo;
public static int bestOfGenR;
public static int bestOfGenG;
public static Chromo bestOfRunChromo;
public static int bestOfRunR;
public static int bestOfRunG;
public static Chromo bestOverAllChromo;
public static int bestOverAllR;
public static int bestOverAllG;
public static double sumRawFitness;
public static double sumRawFitness2; // sum of squares of fitness
public static double sumSclFitness;
public static double sumProFitness;
public static double defaultBest;
public static double defaultWorst;
public static double averageRawFitness;
public static double stdevRawFitness;
public static int G;
public static int R;
public static Random r = new Random();
private static double randnum;
public static int memberIndex[];
private static double memberFitness[];
private static int TmemberIndex;
private static double TmemberFitness;
private static double fitnessStats[][]; // 0=Avg, 1=Best
/*******************************************************************************
* CONSTRUCTORS *
*******************************************************************************/
/*******************************************************************************
* MEMBER METHODS *
*******************************************************************************/
/*******************************************************************************
* STATIC METHODS *
*******************************************************************************/
public static void main(String[] args) throws java.io.IOException{
Calendar dateAndTime = Calendar.getInstance();
Date startTime = dateAndTime.getTime();
// Read Parameter File
System.out.println("\nParameter File Name is: " + args[0] + "\n");
Parameters parmValues = new Parameters(args[0]);
// Write Parameters To Summary Output File
String summaryFileName = Parameters.expID + "_summary.txt";
FileWriter summaryOutput = new FileWriter(summaryFileName);
Parameters.outputParameters(summaryOutput);
// Set up Fitness Statistics matrix
fitnessStats = new double[2][Parameters.generations];
for (int i=0; i<Parameters.generations; i++){
fitnessStats[0][i] = 0;
fitnessStats[1][i] = 0;
}
// Problem Specific Setup - For new new fitness function problems, create
// the appropriate class file (extending FitnessFunction.java) and add
// an else_if block below to instantiate the problem.
// if (Parameters.problemType.equals("NM")){
// problem = new NumberMatch();
// }
// else if (Parameters.problemType.equals("OM")){
// problem = new OneMax();
// }
if (Parameters.problemType.equals("PD")){
problem = new PrisonersDilemmaProblem();
}
else System.out.println("Invalid Problem Type");
System.out.println(problem.name);
// Initialize RNG, array sizes and other objects
r.setSeed(Parameters.seed);
memberIndex = new int[Parameters.popSize];
memberFitness = new double[Parameters.popSize];
member = new Chromo[Parameters.popSize];
child = new Chromo[Parameters.popSize];
bestOfGenChromo = new Chromo();
bestOfRunChromo = new Chromo();
bestOverAllChromo = new Chromo();
if (Parameters.minORmax.equals("max")){
defaultBest = 0;
defaultWorst = 999999999999999999999.0;
}
else{
defaultBest = 999999999999999999999.0;
defaultWorst = 0;
}
bestOverAllChromo.rawFitness = defaultBest;
// Start program for multiple runs
for (R = 1; R <= Parameters.numRuns; R++){
bestOfRunChromo.rawFitness = defaultBest;
System.out.println();
// Initialize First Generation
for (int i=0; i<Parameters.popSize; i++){
member[i] = new Chromo();
child[i] = new Chromo();
}
// Begin Each Run
for (G=0; G<Parameters.generations; G++){
sumProFitness = 0;
sumSclFitness = 0;
sumRawFitness = 0;
sumRawFitness2 = 0;
bestOfGenChromo.rawFitness = defaultBest;
// Test Fitness of Each Member
for (int i=0; i<Parameters.popSize; i++){
member[i].rawFitness = 0;
member[i].sclFitness = 0;
member[i].proFitness = 0;
int numOpponents = Parameters.popSize / 10;
// Set gene values to a randum sequence of 1's and 0's
int [] popSample = new int [Parameters.popSize];
for (int p=0; p<Parameters.popSize; p++) popSample[p] = p;
// shuffle the array
for (int p=0; p<1000; p++){
int i1 = Search.r.nextInt(Parameters.popSize);
int i2 = Search.r.nextInt(Parameters.popSize);
// swap the indices
int temp = popSample[i1];
popSample[i1] = popSample[i2];
popSample[i2] = temp;
}
int countedOpponents = 0;
int index = 0;
Chromo [] opponents = new Chromo[numOpponents];
while (countedOpponents < numOpponents){
int opponentIndex = popSample[index];
if(opponentIndex != i){
opponents[countedOpponents] = member[opponentIndex];
countedOpponents++;
}
index++;
}
problem.doRawFitnessTest(member[i], opponents, 100);
sumRawFitness = sumRawFitness + member[i].rawFitness;
sumRawFitness2 = sumRawFitness2 +
member[i].rawFitness * member[i].rawFitness;
if (Parameters.minORmax.equals("max")){
if (member[i].rawFitness > bestOfGenChromo.rawFitness){
Chromo.copyB2A(bestOfGenChromo, member[i]);
bestOfGenR = R;
bestOfGenG = G;
}
if (member[i].rawFitness > bestOfRunChromo.rawFitness){
Chromo.copyB2A(bestOfRunChromo, member[i]);
bestOfRunR = R;
bestOfRunG = G;
}
if (member[i].rawFitness > bestOverAllChromo.rawFitness){
Chromo.copyB2A(bestOverAllChromo, member[i]);
bestOverAllR = R;
bestOverAllG = G;
}
}
else {
if (member[i].rawFitness < bestOfGenChromo.rawFitness){
Chromo.copyB2A(bestOfGenChromo, member[i]);
bestOfGenR = R;
bestOfGenG = G;
}
if (member[i].rawFitness < bestOfRunChromo.rawFitness){
Chromo.copyB2A(bestOfRunChromo, member[i]);
bestOfRunR = R;
bestOfRunG = G;
}
if (member[i].rawFitness < bestOverAllChromo.rawFitness){
Chromo.copyB2A(bestOverAllChromo, member[i]);
bestOverAllR = R;
bestOverAllG = G;
}
}
}
// Accumulate fitness statistics
fitnessStats[0][G] += sumRawFitness / Parameters.popSize;
fitnessStats[1][G] += bestOfGenChromo.rawFitness;
averageRawFitness = sumRawFitness / Parameters.popSize;
stdevRawFitness = Math.sqrt(
Math.abs(sumRawFitness2 -
sumRawFitness*sumRawFitness/Parameters.popSize)
/
(Parameters.popSize-1)
);
// Output generation statistics to screen
System.out.println(R + "\t" + G + "\t" + (int)bestOfGenChromo.rawFitness + "\t" + averageRawFitness + "\t" + stdevRawFitness);
// Output generation statistics to summary file
summaryOutput.write(" R ");
Hwrite.right(R, 3, summaryOutput);
summaryOutput.write(" G ");
Hwrite.right(G, 3, summaryOutput);
Hwrite.right((int)bestOfGenChromo.rawFitness, 7, summaryOutput);
Hwrite.right(averageRawFitness, 11, 3, summaryOutput);
Hwrite.right(stdevRawFitness, 11, 3, summaryOutput);
summaryOutput.write("\n");
// *********************************************************************
// **************** SCALE FITNESS OF EACH MEMBER AND SUM ***************
// *********************************************************************
switch(Parameters.scaleType){
case 0: // No change to raw fitness
for (int i=0; i<Parameters.popSize; i++){
member[i].sclFitness = member[i].rawFitness + .000001;
sumSclFitness += member[i].sclFitness;
}
break;
case 1: // Fitness not scaled. Only inverted.
for (int i=0; i<Parameters.popSize; i++){
member[i].sclFitness = 1/(member[i].rawFitness + .000001);
sumSclFitness += member[i].sclFitness;
}
break;
case 2: // Fitness scaled by Rank (Maximizing fitness)
// Copy genetic data to temp array
for (int i=0; i<Parameters.popSize; i++){
memberIndex[i] = i;
memberFitness[i] = member[i].rawFitness;
}
// Bubble Sort the array by floating point number
for (int i=Parameters.popSize-1; i>0; i--){
for (int j=0; j<i; j++){
if (memberFitness[j] > memberFitness[j+1]){
TmemberIndex = memberIndex[j];
TmemberFitness = memberFitness[j];
memberIndex[j] = memberIndex[j+1];
memberFitness[j] = memberFitness[j+1];
memberIndex[j+1] = TmemberIndex;
memberFitness[j+1] = TmemberFitness;
}
}
}
// Copy ordered array to scale fitness fields
for (int i=0; i<Parameters.popSize; i++){
member[memberIndex[i]].sclFitness = i;
sumSclFitness += member[memberIndex[i]].sclFitness;
}
break;
case 3: // Fitness scaled by Rank (minimizing fitness)
// Copy genetic data to temp array
for (int i=0; i<Parameters.popSize; i++){
memberIndex[i] = i;
memberFitness[i] = member[i].rawFitness;
}
// Bubble Sort the array by floating point number
for (int i=1; i<Parameters.popSize; i++){
for (int j=(Parameters.popSize - 1); j>=i; j--){
if (memberFitness[j-i] < memberFitness[j]){
TmemberIndex = memberIndex[j-1];
TmemberFitness = memberFitness[j-1];
memberIndex[j-1] = memberIndex[j];
memberFitness[j-1] = memberFitness[j];
memberIndex[j] = TmemberIndex;
memberFitness[j] = TmemberFitness;
}
}
}
// Copy array order to scale fitness fields
for (int i=0; i<Parameters.popSize; i++){
member[memberIndex[i]].sclFitness = i;
sumSclFitness += member[memberIndex[i]].sclFitness;
}
break;
default:
System.out.println("ERROR - No scaling method selected");
}
// *********************************************************************
// ****** PROPORTIONALIZE SCALED FITNESS FOR EACH MEMBER AND SUM *******
// *********************************************************************
for (int i=0; i<Parameters.popSize; i++){
member[i].proFitness = member[i].sclFitness/sumSclFitness;
sumProFitness = sumProFitness + member[i].proFitness;
}
// *********************************************************************
// ************ CROSSOVER AND CREATE NEXT GENERATION *******************
// *********************************************************************
int parent1 = -1;
int parent2 = -1;
// Assumes always two offspring per mating
for (int i=0; i<Parameters.popSize; i=i+2){
// Select Two Parents
parent1 = Chromo.selectParent();
parent2 = parent1;
while (parent2 == parent1){
parent2 = Chromo.selectParent();
}
// Crossover Two Parents to Create Two Children
randnum = r.nextDouble();
if (randnum < Parameters.xoverRate){
Chromo.mateParents(parent1, parent2, member[parent1], member[parent2], child[i], child[i+1]);
}
else {
Chromo.mateParents(parent1, member[parent1], child[i]);
Chromo.mateParents(parent2, member[parent2], child[i+1]);
}
} // End Crossover
// Mutate Children
for (int i=0; i<Parameters.popSize; i++){
child[i].doMutation();
}
// Swap Children with Last Generation
for (int i=0; i<Parameters.popSize; i++){
Chromo.copyB2A(member[i], child[i]);
}
} // Repeat the above loop for each generation
Hwrite.left(bestOfRunR, 4, summaryOutput);
Hwrite.right(bestOfRunG, 4, summaryOutput);
problem.doPrintGenes(bestOfRunChromo, summaryOutput);
System.out.println(R + "\t" + "B" + "\t"+ (int)bestOfRunChromo.rawFitness);
} //End of a Run
Hwrite.left("B", 8, summaryOutput);
problem.doPrintGenes(bestOverAllChromo, summaryOutput);
// Output Fitness Statistics matrix
summaryOutput.write("Gen AvgFit BestFit \n");
for (int i=0; i<Parameters.generations; i++){
Hwrite.left(i, 15, summaryOutput);
Hwrite.left(fitnessStats[0][i]/Parameters.numRuns, 20, 2, summaryOutput);
Hwrite.left(fitnessStats[1][i]/Parameters.numRuns, 20, 2, summaryOutput);
summaryOutput.write("\n");
}
summaryOutput.write("\n");
summaryOutput.close();
System.out.println();
System.out.println("Start: " + startTime);
dateAndTime = Calendar.getInstance();
Date endTime = dateAndTime.getTime();
System.out.println("End : " + endTime);
} // End of Main Class
} // End of Search.Java ******************************************************