-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimulatedAnnealing.java
59 lines (53 loc) · 1.44 KB
/
SimulatedAnnealing.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
import java.util.Arrays;
public class SimulatedAnnealing {
static final double Tmin = .0001;
static final double alpha = 0.9;
static final int numIterations = 100;
static final int M = 5, N = 5;
public static double T = 1;
public static void main(String[] args) {
String[][] sourceArray = new String[M][N];
Solution min = new Solution(Double.MAX_VALUE, null);
Solution currentSol = genRandSol();
while (T > Tmin) {
for (int i = 0; i < numIterations; i++) {
if (currentSol.CVRMSE < min.CVRMSE) {
min = currentSol;
break;
}
Solution newSol = neighbor(currentSol);
double ap = Math.pow(Math.E,
(currentSol.CVRMSE - newSol.CVRMSE) / T);
if (ap > Math.random())
currentSol = newSol;
}
T *= alpha;
}
System.out.println(min.CVRMSE + "\n\n");
for (String[] row : sourceArray) Arrays.fill(row, "X");
for (int object : min.config) {
int[] coord = indexToPoints(object);
sourceArray[coord[0]][coord[1]] = "-";
}
for (String[] row : sourceArray)
System.out.println(Arrays.toString(row));
}
public static Solution neighbor(Solution currentSol) {
return currentSol;
}
public static Solution genRandSol() {
int[] a = {1, 2, 3, 4, 5};
return new Solution(-1, a);
}
public static int[] indexToPoints(int index) {
return new int[]{index % M, index / M};
}
static class Solution {
public double CVRMSE;
public int[] config;
public Solution(double CVRMSE, int[] configuration) {
this.CVRMSE = CVRMSE;
config = configuration;
}
}
}