OR-Tools  7.1
VrpWithTimeLimit.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 com.google.protobuf.Duration;
25 import java.util.logging.Logger;
26 // [END import]
27 
29 public class VrpWithTimeLimit {
30  static {
31  System.loadLibrary("jniortools");
32  }
33 
34  private static final Logger logger = Logger.getLogger(VrpWithTimeLimit.class.getName());
35 
36  // [START solution_printer]
38  static void printSolution(RoutingIndexManager manager, RoutingModel routing, Assignment solution) {
39  // Inspect solution.
40  long maxRouteDistance = 0;
41  for (int i = 0; i < manager.getNumberOfVehicles(); ++i) {
42  long index = routing.start(i);
43  logger.info("Route for Vehicle " + i + ":");
44  long routeDistance = 0;
45  String route = "";
46  while (!routing.isEnd(index)) {
47  route += manager.indexToNode(index) + " -> ";
48  long previousIndex = index;
49  index = solution.value(routing.nextVar(index));
50  routeDistance += routing.getArcCostForVehicle(previousIndex, index, i);
51  }
52  logger.info(route + manager.indexToNode(index));
53  logger.info("Distance of the route: " + routeDistance + "m");
54  maxRouteDistance = Math.max(routeDistance, maxRouteDistance);
55  }
56  logger.info("Maximum of the route distances: " + maxRouteDistance + "m");
57  }
58  // [END solution_printer]
59 
60  public static void main(String[] args) throws Exception {
61  // Instantiate the data problem.
62  // [START data]
63  final int locationNumber = 20;
64  final int vehicleNumber = 5;
65  final int depot = 0;
66  // [END data]
67 
68  // Create Routing Index Manager
69  // [START index_manager]
70  RoutingIndexManager manager =
71  new RoutingIndexManager(locationNumber, vehicleNumber, depot);
72  // [END index_manager]
73 
74  // Create Routing Model.
75  // [START routing_model]
76  RoutingModel routing = new RoutingModel(manager);
77  // [END routing_model]
78 
79  // Create and register a transit callback.
80  // [START transit_callback]
81  final int transitCallbackIndex =
82  routing.registerTransitCallback((long fromIndex, long toIndex) -> {
83  // Convert from routing variable Index to user NodeIndex.
84  int fromNode = manager.indexToNode(fromIndex);
85  int toNode = manager.indexToNode(toIndex);
86  return 1;
87  });
88  // [END transit_callback]
89 
90  // Define cost of each arc.
91  // [START arc_cost]
92  routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
93  // [END arc_cost]
94 
95  // Add Distance constraint.
96  // [START distance_constraint]
97  routing.addDimension(
98  transitCallbackIndex,
99  /*slack=*/0,
100  /*horizon=*/3000,
101  /*start_cumul_to_zero=*/true,
102  "Distance");
103  RoutingDimension distanceDimension = routing.getMutableDimension("Distance");
104  distanceDimension.setGlobalSpanCostCoefficient(100);
105  // [END distance_constraint]
106 
107  // Setting first solution heuristic.
108  // [START parameters]
109  RoutingSearchParameters searchParameters =
111  .toBuilder()
114  .setLogSearch(true)
115  .setTimeLimit(Duration.newBuilder().setSeconds(10).build())
116  .build();
117  // [END parameters]
118 
119  // Solve the problem.
120  // [START solve]
121  Assignment solution = routing.solveWithParameters(searchParameters);
122  // [END solve]
123 
124  // Print solution on console.
125  // [START print_solution]
126  printSolution(manager, routing, solution);
127  // [END print_solution]
128  }
129 }
130 // [END program]
static com.google.ortools.constraintsolver.RoutingSearchParameters defaultRoutingSearchParameters()
RoutingDimension getMutableDimension(String dimension_name)
int registerTransitCallback(LongBinaryOperator callback)
long getArcCostForVehicle(long from_index, long to_index, long vehicle)
Builder setFirstSolutionStrategy(com.google.ortools.constraintsolver.FirstSolutionStrategy.Value value)
Builder setLocalSearchMetaheuristic(com.google.ortools.constraintsolver.LocalSearchMetaheuristic.Value value)
Assignment solveWithParameters(com.google.ortools.constraintsolver.RoutingSearchParameters search_parameters)
static void main(String[] args)
void setArcCostEvaluatorOfAllVehicles(int evaluator_index)
boolean addDimension(int evaluator_index, long slack_max, long capacity, boolean fix_start_cumul_to_zero, String name)