Move Tsp.java & Vrp.java to cp/samples
This commit is contained in:
@@ -1,136 +0,0 @@
|
||||
//
|
||||
// Copyright 2010-2017 Google LLC
|
||||
//
|
||||
// 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.
|
||||
import com.google.ortools.constraintsolver.Assignment;
|
||||
import com.google.ortools.constraintsolver.FirstSolutionStrategy;
|
||||
import com.google.ortools.constraintsolver.LongLongToLong;
|
||||
import com.google.ortools.constraintsolver.LongToLong;
|
||||
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.io.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
class Tsp {
|
||||
static {
|
||||
System.loadLibrary("jniortools");
|
||||
}
|
||||
|
||||
static class RandomManhattan extends LongLongToLong {
|
||||
public RandomManhattan(RoutingIndexManager manager, int size, int seed) {
|
||||
this.xs = new int[size];
|
||||
this.ys = new int[size];
|
||||
this.indexManager = manager;
|
||||
Random generator = new Random(seed);
|
||||
for (int i = 0; i < size; ++i) {
|
||||
xs[i] = generator.nextInt(1000);
|
||||
ys[i] = generator.nextInt(1000);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long run(long firstIndex, long secondIndex) {
|
||||
int firstNode = indexManager.indexToNode(firstIndex);
|
||||
int secondNode = indexManager.indexToNode(secondIndex);
|
||||
return Math.abs(xs[firstNode] - xs[secondNode]) + Math.abs(ys[firstNode] - ys[secondNode]);
|
||||
}
|
||||
|
||||
private int[] xs;
|
||||
private int[] ys;
|
||||
private RoutingIndexManager indexManager;
|
||||
}
|
||||
|
||||
static class ConstantCallback extends LongToLong {
|
||||
@Override
|
||||
public long run(long index) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void solve(int size, int forbidden, int seed) {
|
||||
RoutingIndexManager manager = new RoutingIndexManager(size, 1, 0);
|
||||
RoutingModel routing = new RoutingModel(manager);
|
||||
|
||||
// Setting the cost function.
|
||||
// Put a permanent callback to the distance accessor here. The callback
|
||||
// has the following signature: ResultCallback2<int64, int64, int64>.
|
||||
// The two arguments are the from and to node inidices.
|
||||
LongLongToLong distances = new RandomManhattan(manager, size, seed);
|
||||
routing.setArcCostEvaluatorOfAllVehicles(routing.registerTransitCallback(distances));
|
||||
|
||||
// Forbid node connections (randomly).
|
||||
Random randomizer = new Random();
|
||||
long forbidden_connections = 0;
|
||||
while (forbidden_connections < forbidden) {
|
||||
long from = randomizer.nextInt(size - 1);
|
||||
long to = randomizer.nextInt(size - 1) + 1;
|
||||
if (routing.nextVar(from).contains(to)) {
|
||||
System.out.println("Forbidding connection " + from + " -> " + to);
|
||||
routing.nextVar(from).removeValue(to);
|
||||
++forbidden_connections;
|
||||
}
|
||||
}
|
||||
|
||||
// Add dummy dimension to test API.
|
||||
routing.addDimension(
|
||||
routing.registerUnaryTransitCallback(new ConstantCallback()),
|
||||
size + 1,
|
||||
size + 1,
|
||||
true,
|
||||
"dummy");
|
||||
|
||||
// Solve, returns a solution if any (owned by RoutingModel).
|
||||
RoutingSearchParameters search_parameters =
|
||||
RoutingSearchParameters.newBuilder()
|
||||
.mergeFrom(main.defaultRoutingSearchParameters())
|
||||
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
|
||||
.build();
|
||||
|
||||
Assignment solution = routing.solveWithParameters(search_parameters);
|
||||
if (solution != null) {
|
||||
// Solution cost.
|
||||
System.out.println("Cost = " + solution.objectiveValue());
|
||||
// Inspect solution.
|
||||
// Only one route here; otherwise iterate from 0 to routing.vehicles() - 1
|
||||
int route_number = 0;
|
||||
for (long node = routing.start(route_number);
|
||||
!routing.isEnd(node);
|
||||
node = solution.value(routing.nextVar(node))) {
|
||||
System.out.print("" + node + " -> ");
|
||||
}
|
||||
System.out.println("0");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
int size = 10;
|
||||
if (args.length > 0) {
|
||||
size = Integer.parseInt(args[0]);
|
||||
}
|
||||
|
||||
int forbidden = 0;
|
||||
if (args.length > 1) {
|
||||
forbidden = Integer.parseInt(args[1]);
|
||||
}
|
||||
|
||||
int seed = 0;
|
||||
if (args.length > 2) {
|
||||
seed = Integer.parseInt(args[2]);
|
||||
}
|
||||
|
||||
solve(size, forbidden, seed);
|
||||
}
|
||||
}
|
||||
@@ -1,170 +0,0 @@
|
||||
// Copyright 2018 Google LLC
|
||||
// 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.
|
||||
import static java.lang.Math.abs;
|
||||
|
||||
import com.google.ortools.constraintsolver.Assignment;
|
||||
import com.google.ortools.constraintsolver.FirstSolutionStrategy;
|
||||
import com.google.ortools.constraintsolver.LongLongToLong;
|
||||
import com.google.ortools.constraintsolver.RoutingDimension;
|
||||
import com.google.ortools.constraintsolver.RoutingIndexManager;
|
||||
import com.google.ortools.constraintsolver.RoutingModel;
|
||||
import com.google.ortools.constraintsolver.RoutingDimension;
|
||||
import com.google.ortools.constraintsolver.RoutingSearchParameters;
|
||||
import com.google.ortools.constraintsolver.main;
|
||||
import java.io.*;
|
||||
|
||||
class DataProblem {
|
||||
private int[][] locations_;
|
||||
|
||||
public DataProblem() {
|
||||
locations_ =
|
||||
new int[][] {
|
||||
{4, 4}, {2, 0}, {8, 0}, {0, 1}, {1, 1}, {5, 2}, {7, 2}, {3, 3}, {6, 3}, {5, 5}, {8, 5},
|
||||
{1, 6}, {2, 6}, {3, 7}, {6, 7}, {0, 8}, {7, 8}
|
||||
};
|
||||
|
||||
// Compute locations in meters using the block dimension defined as follow
|
||||
// Manhattan average block: 750ft x 264ft -> 228m x 80m
|
||||
// here we use: 114m x 80m city block
|
||||
// src: https://nyti.ms/2GDoRIe "NY Times: Know Your distance"
|
||||
int[] cityBlock = {228 / 2, 80};
|
||||
for (int i = 0; i < locations_.length; i++) {
|
||||
locations_[i][0] = locations_[i][0] * cityBlock[0];
|
||||
locations_[i][1] = locations_[i][1] * cityBlock[1];
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Gets the number of vehicles.
|
||||
public int getVehicleNumber() {
|
||||
return 4;
|
||||
}
|
||||
/// @brief Gets the locations.
|
||||
public int[][] getLocations() {
|
||||
return locations_;
|
||||
}
|
||||
/// @brief Gets the number of locations.
|
||||
public int getLocationNumber() {
|
||||
return locations_.length;
|
||||
}
|
||||
/// @brief Gets the depot NodeIndex.
|
||||
public int getDepot() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Manhattan distance implemented as a callback.
|
||||
/// @details It uses an array of positions and computes
|
||||
/// the Manhattan distance between the two positions of
|
||||
/// two different indices.
|
||||
class ManhattanDistance extends LongLongToLong {
|
||||
private int[][] distances;
|
||||
private RoutingIndexManager indexManager;
|
||||
|
||||
public ManhattanDistance(DataProblem data, RoutingIndexManager manager) {
|
||||
// precompute distance between location to have distance callback in O(1)
|
||||
distances = new int[data.getLocationNumber()][data.getLocationNumber()];
|
||||
indexManager = manager;
|
||||
for (int fromNode = 0; fromNode < data.getLocationNumber(); ++fromNode) {
|
||||
for (int toNode = 0; toNode < data.getLocationNumber(); ++toNode) {
|
||||
if (fromNode == toNode) distances[fromNode][toNode] = 0;
|
||||
else
|
||||
distances[fromNode][toNode] =
|
||||
abs(data.getLocations()[toNode][0] - data.getLocations()[fromNode][0])
|
||||
+ abs(data.getLocations()[toNode][1] - data.getLocations()[fromNode][1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
/// @brief Returns the manhattan distance between the two nodes.
|
||||
public long run(long fromIndex, long toIndex) {
|
||||
int fromNode = indexManager.indexToNode(fromIndex);
|
||||
int toNode = indexManager.indexToNode(toIndex);
|
||||
return distances[fromNode][toNode];
|
||||
}
|
||||
}
|
||||
|
||||
class Vrp {
|
||||
static {
|
||||
System.loadLibrary("jniortools");
|
||||
}
|
||||
|
||||
/// @brief Add Global Span constraint.
|
||||
static void addDistanceDimension(RoutingModel routing, DataProblem data, int distanceIndex) {
|
||||
String distance = "Distance";
|
||||
routing.addDimension(
|
||||
distanceIndex,
|
||||
0, // null slack
|
||||
3000, // maximum distance per vehicle
|
||||
true, // start cumul to zero
|
||||
distance);
|
||||
RoutingDimension distanceDimension = routing.getDimensionOrDie(distance);
|
||||
// Try to minimize the max distance among vehicles.
|
||||
// /!\ It doesn't mean the standard deviation is minimized
|
||||
distanceDimension.setGlobalSpanCostCoefficient(100);
|
||||
}
|
||||
|
||||
/// @brief Print the solution
|
||||
static void printSolution(
|
||||
DataProblem data, RoutingModel routing, RoutingIndexManager manager, Assignment solution) {
|
||||
// Solution cost.
|
||||
System.out.println("Objective : " + solution.objectiveValue());
|
||||
// Inspect solution.
|
||||
for (int i = 0; i < data.getVehicleNumber(); ++i) {
|
||||
System.out.println("Route for Vehicle " + i + ":");
|
||||
long distance = 0;
|
||||
for (long index = routing.start(i); !routing.isEnd(index); ) {
|
||||
System.out.print(manager.indexToNode((int) index) + " -> ");
|
||||
|
||||
long previousIndex = index;
|
||||
index = solution.value(routing.nextVar(index));
|
||||
distance += routing.getArcCostForVehicle(previousIndex, index, i);
|
||||
}
|
||||
System.out.println(manager.indexToNode((int) routing.end(i)));
|
||||
System.out.println("Distance of the route: " + distance + "m");
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Solves the current routing problem.
|
||||
static void solve() {
|
||||
// Instantiate the data problem.
|
||||
DataProblem data = new DataProblem();
|
||||
|
||||
// Create Routing Model
|
||||
RoutingIndexManager manager =
|
||||
new RoutingIndexManager(data.getLocationNumber(), data.getVehicleNumber(), data.getDepot());
|
||||
RoutingModel routing = new RoutingModel(manager);
|
||||
|
||||
// Setting the cost function.
|
||||
// [todo]: protect callback from the GC
|
||||
LongLongToLong distanceEvaluator = new ManhattanDistance(data, manager);
|
||||
int distanceIndex = routing.registerTransitCallback(distanceEvaluator);
|
||||
routing.setArcCostEvaluatorOfAllVehicles(distanceIndex);
|
||||
addDistanceDimension(routing, data, distanceIndex);
|
||||
|
||||
// Setting first solution heuristic (cheapest addition).
|
||||
RoutingSearchParameters search_parameters =
|
||||
RoutingSearchParameters.newBuilder()
|
||||
.mergeFrom(main.defaultRoutingSearchParameters())
|
||||
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
|
||||
.build();
|
||||
|
||||
Assignment solution = routing.solveWithParameters(search_parameters);
|
||||
printSolution(data, routing, manager, solution);
|
||||
}
|
||||
|
||||
/// @brief Entry point of the program.
|
||||
public static void main(String[] args) throws Exception {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user