Files
ortools-clone/ortools/constraint_solver/samples/SimpleRoutingProgram.java

103 lines
3.6 KiB
Java
Raw Normal View History

2022-06-17 08:40:20 +02:00
// Copyright 2010-2022 Google LLC
2018-12-27 14:58:47 +01:00
// 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.
// [START program]
2020-05-26 09:30:42 +02:00
package com.google.ortools.constraintsolver.samples;
2018-12-27 14:58:47 +01:00
// [START import]
2019-01-10 10:53:01 +01:00
import static java.lang.Math.abs;
import com.google.ortools.Loader;
2018-12-27 14:58:47 +01:00
import com.google.ortools.constraintsolver.Assignment;
2019-03-12 14:51:01 +01:00
import com.google.ortools.constraintsolver.FirstSolutionStrategy;
2018-12-27 14:58:47 +01:00
import com.google.ortools.constraintsolver.RoutingIndexManager;
import com.google.ortools.constraintsolver.RoutingModel;
2019-02-18 15:27:32 +01:00
import com.google.ortools.constraintsolver.RoutingSearchParameters;
2019-03-12 14:51:01 +01:00
import com.google.ortools.constraintsolver.main;
2019-01-09 11:28:14 +01:00
import java.util.logging.Logger;
2018-12-27 14:58:47 +01:00
// [END import]
2019-01-10 10:53:01 +01:00
/** Minimal Routing example to showcase calling the solver.*/
public class SimpleRoutingProgram {
private static final Logger logger = Logger.getLogger(SimpleRoutingProgram.class.getName());
2018-12-27 14:58:47 +01:00
2019-01-10 10:53:01 +01:00
public static void main(String[] args) throws Exception {
Loader.loadNativeLibraries();
2018-12-27 14:58:47 +01:00
// Instantiate the data problem.
// [START data]
2019-01-10 10:53:01 +01:00
final int numLocation = 5;
final int numVehicles = 1;
2018-12-27 14:58:47 +01:00
final int depot = 0;
// [END data]
// Create Routing Index Manager
// [START index_manager]
2019-01-10 10:53:01 +01:00
RoutingIndexManager manager = new RoutingIndexManager(numLocation, numVehicles, depot);
2018-12-27 14:58:47 +01:00
// [END index_manager]
// Create Routing Model.
// [START routing_model]
RoutingModel routing = new RoutingModel(manager);
// [END routing_model]
2019-02-05 15:10:20 +01:00
// Create and register a transit callback.
// [START transit_callback]
2019-02-18 15:27:32 +01:00
final int transitCallbackIndex =
routing.registerTransitCallback((long fromIndex, long toIndex) -> {
// Convert from routing variable Index to user NodeIndex.
int fromNode = manager.indexToNode(fromIndex);
int toNode = manager.indexToNode(toIndex);
return abs(toNode - fromNode);
});
// [END transit_callback]
2018-12-27 14:58:47 +01:00
// Define cost of each arc.
// [START arc_cost]
2019-01-11 16:49:07 +01:00
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
2018-12-27 14:58:47 +01:00
// [END arc_cost]
// Setting first solution heuristic.
// [START parameters]
2019-01-10 10:53:01 +01:00
RoutingSearchParameters searchParameters =
main.defaultRoutingSearchParameters()
.toBuilder()
2018-12-27 14:58:47 +01:00
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
.build();
// [END parameters]
// Solve the problem.
// [START solve]
2019-01-10 10:53:01 +01:00
Assignment solution = routing.solveWithParameters(searchParameters);
2018-12-27 14:58:47 +01:00
// [END solve]
// Print solution on console.
// [START print_solution]
logger.info("Objective: " + solution.objectiveValue());
// Inspect solution.
long index = routing.start(0);
logger.info("Route for Vehicle 0:");
2019-01-10 10:53:01 +01:00
long routeDistance = 0;
2018-12-27 14:58:47 +01:00
String route = "";
2019-01-10 10:53:01 +01:00
while (!routing.isEnd(index)) {
2018-12-27 14:58:47 +01:00
route += manager.indexToNode(index) + " -> ";
2019-01-10 10:53:01 +01:00
long previousIndex = index;
2018-12-27 14:58:47 +01:00
index = solution.value(routing.nextVar(index));
2019-01-10 10:53:01 +01:00
routeDistance += routing.getArcCostForVehicle(previousIndex, index, 0);
2018-12-27 14:58:47 +01:00
}
route += manager.indexToNode(index);
logger.info(route);
2019-01-10 10:53:01 +01:00
logger.info("Distance of the route: " + routeDistance + "m");
2018-12-27 14:58:47 +01:00
// [END print_solution]
}
}
// [END program]