-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithms.java
389 lines (330 loc) · 13.6 KB
/
Algorithms.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
import java.util.*;
public class Algorithms {
static int monetcarloIter = 0;
static int GibbsIter = 0;
static int HillDescentIter = 0;
static int HillDescentRWIter = 0;
static int SimulatedAnnealingIter = 0;
static int GeneticAlgorithmIter = 0;
public static Random myRnd = new Random();
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
/*Solution s = new Solution(4);
System.out.println(s);
System.out.println(s.isGoal());
Solution s1 = new Solution(
new int []{1,3,0,2});
System.out.println(s1);
System.out.println(s1.isGoal());
System.out.println(generateRandomNeighbor(s));
System.out.println(generateRandomNeighbor(s1));
*/
// System.out.println("HillDescent: \n");
// System.out.println(HillDescent(240000, 100) + "\n_________________________");
// System.out.println("SimulatedAnnealing: \n");
// System.out.println(SimulatedAnnealing(240000, 100, 20000.00, .999999) + "\n_________________________");
runThemAll();
// crossOver(new Solution(new int[]{0,1,2,3,4}),new Solution(new int[]{2,3,4,1,3}));
// System.out.println(GA(50, 6, 10));;
}
public static void runThemAll() {
int[] numberOfqueen = new int[]{4, 6, 8, 10, 15, 20, 25, 30, 35, 40, 50, 60, 70, 80};
int monetcarloSucess = 0;
int GibbsSucess = 0;
int HillDescentSucess = 0;
int HillDescentRWSucess = 0;
int SimulatedAnnealingSucess = 0;
int GeneticAlgorithmSucess = 0;
for (int i = 0; i < numberOfqueen.length; i++) {
for (int j = 0; j < 100; j++) {
Solution montecarlo = MonteCarlo(7000, numberOfqueen[i]);
if (montecarlo != null) {
monetcarloSucess++;
monetcarloIter += monetcarloIter;
// System.out.println(monetcarloIter);
}
Solution gibbs = Gibbs(7000, numberOfqueen[i]);
if (gibbs != null) {
GibbsSucess++;
GibbsIter += GibbsIter;
}
Solution hillDescent = HillDescent(70000, numberOfqueen[i]);
if (hillDescent != null) {
HillDescentSucess++;
HillDescentIter += HillDescentIter;
}
Solution hillDescentRW = HillDescentRW(7000, numberOfqueen[i], 0.05);
if (hillDescentRW != null) {
HillDescentRWSucess++;
HillDescentRWIter += HillDescentRWIter;
}
Solution simulatedAnnealing = SimulatedAnnealing(10000, numberOfqueen[i], 10000.00, .98);
if (simulatedAnnealing != null) {
SimulatedAnnealingSucess++;
SimulatedAnnealingIter += SimulatedAnnealingIter;
}
Solution GeneticAlgorithm = GA(10000, numberOfqueen[i], 10);
if (GeneticAlgorithm != null) {
GeneticAlgorithmSucess++;
GeneticAlgorithmIter += GeneticAlgorithmIter;
}
}
System.out.println(numberOfqueen[i] + " --> rate of monetcarloSucess: " + monetcarloSucess + " avarage itaration --> " + getAvarageIter(monetcarloIter, monetcarloSucess));
System.out.println(numberOfqueen[i] + " --> rate of GibbsSucess: " + GibbsSucess + " avarage itaration --> " + getAvarageIter(GibbsIter, GibbsSucess));
System.out.println(numberOfqueen[i] + " --> rate of HillDescentSucess: " + HillDescentSucess + " avarage itaration --> " + getAvarageIter(HillDescentIter, HillDescentSucess));
System.out.println(numberOfqueen[i] + " --> rate of HillDescentRWSucess: " + HillDescentRWSucess + " avarage itaration --> " + getAvarageIter(HillDescentRWIter, HillDescentRWSucess));
System.out.println(numberOfqueen[i] + " --> rate of SimulatedAnnealingSucess: " + SimulatedAnnealingSucess + " avarage itaration --> " + getAvarageIter(SimulatedAnnealingIter, SimulatedAnnealingSucess));
System.out.println(numberOfqueen[i] + " --> rate of GeneticAlgorithmSucess: " + SimulatedAnnealingSucess + " avarage itaration --> " + getAvarageIter(GeneticAlgorithmIter, GeneticAlgorithmSucess));
System.out.println("\n \n \n");
monetcarloSucess = 0;
GibbsSucess = 0;
HillDescentSucess = 0;
HillDescentRWSucess = 0;
SimulatedAnnealingSucess = 0;
GeneticAlgorithmSucess = 0;
monetcarloIter = 0;
GibbsIter = 0;
HillDescentIter = 0;
HillDescentRWIter = 0;
SimulatedAnnealingIter = 0;
GeneticAlgorithmIter = 0;
}
}
public static double getAvarageIter(double totalIter, double rateOFsucess) {
if (rateOFsucess <= 0) {
return 0.0;
} else {
return totalIter / rateOFsucess;
}
}
public static Solution generateRandomly(int n) {
Solution temp = new Solution(n);
for (int i = 0; i < n; i++) {
temp.getQueens()[i] = (int) (myRnd.nextDouble() * n);
}
return temp;
}
public static Solution generateRandomNeighbor(Solution x) {
Solution temp = new Solution(x);
int n = temp.getQueens().length;
int qindex = (int) (myRnd.nextDouble() * n);
int qvalue = (int) (myRnd.nextDouble() * n);
temp.getQueens()[qindex] = qvalue;
return temp;
}
public static Solution MonteCarlo(int maxIter, int n) {
int iteration = 0;
while (iteration++ < maxIter) {
Solution x = generateRandomly(n);
if (x.isGoal()) {
monetcarloIter = iteration;
// System.out.println(iteration);
return x;
}
}
return null;
}
public static Solution Gibbs(int maxIter, int n) {
int iteration = 0;
Solution x = generateRandomly(n);
if (x.isGoal()) {
return x;
}
while (iteration++ < maxIter) {
Solution xtemp = generateRandomNeighbor(x);
if (xtemp.isGoal()) {
GibbsIter = iteration;
// System.out.println(iteration);
return xtemp;
}
x = xtemp;
}
return null;
}
public static Solution HillDescent(int maxIter, int n) {
int iteration = 0;
Solution x = generateRandomly(n);
if (x.isGoal()) {
return x;
}
while (iteration++ < maxIter) {
Solution xtemp = generateRandomNeighbor(x);
if (xtemp.isGoal()) {
HillDescentIter = iteration;
// System.out.println(iteration);
return xtemp;
}
if (xtemp.getQuality() <= x.getQuality()) {
x = xtemp;
}
// System.out.println(iteration +"--> "+x.getQuality());
}
return null;
}
public static Solution HillDescentRW(int maxIter, int n, double wp) {
int iteration = 0;
Solution x = generateRandomly(n);
if (x.isGoal()) {
return x;
}
while (iteration++ < maxIter) {
Solution xtemp = generateRandomNeighbor(x);
if (xtemp.isGoal()) {
HillDescentRWIter = iteration;
// System.out.println(iteration);
return xtemp;
}
if (xtemp.getQuality() <= x.getQuality()) {
x = xtemp;
} else if (myRnd.nextDouble() < wp) {
x = xtemp;
}
//System.out.println(x.getQuality());
}
return null;
}
public static double tempatureControl(double temp, double annealing, int iter) { //anik will implemnet ;)
return (temp * annealing);
// return
}
public static double getAnnealing(double t, Solution x, Solution xtemp) {
double DellQuality = Math.abs(x.getQuality() - xtemp.getQuality());
double e = Math.exp(DellQuality / t);
double annealing = 1 / e;
// double annealing = e;
return annealing;
}
public static Solution SimulatedAnnealing(int maxIter, int n, double T, double annealing) {
int iteration = 0;
Solution x = generateRandomly(n);
if (x.isGoal()) {
return x;
}
while (iteration++ < maxIter) {
Solution xtemp = generateRandomNeighbor(x);
if (xtemp.isGoal()) {
SimulatedAnnealingIter = iteration;
// System.out.println(iteration);
return xtemp;
}
if (xtemp.getQuality() <= x.getQuality()) {
x = xtemp;
} else if (myRnd.nextDouble() < getAnnealing(T, x, xtemp)) {
// System.out.println("Annealing value: " + getAnnealing(T, x, xtemp));
T = tempatureControl(T, annealing, iteration);
// System.out.println("temperture :: " + T);
x = xtemp;
}
//System.out.println(x.getQuality());
}
return null;
}
public static Solution[] crossOver(Solution p0, Solution p1) {
int n = p0.getQueens().length;
Solution[] children = new Solution[2];
children[0] = new Solution(n);
children[1] = new Solution(n);
int crossPoint = (int) (myRnd.nextDouble() * (n - 1)) + 1;
// System.out.println(p0+ " "+p1);
// System.out.println(crossPoint);
for (int i = 0; i < n; i++) {
if (i < crossPoint) {
children[0].getQueens()[i] = p0.getQueens()[i];
} else {
children[1].getQueens()[i] = p0.getQueens()[i];
}
}
for (int i = 0; i < n; i++) {
if (i < crossPoint) {
children[1].getQueens()[i] = p1.getQueens()[i];
} else {
children[0].getQueens()[i] = p1.getQueens()[i];
}
}
// System.out.println(Arrays.toString(children));
return children;
}
public static Solution[] generateOffSpring(Solution[] parent) {
int indexOfOfspring = 0;
Solution[] offspring = new Solution[parent.length * 2];
int n = parent[1].getQueens().length;
for (int i = 0; i < parent.length / 2; i++) {
Solution p0 = parent[(int) (myRnd.nextDouble() * (n - 1))];
Solution p1 = parent[(int) (myRnd.nextDouble() * (n - 1))];
Solution[] COchild;
COchild = crossOver(p0, p1);
offspring[indexOfOfspring] = COchild[0];
indexOfOfspring++;
offspring[indexOfOfspring] = COchild[1];
indexOfOfspring++;
Solution Mchild1 = generateRandomNeighbor(COchild[0]);
offspring[indexOfOfspring] = Mchild1;
indexOfOfspring++;
Solution Mchild2 = generateRandomNeighbor(COchild[1]);
offspring[indexOfOfspring] = Mchild2;
indexOfOfspring++;
}
return offspring;
}
public static Solution[] sortIt(Solution[] a) {
// System.out.println("sort started");
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i].getQuality() > a[i + 1].getQuality()) {
Solution temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
// System.out.println("loop end");
}
return a;
}
public static Solution[] elitism(Solution[] parent, Solution[] offspring) {
Solution[] selection = new Solution[parent.length + offspring.length];
int index = 0;
for (Solution parent1 : parent) {
selection[index++] = parent1;
}
for (Solution offspring1 : offspring) {
selection[index++] = offspring1;
}
// Arrays.sort(selection);
selection = sortIt(selection);
for (int i = 0; i < parent.length; i++) {
parent[i] = selection[i];
}
// System.out.println(parent[0]);
// System.out.println(parent[1]);
return parent;
}
public static Solution GA(int maxGen, int n, int populationSize) {
int gen = 0;
Solution[] p = new Solution[populationSize];//generateRandomly(n);
for (int i = 0; i < populationSize; i++) {
p[i] = generateRandomly(n);
}
// System.out.println(Arrays.toString(p));
for (Solution x : p) {
if (x.isGoal()) {
return x;
}
}
while (gen++ < maxGen) {
// step 1: generate children
Solution[] ptemp = generateOffSpring(p);
// System.out.println("offsprings \n" + Arrays.toString(ptemp));
// step 2: check goal
for (Solution x : ptemp) {
if (x.isGoal()) {
GeneticAlgorithmIter = gen;
return x;
}
}
// step 3: select
p = elitism(p, ptemp);
//p = select(p,ptemp);
}
return null;
}
}