-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvolver.java
More file actions
278 lines (187 loc) · 8.11 KB
/
Evolver.java
File metadata and controls
278 lines (187 loc) · 8.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
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
/**
* Project 74 : Evolver
*
* See history.txt for versioning information
*
* General test-bed class for the GA Engine
*
* (C)2002 Damian Murphy
*
*/
import java.io.*;
import java.lang.*;
import java.util.*;
public class Evolver {
// Global Constants
public static final String VER = "0.7.1";
// Set defaults
static int POP_SIZE;
static int NUM_GENES;
static int INV_RATE; // inversion rate% = INV_RATE/1000
static int GENERATIONS;
static int USE_DISPLAY = 1; // 0 = no window, 1 = use window
static double best[] = new double[2];
static double worst[] = new double[2];
static double[] best_so_far;
static Display status;
public static TSPReader TSPInput;
static long startTime = System.currentTimeMillis();
static long endTime;
static GaPlots plotter;
static String plotfile = "";
// Class Methods
private static int parseCommand(String[] args){
// Retrieve options from the command line
// and set variables accordingly...
// Must have certain parameters upon startup, so
// return -1 to main so we can exit with help given.
if ( args.length == 0 ){
return -1;
}
else {
// Now, we need to parse the command args
//for ( int i=0; i < args.length; i++ ){
INV_RATE = Integer.parseInt(args[0]);
POP_SIZE = Integer.parseInt(args[1]);
NUM_GENES = Integer.parseInt(args[2]);
GENERATIONS = Integer.parseInt(args[3]);
plotfile = args[4];
return 0;
}
}
private static void helpme(){
// Print out basic command-line help & version info
System.out.print("\nEvolver, v" + VER + "\n");
System.out.print("\nUsage:\n");
System.out.print("\tevolver [options] PARAMETERS\n");
System.out.print("\nWhere PARAMETERS are :\n");
System.out.print(" INV_RATE \t : the inversion rate (0..1000)\n");
System.out.print(" POPSIZE \t : population size\n");
System.out.print(" NUM_GENES \t : number of genes in each individual\n");
System.out.print(" GENERATIONS \t : number of generations to run for\n");
System.out.print(" PLOTFILE \t : output file name for plotting info\n\n");
}
private static long getEndTime(){
endTime = System.currentTimeMillis();
return endTime;
}
private static void printRunTime(){
long runtime, hours, minutes, seconds;
runtime = endTime - startTime;
hours = Math.round( (((runtime / 1000) / 60) / 60) );
minutes = Math.round( ((runtime / 1000) / 60) ) % 60 ;
seconds = Math.round( runtime / 1000) % 60;
System.out.print( hours + " hours, " + minutes + " minutes, and " + seconds + " seconds.");
}
/**
* Evolver main method - does all the work!
*
**/
public static void main(String[] args) {
int errorlevel = 0;
int sample = 0;
// Firstly, check & parse the command line
if ( parseCommand(args) == -1 ){
helpme();
System.exit(255); // Bye-bye!
}
TSPInput = new TSPReader();
NUM_GENES = TSPInput.numCities();
plotter = new GaPlots( plotfile );
plotter.init();
best_so_far = new double[ NUM_GENES + 2 ];
Population family = new Population (POP_SIZE, NUM_GENES, INV_RATE);
// Create & show Window
if ( USE_DISPLAY == 1 ){
status = new Display( POP_SIZE, NUM_GENES, INV_RATE, GENERATIONS, VER);
status.show();
}
// Dump out time & other stuff.
System.out.print("Started at UNIX time : " + startTime + "\n\n");
System.out.print("Evolver v" + VER + "\n");
System.out.print("Running on " + System.getProperty("os.name") + ", version " + System.getProperty("os.version") + "\nProcessor type : " + System.getProperty("os.arch") + "\n\n");
for ( int years = 0; years < GENERATIONS; years++){
System.out.print("Generation : " + family.getGeneration() + "\n\n");
family.doFitness();
best = family.getBest();
worst = family.getWorst();
best_so_far = family.getBestSoFar();
System.out.print("Best Pupil : " + (int) best[0]
+ ", Fitness = " + best[1] /*+ ", Birthday = " + family.getAge( (int) best[0] )*/
+ "\nWorst Troublemaker : "
+ (int) worst[0] + ", Fitness = " + worst[1] /*+ ", Birthday = " + family.getAge( (int) worst[0] )*/
+ "\nAverage Fitness = "
+ family.getAverage() + "\nBest Fitness so far = " + best_so_far[NUM_GENES] + ", in Generation " + (int) best_so_far[NUM_GENES + 1] + "\n\n");
// Dump out the top five individuals
System.out.print("+++ TOP 5 +++\n");
family.printTopFive();
System.out.print("=== TOP 5 ===\n\n");
// Update the window display
if ( USE_DISPLAY == 1 ){
status.update( best, worst, family.getGeneration(), family.getAverage(), best_so_far[NUM_GENES], best_so_far[NUM_GENES + 1] );
}
// Save plotting info
if ( sample == 0 ){ // every 10,000 generations, plot a value
plotter.save( family.getGeneration(), best[1] );
}
// Dump out the initial population
if ( years == 0 ){
for (int n=0; n < POP_SIZE; n++ ){
System.out.print("Individual No." + n + " -> ");
family.printIndividual(n);
family.printFitness(n);
System.out.print("\n");
}
}
// Dump out every 'sample' and 'sample+1' population
/*if ( ( sample == 0 ) || ( sample == 1 ) ){
for (int n=0; n < POP_SIZE; n++ ){
System.out.print("Individual No." + n + " -> ");
family.printIndividual(n);
family.printFitness(n);
System.out.print("\n");
}
}*/
// Dump out the final generation
if ( years == (GENERATIONS - 1) ){
for (int n=0; n < POP_SIZE; n++ ){
System.out.print("Individual No." + n + " -> ");
family.printIndividual(n);
family.printFitness(n);
System.out.print("\n");
}
}
family.doSelection();
System.out.print("\n<->\n\n");
sample++;
if ( sample == 10000 ){
sample = 0;
}
}
// Finished the run.
// Dump the best ever individual and quit!
System.out.print("Finished!\n");
System.out.print("Best Ever Individual was found in generation " + (int) best_so_far[NUM_GENES + 1] + ", with fitness of " + best_so_far[NUM_GENES] + "\n");
System.out.print("Best ->");
// Save the best over tour in the plotfile for plotting purposes
plotter.saveBestInit(); // print headers
for ( int n=0; n < NUM_GENES; n++ ){
System.out.print(":" + (int) best_so_far[n] );
plotter.saveBest( (int) best_so_far[n], TSPInput.getX( (int) best_so_far[n] ), TSPInput.getY( (int) best_so_far[n] ) );
}
System.out.print("\n\n");
System.out.print(" In Numerical Order ->");
Arrays.sort( best_so_far, 0, NUM_GENES );
for ( int c=0; c<NUM_GENES; c++){
System.out.print( (int) best_so_far[c] + ":");
}
System.out.print("\n\nRun completed at : " + getEndTime() + "\n");
System.out.print("Run Time was ");
printRunTime();
System.out.print("\n\n");
// Close plotting file
plotter.finish();
System.out.print("\n0 OK, 0:1\n");
System.exit(0); // Bye-bye!
}
}