SimpleRoutingProgram.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]
16 import static java.lang.Math.abs;
17 
24 import java.util.logging.Logger;
25 // [END import]
26 
28 public class SimpleRoutingProgram {
29  static {
30  System.loadLibrary("jniortools");
31  }
32 
33  private static final Logger logger = Logger.getLogger(SimpleRoutingProgram.class.getName());
34 
35  public static void main(String[] args) throws Exception {
36  // Instantiate the data problem.
37  // [START data]
38  final int numLocation = 5;
39  final int numVehicles = 1;
40  final int depot = 0;
41  // [END data]
42 
43  // Create Routing Index Manager
44  // [START index_manager]
45  RoutingIndexManager manager = new RoutingIndexManager(numLocation, numVehicles, depot);
46  // [END index_manager]
47 
48  // Create Routing Model.
49  // [START routing_model]
50  RoutingModel routing = new RoutingModel(manager);
51  // [END routing_model]
52 
53  // Create and register a transit callback.
54  // [START transit_callback]
55  final int transitCallbackIndex =
56  routing.registerTransitCallback((long fromIndex, long toIndex) -> {
57  // Convert from routing variable Index to user NodeIndex.
58  int fromNode = manager.indexToNode(fromIndex);
59  int toNode = manager.indexToNode(toIndex);
60  return abs(toNode - fromNode);
61  });
62  // [END transit_callback]
63 
64  // Define cost of each arc.
65  // [START arc_cost]
66  routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
67  // [END arc_cost]
68 
69  // Setting first solution heuristic.
70  // [START parameters]
71  RoutingSearchParameters searchParameters =
73  .toBuilder()
75  .build();
76  // [END parameters]
77 
78  // Solve the problem.
79  // [START solve]
80  Assignment solution = routing.solveWithParameters(searchParameters);
81  // [END solve]
82 
83  // Print solution on console.
84  // [START print_solution]
85  logger.info("Objective: " + solution.objectiveValue());
86  // Inspect solution.
87  long index = routing.start(0);
88  logger.info("Route for Vehicle 0:");
89  long routeDistance = 0;
90  String route = "";
91  while (!routing.isEnd(index)) {
92  route += manager.indexToNode(index) + " -> ";
93  long previousIndex = index;
94  index = solution.value(routing.nextVar(index));
95  routeDistance += routing.getArcCostForVehicle(previousIndex, index, 0);
96  }
97  route += manager.indexToNode(index);
98  logger.info(route);
99  logger.info("Distance of the route: " + routeDistance + "m");
100  // [END print_solution]
101  }
102 }
103 // [END program]
static com.google.ortools.constraintsolver.RoutingSearchParameters defaultRoutingSearchParameters()
int registerTransitCallback(LongBinaryOperator callback)
long getArcCostForVehicle(long from_index, long to_index, long vehicle)
static void main(String[] args)
.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)
Minimal Routing example to showcase calling the solver.