forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoutingSolverTest.java
659 lines (597 loc) · 27.3 KB
/
RoutingSolverTest.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// Copyright 2010-2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.ortools.constraintsolver;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.auto.value.AutoValue;
import com.google.ortools.Loader;
import com.google.ortools.constraintsolver.Assignment;
import com.google.ortools.constraintsolver.FirstSolutionStrategy;
import com.google.ortools.constraintsolver.RoutingIndexManager;
import com.google.ortools.constraintsolver.RoutingModel;
import com.google.ortools.constraintsolver.RoutingModelParameters;
import com.google.ortools.constraintsolver.RoutingSearchParameters;
import com.google.protobuf.Duration;
import java.util.ArrayList;
import java.util.function.LongBinaryOperator;
import java.util.function.LongUnaryOperator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** Tests the Routing java interface. */
public final class RoutingSolverTest {
@AutoValue
abstract static class Location {
static Location create(Integer latitude, Integer longitude) {
return new AutoValue_RoutingSolverTest_Location(latitude, longitude);
}
abstract Integer latitude();
abstract Integer longitude();
}
private ArrayList<Location> coordinates;
@BeforeEach
public void setUp() {
Loader.loadNativeLibraries();
coordinates = new ArrayList<>();
coordinates.add(Location.create(0, 0));
coordinates.add(Location.create(-1, 0));
coordinates.add(Location.create(-1, 2));
coordinates.add(Location.create(2, 1));
coordinates.add(Location.create(1, 0));
}
public LongBinaryOperator createManhattanCostCallback(RoutingIndexManager manager) {
return (long i, long j) -> {
final int firstIndex = manager.indexToNode(i);
final int secondIndex = manager.indexToNode(j);
final Location firstCoordinate = coordinates.get(firstIndex);
final Location secondCoordinate = coordinates.get(secondIndex);
return (long) Math.abs(firstCoordinate.latitude() - secondCoordinate.latitude())
+ Math.abs(firstCoordinate.longitude() - secondCoordinate.longitude());
};
}
public LongUnaryOperator createUnaryCostCallback(RoutingIndexManager manager) {
return (long fromIndex) -> {
final int fromNode = manager.indexToNode(fromIndex);
final Location firstCoordinate = coordinates.get(fromNode);
return (long) Math.abs(firstCoordinate.latitude()) + Math.abs(firstCoordinate.longitude());
};
}
public LongBinaryOperator createReturnOneCallback() {
return (long i, long j) -> 1;
}
@Test
public void testRoutingModel_checkGlobalRefGuard() {
for (int i = 0; i < 500; ++i) {
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
assertNotNull(manager);
final RoutingModel model = new RoutingModel(manager);
assertNotNull(model);
LongBinaryOperator transit = (long fromIndex, long toIndex) -> {
final int fromNode = manager.indexToNode(fromIndex);
final int toNode = manager.indexToNode(toIndex);
return (long) Math.abs(toNode - fromNode);
};
model.registerTransitCallback(transit);
System.gc(); // model should keep alive the callback
}
}
@Test
public void testRoutingIndexManager() {
final RoutingIndexManager manager = new RoutingIndexManager(42, 3, 7);
assertNotNull(manager);
assertEquals(42, manager.getNumberOfNodes());
assertEquals(3, manager.getNumberOfVehicles());
assertEquals(42 + 3 * 2 - 1, manager.getNumberOfIndices());
for (int i = 0; i < manager.getNumberOfVehicles(); ++i) {
assertEquals(7, manager.indexToNode(manager.getStartIndex(i)));
assertEquals(7, manager.indexToNode(manager.getEndIndex(i)));
}
}
@Test
public void testRoutingIndexManager_multiDepotSame() {
final RoutingIndexManager manager =
new RoutingIndexManager(42, 3, new int[] {7, 7, 7}, new int[] {7, 7, 7});
assertNotNull(manager);
assertEquals(42, manager.getNumberOfNodes());
assertEquals(3, manager.getNumberOfVehicles());
assertEquals(42 + 3 * 2 - 1, manager.getNumberOfIndices());
for (int i = 0; i < manager.getNumberOfVehicles(); ++i) {
assertEquals(7, manager.indexToNode(manager.getStartIndex(i)));
assertEquals(7, manager.indexToNode(manager.getEndIndex(i)));
}
}
@Test
public void testRoutingIndexManager_multiDepotAllDifferent() {
final RoutingIndexManager manager =
new RoutingIndexManager(42, 3, new int[] {1, 2, 3}, new int[] {4, 5, 6});
assertNotNull(manager);
assertEquals(42, manager.getNumberOfNodes());
assertEquals(3, manager.getNumberOfVehicles());
assertEquals(42, manager.getNumberOfIndices());
for (int i = 0; i < manager.getNumberOfVehicles(); ++i) {
assertEquals(i + 1, manager.indexToNode(manager.getStartIndex(i)));
assertEquals(i + 4, manager.indexToNode(manager.getEndIndex(i)));
}
}
@Test
public void testRoutingModel() {
final RoutingIndexManager manager =
new RoutingIndexManager(42, 3, new int[] {1, 2, 3}, new int[] {4, 5, 6});
assertNotNull(manager);
final RoutingModel model = new RoutingModel(manager);
assertNotNull(model);
for (int i = 0; i < manager.getNumberOfVehicles(); ++i) {
assertEquals(i + 1, manager.indexToNode(model.start(i)));
assertEquals(i + 4, manager.indexToNode(model.end(i)));
}
}
@Test
public void testRoutingModelParameters() {
final RoutingModelParameters parameters = main.defaultRoutingModelParameters();
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
final RoutingModel model = new RoutingModel(manager, parameters);
assertEquals(1, model.vehicles());
}
@Test
public void testRoutingModel_solveWithParameters() {
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
final RoutingModel model = new RoutingModel(manager);
final RoutingSearchParameters parameters = main.defaultRoutingSearchParameters();
final LongBinaryOperator callback = createManhattanCostCallback(manager);
final int cost = model.registerTransitCallback(callback);
System.gc();
model.setArcCostEvaluatorOfAllVehicles(cost);
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
Assignment solution = model.solveWithParameters(parameters);
assertEquals(10, solution.objectiveValue());
solution = model.solveFromAssignmentWithParameters(solution, parameters);
assertEquals(10, solution.objectiveValue());
}
@Test
public void testRoutingModel_costsAndSolve() {
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
final RoutingModel model = new RoutingModel(manager);
assertEquals(5, model.nodes());
final LongBinaryOperator callback = createManhattanCostCallback(manager);
final int cost = model.registerTransitCallback(callback);
System.gc();
model.setArcCostEvaluatorOfAllVehicles(cost);
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(10, solution.objectiveValue());
}
@Test
public void testRoutingModel_matrixTransitOwnership() {
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
final RoutingModel model = new RoutingModel(manager);
assertEquals(5, model.nodes());
final long[][] matrix = {
{0, 1, 3, 3, 1},
{1, 0, 2, 4, 2},
{3, 2, 0, 4, 4},
{3, 4, 4, 0, 2},
{1, 2, 4, 2, 0},
};
final int cost = model.registerTransitMatrix(matrix);
System.gc(); // model should keep alive the callback
model.setArcCostEvaluatorOfAllVehicles(cost);
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(10, solution.objectiveValue());
}
@Test
public void testRoutingModel_transitCallbackOwnership() {
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
final RoutingModel model = new RoutingModel(manager);
assertEquals(5, model.nodes());
final int cost = model.registerTransitCallback(createManhattanCostCallback(manager));
System.gc(); // model should keep alive the callback
model.setArcCostEvaluatorOfAllVehicles(cost);
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(10, solution.objectiveValue());
}
@Test
public void testRoutingModel_lambdaTransitCallbackOwnership() {
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
final RoutingModel model = new RoutingModel(manager);
assertEquals(5, model.nodes());
final int cost = model.registerTransitCallback(
(long fromIndex, long toIndex) -> {
final int fromNode = manager.indexToNode(fromIndex);
final int toNode = manager.indexToNode(toIndex);
return (long) Math.abs(toNode - fromNode);
});
System.gc(); // model should keep alive the callback
model.setArcCostEvaluatorOfAllVehicles(cost);
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(8, solution.objectiveValue());
}
@Test
public void testRoutingModel_unaryTransitVectorOwnership() {
final RoutingIndexManager manager = new RoutingIndexManager(10, 1, 0);
final RoutingModel model = new RoutingModel(manager);
assertEquals(10, model.nodes());
final long[] vector = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
final int cost = model.registerUnaryTransitVector(vector);
System.gc(); // model should keep alive the callback
model.setArcCostEvaluatorOfAllVehicles(cost);
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(45, solution.objectiveValue());
}
@Test
public void testRoutingModel_unaryTransitCallbackOwnership() {
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
final RoutingModel model = new RoutingModel(manager);
assertEquals(5, model.nodes());
final int cost = model.registerUnaryTransitCallback(createUnaryCostCallback(manager));
System.gc(); // model should keep alive the callback
model.setArcCostEvaluatorOfAllVehicles(cost);
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(8, solution.objectiveValue());
}
@Test
public void testRoutingModel_lambdaUnaryTransitCallbackOwnership() {
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
final RoutingModel model = new RoutingModel(manager);
assertEquals(5, model.nodes());
final int cost = model.registerUnaryTransitCallback(
(long fromIndex) -> {
final int fromNode = manager.indexToNode(fromIndex);
return (long) Math.abs(fromNode);
});
System.gc(); // model should keep alive the callback
model.setArcCostEvaluatorOfAllVehicles(cost);
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(10, solution.objectiveValue());
}
@Test
public void testRoutingModel_routesToAssignment() {
final int vehicles = coordinates.size() - 1;
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), vehicles, 0);
final RoutingModel model = new RoutingModel(manager);
model.closeModel();
long[][] routes = new long[vehicles][];
for (int i = 0; i < vehicles; ++i) {
// Each route has a single node
routes[i] = new long[1];
routes[i][0] = manager.nodeToIndex(i + 1);
}
Assignment assignment = new Assignment(model.solver());
model.routesToAssignment(routes, false, true, assignment);
for (int i = 0; i < vehicles; ++i) {
assertEquals(assignment.value(model.nextVar(model.start(i))), i + 1);
assertEquals(assignment.value(model.nextVar(i + 1)), model.end(i));
}
}
@Test
public void testRoutingModel_addDisjunction() {
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
final RoutingModel model = new RoutingModel(manager);
final LongBinaryOperator callback = createManhattanCostCallback(manager);
final int cost = model.registerTransitCallback(callback);
model.setArcCostEvaluatorOfAllVehicles(cost);
int[] a = new int[2];
a[0] = 2;
a[1] = 3;
int[] b = new int[1];
b[0] = 1;
int[] c = new int[1];
c[0] = 4;
model.addDisjunction(manager.nodesToIndices(a));
model.addDisjunction(manager.nodesToIndices(b));
model.addDisjunction(manager.nodesToIndices(c));
Assignment solution = model.solve(null);
assertEquals(8, solution.objectiveValue());
}
@Test
public void testRoutingModel_addConstantDimension() {
final RoutingIndexManager manager = new RoutingIndexManager(10, 1, 0);
final RoutingModel model = new RoutingModel(manager);
assertEquals(10, model.nodes());
final IntBoolPair pair = model.addConstantDimension(1,
/*capacity=*/100,
/*fix_start_cumul_to_zero=*/true, "Dimension");
assertEquals(1, pair.getFirst());
assertTrue(pair.getSecond());
RoutingDimension dimension = model.getMutableDimension("Dimension");
dimension.setSpanCostCoefficientForAllVehicles(2);
RoutingSearchParameters searchParameters =
RoutingSearchParameters.newBuilder()
.mergeFrom(main.defaultRoutingSearchParameters())
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
.setTimeLimit(Duration.newBuilder().setSeconds(10))
.build();
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solveWithParameters(searchParameters);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(20, solution.objectiveValue());
}
@Test
public void testRoutingModel_addVectorDimension() {
final RoutingIndexManager manager = new RoutingIndexManager(10, 1, 0);
final RoutingModel model = new RoutingModel(manager);
assertEquals(10, model.nodes());
final long[] vector = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
final IntBoolPair pair = model.addVectorDimension(vector,
/*capacity=*/100,
/*fix_start_cumul_to_zero=*/true, "Dimension");
assertEquals(1, pair.getFirst());
assertTrue(pair.getSecond());
System.gc(); // model should keep alive the callback
model.setArcCostEvaluatorOfAllVehicles(pair.getFirst());
RoutingSearchParameters searchParameters =
RoutingSearchParameters.newBuilder()
.mergeFrom(main.defaultRoutingSearchParameters())
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
.setTimeLimit(Duration.newBuilder().setSeconds(10))
.build();
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solveWithParameters(searchParameters);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(45, solution.objectiveValue());
}
@Test
public void testRoutingModel_addMatrixDimension() {
final RoutingIndexManager manager = new RoutingIndexManager(5, 1, 0);
final RoutingModel model = new RoutingModel(manager);
assertEquals(5, model.nodes());
final long[][] matrix = {
{0, 1, 3, 3, 1},
{1, 0, 2, 4, 2},
{3, 2, 0, 4, 4},
{3, 4, 4, 0, 2},
{1, 2, 4, 2, 0},
};
final IntBoolPair pair = model.addMatrixDimension(matrix,
/*capacity=*/100,
/*fix_start_cumul_to_zero=*/true, "Dimension");
assertEquals(1, pair.getFirst());
assertTrue(pair.getSecond());
System.gc(); // model should keep alive the callback
model.setArcCostEvaluatorOfAllVehicles(pair.getFirst());
final RoutingSearchParameters searchParameters =
RoutingSearchParameters.newBuilder()
.mergeFrom(main.defaultRoutingSearchParameters())
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
.setTimeLimit(Duration.newBuilder().setSeconds(10))
.build();
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solveWithParameters(searchParameters);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(10, solution.objectiveValue());
}
@Test
public void testRoutingModel_addDimension() {
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
final RoutingModel model = new RoutingModel(manager);
final LongBinaryOperator manhattanCostCallback = createManhattanCostCallback(manager);
final int cost = model.registerTransitCallback(manhattanCostCallback);
model.setArcCostEvaluatorOfAllVehicles(cost);
final LongBinaryOperator transit = (long firstIndex, long secondIndex) -> {
int firstNode = manager.indexToNode(firstIndex);
int secondNode = manager.indexToNode(secondIndex);
if (firstNode >= coordinates.size()) {
firstNode = 0;
}
if (secondNode >= coordinates.size()) {
secondNode = 0;
}
long distanceTime = manhattanCostCallback.applyAsLong(firstIndex, secondIndex) * 10;
long visitingTime = 1;
return distanceTime + visitingTime;
};
assertTrue(
model.addDimension(model.registerTransitCallback(transit), 1000, 1000, true, "time"));
RoutingDimension dimension = model.getMutableDimension("time");
for (int i = 1; i < coordinates.size(); i++) {
int[] a = new int[1];
a[0] = i;
model.addDisjunction(manager.nodesToIndices(a), 10);
dimension.cumulVar(i).setMin(0);
dimension.cumulVar(i).setMax(40);
}
RoutingSearchParameters searchParameters =
RoutingSearchParameters.newBuilder()
.mergeFrom(main.defaultRoutingSearchParameters())
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
.setTimeLimit(Duration.newBuilder().setSeconds(10))
.build();
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
Assignment solution = model.solveWithParameters(searchParameters);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
solution = model.solve(solution);
assertNotNull(solution);
assertTrue(solution.objectiveValue() >= 24 && solution.objectiveValue() <= 30);
for (long i = 1; i < coordinates.size(); i++) {
assertTrue(
solution.min(dimension.cumulVar(i)) >= 0 && solution.max(dimension.cumulVar(i)) <= 40);
}
}
@Test
public void testRoutingModel_dimensionVehicleSpanCost() {
final RoutingIndexManager manager = new RoutingIndexManager(2, 1, 0);
final RoutingModel model = new RoutingModel(manager);
final LongBinaryOperator callback = createReturnOneCallback();
assertTrue(
model.addDimension(model.registerTransitCallback(callback), 1000, 1000, true, "time"));
RoutingDimension dimension = model.getMutableDimension("time");
dimension.cumulVar(1).setMin(10);
dimension.setSpanCostCoefficientForAllVehicles(2);
assertEquals(2, dimension.getSpanCostCoefficientForVehicle(0));
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(2 * (10 + 1), solution.objectiveValue());
}
@Test
public void testRoutingModel_dimensionGlobalSpanCost() {
final RoutingIndexManager manager = new RoutingIndexManager(3, 2, 0);
final RoutingModel model = new RoutingModel(manager);
final LongBinaryOperator callback = createReturnOneCallback();
assertTrue(
model.addDimension(model.registerTransitCallback(callback), 1000, 1000, false, "time"));
RoutingDimension timeDimension = model.getMutableDimension("time");
timeDimension.cumulVar(1).setMin(10);
model.vehicleVar(1).setValue(0);
timeDimension.cumulVar(2).setMax(2);
model.vehicleVar(2).setValue(1);
timeDimension.setGlobalSpanCostCoefficient(2);
assertEquals(2, timeDimension.getGlobalSpanCostCoefficient());
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
assertEquals(2 * (11 - 1), solution.objectiveValue());
}
@Test
public void testRoutingModel_cumulVarSoftUpperBound() {
final RoutingIndexManager manager = new RoutingIndexManager(2, 1, 0);
final RoutingModel model = new RoutingModel(manager);
final LongBinaryOperator callback = createReturnOneCallback();
assertTrue(
model.addDimension(model.registerTransitCallback(callback), 1000, 1000, false, "time"));
RoutingDimension dimension = model.getMutableDimension("time");
assertEquals(1000, dimension.getCumulVarSoftUpperBound(1));
assertEquals(0, dimension.getCumulVarSoftUpperBoundCoefficient(1));
dimension.setCumulVarSoftUpperBound(1, 5, 1);
assertEquals(5, dimension.getCumulVarSoftUpperBound(1));
assertEquals(1, dimension.getCumulVarSoftUpperBoundCoefficient(1));
}
@Test
public void testRoutingModel_addDimensionWithVehicleCapacity() {
final RoutingIndexManager manager = new RoutingIndexManager(1, 3, 0);
final RoutingModel model = new RoutingModel(manager);
final LongBinaryOperator callback = createReturnOneCallback();
final long[] capacity = {5, 6, 7};
model.addDimensionWithVehicleCapacity(
model.registerTransitCallback(callback), 1000, capacity, false, "dim");
RoutingDimension dimension = model.getMutableDimension("dim");
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
for (int vehicle = 0; vehicle < 3; ++vehicle) {
assertEquals(vehicle + 4, solution.max(dimension.cumulVar(model.start(vehicle))));
assertEquals(vehicle + 5, solution.max(dimension.cumulVar(model.end(vehicle))));
}
}
@Test
public void testRoutingModel_addDimensionWithVehicleTransits() {
final RoutingIndexManager manager = new RoutingIndexManager(1, 3, 0);
final RoutingModel model = new RoutingModel(manager);
final LongBinaryOperator[] callbacks = new LongBinaryOperator[3];
int[] transits = new int[3];
for (int i = 0; i < 3; ++i) {
final int value = i + 1;
callbacks[i] = (long firstIndex, long secondIndex) -> value;
transits[i] = model.registerTransitCallback(callbacks[i]);
}
long capacity = 5;
model.addDimensionWithVehicleTransits(transits, 1000, capacity, false, "dim");
RoutingDimension dimension = model.getMutableDimension("dim");
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
for (int vehicle = 0; vehicle < 3; ++vehicle) {
assertEquals(
capacity - (vehicle + 1), solution.max(dimension.cumulVar(model.start(vehicle))));
assertEquals(capacity, solution.max(dimension.cumulVar(model.end(vehicle))));
}
}
@Test
public void testRoutingModel_addDimensionWithVehicleTransitAndCapacity() {
final RoutingIndexManager manager = new RoutingIndexManager(1, 3, 0);
final RoutingModel model = new RoutingModel(manager);
final LongBinaryOperator[] callbacks = new LongBinaryOperator[3];
final int[] transits = new int[3];
for (int i = 0; i < 3; ++i) {
final int value = i + 1;
callbacks[i] = (long firstIndex, long secondIndex) -> value;
transits[i] = model.registerTransitCallback(callbacks[i]);
}
long[] capacity = new long[3];
for (int i = 0; i < 3; ++i) {
capacity[i] = i + 5L;
}
model.addDimensionWithVehicleTransitAndCapacity(transits, 1000, capacity, false, "dim");
final RoutingDimension dimension = model.getMutableDimension("dim");
assertEquals(RoutingModel.ROUTING_NOT_SOLVED, model.status());
final Assignment solution = model.solve(null);
assertEquals(RoutingModel.ROUTING_SUCCESS, model.status());
assertNotNull(solution);
for (int vehicle = 0; vehicle < 3; ++vehicle) {
assertEquals(4, solution.max(dimension.cumulVar(model.start(vehicle))));
assertEquals(vehicle + 5, solution.max(dimension.cumulVar(model.end(vehicle))));
}
}
@Test
public void testRoutingModel_intVarVectorGetter() {
final RoutingIndexManager manager = new RoutingIndexManager(coordinates.size(), 1, 0);
final RoutingModel model = new RoutingModel(manager);
final LongBinaryOperator manhattanCostCallback = createManhattanCostCallback(manager);
final int cost = model.registerTransitCallback(manhattanCostCallback);
model.setArcCostEvaluatorOfAllVehicles(cost);
final LongBinaryOperator transit = (long firstIndex, long secondIndex) -> {
int firstNode = manager.indexToNode(firstIndex);
int secondNode = manager.indexToNode(secondIndex);
if (firstNode >= coordinates.size()) {
firstNode = 0;
}
if (secondNode >= coordinates.size()) {
secondNode = 0;
}
long distanceTime = manhattanCostCallback.applyAsLong(firstIndex, secondIndex) * 10;
long visitingTime = 1;
return distanceTime + visitingTime;
};
assertTrue(
model.addDimension(model.registerTransitCallback(transit), 1000, 1000, true, "time"));
RoutingDimension timeDimension = model.getMutableDimension("time");
IntVar[] cumuls = timeDimension.cumuls();
assertThat(cumuls).isNotEmpty();
IntVar[] transits = timeDimension.transits();
assertThat(transits).isNotEmpty();
IntVar[] slacks = timeDimension.slacks();
assertThat(slacks).isNotEmpty();
IntVar[] nexts = model.nexts();
assertThat(nexts).isNotEmpty();
IntVar[] vehicleVars = model.vehicleVars();
assertThat(vehicleVars).isNotEmpty();
}
}