forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleRoutingTest.java
117 lines (100 loc) · 3.34 KB
/
SimpleRoutingTest.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
package com.google.ortools.contrib;
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.RoutingSearchParameters;
import com.google.ortools.constraintsolver.main;
import java.util.ArrayList;
import java.util.function.LongBinaryOperator;
public class SimpleRoutingTest {
// Static Add Library
static {
System.loadLibrary("jniortools");
}
private ArrayList<Integer> globalRes;
private long globalResCost;
private int[][] costMatrix;
public ArrayList<Integer> getGlobalRes() {
return globalRes;
}
public void setGlobalRes(ArrayList<Integer> globalRes) {
this.globalRes = globalRes;
}
public long getGlobalResCost() {
return globalResCost;
}
public void setGlobalResCost(int globalResCost) {
this.globalResCost = globalResCost;
}
public int[][] getCostMatrix() {
return costMatrix;
}
public void setCostMatrix(int[][] costMatrix) {
this.costMatrix = costMatrix;
}
public SimpleRoutingTest(int[][] costMatrix) {
super();
this.costMatrix = costMatrix;
globalRes = new ArrayList();
}
// Node Distance Evaluation
public static class NodeDistance implements LongBinaryOperator {
private int[][] costMatrix;
private RoutingIndexManager indexManager;
public NodeDistance(RoutingIndexManager manager, int[][] costMatrix) {
this.costMatrix = costMatrix;
this.indexManager = manager;
}
@Override
public long applyAsLong(long firstIndex, long secondIndex) {
final int firstNode = indexManager.indexToNode(firstIndex);
final int secondNode = indexManager.indexToNode(secondIndex);
return costMatrix[firstNode][secondNode];
}
}
// Solve Method
public void solve() {
RoutingIndexManager manager = new RoutingIndexManager(costMatrix.length, 1, 0);
RoutingModel routing = new RoutingModel(manager);
RoutingSearchParameters parameters =
RoutingSearchParameters.newBuilder()
.mergeFrom(main.defaultRoutingSearchParameters())
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
.build();
NodeDistance distances = new NodeDistance(manager, costMatrix);
routing.setArcCostEvaluatorOfAllVehicles(routing.registerTransitCallback(distances));
Assignment solution = routing.solve();
if (solution != null) {
int route_number = 0;
for (long node = routing.start(route_number);
!routing.isEnd(node);
node = solution.value(routing.nextVar(node))) {
globalRes.add((int) node);
}
}
globalResCost = solution.objectiveValue();
System.out.println("cost = " + globalResCost);
}
public static void main(String[] args) throws Exception {
int[][] values = new int[4][4];
values[0][0] = 0;
values[0][1] = 5;
values[0][2] = 3;
values[0][3] = 6;
values[1][0] = 5;
values[1][1] = 0;
values[1][2] = 8;
values[1][3] = 1;
values[2][0] = 3;
values[2][1] = 8;
values[2][2] = 0;
values[2][3] = 4;
values[3][0] = 6;
values[3][1] = 1;
values[3][2] = 4;
values[3][3] = 0;
SimpleRoutingTest model = new SimpleRoutingTest(values);
model.solve();
}
}