-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperator.java
More file actions
404 lines (343 loc) · 13.2 KB
/
Copy pathOperator.java
File metadata and controls
404 lines (343 loc) · 13.2 KB
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package CVRP;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Operator
{
private static Random random = new Random(System.currentTimeMillis());
public static int randomInt(int min,int max)
{
int rand=0;
rand=min+random.nextInt(max-min);//[min,max)
//System.out.println(random.nextBoolean());
return rand;
}
public static List<Integer> swap(List<Integer> inputFoodSource)
{
List<Integer> foodSource = new ArrayList<Integer>();
foodSource.addAll(inputFoodSource);
while(true)
{
int i1=0,i2=0,element1=0,element2=0;
i1=Operator.randomInt(1,foodSource.size()-1);//first and last elements are start nodes, exclude
do
{
i2=Operator.randomInt(1,foodSource.size()-1);
}
while(i1==i2);//keep finding until i1!=i2
//System.out.println(i1);
//System.out.println(i2);
//do not allow depot node to be swapped
if(foodSource.get(i1)==1||foodSource.get(i2)==1)//if any of the selected node are depot nodes, find a new swap set
continue;
element1=foodSource.get(i1);
element2=foodSource.get(i2);
//allow start node to be swapped
/*
if(foodSource.get(i1)==1&&foodSource.get(i2)==1)//if both selected node are depot nodes, find a new swap set
continue;
if((element1=foodSource.get(i1))==1&&(element2=foodSource.get(i2))!=1)
{
if((foodSource.get(i2-1)==1)||(foodSource.get(i2+1)==1))// second node is adjacent to a depot node, find another set
continue;
}
if((element1=foodSource.get(i1))!=1&&(element2=foodSource.get(i2))==1)
{
if((foodSource.get(i1-1)==1)||(foodSource.get(i1+1)==1))// first node is adjacent to a depot node, find another set
continue;
}
*/
foodSource.set(i1,element2);
foodSource.set(i2,element1);
break;
}
return foodSource;
}
public static List<Integer> swapSubsequence(List<Integer> inputFoodSource)
{
List<Integer> invalidIndex = new ArrayList<Integer>();//index that should not be chosen
List<Integer> indexList = new ArrayList<Integer>();
int index=0;
List<Integer> foodSource=new ArrayList<Integer>();
foodSource.addAll(inputFoodSource);//Copy the value into local variables
do//find four diverse index that contains depot nodes
{
index=Operator.randomInt(1,foodSource.size()-1);
if(!invalidIndex.contains(index)&&foodSource.get(index)!=1)//
{
indexList.add(index);
invalidIndex.add(index-1);
invalidIndex.add(index);// node already taken, should not be taken any more
invalidIndex.add(index+1);//adjacent nodes should also be excluded, add adjacent nodes to the invalid list
}
}while(indexList.size()<4);
Collections.sort(indexList);
List<Integer> subList1=new ArrayList<Integer>();
List<Integer> subList2=new ArrayList<Integer>();
List<Integer> subList3=new ArrayList<Integer>();
List<Integer> subList4=new ArrayList<Integer>();
List<Integer> subList5=new ArrayList<Integer>();
subList1.addAll(foodSource.subList(0, indexList.get(0)));
subList2.addAll(foodSource.subList(indexList.get(0), indexList.get(1)+1));
subList3.addAll(foodSource.subList(indexList.get(1)+1, indexList.get(2)));
subList4.addAll(foodSource.subList(indexList.get(2), indexList.get(3)+1));
subList5.addAll(foodSource.subList(indexList.get(3)+1, foodSource.size()));
foodSource.clear();
foodSource.addAll(subList1);
foodSource.addAll(subList4);
foodSource.addAll(subList3);
foodSource.addAll(subList2);
foodSource.addAll(subList5);
return foodSource;
}
public static List<Integer> insert(List<Integer> inputFoodSource)
{
List<Integer> indexList=new ArrayList<Integer>();
List<Integer> invalidIndex=new ArrayList<Integer>();
int index=0;
List<Integer> foodSource = new ArrayList<Integer>();
foodSource.addAll(inputFoodSource);
do
{
index=Operator.randomInt(1,foodSource.size()-1);
if(!invalidIndex.contains(index)&&foodSource.get(index)!=1)//
{
indexList.add(index);
invalidIndex.add(index-1);
invalidIndex.add(index);// node foodn taken, should not be taken any more
invalidIndex.add(index+1);//adjacent nodes should also be excluded, add adjacent nodes to the invalid list
}
}while(indexList.size()<2);
Collections.sort(indexList);
if(random.nextBoolean()/*randomInt(0,2)==0*/)//randomly generate 0 or 1
{
List<Integer> subList1=new ArrayList<Integer>();
List<Integer> subList2=new ArrayList<Integer>();
List<Integer> subList3=new ArrayList<Integer>();
int element;
subList1.addAll(foodSource.subList(0,indexList.get(0)));
element=foodSource.get(indexList.get(0));
subList2.addAll(foodSource.subList(indexList.get(0)+1,indexList.get(1)));
subList3.addAll(foodSource.subList(indexList.get(1),foodSource.size()));
foodSource.clear();
foodSource.addAll(subList1);
foodSource.addAll(subList2);
foodSource.add(element);
foodSource.addAll(subList3);
}
else
{
List<Integer> subList1=new ArrayList<Integer>();
List<Integer> subList2=new ArrayList<Integer>();
List<Integer> subList3=new ArrayList<Integer>();
int element;
subList1.addAll(foodSource.subList(0,indexList.get(0)));
subList2.addAll(foodSource.subList(indexList.get(0),indexList.get(1)));
element=foodSource.get(indexList.get(1));
subList3.addAll(foodSource.subList(indexList.get(1)+1,foodSource.size()));
foodSource.clear();
foodSource.addAll(subList1);
foodSource.add(element);
foodSource.addAll(subList2);
foodSource.addAll(subList3);
}
return foodSource;
}
public static List<Integer> insertSubsequence(List<Integer> inputFoodSource)
{
List<Integer> indexList=new ArrayList<Integer>();
List<Integer> invalidIndex=new ArrayList<Integer>();
int index=0;
List<Integer> foodSource = new ArrayList<Integer>();
foodSource.addAll(inputFoodSource);
do
{
index=Operator.randomInt(1,foodSource.size()-1);
if(!invalidIndex.contains(index)&&foodSource.get(index)!=1)//
{
indexList.add(index);
invalidIndex.add(index-1);
invalidIndex.add(index);// node foodn taken, should not be taken any more
invalidIndex.add(index+1);//adjacent nodes should also be excluded, add adjacent nodes to the invalid list
}
}while(indexList.size()<3);
Collections.sort(indexList);
if(random.nextBoolean()/*randomInt(0,2)==0*/)//randomly generate 0 or 1
{
List<Integer> subList1=new ArrayList<Integer>();
List<Integer> subList2=new ArrayList<Integer>();
List<Integer> subList3=new ArrayList<Integer>();
List<Integer> subList4=new ArrayList<Integer>();
subList1.addAll(foodSource.subList(0,indexList.get(0)));
subList2.addAll(foodSource.subList(indexList.get(0),indexList.get(1)+1));//target swap subsequence
subList3.addAll(foodSource.subList(indexList.get(1)+1,indexList.get(2)));
subList4.addAll(foodSource.subList(indexList.get(2),foodSource.size()));
foodSource.clear();
foodSource.addAll(subList1);
foodSource.addAll(subList3);
foodSource.addAll(subList2);
foodSource.addAll(subList4);
}
else
{
List<Integer> subList1=new ArrayList<Integer>();
List<Integer> subList2=new ArrayList<Integer>();
List<Integer> subList3=new ArrayList<Integer>();
List<Integer> subList4=new ArrayList<Integer>();
subList1.addAll(foodSource.subList(0,indexList.get(0)));
subList2.addAll(foodSource.subList(indexList.get(0),indexList.get(1)));
subList3.addAll(foodSource.subList(indexList.get(1),indexList.get(2)+1));//target swap subsequence
subList4.addAll(foodSource.subList(indexList.get(2)+1,foodSource.size()));
foodSource.clear();
foodSource.addAll(subList1);
foodSource.addAll(subList3);
foodSource.addAll(subList2);
foodSource.addAll(subList4);
}
return foodSource;
}
public static List<Integer> reverseSubsequence(List<Integer> inputFoodSource)
{
List<Integer> indexList=new ArrayList<Integer>();
List<Integer> invalidIndex=new ArrayList<Integer>();
int index=0;
List<Integer> foodSource = new ArrayList<Integer>();
foodSource.addAll(inputFoodSource);
do
{
index=Operator.randomInt(1,foodSource.size()-1);
if(!invalidIndex.contains(index)&&foodSource.get(index)!=1)//
{
indexList.add(index);
invalidIndex.add(index-1);
invalidIndex.add(index);// node foodn taken, should not be taken any more
invalidIndex.add(index+1);//adjacent nodes should also be excluded, add adjacent nodes to the invalid list
}
}while(indexList.size()<2);
Collections.sort(indexList);
List<Integer> subList = new ArrayList<Integer>();
subList.addAll(foodSource.subList(indexList.get(0), indexList.get(1)+1));
Collections.reverse(subList);
for(int i=indexList.get(0);i<=indexList.get(1);i++)
{
foodSource.set(i, subList.get(i-indexList.get(0)));
}
return foodSource;
}
public static List<Integer> swapReverseSubsequence(List<Integer> inputFoodSource)
{
List<Integer> invalidIndex = new ArrayList<Integer>();//index that should not be chosen
List<Integer> indexList = new ArrayList<Integer>();
int index=0;
List<Integer> foodSource = new ArrayList<Integer>();
foodSource.addAll(inputFoodSource);
do//find four diverse
{
index=Operator.randomInt(1,foodSource.size()-1);
if(!invalidIndex.contains(index)&&foodSource.get(index)!=1)//
{
indexList.add(index);
invalidIndex.add(index-1);
invalidIndex.add(index);// node foodn taken, should not be taken any more
invalidIndex.add(index+1);//adjacent nodes should also be excluded, add adjacent nodes to the invalid list
}
}while(indexList.size()<4);
Collections.sort(indexList);
List<Integer> subList1=new ArrayList<Integer>();
List<Integer> subList2=new ArrayList<Integer>();
List<Integer> subList3=new ArrayList<Integer>();
List<Integer> subList4=new ArrayList<Integer>();
List<Integer> subList5=new ArrayList<Integer>();
subList1.addAll(foodSource.subList(0, indexList.get(0)));//[0,get(0))
subList2.addAll(foodSource.subList(indexList.get(0), indexList.get(1)+1));//[get(0),get(1)]
Collections.reverse(subList2);
subList3.addAll(foodSource.subList(indexList.get(1)+1, indexList.get(2)));
subList4.addAll(foodSource.subList(indexList.get(2), indexList.get(3)+1));
Collections.reverse(subList4);
subList5.addAll(foodSource.subList(indexList.get(3)+1, foodSource.size()));
foodSource.clear();
foodSource.addAll(subList1);
foodSource.addAll(subList4);
foodSource.addAll(subList3);
foodSource.addAll(subList2);
foodSource.addAll(subList5);
return foodSource;
}
public static List<Integer> insertReverseSubsequence(List<Integer> inputFoodSource)
{
List<Integer> indexList=new ArrayList<Integer>();
List<Integer> invalidIndex=new ArrayList<Integer>();
int index=0;
List<Integer> foodSource = new ArrayList<Integer>();
foodSource.addAll(inputFoodSource);
do
{
index=Operator.randomInt(1,foodSource.size()-1);
if(!invalidIndex.contains(index)&&foodSource.get(index)!=1)//
{
indexList.add(index);
invalidIndex.add(index-1);
invalidIndex.add(index);// node foodn taken, should not be taken any more
invalidIndex.add(index+1);//adjacent nodes should also be excluded, add adjacent nodes to the invalid list
}
}while(indexList.size()<3);
Collections.sort(indexList);
if(random.nextBoolean()/*randomInt(0,2)==0*/)//randomly generate 0 or 1
{
List<Integer> subList1=new ArrayList<Integer>();
List<Integer> subList2=new ArrayList<Integer>();
List<Integer> subList3=new ArrayList<Integer>();
List<Integer> subList4=new ArrayList<Integer>();
subList1.addAll(foodSource.subList(0,indexList.get(0)));
subList2.addAll(foodSource.subList(indexList.get(0),indexList.get(1)+1));//target swap subsequence
Collections.reverse(subList2);
subList3.addAll(foodSource.subList(indexList.get(1)+1,indexList.get(2)));
subList4.addAll(foodSource.subList(indexList.get(2),foodSource.size()));
foodSource.clear();
foodSource.addAll(subList1);
foodSource.addAll(subList3);
foodSource.addAll(subList2);
foodSource.addAll(subList4);
}
else
{
List<Integer> subList1=new ArrayList<Integer>();
List<Integer> subList2=new ArrayList<Integer>();
List<Integer> subList3=new ArrayList<Integer>();
List<Integer> subList4=new ArrayList<Integer>();
subList1.addAll(foodSource.subList(0,indexList.get(0)));
subList2.addAll(foodSource.subList(indexList.get(0),indexList.get(1)));
subList3.addAll(foodSource.subList(indexList.get(1),indexList.get(2)+1));//target swap subsequence
Collections.reverse(subList3);
subList4.addAll(foodSource.subList(indexList.get(2)+1,foodSource.size()));
foodSource.clear();
foodSource.addAll(subList1);
foodSource.addAll(subList3);
foodSource.addAll(subList2);
foodSource.addAll(subList4);
}
return foodSource;
}
public static List<Integer> combined(List<Integer> inputFoodSource)
{
List<Integer> foodSource = new ArrayList<Integer>();
int rand=0;
rand=Operator.randomInt(0,3);
switch(rand)
{
case 0:
foodSource.addAll(Operator.swapReverseSubsequence(inputFoodSource));
break;
case 1:
foodSource.addAll(Operator.reverseSubsequence(inputFoodSource));
break;
case 2:
foodSource.addAll(Operator.swap(inputFoodSource));
break;
default:
break;
}
return foodSource;
}
}