-
Notifications
You must be signed in to change notification settings - Fork 22
/
MetaTest.java
200 lines (183 loc) · 7.06 KB
/
MetaTest.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
/* MetaTest class
*
* binMeta project
*
* This version of MetaTest was used for the computational experiments presented in:
* S.B. Hengeveld, A. Mucherino,
* Variable Neighborhood Search in Hamming Space, 2023.
*
* The output is always printed in LaTex format.
* The LaTex table indicates how close the meta-heuristic search was able to approach
* the known upper bound. When the meta-heuristic search finds a solution that is
* better than the upper bound, the printed value will be negative. When the upper
* bound is not known, then it's the objective function value that is reported
* (a star* shows that this is the case in the table).
*
* last update: April 24, 2023
*
* AM
*/
import java.lang.reflect.*;
import java.util.Random;
public class MetaTest
{
// test parameters
static int NTESTS = 9; // number of tests performed per Objective and per meta-heuristics
static int TIMEMAX = 1000; // max time for meta-heuristic methods
static boolean SHOWTIME = false;
// objectives for experiments reported in the table in the article given above
static String[] objectives = {"NumberPartition","SubsetSum","Knapsack"};
static int[] problemSize = {600,600,600};
static int[] problemStep = {100,100,100};
// objectives for other experiments not reported in the article
// static String[] objectives = {"BitCounter","ColorPartition","Pi","Fermat","SetCover"};
// static int[] problemSize = {1000,100,100,5,100};
// static int[] problemStep = {200,100,100,5,10};
// meta-heuristics
static String[] methods = {"LocalOpt","RandomWalk","MultiStart","WolfSearch","VariableNeighbourhoodSearch"};
static String[] vnsTypes = {"basic","cyclic","pipe","jumping","skewed","nested"};
// Main
public static void main(String[] args)
{
// using Java reflection to get objectives and instances
Class<?> obj = null;
// first two lines of LaTex table
int number = 1;
if (SHOWTIME) number = 2;
System.out.print("\\multicolumn{3}{c}{instance}");
String second = "objective & name & size";
for (int imeth = 0; imeth < methods.length; imeth++)
{
if (!methods[imeth].equals("VariableNeighbourhoodSearch"))
{
System.out.print(" & \\multicolumn{" + number + "}{c}{" + methods[imeth] + "}");
second = second + " & value";
if (SHOWTIME) second = second + " & time";
}
else
{
for (int itype = 0; itype < vnsTypes.length; itype++)
{
System.out.print(" & \\multicolumn{" + number + "}{c}{" + methods[imeth] + "(" + vnsTypes[itype] + ")}");
second = second + " & value";
if (SHOWTIME) second = second + " & time";
}
}
}
System.out.println(" \\\\");
System.out.println(second + " \\\\");
// selection of the objective
for (int iobj = 0; iobj < objectives.length; iobj++)
{
try
{
obj = Class.forName(objectives[iobj]);
}
catch (Exception e)
{
System.out.println("Objective " + objectives[iobj] + " not found; skipping...");
continue;
}
// getting the constructor for random instance generation
//
// Two different lists of arguments are possible:
// - one for the toy objectives,
// - another for the ones related to classical combinatorial problems.
Constructor<?> constructor = null;
int nargs = 0;
try
{
constructor = obj.getConstructor(int.class);
nargs = 1;
}
catch (Exception e)
{
try
{
constructor = obj.getConstructor(int.class,int.class,int.class,Random.class);
nargs = 4;
}
catch (Exception f)
{
System.out.println("Something went wrong with the constructor of Objective " + objectives[iobj] + "; skipping...");
continue;
}
}
// performing tests
Random R = new Random(999);
Object objobj = null;
int n = problemSize[iobj];
for (int itest = 0; itest < NTESTS; itest++)
{
try
{
if (nargs == 1)
objobj = constructor.newInstance(n);
else
objobj = constructor.newInstance(n,1,n+1,R);
}
catch (Exception e)
{
System.out.println("Something went wrong when invoking constructor of Objective " + objectives[iobj] + "; skipping...");
continue;
}
if (objobj == null) continue;
Objective current = (Objective) objobj;
Data D = current.solutionSample();
int size = D.numberOfBits();
// printing main info in the LaTex table
System.out.print(objectives[iobj] + " & " + (itest+1) + " & " + size);
// solving the instance with the meta-heuristic methods
Class<?> meta = null;
Constructor<?> metaConstructor = null;
Object metaObject = null;
for (int imeth = 0; imeth < methods.length; imeth++)
{
try
{
meta = Class.forName(methods[imeth]);
metaConstructor = meta.getConstructor(Data.class,Objective.class,long.class);
metaObject = metaConstructor.newInstance(D,current,TIMEMAX);
}
catch (Exception e)
{
System.out.println("Unknown meta-heuristic search " + methods[imeth] + "; skipping...");
continue;
}
if (meta == null) continue;
binMeta mh = (binMeta) metaObject;
// running!
int itype = 0;
do
{
long startime = System.currentTimeMillis();
if (!methods[imeth].equals("VariableNeighbourhoodSearch"))
{
itype = -1;
mh.optimize();
}
else
{
VariableNeighbourhoodSearch vns = (VariableNeighbourhoodSearch) mh;
vns.setType(vnsTypes[itype]);
vns.optimize();
}
long time = System.currentTimeMillis() - startime;
// printing the results
Double ub = current.upperBound();
double result = current.value(mh.getSolution());
if (ub != null) result = result - ub;
System.out.print(" & " + result);
if (ub == null) System.out.print("*");
if (SHOWTIME) System.out.print(" & " + time);
// more tests?
itype++;
}
while (itype != 0 && itype < vnsTypes.length);
}
System.out.println(" \\\\");
n = n + problemStep[iobj];
}
}
}
}