-
Notifications
You must be signed in to change notification settings - Fork 0
/
TesterPSO.java
208 lines (178 loc) · 5.92 KB
/
TesterPSO.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
/* TesterPSO.java
*
* Runs ParticleSwarmOptimization.java and logs the results into a file using Writer.java.
* PSO testing setup is according to pass/fail criteria
* Pass criteria - 50 success
* Fail criteria - 100 failures
*
* @author: James M. Bayon-on
* @version: 1.3
*/
public class TesterPSO {
Writer logWriter;
ParticleSwarmOptimization pso;
int MAX_RUN;
int MAX_LENGTH;
long[] runtimes;
/* Instantiates the TesterPSO class
*
*/
public TesterPSO() {
logWriter = new Writer();
MAX_RUN = 50;
runtimes = new long[MAX_RUN];
}
/* Test method accepts the N/max length, and parameters mutation rate and max epoch to set for the PSO accordingly.
*
* @param: max length/n
* @param: max velocity for PSO
* @param: max epoch for PSO
*/
public void test(int maxLength, double maxVelocity, int maxEpoch) {
MAX_LENGTH = maxLength;
pso = new ParticleSwarmOptimization(MAX_LENGTH); //instantiate and define params for PSO here
pso.setVMax(maxVelocity);
pso.setMaxEpoch(maxEpoch);
long testStart = System.nanoTime();
String filepath = "PSO-N"+MAX_LENGTH+"-"+maxVelocity+"-"+maxEpoch+".txt";
long startTime = 0;
long endTime = 0;
long totalTime = 0;
int fail = 0;
int success = 0;
logParameters();
for(int i = 0; i < MAX_RUN; ) { //run 50 sucess to pass passing criteria
startTime = System.nanoTime();
if(pso.algorithm()) {
endTime = System.nanoTime();
totalTime = endTime - startTime;
System.out.println("Done");
System.out.println("run "+(i+1));
System.out.println("time in nanoseconds: "+totalTime);
System.out.println("Success!");
runtimes[i] = totalTime;
i++;
success++;
//write to log
logWriter.add((String)("Run: "+i));
logWriter.add((String)("Runtime in nanoseconds: "+totalTime));
logWriter.add((String)("Found at epoch: "+pso.getEpoch()));
logWriter.add((String)("Population size: "+pso.getPopSize()));
logWriter.add("");
for(Particle p: pso.getSolutions()) { //write solutions to log file
logWriter.add(p);
logWriter.add("");
}
} else { //count failures for failing criteria
fail++;
System.out.println("Fail!");
}
if(fail >= 100) {
System.out.println("Cannot find solution with these params");
break;
}
startTime = 0; //reset time
endTime = 0;
totalTime = 0;
}
System.out.println("Number of Success: " +success);
System.out.println("Number of failures: "+fail);
logWriter.add("Runtime summary");
logWriter.add("");
for(int x = 0; x < runtimes.length; x++){ //print runtime summary
logWriter.add(Long.toString(runtimes[x]));
}
long testEnd = System.nanoTime();
logWriter.add(Long.toString(testStart));
logWriter.add(Long.toString(testEnd));
logWriter.add(Long.toString(testEnd - testStart));
logWriter.writeFile(filepath);
printRuntimes();
}
/* Converts the parameters of PSO to string and adds it to the string list in the writer class
*
*/
public void logParameters() {
logWriter.add("Particle Swarm Optimization Algorithm");
logWriter.add("Parameters");
logWriter.add((String)("MAX_LENGTH/N: "+MAX_LENGTH));
logWriter.add((String)("STARTING_POPULATION: "+pso.getParticleCount()));
logWriter.add((String)("MAX_EPOCHS: "+pso.getMaxEpoch()));
logWriter.add((String)("MAX_VELOCITY: "+pso.getVmax()));
logWriter.add((String)("MINIMUM_SHUFFLES: "+pso.getShuffleMin()));
logWriter.add((String)("MAXIMUM_SHUFFLES: "+pso.getShuffleMax()));
logWriter.add("");
}
/* Prints the runtime summary in the console
*
*/
public void printRuntimes() {
for(long x: runtimes){
System.out.println("run with time "+x+" nanoseconds");
}
}
public static void main(String args[]) {
TesterPSO tester = new TesterPSO();
tester.test(4, 4, 1000);
/* tester.test(8, 4, 1000);
tester.test(12, 4, 1000);
tester.test(16, 4, 1000);
tester.test(20, 4, 1000);
tester.test(16, 4, 1000);
tester.test(16, 8, 1000);
tester.test(16, 12, 1000);
tester.test(16, 16, 1000);
tester.test(16, 20, 1000);
tester.test(16, 4, 5000);
tester.test(16, 8, 5000);
tester.test(16, 12, 5000);
tester.test(16, 16, 5000);
tester.test(16, 20, 5000);
tester.test(16, 4, 10000);
tester.test(16, 8, 10000);
tester.test(16, 12, 10000);
tester.test(16, 16, 10000);
tester.test(16, 20, 10000);
tester.test(16, 4, 50000);
tester.test(16, 8, 50000);
tester.test(16, 12, 50000);
tester.test(16, 16, 50000);
tester.test(16, 20, 50000);
tester.test(16, 4, 100000);
tester.test(16, 8, 100000);
tester.test(16, 12, 100000);
tester.test(16, 16, 100000);
tester.test(16, 20, 100000);
tester.test(20, 4, 1000);
tester.test(20, 8, 1000);
tester.test(20, 12, 1000);
tester.test(20, 16, 1000);
tester.test(20, 20, 1000);
tester.test(20, 4, 5000);
tester.test(20, 8, 5000);
tester.test(20, 12, 5000);
tester.test(20, 16, 5000);
tester.test(20, 20, 5000);
tester.test(20, 4, 10000);
tester.test(20, 8, 10000);
tester.test(20, 12, 10000);
tester.test(20, 16, 10000);
tester.test(20, 20, 10000);
tester.test(20, 4, 50000);
tester.test(20, 8, 50000);
tester.test(20, 12, 50000);
tester.test(20, 16, 50000);
tester.test(20, 20, 50000);
tester.test(20, 4, 100000);
tester.test(20, 8, 100000);
tester.test(20, 12, 100000);
tester.test(20, 16, 100000);
tester.test(20, 20, 100000);
tester.test(20, 4, 500000);
tester.test(20, 8, 500000);
tester.test(20, 12, 500000);
tester.test(20, 16, 500000);
tester.test(20, 20, 500000);
*/
}
}