forked from mucherino/binMeta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWolfSearch.java
330 lines (300 loc) · 11.9 KB
/
WolfSearch.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
/* WolfSearch class
*
* binMeta project
*
* initial version coded by Remi Viotty, M1 Info 2019-20
*
* last update: April 16, 2023
*
* AM
*/
import java.util.ArrayList;
import java.util.Random;
public class WolfSearch extends binMeta implements Objective
{
private boolean isObjective; // true if the parameters are decision variables
private int np; // population size
private int minVision; // minimal vision of wolves
private int maxVision; // maximal vision of wolves
private double minThreat; // minimal threat probability
private double maxThreat; // maximal threat probability
private int memorySize; // the size of wolf memory
private Memory wolves; // Wolf population
// WolfSearch constructor
public WolfSearch(Data startPoint,Objective obj,int np,int memorySize,int minVision,int maxVision,double minThreat,double maxThreat,long maxTime)
{
this.isObjective = false;
try
{
if (startPoint == null) throw new Exception("WolfSearch: the reference to the Data starting point is null");
if (obj == null) throw new Exception("WolfSearch: the reference to the objective is null");
this.obj = obj;
Data ref = this.obj.solutionSample();
int n = ref.numberOfBits();
if (n != startPoint.numberOfBits()) throw new Exception("WolfSearch: number of bits in starting point does not match with Objective instance");
if (n < 3) throw new Exception("WolfSearch: number of involved bits is so small that the use of meta-heuristics is not justified");
if (memorySize <= 0) throw new Exception("WolfSearch: specified size of the wolf memory is nonpositive");
this.memorySize = memorySize;
if (maxVision < 0 || maxVision > n) throw new Exception("WolfSearch: meaningless maximum vision parameter");
if (minVision > maxVision) throw new Exception("WolfSearch: minimum vision parameter seems to be larger than specified maximum vision");
this.minVision = minVision;
this.maxVision = maxVision;
if (minThreat < 0.0 || minThreat > 1.0) throw new Exception("WolfSearch: specified minimun threat is not a probability (must be in [0,1])");
if (maxThreat < 0.0 || maxThreat > 1.0) throw new Exception("WolfSearch: specified maximun threat is not a probability (must be in [0,1])");
if (minThreat > maxThreat) throw new Exception("WolfSearch: minimum threat seems to be larger than maximum probability threat");
this.minThreat = minThreat;
this.maxThreat = maxThreat;
if (np <= 0) throw new Exception("WolfSearch: specified population size is nonpositive");
this.np = np;
Random R = new Random();
this.wolves = new Memory(this.np,"fifo",3); // 3 Memory parameters: vision, pbThrets, ephemeral memory
while (!this.wolves.isFull())
{
Data D = startPoint.randomSelectInNeighbourhood(1 + R.nextInt(n/2));
int k = this.wolves.add(D,this.obj.value(D));
int vision = minVision;
if (maxVision - minVision > 0) vision = vision + R.nextInt(maxVision - minVision);
double pbThreat = minThreat + R.nextDouble()*(maxThreat - minThreat);
Memory ephemeral = new Memory(this.memorySize);
this.wolves.setParameter(k,0,vision);
this.wolves.setParameter(k,1,pbThreat);
this.wolves.setParameter(k,2,ephemeral);
}
if (maxTime <= 0) throw new Exception("WolfSearch: specified maximum execution time is nonpositive");
this.maxTime = maxTime;
this.solution = null;
this.objValue = null;
this.metaName = "WolfSearch";
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
// WolfSearch constructor
public WolfSearch(Data startPoint,Objective obj,long maxTime)
{
this(startPoint,obj,100,100,2,(int) Math.floor(0.7*startPoint.numberOfBits()),0.1,0.4,maxTime);
}
// WolfSearch constructor (previously constructor #1)
public WolfSearch(Objective obj,int np,int memorySize,int minVision,int maxVision,double minThreat,double maxThreat,long maxTime)
{
this(obj.solutionSample(),obj,np,memorySize,minVision,maxVision,minThreat,maxThreat,maxTime);
}
// WolfSearch constructor
public WolfSearch(Objective obj,long maxTime)
{
this.isObjective = true;
try
{
if (obj == null) throw new Exception("WolfSearch: the reference to the objective is null");
this.obj = obj;
if (maxTime <= 0) throw new Exception("WolfSearch: specified maximum execution time is nonpositive");
this.maxTime = maxTime;
this.np = 0;
this.memorySize = 0;
this.wolves = null;
this.objValue = null;
this.metaName = "Objective WolfSearch";
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
// getName
@Override
public String getName()
{
return new String(this.metaName);
}
// solutionSample
@Override
public Data solutionSample()
{
int size = 29; // 9(np) + 8(memorySize) + 2(minVision) + 2(maxVision) + 4(minThreat) + 4(maxThreat)
return new Data(size,0.5);
}
// upperBound
@Override
public Double upperBound()
{
return null;
}
// optimize (by WolfSearch)
@Override
public void optimize()
{
try
{
if (this.isObjective) throw new Exception("WolfSearch: direct call to 'optimize' is not allowed when the object is initialized as an Objective");
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
// getting started
Random R = new Random();
int it = 0;
long localTime = Math.max(100L,this.maxTime/10L);
long startime = System.currentTimeMillis();
// main loop
while (System.currentTimeMillis() - startime < this.maxTime)
{
// for every wolf
for (int i = 0; i < this.np && System.currentTimeMillis() - startime < this.maxTime; i++)
{
// current wolf
Data wolf = this.wolves.getData(i);
double wolfValue = this.wolves.getValue(i);
int vision = (int) this.wolves.getParameter(i,0);
double pbThreat = (double) this.wolves.getParameter(i,1);
Memory wolfMemory = (Memory) this.wolves.getParameter(i,2);
// trying to prey for new food initiatively
Data D = wolf.randomSelectInNeighbourhood(vision);
if (R.nextInt(2) == 0)
{
LocalOpt lopt = new LocalOpt(D,this.obj,localTime);
lopt.optimize();
D = lopt.getSolution();
}
double value = this.obj.value(D);
if (!wolfMemory.contains(D) && value < wolfValue)
{
// preying initiatively worked out
wolfMemory.add(wolf);
this.wolves.set(i,D,value);
}
else
{
// preying new food passively (interacting with other wolves)
int toapproach = -1;
int repulsion = wolf.numberOfBits();
for (int j = 0; j < this.np; j++)
{
// is there any other wolf doing better than wolf i ?
if (i != j)
{
Data other = this.wolves.getData(j);
int h = wolf.hammingDistanceTo(other);
if (h < vision) // wolf i can see the other
{
value = this.wolves.getValue(j);
if (value < wolfValue && h < repulsion)
{
toapproach = j;
repulsion = h;
}
}
}
}
if (toapproach != -1)
{
// wolf i joins the selected wolf
Data other = this.wolves.getData(toapproach);
wolfMemory.add(wolf);
D = other.randomSelectInNeighbourhood(1);
value = this.obj.value(D);
this.wolves.set(i,D,value);
}
// any threats?
if (R.nextDouble() < pbThreat)
{
D = wolf.randomSelectInNeighbourhood(vision,vision);
value = this.obj.value(D);
this.wolves.set(i,D,value);
}
}
}
// verifying best current solution
int bestIndex = this.wolves.indexOfBest();
Data newBest = this.wolves.getData(bestIndex);
double newBestValue = this.wolves.getValue(bestIndex);
if (this.objValue == null || this.objValue > newBestValue)
{
this.solution = new Data(newBest);
this.objValue = newBestValue;
}
// preparing for next iteration
it++;
}
}
// loadParameters (private method)
private void loadParameters(Data D)
{
try
{
if (!this.isObjective)
throw new Exception("WolfSearch: call to 'loadParameters' is not allowed when the object is not initialized as an Objective");
if (D == null) throw new Exception("WolfSearch: Data object is null");
if (D.numberOfBits() != 29) throw new Exception("WolfSearch: Unexpected bit string length in Data object");
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
// size in terms of bits of internal objective
int n = this.obj.solutionSample().numberOfBits();
// extracting decision variables from Data object, and copying in attributes
this.np = new Data(D,0,9).posIntValue(); // 10 bits (np)
this.memorySize = new Data(D,9,17).posIntValue(); // 8 bits (memorySize)
int[] visions = {n/8,2*n/8,3*n/8,4*n/8,5*n/8,6*n/8,7*n/8,n};
int min = new Data(D,17,19).intValue(); // 2 bits (minVision)
this.minVision = visions[min];
int max = new Data(D,19,21).intValue(); // 2 bits (maxVision)
this.maxVision = visions[min + max];
this.minThreat = 0.5*(new Data(D,21,25).doubleValueNormalized()); // 4 bits (minThreat)
this.maxThreat = minThreat + 0.5*(new Data(D,25,29).doubleValueNormalized()); // 4 bits (maxThreat)
}
// parametersToString
public String parametersToString(Data D)
{
String print = "[";
this.loadParameters(D);
print = print + "np = " + this.np + " | ";
print = print + "memorySize = " + this.memorySize + " | ";
print = print + "minVision = " + this.minVision + " | ";
print = print + "maxVision = " + this.maxVision + " | ";
print = print + "minThreat = " + this.minThreat + " | ";
print = print + "maxThreat = " + this.maxThreat + "]";
return print;
}
// value
@Override
public double value(Data D)
{
// loading parameters from Data object
this.loadParameters(D);
// running WolfSearch with these parameters
WolfSearch WS = new WolfSearch(this.obj,np,memorySize,minVision,maxVision,minThreat,maxThreat,this.maxTime);
// optimizing
WS.optimize();
// WolfSearch objective value corresponds to value of found solution
return WS.objValue;
}
/* static methods defining some problem instances */
// instance01 (SubsetSum, 2ms)
public static WolfSearch instance01()
{
Random R = new Random();
int n = 6 + R.nextInt(14);
Objective obj = new SubsetSum(n,2,n,R);
return new WolfSearch(obj,2);
}
// instance02 (Fermat, 5ms)
public static WolfSearch instance02()
{
Objective obj = new Fermat(2,5);
return new WolfSearch(obj,5);
}
// instance03 (Pi, 9ms)
public static WolfSearch instance03()
{
Objective obj = new Pi(20,4);
return new WolfSearch(obj,9);
}
}