VrpTimeWindows.java
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 // [START program]
15 // [START import]
24 import java.util.logging.Logger;
25 // [END import]
26 
28 public class VrpTimeWindows {
29  static {
30  System.loadLibrary("jniortools");
31  }
32 
33  private static final Logger logger = Logger.getLogger(VrpTimeWindows.class.getName());
34 
35  // [START data_model]
36  static class DataModel {
37  public final long[][] timeMatrix = {
38  {0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7},
39  {6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14},
40  {9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9},
41  {8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16},
42  {7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14},
43  {3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8},
44  {6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5},
45  {2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10},
46  {3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6},
47  {2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5},
48  {6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4},
49  {6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10},
50  {4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8},
51  {4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6},
52  {5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2},
53  {9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9},
54  {7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0},
55  };
56  public final long[][] timeWindows = {
57  {0, 5}, // depot
58  {7, 12}, // 1
59  {10, 15}, // 2
60  {5, 14}, // 3
61  {5, 13}, // 4
62  {0, 5}, // 5
63  {5, 10}, // 6
64  {0, 10}, // 7
65  {5, 10}, // 8
66  {0, 5}, // 9
67  {10, 16}, // 10
68  {10, 15}, // 11
69  {0, 5}, // 12
70  {5, 10}, // 13
71  {7, 12}, // 14
72  {10, 15}, // 15
73  {5, 15}, // 16
74  };
75  public final int vehicleNumber = 4;
76  public final int depot = 0;
77  }
78  // [END data_model]
79 
80  // [START solution_printer]
82  static void printSolution(
83  DataModel data, RoutingModel routing, RoutingIndexManager manager, Assignment solution) {
84  RoutingDimension timeDimension = routing.getMutableDimension("Time");
85  long totalTime = 0;
86  for (int i = 0; i < data.vehicleNumber; ++i) {
87  long index = routing.start(i);
88  logger.info("Route for Vehicle " + i + ":");
89  String route = "";
90  while (!routing.isEnd(index)) {
91  IntVar timeVar = timeDimension.cumulVar(index);
92  route += manager.indexToNode(index) + " Time(" + solution.min(timeVar) + ","
93  + solution.max(timeVar) + ") -> ";
94  index = solution.value(routing.nextVar(index));
95  }
96  IntVar timeVar = timeDimension.cumulVar(index);
97  route += manager.indexToNode(index) + " Time(" + solution.min(timeVar) + ","
98  + solution.max(timeVar) + ")";
99  logger.info(route);
100  logger.info("Time of the route: " + solution.min(timeVar) + "min");
101  totalTime += solution.min(timeVar);
102  }
103  logger.info("Total time of all routes: " + totalTime + "min");
104  }
105  // [END solution_printer]
106 
107  public static void main(String[] args) throws Exception {
108  // Instantiate the data problem.
109  // [START data]
110  final DataModel data = new DataModel();
111  // [END data]
112 
113  // Create Routing Index Manager
114  // [START index_manager]
115  RoutingIndexManager manager =
116  new RoutingIndexManager(data.timeMatrix.length, data.vehicleNumber, data.depot);
117  // [END index_manager]
118 
119  // Create Routing Model.
120  // [START routing_model]
121  RoutingModel routing = new RoutingModel(manager);
122  // [END routing_model]
123 
124  // Create and register a transit callback.
125  // [START transit_callback]
126  final int transitCallbackIndex =
127  routing.registerTransitCallback((long fromIndex, long toIndex) -> {
128  // Convert from routing variable Index to user NodeIndex.
129  int fromNode = manager.indexToNode(fromIndex);
130  int toNode = manager.indexToNode(toIndex);
131  return data.timeMatrix[fromNode][toNode];
132  });
133  // [END transit_callback]
134 
135  // Define cost of each arc.
136  // [START arc_cost]
137  routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
138  // [END arc_cost]
139 
140  // Add Time constraint.
141  // [START time_constraint]
142  routing.addDimension(transitCallbackIndex, // transit callback
143  30, // allow waiting time
144  30, // vehicle maximum capacities
145  false, // start cumul to zero
146  "Time");
147  RoutingDimension timeDimension = routing.getMutableDimension("Time");
148  // Add time window constraints for each location except depot.
149  for (int i = 1; i < data.timeWindows.length; ++i) {
150  long index = manager.nodeToIndex(i);
151  timeDimension.cumulVar(index).setRange(data.timeWindows[i][0], data.timeWindows[i][1]);
152  }
153  // Add time window constraints for each vehicle start node.
154  for (int i = 0; i < data.vehicleNumber; ++i) {
155  long index = routing.start(i);
156  timeDimension.cumulVar(index).setRange(data.timeWindows[0][0], data.timeWindows[0][1]);
157  }
158  // [END time_constraint]
159 
160  // Instantiate route start and end times to produce feasible times.
161  // [START depot_start_end_times]
162  for (int i = 0; i < data.vehicleNumber; ++i) {
163  routing.addVariableMinimizedByFinalizer(timeDimension.cumulVar(routing.start(i)));
164  routing.addVariableMinimizedByFinalizer(timeDimension.cumulVar(routing.end(i)));
165  }
166  // [END depot_start_end_times]
167 
168  // Setting first solution heuristic.
169  // [START parameters]
170  RoutingSearchParameters searchParameters =
172  .toBuilder()
174  .build();
175  // [END parameters]
176 
177  // Solve the problem.
178  // [START solve]
179  Assignment solution = routing.solveWithParameters(searchParameters);
180  // [END solve]
181 
182  // Print solution on console.
183  // [START print_solution]
184  printSolution(data, routing, manager, solution);
185  // [END print_solution]
186  }
187 }
188 // [END program]
static com.google.ortools.constraintsolver.RoutingSearchParameters defaultRoutingSearchParameters()
RoutingDimension getMutableDimension(String dimension_name)
int registerTransitCallback(LongBinaryOperator callback)
static void main(String[] args)
Minimal VRP.
.lang.Override com.google.ortools.constraintsolver.RoutingSearchParameters build()
Builder setFirstSolutionStrategy(com.google.ortools.constraintsolver.FirstSolutionStrategy.Value value)
Assignment solveWithParameters(com.google.ortools.constraintsolver.RoutingSearchParameters search_parameters)
void setArcCostEvaluatorOfAllVehicles(int evaluator_index)
boolean addDimension(int evaluator_index, long slack_max, long capacity, boolean fix_start_cumul_to_zero, String name)