Update samples
This commit is contained in:
@@ -32,9 +32,9 @@ public class SimpleRoutingProgram {
|
||||
// Create Routing Index Manager
|
||||
// [START index_manager]
|
||||
RoutingIndexManager manager = new RoutingIndexManager(
|
||||
numLocation,
|
||||
numVehicles,
|
||||
depot);
|
||||
numLocation,
|
||||
numVehicles,
|
||||
depot);
|
||||
// [END index_manager]
|
||||
|
||||
// Create Routing Model.
|
||||
@@ -44,13 +44,14 @@ public class SimpleRoutingProgram {
|
||||
|
||||
// Define cost of each arc.
|
||||
// [START arc_cost]
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(
|
||||
routing.RegisterTransitCallback(
|
||||
(long fromIndex, long toIndex) => {
|
||||
var fromNode = manager.IndexToNode(fromIndex);
|
||||
var toNode = manager.IndexToNode(toIndex);
|
||||
return Math.Abs(toNode - fromNode); }
|
||||
));
|
||||
int transitCallbackIndex = routing.RegisterTransitCallback(
|
||||
(long fromIndex, long toIndex) => {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
var fromNode = manager.IndexToNode(fromIndex);
|
||||
var toNode = manager.IndexToNode(toIndex);
|
||||
return Math.Abs(toNode - fromNode); }
|
||||
);
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
|
||||
// [END arc_cost]
|
||||
|
||||
// Setting first solution heuristic.
|
||||
|
||||
@@ -53,12 +53,16 @@ public class SimpleRoutingProgram {
|
||||
|
||||
// Define cost of each arc.
|
||||
// [START arc_cost]
|
||||
routing.setArcCostEvaluatorOfAllVehicles(routing.registerTransitCallback(new LongLongToLong() {
|
||||
int transitCallbackIndex = routing.registerTransitCallback(new LongLongToLong() {
|
||||
@Override
|
||||
public long run(long fromIndex, long toIndex) {
|
||||
return abs(fromIndex - toIndex);
|
||||
// Convert from routing variable Index to user NodeIndex.
|
||||
int fromNode = manager.indexToNode(fromIndex);
|
||||
int toNode = manager.indexToNode(toIndex);
|
||||
return abs(toNode - fromNode);
|
||||
}
|
||||
}));
|
||||
});
|
||||
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
|
||||
// [END arc_cost]
|
||||
|
||||
// Setting first solution heuristic.
|
||||
|
||||
@@ -83,6 +83,7 @@ public class Tsp {
|
||||
/// Returns the manhattan distance between the two nodes
|
||||
/// </summary>
|
||||
public override long Run(long FromIndex, long ToIndex) {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
int FromNode = indexManager_.IndexToNode(FromIndex);
|
||||
int ToNode = indexManager_.IndexToNode(ToIndex);
|
||||
return distancesMatrix_[FromNode, ToNode];
|
||||
|
||||
@@ -77,14 +77,14 @@ public class Tsp {
|
||||
static class ManhattanDistance extends LongLongToLong {
|
||||
public ManhattanDistance(DataModel data, RoutingIndexManager manager) {
|
||||
// precompute distance between location to have distance callback in O(1)
|
||||
distanceMatrix_ = new long[data.locations.length][data.locations.length];
|
||||
indexManager_ = manager;
|
||||
distanceMatrix = new long[data.locations.length][data.locations.length];
|
||||
indexManager = manager;
|
||||
for (int fromNode = 0; fromNode < data.locations.length; ++fromNode) {
|
||||
for (int toNode = 0; toNode < data.locations.length; ++toNode) {
|
||||
if (fromNode == toNode) {
|
||||
distanceMatrix_[fromNode][toNode] = 0;
|
||||
distanceMatrix[fromNode][toNode] = 0;
|
||||
} else {
|
||||
distanceMatrix_[fromNode][toNode] =
|
||||
distanceMatrix[fromNode][toNode] =
|
||||
(long) abs(data.locations[toNode][0] - data.locations[fromNode][0])
|
||||
+ (long) abs(data.locations[toNode][1] - data.locations[fromNode][1]);
|
||||
}
|
||||
@@ -94,12 +94,13 @@ public class Tsp {
|
||||
|
||||
@Override
|
||||
public long run(long fromIndex, long toIndex) {
|
||||
int fromNode = indexManager_.indexToNode(fromIndex);
|
||||
int toNode = indexManager_.indexToNode(toIndex);
|
||||
return distanceMatrix_[fromNode][toNode];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
int fromNode = indexManager.indexToNode(fromIndex);
|
||||
int toNode = indexManager.indexToNode(toIndex);
|
||||
return distanceMatrix[fromNode][toNode];
|
||||
}
|
||||
private final long[][] distanceMatrix_;
|
||||
private final RoutingIndexManager indexManager_;
|
||||
private final long[][] distanceMatrix;
|
||||
private final RoutingIndexManager indexManager;
|
||||
}
|
||||
// [END manhattan_distance]
|
||||
|
||||
|
||||
@@ -101,6 +101,7 @@ public class TspDistanceMatrix {
|
||||
// [START arc_cost]
|
||||
int transitCallbackIndex = routing.RegisterTransitCallback(
|
||||
(long fromIndex, long toIndex) => {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
var fromNode = manager.IndexToNode(fromIndex);
|
||||
var toNode = manager.IndexToNode(toIndex);
|
||||
return data.GetDistanceMatrix()[fromNode, toNode]; }
|
||||
|
||||
@@ -69,18 +69,18 @@ public class TspDistanceMatrix {
|
||||
/// two different indices.
|
||||
static class ManhattanDistance extends LongLongToLong {
|
||||
public ManhattanDistance(DataModel data, RoutingIndexManager manager) {
|
||||
// precompute distance between location to have distance callback in O(1)
|
||||
distanceMatrix_ = data.distanceMatrix;
|
||||
indexManager_ = manager;
|
||||
distanceMatrix = data.distanceMatrix;
|
||||
indexManager = manager;
|
||||
}
|
||||
@Override
|
||||
public long run(long fromIndex, long toIndex) {
|
||||
int fromNode = indexManager_.indexToNode(fromIndex);
|
||||
int toNode = indexManager_.indexToNode(toIndex);
|
||||
return distanceMatrix_[fromNode][toNode];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
int fromNode = indexManager.indexToNode(fromIndex);
|
||||
int toNode = indexManager.indexToNode(toIndex);
|
||||
return distanceMatrix[fromNode][toNode];
|
||||
}
|
||||
private final long[][] distanceMatrix_;
|
||||
private final RoutingIndexManager indexManager_;
|
||||
private final long[][] distanceMatrix;
|
||||
private final RoutingIndexManager indexManager;
|
||||
}
|
||||
// [END manhattan_distance]
|
||||
|
||||
|
||||
@@ -107,6 +107,7 @@ public class Vrp {
|
||||
// [START arc_cost]
|
||||
int transitCallbackIndex = routing.RegisterTransitCallback(
|
||||
(long fromIndex, long toIndex) => {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
var fromNode = manager.IndexToNode(fromIndex);
|
||||
var toNode = manager.IndexToNode(toIndex);
|
||||
return data.GetDistanceMatrix()[fromNode, toNode]; }
|
||||
|
||||
@@ -69,18 +69,18 @@ public class Vrp {
|
||||
/// two different indices.
|
||||
static class ManhattanDistance extends LongLongToLong {
|
||||
public ManhattanDistance(DataModel data, RoutingIndexManager manager) {
|
||||
// precompute distance between location to have distance callback in O(1)
|
||||
distanceMatrix_ = data.distanceMatrix;
|
||||
indexManager_ = manager;
|
||||
distanceMatrix = data.distanceMatrix;
|
||||
indexManager = manager;
|
||||
}
|
||||
@Override
|
||||
public long run(long fromIndex, long toIndex) {
|
||||
int fromNode = indexManager_.indexToNode(fromIndex);
|
||||
int toNode = indexManager_.indexToNode(toIndex);
|
||||
return distanceMatrix_[fromNode][toNode];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
int fromNode = indexManager.indexToNode(fromIndex);
|
||||
int toNode = indexManager.indexToNode(toIndex);
|
||||
return distanceMatrix[fromNode][toNode];
|
||||
}
|
||||
private final long[][] distanceMatrix_;
|
||||
private final RoutingIndexManager indexManager_;
|
||||
private final long[][] distanceMatrix;
|
||||
private final RoutingIndexManager indexManager;
|
||||
}
|
||||
// [END manhattan_distance]
|
||||
|
||||
|
||||
@@ -119,6 +119,7 @@ public class VrpCapacity {
|
||||
// [START arc_cost]
|
||||
int transitCallbackIndex = routing.RegisterTransitCallback(
|
||||
(long fromIndex, long toIndex) => {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
var fromNode = manager.IndexToNode(fromIndex);
|
||||
var toNode = manager.IndexToNode(toIndex);
|
||||
return data.GetDistanceMatrix()[fromNode, toNode]; }
|
||||
@@ -130,6 +131,7 @@ public class VrpCapacity {
|
||||
// [START capacity_constraint]
|
||||
int demandCallbackIndex = routing.RegisterUnaryTransitCallback(
|
||||
(long fromIndex) => {
|
||||
// Convert from routing variable Index to demand NodeIndex.
|
||||
var fromNode = manager.IndexToNode(fromIndex);
|
||||
return data.GetDemands()[fromNode]; }
|
||||
);
|
||||
|
||||
@@ -74,36 +74,36 @@ public class VrpCapacity {
|
||||
/// two different indices.
|
||||
static class ManhattanDistance extends LongLongToLong {
|
||||
public ManhattanDistance(DataModel data, RoutingIndexManager manager) {
|
||||
// precompute distance between location to have distance callback in O(1)
|
||||
distanceMatrix_ = data.distanceMatrix;
|
||||
indexManager_ = manager;
|
||||
distanceMatrix = data.distanceMatrix;
|
||||
indexManager = manager;
|
||||
}
|
||||
@Override
|
||||
public long run(long fromIndex, long toIndex) {
|
||||
int fromNode = indexManager_.indexToNode(fromIndex);
|
||||
int toNode = indexManager_.indexToNode(toIndex);
|
||||
return distanceMatrix_[fromNode][toNode];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
int fromNode = indexManager.indexToNode(fromIndex);
|
||||
int toNode = indexManager.indexToNode(toIndex);
|
||||
return distanceMatrix[fromNode][toNode];
|
||||
}
|
||||
private final long[][] distanceMatrix_;
|
||||
private final RoutingIndexManager indexManager_;
|
||||
private final long[][] distanceMatrix;
|
||||
private final RoutingIndexManager indexManager;
|
||||
}
|
||||
// [END manhattan_distance]
|
||||
|
||||
// [START demands]
|
||||
static class DemandCallback extends LongToLong {
|
||||
public DemandCallback(DataModel data, RoutingIndexManager manager) {
|
||||
// precompute distance between location to have distance callback in O(1)
|
||||
demands_ = data.demands;
|
||||
indexManager_ = manager;
|
||||
demands = data.demands;
|
||||
indexManager = manager;
|
||||
}
|
||||
@Override
|
||||
public long run(long fromIndex) {
|
||||
int fromNode = indexManager_.indexToNode(fromIndex);
|
||||
return demands_[fromNode];
|
||||
// Convert from routing variable Index to demand NodeIndex.
|
||||
int fromNode = indexManager.indexToNode(fromIndex);
|
||||
return demands[fromNode];
|
||||
}
|
||||
|
||||
private final long[] demands_;
|
||||
private final RoutingIndexManager indexManager_;
|
||||
private final long[] demands;
|
||||
private final RoutingIndexManager indexManager;
|
||||
}
|
||||
// [END demands]
|
||||
|
||||
|
||||
@@ -131,6 +131,7 @@ public class VrpDropNodes {
|
||||
// [START arc_cost]
|
||||
int transitCallbackIndex = routing.RegisterTransitCallback(
|
||||
(long fromIndex, long toIndex) => {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
var fromNode = manager.IndexToNode(fromIndex);
|
||||
var toNode = manager.IndexToNode(toIndex);
|
||||
return data.GetDistanceMatrix()[fromNode, toNode]; }
|
||||
@@ -142,6 +143,7 @@ public class VrpDropNodes {
|
||||
// [START capacity_constraint]
|
||||
int demandCallbackIndex = routing.RegisterUnaryTransitCallback(
|
||||
(long fromIndex) => {
|
||||
// Convert from routing variable Index to demand NodeIndex.
|
||||
var fromNode = manager.IndexToNode(fromIndex);
|
||||
return data.GetDemands()[fromNode]; }
|
||||
);
|
||||
|
||||
@@ -74,35 +74,35 @@ public class VrpDropNodes {
|
||||
/// two different indices.
|
||||
static class ManhattanDistance extends LongLongToLong {
|
||||
public ManhattanDistance(DataModel data, RoutingIndexManager manager) {
|
||||
// precompute distance between location to have distance callback in O(1)
|
||||
distanceMatrix_ = data.distanceMatrix;
|
||||
indexManager_ = manager;
|
||||
distanceMatrix = data.distanceMatrix;
|
||||
indexManager = manager;
|
||||
}
|
||||
@Override
|
||||
public long run(long fromIndex, long toIndex) {
|
||||
int fromNode = indexManager_.indexToNode(fromIndex);
|
||||
int toNode = indexManager_.indexToNode(toIndex);
|
||||
return distanceMatrix_[fromNode][toNode];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
int fromNode = indexManager.indexToNode(fromIndex);
|
||||
int toNode = indexManager.indexToNode(toIndex);
|
||||
return distanceMatrix[fromNode][toNode];
|
||||
}
|
||||
private final long[][] distanceMatrix_;
|
||||
private final RoutingIndexManager indexManager_;
|
||||
private final long[][] distanceMatrix;
|
||||
private final RoutingIndexManager indexManager;
|
||||
}
|
||||
// [END manhattan_distance]
|
||||
|
||||
// [START demands]
|
||||
static class DemandCallback extends LongToLong {
|
||||
public DemandCallback(DataModel data, RoutingIndexManager manager) {
|
||||
// precompute distance between location to have distance callback in O(1)
|
||||
demands_ = data.demands;
|
||||
indexManager_ = manager;
|
||||
demands = data.demands;
|
||||
indexManager = manager;
|
||||
}
|
||||
@Override
|
||||
public long run(long fromIndex) {
|
||||
int fromNode = indexManager_.indexToNode(fromIndex);
|
||||
return demands_[fromNode];
|
||||
// Convert from routing variable Index to demands NodeIndex.
|
||||
int fromNode = indexManager.indexToNode(fromIndex);
|
||||
return demands[fromNode];
|
||||
}
|
||||
private final long[] demands_;
|
||||
private final RoutingIndexManager indexManager_;
|
||||
private final long[] demands;
|
||||
private final RoutingIndexManager indexManager;
|
||||
}
|
||||
// [END demands]
|
||||
|
||||
|
||||
@@ -108,6 +108,7 @@ public class VrpGlobalSpan {
|
||||
// [START arc_cost]
|
||||
int transitCallbackIndex = routing.RegisterTransitCallback(
|
||||
(long fromIndex, long toIndex) => {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
var fromNode = manager.IndexToNode(fromIndex);
|
||||
var toNode = manager.IndexToNode(toIndex);
|
||||
return data.GetDistanceMatrix()[fromNode, toNode]; }
|
||||
|
||||
@@ -70,18 +70,18 @@ public class VrpGlobalSpan {
|
||||
/// two different indices.
|
||||
static class ManhattanDistance extends LongLongToLong {
|
||||
public ManhattanDistance(DataModel data, RoutingIndexManager manager) {
|
||||
// precompute distance between location to have distance callback in O(1)
|
||||
distanceMatrix_ = data.distanceMatrix;
|
||||
indexManager_ = manager;
|
||||
distanceMatrix = data.distanceMatrix;
|
||||
indexManager = manager;
|
||||
}
|
||||
@Override
|
||||
public long run(long fromIndex, long toIndex) {
|
||||
int fromNode = indexManager_.indexToNode(fromIndex);
|
||||
int toNode = indexManager_.indexToNode(toIndex);
|
||||
return distanceMatrix_[fromNode][toNode];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
int fromNode = indexManager.indexToNode(fromIndex);
|
||||
int toNode = indexManager.indexToNode(toIndex);
|
||||
return distanceMatrix[fromNode][toNode];
|
||||
}
|
||||
private final long[][] distanceMatrix_;
|
||||
private final RoutingIndexManager indexManager_;
|
||||
private final long[][] distanceMatrix;
|
||||
private final RoutingIndexManager indexManager;
|
||||
}
|
||||
// [END manhattan_distance]
|
||||
|
||||
|
||||
@@ -114,6 +114,7 @@ public class VrpStartsEnds {
|
||||
// [START arc_cost]
|
||||
int transitCallbackIndex = routing.RegisterTransitCallback(
|
||||
(long fromIndex, long toIndex) => {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
var fromNode = manager.IndexToNode(fromIndex);
|
||||
var toNode = manager.IndexToNode(toIndex);
|
||||
return data.GetDistanceMatrix()[fromNode, toNode]; }
|
||||
|
||||
@@ -72,18 +72,18 @@ public class VrpStartsEnds {
|
||||
/// two different indices.
|
||||
static class ManhattanDistance extends LongLongToLong {
|
||||
public ManhattanDistance(DataModel data, RoutingIndexManager manager) {
|
||||
// precompute distance between location to have distance callback in O(1)
|
||||
distanceMatrix_ = data.distanceMatrix;
|
||||
indexManager_ = manager;
|
||||
distanceMatrix = data.distanceMatrix;
|
||||
indexManager = manager;
|
||||
}
|
||||
@Override
|
||||
public long run(long fromIndex, long toIndex) {
|
||||
int fromNode = indexManager_.indexToNode(fromIndex);
|
||||
int toNode = indexManager_.indexToNode(toIndex);
|
||||
return distanceMatrix_[fromNode][toNode];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
int fromNode = indexManager.indexToNode(fromIndex);
|
||||
int toNode = indexManager.indexToNode(toIndex);
|
||||
return distanceMatrix[fromNode][toNode];
|
||||
}
|
||||
private final long[][] distanceMatrix_;
|
||||
private final RoutingIndexManager indexManager_;
|
||||
private final long[][] distanceMatrix;
|
||||
private final RoutingIndexManager indexManager;
|
||||
}
|
||||
// [END manhattan_distance]
|
||||
|
||||
|
||||
@@ -141,6 +141,7 @@ public class VrpTimeWindows {
|
||||
// [START arc_cost]
|
||||
int transitCallbackIndex = routing.RegisterTransitCallback(
|
||||
(long fromIndex, long toIndex) => {
|
||||
// Convert from routing variable Index to time matrix NodeIndex.
|
||||
var fromNode = manager.IndexToNode(fromIndex);
|
||||
var toNode = manager.IndexToNode(toIndex);
|
||||
return data.GetTimeMatrix()[fromNode, toNode]; }
|
||||
|
||||
@@ -84,23 +84,23 @@ public class VrpTimeWindows {
|
||||
}
|
||||
// [END data_model]
|
||||
|
||||
// [START times]
|
||||
// [START time_callback]
|
||||
static class TimeCallback extends LongLongToLong {
|
||||
public TimeCallback(DataModel data, RoutingIndexManager manager) {
|
||||
// precompute distance between location to have distance callback in O(1)
|
||||
timeMatrix_ = data.timeMatrix;
|
||||
indexManager_ = manager;
|
||||
timeMatrix = data.timeMatrix;
|
||||
indexManager = manager;
|
||||
}
|
||||
@Override
|
||||
public long run(long fromIndex, long toIndex) {
|
||||
int fromNode = indexManager_.indexToNode(fromIndex);
|
||||
int toNode = indexManager_.indexToNode(toIndex);
|
||||
return timeMatrix_[fromNode][toNode];
|
||||
// Convert from routing variable Index to time matrix NodeIndex.
|
||||
int fromNode = indexManager.indexToNode(fromIndex);
|
||||
int toNode = indexManager.indexToNode(toIndex);
|
||||
return timeMatrix[fromNode][toNode];
|
||||
}
|
||||
private final long[][] timeMatrix_;
|
||||
private final RoutingIndexManager indexManager_;
|
||||
private final long[][] timeMatrix;
|
||||
private final RoutingIndexManager indexManager;
|
||||
}
|
||||
// [END times]
|
||||
// [END time_callback]
|
||||
|
||||
// [START solution_printer]
|
||||
/// @brief Print the solution.
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
// [START program]
|
||||
// [START import]
|
||||
#include <cmath>
|
||||
#include "ortools/constraint_solver/routing.h"
|
||||
#include "ortools/constraint_solver/routing_enums.pb.h"
|
||||
#include "ortools/constraint_solver/routing_index_manager.h"
|
||||
@@ -41,8 +42,14 @@ void SimpleRoutingProgram() {
|
||||
|
||||
// Define cost of each arc.
|
||||
// [START arc_cost]
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(
|
||||
routing.RegisterTransitCallback([](int64, int64) -> int64 { return 1; }));
|
||||
int distance_call_index = routing.RegisterTransitCallback(
|
||||
[&manager](int64 from_index, int64 to_index) -> int64 {
|
||||
// Convert from routing variable Index to user NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
return std::abs(to_node - from_node);
|
||||
});
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(distance_call_index);
|
||||
// [END arc_cost]
|
||||
|
||||
// Setting first solution heuristic.
|
||||
|
||||
@@ -43,8 +43,15 @@ def main():
|
||||
|
||||
# Define cost of each arc.
|
||||
# [START arc_cost]
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(
|
||||
routing.RegisterTransitCallback(lambda from_index, to_index: 1))
|
||||
def distance_callback(from_index, to_index):
|
||||
"""Returns the absolute difference between the two nodes."""
|
||||
# Convert from routing variable Index to user NodeIndex.
|
||||
from_node = int(manager.IndexToNode(from_index))
|
||||
to_node = int(manager.IndexToNode(to_index))
|
||||
return abs(to_node - from_node)
|
||||
|
||||
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
|
||||
# [END arc_cost]
|
||||
|
||||
# Setting first solution heuristic.
|
||||
|
||||
@@ -67,7 +67,7 @@ struct DataModel {
|
||||
std::vector<std::vector<int64>> GenerateManhattanDistanceMatrix(
|
||||
const DataModel& data) {
|
||||
std::vector<std::vector<int64>> distances = std::vector<std::vector<int64>>(
|
||||
data.num_locations, std::vector<int64>(data.num_locations, 0LL));
|
||||
data.num_locations, std::vector<int64>(data.num_locations, int64{0}));
|
||||
for (int fromNode = 0; fromNode < data.num_locations; fromNode++) {
|
||||
for (int toNode = 0; toNode < data.num_locations; toNode++) {
|
||||
if (fromNode != toNode)
|
||||
@@ -130,8 +130,10 @@ void Tsp() {
|
||||
const auto distance_matrix = GenerateManhattanDistanceMatrix(data);
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&distance_matrix, &manager](int64 from_index, int64 to_index) -> int64 {
|
||||
return distance_matrix[manager.IndexToNode(from_index).value()]
|
||||
[manager.IndexToNode(to_index).value()];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
return distance_matrix[from_node][to_node];
|
||||
});
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
|
||||
// [END arc_cost]
|
||||
|
||||
@@ -39,7 +39,6 @@ def create_data_model():
|
||||
(0, 8), (7, 8),]
|
||||
# Convert locations in meters using a city block dimension of 114m x 80m.
|
||||
data['locations'] = [(l[0] * 114, l[1] * 80) for l in locations]
|
||||
data['num_locations'] = len(data['locations'])
|
||||
data['num_vehicles'] = 1
|
||||
data['depot'] = 0
|
||||
return data
|
||||
@@ -64,6 +63,7 @@ def create_distance_callback(data, manager):
|
||||
|
||||
def distance_callback(from_index, to_index):
|
||||
"""Returns the manhattan distance between the two nodes."""
|
||||
# Convert from routing variable Index to distance matrix NodeIndex.
|
||||
from_node = index_manager_.IndexToNode(from_index)
|
||||
to_node = index_manager_.IndexToNode(to_index)
|
||||
return distances_[from_node][to_node]
|
||||
@@ -99,8 +99,8 @@ def main():
|
||||
|
||||
# Create the routing index manager.
|
||||
# [START index_manager]
|
||||
manager = pywrapcp.RoutingIndexManager(data['num_locations'],
|
||||
data['num_vehicles'], data['depot'])
|
||||
manager = pywrapcp.RoutingIndexManager(
|
||||
len(data['locations']), data['num_vehicles'], data['depot'])
|
||||
# [END index_manager]
|
||||
|
||||
# Create Routing Model.
|
||||
|
||||
@@ -117,8 +117,10 @@ void Tsp() {
|
||||
// [START arc_cost]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64 from_index, int64 to_index) -> int64 {
|
||||
return data.distance_matrix[manager.IndexToNode(from_index).value()]
|
||||
[manager.IndexToNode(to_index).value()];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
return data.distance_matrix[from_node][to_node];
|
||||
});
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
|
||||
// [END arc_cost]
|
||||
|
||||
@@ -95,7 +95,6 @@ def create_data_model():
|
||||
536, 194, 798, 0
|
||||
],
|
||||
]
|
||||
data['num_locations'] = len(data['distance_matrix'])
|
||||
data['num_vehicles'] = 1
|
||||
data['depot'] = 0
|
||||
return data
|
||||
@@ -129,8 +128,8 @@ def main():
|
||||
|
||||
# Create the routing index manager.
|
||||
# [START index_manager]
|
||||
manager = pywrapcp.RoutingIndexManager(data['num_locations'],
|
||||
data['num_vehicles'], data['depot'])
|
||||
manager = pywrapcp.RoutingIndexManager(
|
||||
len(data['distance_matrix']), data['num_vehicles'], data['depot'])
|
||||
# [END index_manager]
|
||||
|
||||
# Create Routing Model.
|
||||
@@ -143,6 +142,7 @@ def main():
|
||||
# [START arc_cost]
|
||||
def distance_callback(from_index, to_index):
|
||||
"""Returns the manhattan distance between the two nodes."""
|
||||
# Convert from routing variable Index to distance matrix NodeIndex.
|
||||
from_node = manager.IndexToNode(from_index)
|
||||
to_node = manager.IndexToNode(to_index)
|
||||
return data['distance_matrix'][from_node][to_node]
|
||||
|
||||
@@ -122,8 +122,10 @@ void Vrp() {
|
||||
// [START arc_cost]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64 from_index, int64 to_index) -> int64 {
|
||||
return data.distance_matrix[manager.IndexToNode(from_index).value()]
|
||||
[manager.IndexToNode(to_index).value()];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
return data.distance_matrix[from_node][to_node];
|
||||
});
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
|
||||
// [END arc_cost]
|
||||
|
||||
@@ -94,7 +94,6 @@ def create_data_model():
|
||||
536, 194, 798, 0
|
||||
],
|
||||
]
|
||||
data['num_locations'] = len(data['distance_matrix'])
|
||||
data['num_vehicles'] = 4
|
||||
data['depot'] = 0
|
||||
return data
|
||||
@@ -133,8 +132,8 @@ def main():
|
||||
|
||||
# Create the routing index manager.
|
||||
# [START index_manager]
|
||||
manager = pywrapcp.RoutingIndexManager(data['num_locations'],
|
||||
data['num_vehicles'], data['depot'])
|
||||
manager = pywrapcp.RoutingIndexManager(
|
||||
len(data['distance_matrix']), data['num_vehicles'], data['depot'])
|
||||
# [END index_manager]
|
||||
|
||||
# Create Routing Model.
|
||||
@@ -146,6 +145,7 @@ def main():
|
||||
# [START arc_cost]
|
||||
def distance_callback(from_index, to_index):
|
||||
"""Returns the manhattan distance between the two nodes."""
|
||||
# Convert from routing variable Index to distance matrix NodeIndex.
|
||||
from_node = manager.IndexToNode(from_index)
|
||||
to_node = manager.IndexToNode(to_index)
|
||||
return data['distance_matrix'][from_node][to_node]
|
||||
|
||||
@@ -133,8 +133,10 @@ void VrpCapacity() {
|
||||
// [START arc_cost]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64 from_index, int64 to_index) -> int64 {
|
||||
return data.distance_matrix[manager.IndexToNode(from_index).value()]
|
||||
[manager.IndexToNode(to_index).value()];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
return data.distance_matrix[from_node][to_node];
|
||||
});
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
|
||||
// [END arc_cost]
|
||||
@@ -143,7 +145,9 @@ void VrpCapacity() {
|
||||
// [START capacity_constraint]
|
||||
const int demand_callback_index = routing.RegisterUnaryTransitCallback(
|
||||
[&data, &manager](int64 from_index) -> int64 {
|
||||
return data.demands[manager.IndexToNode(from_index).value()];
|
||||
// Convert from routing variable Index to demand NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
return data.demands[from_node];
|
||||
});
|
||||
routing.AddDimensionWithVehicleCapacity(
|
||||
demand_callback_index, // transit callback index
|
||||
|
||||
@@ -94,7 +94,6 @@ def create_data_model():
|
||||
536, 194, 798, 0
|
||||
],
|
||||
]
|
||||
data['num_locations'] = len(data['distance_matrix'])
|
||||
data['demands'] = [0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8]
|
||||
data['num_vehicles'] = 4
|
||||
data['vehicle_capacities'] = [15, 15, 15, 15]
|
||||
@@ -143,8 +142,8 @@ def main():
|
||||
|
||||
# Create the routing index manager.
|
||||
# [START index_manager]
|
||||
manager = pywrapcp.RoutingIndexManager(data['num_locations'],
|
||||
data['num_vehicles'], data['depot'])
|
||||
manager = pywrapcp.RoutingIndexManager(
|
||||
len(data['distance_matrix']), data['num_vehicles'], data['depot'])
|
||||
# [END index_manager]
|
||||
|
||||
# Create Routing Model.
|
||||
@@ -157,6 +156,7 @@ def main():
|
||||
# [START arc_cost]
|
||||
def distance_callback(from_index, to_index):
|
||||
"""Returns the manhattan distance between the two nodes."""
|
||||
# Convert from routing variable Index to distance matrix NodeIndex.
|
||||
from_node = manager.IndexToNode(from_index)
|
||||
to_node = manager.IndexToNode(to_index)
|
||||
return data['distance_matrix'][from_node][to_node]
|
||||
@@ -169,7 +169,8 @@ def main():
|
||||
# Add Capacity constraint.
|
||||
# [START capacity_constraint]
|
||||
def demand_callback(from_index):
|
||||
"""Returns the demand.of the node."""
|
||||
"""Returns the demand of the node."""
|
||||
# Convert from routing variable Index to demands NodeIndex.
|
||||
from_node = manager.IndexToNode(from_index)
|
||||
return data['demands'][from_node]
|
||||
|
||||
|
||||
@@ -143,8 +143,10 @@ void VrpDropNodes() {
|
||||
// [START arc_cost]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64 from_index, int64 to_index) -> int64 {
|
||||
return data.distance_matrix[manager.IndexToNode(from_index).value()]
|
||||
[manager.IndexToNode(to_index).value()];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
return data.distance_matrix[from_node][to_node];
|
||||
});
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
|
||||
// [END arc_cost]
|
||||
@@ -153,7 +155,9 @@ void VrpDropNodes() {
|
||||
// [START capacity_constraint]
|
||||
const int demand_callback_index = routing.RegisterUnaryTransitCallback(
|
||||
[&data, &manager](int64 from_index) -> int64 {
|
||||
return data.demands[manager.IndexToNode(from_index).value()];
|
||||
// Convert from routing variable Index to demand NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
return data.demands[from_node];
|
||||
});
|
||||
routing.AddDimensionWithVehicleCapacity(
|
||||
demand_callback_index, // transit callback index
|
||||
|
||||
@@ -95,7 +95,6 @@ def create_data_model():
|
||||
536, 194, 798, 0
|
||||
],
|
||||
]
|
||||
data['num_locations'] = len(data['distance_matrix'])
|
||||
data['demands'] = [0, 1, 1, 3, 6, 3, 6, 8, 8, 1, 2, 1, 2, 6, 6, 8, 8]
|
||||
data['num_vehicles'] = 4
|
||||
data['vehicle_capacities'] = [15, 15, 15, 15]
|
||||
@@ -153,8 +152,8 @@ def main():
|
||||
|
||||
# Create the routing index manager.
|
||||
# [START index_manager]
|
||||
manager = pywrapcp.RoutingIndexManager(data['num_locations'],
|
||||
data['num_vehicles'], data['depot'])
|
||||
manager = pywrapcp.RoutingIndexManager(
|
||||
len(data['distance_matrix']), data['num_vehicles'], data['depot'])
|
||||
# [END index_manager]
|
||||
|
||||
# Create Routing Model.
|
||||
@@ -167,6 +166,7 @@ def main():
|
||||
# [START arc_cost]
|
||||
def distance_callback(from_index, to_index):
|
||||
"""Returns the manhattan distance between the two nodes."""
|
||||
# Convert from routing variable Index to distance matrix NodeIndex.
|
||||
from_node = manager.IndexToNode(from_index)
|
||||
to_node = manager.IndexToNode(to_index)
|
||||
return data['distance_matrix'][from_node][to_node]
|
||||
@@ -177,8 +177,14 @@ def main():
|
||||
|
||||
# Add Capacity constraint.
|
||||
# [START capacity_constraint]
|
||||
def demand_callback(from_index):
|
||||
"""Returns the demand of the node."""
|
||||
# Convert from routing variable Index to demands NodeIndex.
|
||||
from_node = manager.IndexToNode(from_index)
|
||||
return data['demands'][from_node]
|
||||
|
||||
demand_callback_index = routing.RegisterUnaryTransitCallback(
|
||||
(lambda from_index: data['demands'][manager.IndexToNode(from_index)]))
|
||||
demand_callback)
|
||||
routing.AddDimensionWithVehicleCapacity(
|
||||
demand_callback_index,
|
||||
0, # null capacity slack
|
||||
|
||||
@@ -122,8 +122,10 @@ void VrpGlobalSpan() {
|
||||
// [START arc_cost]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64 from_index, int64 to_index) -> int64 {
|
||||
return data.distance_matrix[manager.IndexToNode(from_index).value()]
|
||||
[manager.IndexToNode(to_index).value()];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
return data.distance_matrix[from_node][to_node];
|
||||
});
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
|
||||
// [END arc_cost]
|
||||
|
||||
@@ -95,7 +95,6 @@ def create_data_model():
|
||||
536, 194, 798, 0
|
||||
],
|
||||
]
|
||||
data['num_locations'] = len(data['distance_matrix'])
|
||||
data['num_vehicles'] = 4
|
||||
data['depot'] = 0
|
||||
return data
|
||||
@@ -134,8 +133,8 @@ def main():
|
||||
|
||||
# Create the routing index manager.
|
||||
# [START index_manager]
|
||||
manager = pywrapcp.RoutingIndexManager(data['num_locations'],
|
||||
data['num_vehicles'], data['depot'])
|
||||
manager = pywrapcp.RoutingIndexManager(
|
||||
len(data['distance_matrix']), data['num_vehicles'], data['depot'])
|
||||
# [END index_manager]
|
||||
|
||||
# Create Routing Model.
|
||||
@@ -148,6 +147,7 @@ def main():
|
||||
# [START arc_cost]
|
||||
def distance_callback(from_index, to_index):
|
||||
"""Returns the manhattan distance between the two nodes."""
|
||||
# Convert from routing variable Index to distance matrix NodeIndex.
|
||||
from_node = manager.IndexToNode(from_index)
|
||||
to_node = manager.IndexToNode(to_index)
|
||||
return data['distance_matrix'][from_node][to_node]
|
||||
|
||||
@@ -134,8 +134,10 @@ void VrpStartsEnds() {
|
||||
// [START arc_cost]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64 from_index, int64 to_index) -> int64 {
|
||||
return data.distance_matrix[manager.IndexToNode(from_index).value()]
|
||||
[manager.IndexToNode(to_index).value()];
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
return data.distance_matrix[from_node][to_node];
|
||||
});
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
|
||||
// [END arc_cost]
|
||||
|
||||
@@ -95,7 +95,6 @@ def create_data_model():
|
||||
536, 194, 798, 0
|
||||
],
|
||||
]
|
||||
data['num_locations'] = len(data['distance_matrix'])
|
||||
data['num_vehicles'] = 4
|
||||
data['starts'] = [1, 2, 15, 16]
|
||||
data['ends'] = [0, 0, 0, 0]
|
||||
@@ -135,9 +134,9 @@ def main():
|
||||
|
||||
# Create the routing index manager.
|
||||
# [START index_manager]
|
||||
manager = pywrapcp.RoutingIndexManager(data['num_locations'],
|
||||
data['num_vehicles'], data['starts'],
|
||||
data['ends'])
|
||||
manager = pywrapcp.RoutingIndexManager(
|
||||
len(data['distance_matrix']), data['num_vehicles'], data['starts'],
|
||||
data['ends'])
|
||||
# [END index_manager]
|
||||
|
||||
# Create Routing Model.
|
||||
@@ -150,6 +149,7 @@ def main():
|
||||
# [START arc_cost]
|
||||
def distance_callback(from_index, to_index):
|
||||
"""Returns the manhattan distance between the two nodes."""
|
||||
# Convert from routing variable Index to distance matrix NodeIndex.
|
||||
from_node = manager.IndexToNode(from_index)
|
||||
to_node = manager.IndexToNode(to_index)
|
||||
return data['distance_matrix'][from_node][to_node]
|
||||
|
||||
@@ -135,8 +135,10 @@ void VrpTimeWindows() {
|
||||
// [START arc_cost]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64 from_index, int64 to_index) -> int64 {
|
||||
return data.time_matrix[manager.IndexToNode(from_index).value()]
|
||||
[manager.IndexToNode(to_index).value()];
|
||||
// Convert from routing variable Index to time matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
return data.time_matrix[from_node][to_node];
|
||||
});
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
|
||||
// [END arc_cost]
|
||||
|
||||
@@ -125,6 +125,7 @@ def main():
|
||||
# [START arc_cost]
|
||||
def time_callback(from_index, to_index):
|
||||
"""Returns the manhattan distance travel time between the two nodes."""
|
||||
# Convert from routing variable Index to time matrix NodeIndex.
|
||||
from_node = manager.IndexToNode(from_index)
|
||||
to_node = manager.IndexToNode(to_index)
|
||||
return data['time_matrix'][from_node][to_node]
|
||||
|
||||
Reference in New Issue
Block a user