Routing Samples cleanup

This commit is contained in:
Corentin Le Molgat
2019-01-11 12:03:44 +01:00
parent 4336e4e993
commit 9271923b7b
35 changed files with 123 additions and 109 deletions

View File

@@ -140,8 +140,8 @@ public class Tsp {
LongLongToLong distanceEvaluator = new ManhattanDistance(data, manager);
//protect callbacks from the GC
GC.KeepAlive(distanceEvaluator);
int transitCostIndex = routing.RegisterTransitCallback(distanceEvaluator);
routing.SetArcCostEvaluatorOfAllVehicles(transitCostIndex);
int transitCallbackIndex = routing.RegisterTransitCallback(distanceEvaluator);
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Setting first solution heuristic.

View File

@@ -146,8 +146,8 @@ public class Tsp {
// Define cost of each arc.
// [START arc_cost]
LongLongToLong distanceEvaluator = new ManhattanDistance(data, manager);
int transitCostIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCostIndex);
int transitCallbackIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Setting first solution heuristic.

View File

@@ -99,13 +99,13 @@ public class TspDistanceMatrix {
// Define cost of each arc.
// [START arc_cost]
int transitCostIndex = routing.RegisterTransitCallback(
int transitCallbackIndex = routing.RegisterTransitCallback(
(long fromIndex, long toIndex) => {
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return data.GetDistanceMatrix()[fromNode, toNode]; }
);
routing.SetArcCostEvaluatorOfAllVehicles(transitCostIndex);
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Setting first solution heuristic.

View File

@@ -127,8 +127,8 @@ public class TspDistanceMatrix {
// Define cost of each arc.
// [START arc_cost]
LongLongToLong distanceEvaluator = new ManhattanDistance(data, manager);
int transitCostIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCostIndex);
int transitCallbackIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Setting first solution heuristic.

View File

@@ -105,13 +105,13 @@ public class Vrp {
// Define cost of each arc.
// [START arc_cost]
int transitCostIndex = routing.RegisterTransitCallback(
int transitCallbackIndex = routing.RegisterTransitCallback(
(long fromIndex, long toIndex) => {
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return data.GetDistanceMatrix()[fromNode, toNode]; }
);
routing.SetArcCostEvaluatorOfAllVehicles(transitCostIndex);
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Setting first solution heuristic.

View File

@@ -132,8 +132,8 @@ public class Vrp {
// Define cost of each arc.
// [START arc_cost]
LongLongToLong distanceEvaluator = new ManhattanDistance(data, manager);
int transitCostIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCostIndex);
int transitCallbackIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Setting first solution heuristic.

View File

@@ -117,24 +117,24 @@ public class VrpCapacity {
// Define cost of each arc.
// [START arc_cost]
int transitCostIndex = routing.RegisterTransitCallback(
int transitCallbackIndex = routing.RegisterTransitCallback(
(long fromIndex, long toIndex) => {
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return data.GetDistanceMatrix()[fromNode, toNode]; }
);
routing.SetArcCostEvaluatorOfAllVehicles(transitCostIndex);
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Add Capacity constraint.
// [START capacity_constraint]
int demandCostIndex = routing.RegisterUnaryTransitCallback(
int demandCallbackIndex = routing.RegisterUnaryTransitCallback(
(long fromIndex) => {
var fromNode = manager.IndexToNode(fromIndex);
return data.GetDemands()[fromNode]; }
);
routing.AddDimensionWithVehicleCapacity(
demandCostIndex, 0, // null capacity slack
demandCallbackIndex, 0, // null capacity slack
data.GetVehicleCapacities(), // vehicle maximum capacities
true, // start cumul to zero
"Capacity");

View File

@@ -161,15 +161,15 @@ public class VrpCapacity {
// Define cost of each arc.
// [START arc_cost]
LongLongToLong distanceEvaluator = new ManhattanDistance(data, manager);
int transitCostIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCostIndex);
int transitCallbackIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Add Capacity constraint.
// [START capacity_constraint]
LongToLong demandEvaluator = new DemandCallback(data, manager);
int demandCostIndex = routing.registerUnaryTransitCallback(demandEvaluator);
routing.addDimensionWithVehicleCapacity(demandCostIndex, 0, // null capacity slack
int demandCallbackIndex = routing.registerUnaryTransitCallback(demandEvaluator);
routing.addDimensionWithVehicleCapacity(demandCallbackIndex, 0, // null capacity slack
data.vehicleCapacities, // vehicle maximum capacities
true, // start cumul to zero
"Capacity");

View File

@@ -129,24 +129,24 @@ public class VrpDropNodes {
// Define cost of each arc.
// [START arc_cost]
int transitCostIndex = routing.RegisterTransitCallback(
int transitCallbackIndex = routing.RegisterTransitCallback(
(long fromIndex, long toIndex) => {
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return data.GetDistanceMatrix()[fromNode, toNode]; }
);
routing.SetArcCostEvaluatorOfAllVehicles(transitCostIndex);
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Add Capacity constraint.
// [START capacity_constraint]
int demandCostIndex = routing.RegisterUnaryTransitCallback(
int demandCallbackIndex = routing.RegisterUnaryTransitCallback(
(long fromIndex) => {
var fromNode = manager.IndexToNode(fromIndex);
return data.GetDemands()[fromNode]; }
);
routing.AddDimensionWithVehicleCapacity(
demandCostIndex, 0, // null capacity slack
demandCallbackIndex, 0, // null capacity slack
data.GetVehicleCapacities(), // vehicle maximum capacities
true, // start cumul to zero
"Capacity");

View File

@@ -171,15 +171,15 @@ public class VrpDropNodes {
// Define cost of each arc.
// [START arc_cost]
LongLongToLong distanceEvaluator = new ManhattanDistance(data, manager);
int transitCostIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCostIndex);
int transitCallbackIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Add Capacity constraint.
// [START capacity_constraint]
LongToLong demandEvaluator = new DemandCallback(data, manager);
int demandCostIndex = routing.registerUnaryTransitCallback(demandEvaluator);
routing.addDimensionWithVehicleCapacity(demandCostIndex, 0, // null capacity slack
int demandCallbackIndex = routing.registerUnaryTransitCallback(demandEvaluator);
routing.addDimensionWithVehicleCapacity(demandCallbackIndex, 0, // null capacity slack
data.vehicleCapacities, // vehicle maximum capacities
true, // start cumul to zero
"Capacity");

View File

@@ -106,18 +106,18 @@ public class VrpGlobalSpan {
// Define cost of each arc.
// [START arc_cost]
int transitCostIndex = routing.RegisterTransitCallback(
int transitCallbackIndex = routing.RegisterTransitCallback(
(long fromIndex, long toIndex) => {
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return data.GetDistanceMatrix()[fromNode, toNode]; }
);
routing.SetArcCostEvaluatorOfAllVehicles(transitCostIndex);
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Add Distance constraint.
// [START distance_constraint]
routing.AddDimension(transitCostIndex, 0, 3000,
routing.AddDimension(transitCallbackIndex, 0, 3000,
true, // start cumul to zero
"Distance");
RoutingDimension distanceDimension = routing.GetMutableDimension("Distance");

View File

@@ -132,13 +132,13 @@ public class VrpGlobalSpan {
// Define cost of each arc.
// [START arc_cost]
LongLongToLong distanceEvaluator = new ManhattanDistance(data, manager);
int transitCostIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCostIndex);
int transitCallbackIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Add Distance constraint.
// [START distance_constraint]
routing.addDimension(transitCostIndex, 0, 3000,
routing.addDimension(transitCallbackIndex, 0, 3000,
true, // start cumul to zero
"Distance");
RoutingDimension distanceDimension = routing.getMutableDimension("Distance");

View File

@@ -112,18 +112,18 @@ public class VrpStartsEnds {
// Define cost of each arc.
// [START arc_cost]
int transitCostIndex = routing.RegisterTransitCallback(
int transitCallbackIndex = routing.RegisterTransitCallback(
(long fromIndex, long toIndex) => {
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return data.GetDistanceMatrix()[fromNode, toNode]; }
);
routing.SetArcCostEvaluatorOfAllVehicles(transitCostIndex);
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Add Distance constraint.
// [START distance_constraint]
routing.AddDimension(transitCostIndex, 0, 2000,
routing.AddDimension(transitCallbackIndex, 0, 2000,
true, // start cumul to zero
"Distance");
RoutingDimension distanceDimension = routing.GetMutableDimension("Distance");

View File

@@ -135,13 +135,13 @@ public class VrpStartsEnds {
// Define cost of each arc.
// [START arc_cost]
LongLongToLong distanceEvaluator = new ManhattanDistance(data, manager);
int transitCostIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCostIndex);
int transitCallbackIndex = routing.registerTransitCallback(distanceEvaluator);
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Add Distance constraint.
// [START distance_constraint]
routing.addDimension(transitCostIndex, 0, 2000,
routing.addDimension(transitCallbackIndex, 0, 2000,
true, // start cumul to zero
"Distance");
RoutingDimension distanceDimension = routing.getMutableDimension("Distance");

View File

@@ -139,19 +139,19 @@ public class VrpTimeWindows {
// Define cost of each arc.
// [START arc_cost]
int transitCostIndex = routing.RegisterTransitCallback(
int transitCallbackIndex = routing.RegisterTransitCallback(
(long fromIndex, long toIndex) => {
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return data.GetTimeMatrix()[fromNode, toNode]; }
);
routing.SetArcCostEvaluatorOfAllVehicles(transitCostIndex);
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Add Distance constraint.
// [START time_constraint]
routing.AddDimension(
transitCostIndex, // transit callback
transitCallbackIndex, // transit callback
30, // allow waiting time
30, // vehicle maximum capacities
false, // start cumul to zero

View File

@@ -155,13 +155,13 @@ public class VrpTimeWindows {
// Define cost of each arc.
// [START arc_cost]
LongLongToLong timeCallback = new TimeCallback(data, manager);
int transitCostIndex = routing.registerTransitCallback(timeCallback);
routing.setArcCostEvaluatorOfAllVehicles(transitCostIndex);
int transitCallbackIndex = routing.registerTransitCallback(timeCallback);
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// [END arc_cost]
// Add Time constraint.
// [START time_constraint]
routing.addDimension(transitCostIndex, // transit callback
routing.addDimension(transitCallbackIndex, // transit callback
30, // allow waiting time
30, // vehicle maximum capacities
false, // start cumul to zero

View File

@@ -95,10 +95,10 @@ def create_data_model():
(0, 1000),
(75, 8500), (75, 8500), # 1, 2
(60, 7000), (45, 5500), # 3, 4
(0, 8000), (50, 6000), # 5, 6
(0, 1000), (10, 2000), # 7, 8
(0, 1000), (75, 8500), # 9, 10
(85, 9500), (5, 1500), # 11, 12
(0, 8000), (50, 6000), # 5, 6
(0, 1000), (10, 2000), # 7, 8
(0, 1000), (75, 8500), # 9, 10
(85, 9500), (5, 1500), # 11, 12
(15, 2500), (10, 2000), # 13, 14
(45, 5500), (30, 4000)] # 15, 16
data['num_vehicles'] = 3
@@ -114,8 +114,8 @@ def create_data_model():
#######################
def manhattan_distance(position_1, position_2):
"""Computes the Manhattan distance between two points"""
return (
abs(position_1[0] - position_2[0]) + abs(position_1[1] - position_2[1]))
return (abs(position_1[0] - position_2[0]) +
abs(position_1[1] - position_2[1]))
def create_distance_evaluator(data):
@@ -198,8 +198,8 @@ def create_time_evaluator(data):
if from_node == to_node:
travel_time = 0
else:
travel_time = manhattan_distance(data['locations'][from_node], data[
'locations'][to_node]) / data['vehicle_speed']
travel_time = manhattan_distance(data['locations'][
from_node], data['locations'][to_node]) / data['vehicle_speed']
return travel_time
_total_time = {}

View File

@@ -17,6 +17,7 @@
from __future__ import print_function
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import pywrapcp
# [END import]

View File

@@ -128,12 +128,12 @@ void Tsp() {
// Define cost of each arc.
// [START arc_cost]
const auto distance_matrix = GenerateManhattanDistanceMatrix(data);
const int transit_cost_id = routing.RegisterTransitCallback(
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()];
});
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id);
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
// [END arc_cost]
// Setting first solution heuristic.

View File

@@ -111,8 +111,8 @@ def main():
# Define cost of each arc.
# [START arc_cost]
distance_callback = create_distance_callback(data, manager)
transit_cost_id = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id)
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# [END arc_cost]
# Setting first solution heuristic.
@@ -136,3 +136,4 @@ def main():
if __name__ == '__main__':
main()
# [END program]

View File

@@ -115,12 +115,12 @@ void Tsp() {
// Define cost of each arc.
// [START arc_cost]
const int transit_cost_id = routing.RegisterTransitCallback(
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()];
});
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id);
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
// [END arc_cost]
// Setting first solution heuristic.

View File

@@ -142,12 +142,13 @@ def main():
# Define cost of each arc.
# [START arc_cost]
def distance_callback(from_index, to_index):
"""Returns the manhattan distance between the two nodes."""
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
transit_cost_id = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id)
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# [END arc_cost]
# Setting first solution heuristic.

View File

@@ -120,12 +120,12 @@ void Vrp() {
// Define cost of each arc.
// [START arc_cost]
const int transit_cost_id = routing.RegisterTransitCallback(
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()];
});
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id);
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
// [END arc_cost]
// Setting first solution heuristic.

View File

@@ -146,12 +146,13 @@ def main():
# Define cost of each arc.
# [START arc_cost]
def distance_callback(from_index, to_index):
"""Returns the manhattan distance between the two nodes."""
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
transit_cost_id = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id)
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# [END arc_cost]
# Setting first solution heuristic.

View File

@@ -131,24 +131,25 @@ void VrpCapacity() {
// Define cost of each arc.
// [START arc_cost]
const int transit_cost_id = routing.RegisterTransitCallback(
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()];
});
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id);
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
// [END arc_cost]
// Add Capacity constraint.
// [START capacity_constraint]
const int demand_cost_id = routing.RegisterUnaryTransitCallback(
const int demand_callback_index = routing.RegisterUnaryTransitCallback(
[&data, &manager](int64 from_index) -> int64 {
return data.demands[manager.IndexToNode(from_index).value()];
});
routing.AddDimensionWithVehicleCapacity(
demand_cost_id, int64{0}, // null capacity slack
data.vehicle_capacities, // vehicle maximum capacities
true, // start cumul to zero
demand_callback_index, // transit callback index
int64{0}, // null capacity slack
data.vehicle_capacities, // vehicle maximum capacities
true, // start cumul to zero
"Capacity");
// [END capacity_constraint]

View File

@@ -157,24 +157,27 @@ def main():
# Define cost of each arc.
# [START arc_cost]
def distance_callback(from_index, to_index):
"""Returns the manhattan distance between the two nodes."""
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
transit_cost_id = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id)
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# [END arc_cost]
# Add Capacity constraint.
# [START capacity_constraint]
def demand_callback(from_index):
"""Returns the demand.of the node."""
from_node = manager.IndexToNode(from_index)
return data['demands'][from_node]
demand_cost_id = routing.RegisterUnaryTransitCallback(demand_callback)
demand_callback_index = routing.RegisterUnaryTransitCallback(
demand_callback)
routing.AddDimensionWithVehicleCapacity(
demand_cost_id,
demand_callback_index,
0, # null capacity slack
data['vehicle_capacities'], # vehicle maximum capacities
True, # start cumul to zero

View File

@@ -141,24 +141,25 @@ void VrpDropNodes() {
// Define cost of each arc.
// [START arc_cost]
const int transit_cost_id = routing.RegisterTransitCallback(
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()];
});
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id);
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
// [END arc_cost]
// Add Capacity constraint.
// [START capacity_constraint]
const int demand_cost_id = routing.RegisterUnaryTransitCallback(
const int demand_callback_index = routing.RegisterUnaryTransitCallback(
[&data, &manager](int64 from_index) -> int64 {
return data.demands[manager.IndexToNode(from_index).value()];
});
routing.AddDimensionWithVehicleCapacity(
demand_cost_id, int64{0}, // null capacity slack
data.vehicle_capacities, // vehicle maximum capacities
true, // start cumul to zero
demand_callback_index, // transit callback index
int64{0}, // null capacity slack
data.vehicle_capacities, // vehicle maximum capacities
true, // start cumul to zero
"Capacity");
// Allow to drop nodes.
int64 penalty{1000};

View File

@@ -111,7 +111,8 @@ def print_solution(data, manager, routing, assignment):
# Display dropped nodes.
dropped_nodes = 'Dropped nodes:'
for node in range(routing.Size()):
if routing.IsStart(node) or routing.IsEnd(node): continue
if routing.IsStart(node) or routing.IsEnd(node):
continue
if assignment.Value(routing.NextVar(node)) == node:
dropped_nodes += ' {}'.format(manager.IndexToNode(node))
print(dropped_nodes)
@@ -165,20 +166,21 @@ def main():
# Define cost of each arc.
# [START arc_cost]
def distance_callback(from_index, to_index):
"""Returns the manhattan distance between the two nodes."""
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
transit_cost_id = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id)
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# [END arc_cost]
# Add Capacity constraint.
# [START capacity_constraint]
demand_cost_id = routing.RegisterUnaryTransitCallback(
demand_callback_index = routing.RegisterUnaryTransitCallback(
(lambda from_index: data['demands'][manager.IndexToNode(from_index)]))
routing.AddDimensionWithVehicleCapacity(
demand_cost_id,
demand_callback_index,
0, # null capacity slack
data['vehicle_capacities'], # vehicle maximum capacities
True, # start cumul to zero

View File

@@ -120,17 +120,17 @@ void VrpGlobalSpan() {
// Define cost of each arc.
// [START arc_cost]
const int transit_cost_id = routing.RegisterTransitCallback(
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()];
});
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id);
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
// [END arc_cost]
// Add Distance constraint.
// [START distance_constraint]
routing.AddDimension(transit_cost_id, 0, 3000,
routing.AddDimension(transit_callback_index, 0, 3000,
true, // start cumul to zero
"Distance");
const RoutingDimension& distance_dimension =

View File

@@ -147,19 +147,20 @@ def main():
# Define cost of each arc.
# [START arc_cost]
def distance_callback(from_index, to_index):
"""Returns the manhattan distance between the two nodes."""
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
transit_cost_id = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id)
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# [END arc_cost]
# Add Distance constraint.
# [START distance_constraint]
dimension_name = 'Distance'
routing.AddDimension(
transit_cost_id,
transit_callback_index,
0, # no slack
3000, # vehicle maximum travel distance
True, # start cumul to zero

View File

@@ -15,7 +15,6 @@
# [START import]
from __future__ import print_function
from collections import namedtuple
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
# [END import]
@@ -158,6 +157,7 @@ def main():
# Define cost of each arc.
# [START arc_cost]
def distance_callback(from_index, to_index):
"""Returns the manhattan distance between the two nodes."""
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]

View File

@@ -132,17 +132,17 @@ void VrpStartsEnds() {
// Define cost of each arc.
// [START arc_cost]
const int transit_cost_id = routing.RegisterTransitCallback(
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()];
});
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id);
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
// [END arc_cost]
// Add Distance constraint.
// [START distance_constraint]
routing.AddDimension(transit_cost_id, 0, 2000,
routing.AddDimension(transit_callback_index, 0, 2000,
/*fix_start_cumul_to_zero=*/true, "Distance");
const RoutingDimension& distance_dimension =
routing.GetDimensionOrDie("Distance");

View File

@@ -149,19 +149,20 @@ def main():
# Define cost of each arc.
# [START arc_cost]
def distance_callback(from_index, to_index):
"""Returns the manhattan distance between the two nodes."""
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
transit_cost_id = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id)
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# [END arc_cost]
# Add Distance constraint.
# [START distance_constraint]
dimension_name = 'Distance'
routing.AddDimension(
transit_cost_id,
transit_callback_index,
0, # no slack
2000, # vehicle maximum travel distance
True, # start cumul to zero

View File

@@ -133,21 +133,21 @@ void VrpTimeWindows() {
// Define cost of each arc.
// [START arc_cost]
const int transit_cost_id = routing.RegisterTransitCallback(
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()];
});
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id);
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
// [END arc_cost]
// Add Time constraint.
// [START time_constraint]
std::string time{"Time"};
routing.AddDimension(transit_cost_id, // transit callback index
int64{30}, // allow waiting time
int64{30}, // maximum time per vehicle
false, // Don't force start cumul to zero
routing.AddDimension(transit_callback_index, // transit callback index
int64{30}, // allow waiting time
int64{30}, // maximum time per vehicle
false, // Don't force start cumul to zero
time);
const RoutingDimension& time_dimension = routing.GetDimensionOrDie(time);
// Add time window constraints for each location except depot

View File

@@ -124,19 +124,20 @@ def main():
# Define cost of each arc.
# [START arc_cost]
def time_callback(from_index, to_index):
"""Returns the manhattan distance travel time between the two nodes."""
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['time_matrix'][from_node][to_node]
transit_cost_id = routing.RegisterTransitCallback(time_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_cost_id)
transit_callback_index = routing.RegisterTransitCallback(time_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# [END arc_cost]
# Add Time Windows constraint.
# [START time_windows_constraint]
time = 'Time'
routing.AddDimension(
transit_cost_id,
transit_callback_index,
30, # allow waiting time
30, # maximum time per vehicle
False, # Don't force start cumul to zero.