2022-01-14 17:15:11 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-09-26 10:51:44 +02:00
|
|
|
# This Python file uses the following encoding: utf-8
|
|
|
|
|
# Copyright 2015 Tin Arm Engineering AB
|
|
|
|
|
# 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.
|
|
|
|
|
"""Capacitated Vehicle Routing Problem (CVRP).
|
|
|
|
|
|
2025-01-30 14:28:07 +01:00
|
|
|
This is a sample using the routing library python wrapper to solve a CVRP
|
|
|
|
|
problem while allowing multiple trips, i.e., vehicles can return to a depot
|
|
|
|
|
to reset their load ("reload").
|
2021-03-24 11:27:02 +11:00
|
|
|
|
2025-01-30 14:28:07 +01:00
|
|
|
A description of the CVRP problem can be found here:
|
|
|
|
|
http://en.wikipedia.org/wiki/Vehicle_routing_problem.
|
2018-09-26 10:51:44 +02:00
|
|
|
|
2025-01-30 14:28:07 +01:00
|
|
|
Distances are in meters.
|
2021-03-24 11:27:02 +11:00
|
|
|
|
2025-01-30 14:28:07 +01:00
|
|
|
In order to implement multiple trips, new nodes are introduced at the same
|
|
|
|
|
locations of the original depots. These additional nodes can be dropped
|
|
|
|
|
from the schedule at 0 cost.
|
2021-03-24 11:27:02 +11:00
|
|
|
|
2025-01-30 14:28:07 +01:00
|
|
|
The max_slack parameter associated to the capacity constraints of all nodes
|
|
|
|
|
can be set to be the maximum of the vehicles' capacities, rather than 0 like
|
|
|
|
|
in a traditional CVRP. Slack is required since before a solution is found,
|
|
|
|
|
it is not known how much capacity will be transferred at the new nodes. For
|
|
|
|
|
all the other (original) nodes, the slack is then re-set to 0.
|
2021-03-24 11:27:02 +11:00
|
|
|
|
2025-01-30 14:28:07 +01:00
|
|
|
The above two considerations are implemented in `add_capacity_constraints()`.
|
2021-03-24 11:27:02 +11:00
|
|
|
|
2025-01-30 14:28:07 +01:00
|
|
|
Last, it is useful to set a large distance between the initial depot and the
|
|
|
|
|
new nodes introduced, to avoid schedules having spurious transits through
|
|
|
|
|
those new nodes unless it's necessary to reload. This consideration is taken
|
|
|
|
|
into account in `create_distance_evaluator()`.
|
2018-09-26 10:51:44 +02:00
|
|
|
"""
|
|
|
|
|
|
2018-10-31 16:18:18 +01:00
|
|
|
from functools import partial
|
|
|
|
|
|
2018-09-26 10:51:44 +02:00
|
|
|
from ortools.constraint_solver import pywrapcp
|
|
|
|
|
from ortools.constraint_solver import routing_enums_pb2
|
|
|
|
|
|
2018-11-11 09:39:59 +01:00
|
|
|
|
2018-09-26 10:51:44 +02:00
|
|
|
###########################
|
|
|
|
|
# Problem Data Definition #
|
|
|
|
|
###########################
|
|
|
|
|
def create_data_model():
|
2018-11-28 10:37:45 +01:00
|
|
|
"""Stores the data for the problem"""
|
|
|
|
|
data = {}
|
|
|
|
|
_capacity = 15
|
|
|
|
|
# Locations in block unit
|
|
|
|
|
_locations = [
|
|
|
|
|
(4, 4), # depot
|
2021-03-24 11:27:02 +11:00
|
|
|
(4, 4), # unload depot_first
|
2018-11-28 10:37:45 +01:00
|
|
|
(4, 4), # unload depot_second
|
2021-03-24 11:27:02 +11:00
|
|
|
(4, 4), # unload depot_third
|
2018-11-28 10:37:45 +01:00
|
|
|
(4, 4), # unload depot_fourth
|
|
|
|
|
(4, 4), # unload depot_fifth
|
|
|
|
|
(2, 0),
|
|
|
|
|
(8, 0), # locations to visit
|
|
|
|
|
(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),
|
2025-01-29 13:25:44 +01:00
|
|
|
(7, 8),
|
2018-11-28 10:37:45 +01:00
|
|
|
]
|
|
|
|
|
# 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'
|
2025-01-29 13:25:44 +01:00
|
|
|
data["locations"] = [(l[0] * 114, l[1] * 80) for l in _locations]
|
|
|
|
|
data["num_locations"] = len(data["locations"])
|
|
|
|
|
data["demands"] = [
|
|
|
|
|
0, # depot
|
|
|
|
|
-_capacity, # unload depot_first
|
|
|
|
|
-_capacity, # unload depot_second
|
|
|
|
|
-_capacity, # unload depot_third
|
|
|
|
|
-_capacity, # unload depot_fourth
|
|
|
|
|
-_capacity, # unload depot_fifth
|
|
|
|
|
3,
|
|
|
|
|
3, # 1, 2
|
|
|
|
|
3,
|
|
|
|
|
4, # 3, 4
|
|
|
|
|
3,
|
|
|
|
|
4, # 5, 6
|
|
|
|
|
8,
|
|
|
|
|
8, # 7, 8
|
|
|
|
|
3,
|
|
|
|
|
3, # 9,10
|
|
|
|
|
3,
|
|
|
|
|
3, # 11,12
|
|
|
|
|
4,
|
|
|
|
|
4, # 13, 14
|
|
|
|
|
8,
|
|
|
|
|
8,
|
|
|
|
|
] # 15, 16
|
|
|
|
|
data["time_per_demand_unit"] = 5 # 5 minutes/unit
|
|
|
|
|
data["time_windows"] = [
|
|
|
|
|
(0, 0), # depot
|
|
|
|
|
(0, 1000), # unload depot_first
|
|
|
|
|
(0, 1000), # unload depot_second
|
|
|
|
|
(0, 1000), # unload depot_third
|
|
|
|
|
(0, 1000), # unload depot_fourth
|
|
|
|
|
(0, 1000), # unload depot_fifth
|
|
|
|
|
(75, 850),
|
|
|
|
|
(75, 850), # 1, 2
|
|
|
|
|
(60, 700),
|
|
|
|
|
(45, 550), # 3, 4
|
|
|
|
|
(0, 800),
|
|
|
|
|
(50, 600), # 5, 6
|
|
|
|
|
(0, 1000),
|
|
|
|
|
(10, 200), # 7, 8
|
|
|
|
|
(0, 1000),
|
|
|
|
|
(75, 850), # 9, 10
|
|
|
|
|
(85, 950),
|
|
|
|
|
(5, 150), # 11, 12
|
|
|
|
|
(15, 250),
|
|
|
|
|
(10, 200), # 13, 14
|
|
|
|
|
(45, 550),
|
|
|
|
|
(30, 400),
|
|
|
|
|
] # 15, 16
|
|
|
|
|
data["num_vehicles"] = 3
|
|
|
|
|
data["vehicle_capacity"] = _capacity
|
|
|
|
|
data["vehicle_max_distance"] = 10_000
|
|
|
|
|
data["vehicle_max_time"] = 1_500
|
|
|
|
|
data["vehicle_speed"] = 5 * 60 / 3.6 # Travel speed: 5km/h to convert in m/min
|
|
|
|
|
data["depot"] = 0
|
2018-11-28 10:37:45 +01:00
|
|
|
return data
|
2018-11-11 09:39:59 +01:00
|
|
|
|
2018-09-26 10:51:44 +02:00
|
|
|
|
|
|
|
|
#######################
|
|
|
|
|
# Problem Constraints #
|
|
|
|
|
#######################
|
|
|
|
|
def manhattan_distance(position_1, position_2):
|
2018-11-28 10:37:45 +01:00
|
|
|
"""Computes the Manhattan distance between two points"""
|
2025-01-29 13:25:44 +01:00
|
|
|
return abs(position_1[0] - position_2[0]) + abs(position_1[1] - position_2[1])
|
2018-11-11 09:39:59 +01:00
|
|
|
|
2018-09-26 10:51:44 +02:00
|
|
|
|
|
|
|
|
def create_distance_evaluator(data):
|
2018-11-28 10:37:45 +01:00
|
|
|
"""Creates callback to return distance between points."""
|
|
|
|
|
_distances = {}
|
|
|
|
|
# precompute distance between location to have distance callback in O(1)
|
2025-01-29 13:25:44 +01:00
|
|
|
for from_node in range(data["num_locations"]):
|
2018-11-28 10:37:45 +01:00
|
|
|
_distances[from_node] = {}
|
2025-01-29 13:25:44 +01:00
|
|
|
for to_node in range(data["num_locations"]):
|
2018-11-28 10:37:45 +01:00
|
|
|
if from_node == to_node:
|
|
|
|
|
_distances[from_node][to_node] = 0
|
2020-12-03 00:46:02 +01:00
|
|
|
# Forbid start/end/reload node to be consecutive.
|
2020-12-03 00:52:23 +01:00
|
|
|
elif from_node in range(6) and to_node in range(6):
|
2025-01-29 13:25:44 +01:00
|
|
|
_distances[from_node][to_node] = data["vehicle_max_distance"]
|
2018-11-28 10:37:45 +01:00
|
|
|
else:
|
2025-01-29 13:25:44 +01:00
|
|
|
_distances[from_node][to_node] = manhattan_distance(
|
|
|
|
|
data["locations"][from_node], data["locations"][to_node]
|
|
|
|
|
)
|
2018-11-28 10:37:45 +01:00
|
|
|
|
|
|
|
|
def distance_evaluator(manager, from_node, to_node):
|
|
|
|
|
"""Returns the manhattan distance between the two nodes"""
|
2025-01-29 13:25:44 +01:00
|
|
|
return _distances[manager.IndexToNode(from_node)][manager.IndexToNode(to_node)]
|
2018-11-28 10:37:45 +01:00
|
|
|
|
|
|
|
|
return distance_evaluator
|
2018-10-31 16:18:18 +01:00
|
|
|
|
|
|
|
|
|
2020-12-03 00:46:02 +01:00
|
|
|
def add_distance_dimension(routing, manager, data, distance_evaluator_index):
|
2018-11-28 10:37:45 +01:00
|
|
|
"""Add Global Span constraint"""
|
2023-04-13 11:49:14 +02:00
|
|
|
del manager
|
2025-01-29 13:25:44 +01:00
|
|
|
distance = "Distance"
|
2018-11-28 10:37:45 +01:00
|
|
|
routing.AddDimension(
|
|
|
|
|
distance_evaluator_index,
|
|
|
|
|
0, # null slack
|
2025-01-29 13:25:44 +01:00
|
|
|
data["vehicle_max_distance"], # maximum distance per vehicle
|
2018-11-28 10:37:45 +01:00
|
|
|
True, # start cumul to zero
|
2025-01-29 13:25:44 +01:00
|
|
|
distance,
|
|
|
|
|
)
|
2018-11-28 10:37:45 +01:00
|
|
|
distance_dimension = routing.GetDimensionOrDie(distance)
|
|
|
|
|
# Try to minimize the max distance among vehicles.
|
|
|
|
|
# /!\ It doesn't mean the standard deviation is minimized
|
|
|
|
|
distance_dimension.SetGlobalSpanCostCoefficient(100)
|
2018-11-11 09:39:59 +01:00
|
|
|
|
2018-09-26 10:51:44 +02:00
|
|
|
|
|
|
|
|
def create_demand_evaluator(data):
|
2018-11-28 10:37:45 +01:00
|
|
|
"""Creates callback to get demands at each location."""
|
2025-01-29 13:25:44 +01:00
|
|
|
_demands = data["demands"]
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2018-11-28 10:37:45 +01:00
|
|
|
def demand_evaluator(manager, from_node):
|
|
|
|
|
"""Returns the demand of the current node"""
|
|
|
|
|
return _demands[manager.IndexToNode(from_node)]
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2018-11-28 10:37:45 +01:00
|
|
|
return demand_evaluator
|
2018-10-31 16:18:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_capacity_constraints(routing, manager, data, demand_evaluator_index):
|
2018-11-28 10:37:45 +01:00
|
|
|
"""Adds capacity constraint"""
|
2025-01-29 13:25:44 +01:00
|
|
|
vehicle_capacity = data["vehicle_capacity"]
|
|
|
|
|
capacity = "Capacity"
|
2018-11-28 10:37:45 +01:00
|
|
|
routing.AddDimension(
|
|
|
|
|
demand_evaluator_index,
|
2020-12-03 00:18:05 +01:00
|
|
|
vehicle_capacity,
|
2018-11-28 10:37:45 +01:00
|
|
|
vehicle_capacity,
|
|
|
|
|
True, # start cumul to zero
|
2025-01-29 13:25:44 +01:00
|
|
|
capacity,
|
|
|
|
|
)
|
2018-11-28 10:37:45 +01:00
|
|
|
|
|
|
|
|
# Add Slack for reseting to zero unload depot nodes.
|
|
|
|
|
# e.g. vehicle with load 10/15 arrives at node 1 (depot unload)
|
|
|
|
|
# so we have CumulVar = 10(current load) + -15(unload) + 5(slack) = 0.
|
|
|
|
|
capacity_dimension = routing.GetDimensionOrDie(capacity)
|
2020-12-03 00:18:05 +01:00
|
|
|
# Allow to drop reloading nodes with zero cost.
|
2021-03-24 11:27:02 +11:00
|
|
|
for node in [1, 2, 3, 4, 5]:
|
|
|
|
|
node_index = manager.NodeToIndex(node)
|
2018-11-28 10:37:45 +01:00
|
|
|
routing.AddDisjunction([node_index], 0)
|
2018-11-11 09:39:59 +01:00
|
|
|
|
2020-12-03 00:18:05 +01:00
|
|
|
# Allow to drop regular node with a cost.
|
2025-01-29 13:25:44 +01:00
|
|
|
for node in range(6, len(data["demands"])):
|
2021-03-24 11:27:02 +11:00
|
|
|
node_index = manager.NodeToIndex(node)
|
|
|
|
|
capacity_dimension.SlackVar(node_index).SetValue(0)
|
2020-12-03 00:18:05 +01:00
|
|
|
routing.AddDisjunction([node_index], 100_000)
|
|
|
|
|
|
2018-09-26 10:51:44 +02:00
|
|
|
|
|
|
|
|
def create_time_evaluator(data):
|
2018-11-28 10:37:45 +01:00
|
|
|
"""Creates callback to get total times between locations."""
|
|
|
|
|
|
|
|
|
|
def service_time(data, node):
|
|
|
|
|
"""Gets the service time for the specified location."""
|
2025-01-29 13:25:44 +01:00
|
|
|
return abs(data["demands"][node]) * data["time_per_demand_unit"]
|
2018-11-28 10:37:45 +01:00
|
|
|
|
|
|
|
|
def travel_time(data, from_node, to_node):
|
|
|
|
|
"""Gets the travel times between two locations."""
|
|
|
|
|
if from_node == to_node:
|
|
|
|
|
travel_time = 0
|
|
|
|
|
else:
|
2025-01-29 13:25:44 +01:00
|
|
|
travel_time = (
|
|
|
|
|
manhattan_distance(
|
|
|
|
|
data["locations"][from_node], data["locations"][to_node]
|
|
|
|
|
)
|
|
|
|
|
/ data["vehicle_speed"]
|
|
|
|
|
)
|
2018-11-28 10:37:45 +01:00
|
|
|
return travel_time
|
|
|
|
|
|
|
|
|
|
_total_time = {}
|
|
|
|
|
# precompute total time to have time callback in O(1)
|
2025-01-29 13:25:44 +01:00
|
|
|
for from_node in range(data["num_locations"]):
|
2018-11-28 10:37:45 +01:00
|
|
|
_total_time[from_node] = {}
|
2025-01-29 13:25:44 +01:00
|
|
|
for to_node in range(data["num_locations"]):
|
2018-11-28 10:37:45 +01:00
|
|
|
if from_node == to_node:
|
|
|
|
|
_total_time[from_node][to_node] = 0
|
|
|
|
|
else:
|
|
|
|
|
_total_time[from_node][to_node] = int(
|
2025-01-29 13:25:44 +01:00
|
|
|
service_time(data, from_node)
|
|
|
|
|
+ travel_time(data, from_node, to_node)
|
|
|
|
|
)
|
2018-11-28 10:37:45 +01:00
|
|
|
|
|
|
|
|
def time_evaluator(manager, from_node, to_node):
|
|
|
|
|
"""Returns the total time between the two nodes"""
|
2025-01-29 13:25:44 +01:00
|
|
|
return _total_time[manager.IndexToNode(from_node)][manager.IndexToNode(to_node)]
|
2018-11-28 10:37:45 +01:00
|
|
|
|
|
|
|
|
return time_evaluator
|
2018-10-31 16:18:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_time_window_constraints(routing, manager, data, time_evaluator):
|
2018-11-28 10:37:45 +01:00
|
|
|
"""Add Time windows constraint"""
|
2025-01-29 13:25:44 +01:00
|
|
|
time = "Time"
|
|
|
|
|
max_time = data["vehicle_max_time"]
|
2018-11-28 10:37:45 +01:00
|
|
|
routing.AddDimension(
|
|
|
|
|
time_evaluator,
|
2020-12-03 00:46:02 +01:00
|
|
|
max_time, # allow waiting time
|
|
|
|
|
max_time, # maximum time per vehicle
|
2018-11-28 10:37:45 +01:00
|
|
|
False, # don't force start cumul to zero since we are giving TW to start nodes
|
2025-01-29 13:25:44 +01:00
|
|
|
time,
|
|
|
|
|
)
|
2018-11-28 10:37:45 +01:00
|
|
|
time_dimension = routing.GetDimensionOrDie(time)
|
|
|
|
|
# Add time window constraints for each location except depot
|
|
|
|
|
# and 'copy' the slack var in the solution object (aka Assignment) to print it
|
2025-01-29 13:25:44 +01:00
|
|
|
for location_idx, time_window in enumerate(data["time_windows"]):
|
2018-11-28 10:37:45 +01:00
|
|
|
if location_idx == 0:
|
|
|
|
|
continue
|
|
|
|
|
index = manager.NodeToIndex(location_idx)
|
|
|
|
|
time_dimension.CumulVar(index).SetRange(time_window[0], time_window[1])
|
|
|
|
|
routing.AddToAssignment(time_dimension.SlackVar(index))
|
|
|
|
|
# Add time window constraints for each vehicle start node
|
|
|
|
|
# and 'copy' the slack var in the solution object (aka Assignment) to print it
|
2025-01-29 13:25:44 +01:00
|
|
|
for vehicle_id in range(data["num_vehicles"]):
|
2018-11-28 10:37:45 +01:00
|
|
|
index = routing.Start(vehicle_id)
|
2025-01-29 13:25:44 +01:00
|
|
|
time_dimension.CumulVar(index).SetRange(
|
|
|
|
|
data["time_windows"][0][0], data["time_windows"][0][1]
|
|
|
|
|
)
|
2018-11-28 10:37:45 +01:00
|
|
|
routing.AddToAssignment(time_dimension.SlackVar(index))
|
|
|
|
|
# Warning: Slack var is not defined for vehicle's end node
|
2025-01-29 13:25:44 +01:00
|
|
|
# routing.AddToAssignment(time_dimension.SlackVar(self.routing.End(vehicle_id)))
|
2018-11-11 09:39:59 +01:00
|
|
|
|
2018-09-26 10:51:44 +02:00
|
|
|
|
|
|
|
|
###########
|
|
|
|
|
# Printer #
|
|
|
|
|
###########
|
2025-01-29 13:25:44 +01:00
|
|
|
def print_solution(
|
|
|
|
|
data, manager, routing, assignment
|
|
|
|
|
): # pylint:disable=too-many-locals
|
2018-11-28 10:37:45 +01:00
|
|
|
"""Prints assignment on console"""
|
2025-01-29 13:25:44 +01:00
|
|
|
print(f"Objective: {assignment.ObjectiveValue()}")
|
2018-11-28 10:37:45 +01:00
|
|
|
total_distance = 0
|
|
|
|
|
total_load = 0
|
|
|
|
|
total_time = 0
|
2025-01-29 13:25:44 +01:00
|
|
|
capacity_dimension = routing.GetDimensionOrDie("Capacity")
|
|
|
|
|
time_dimension = routing.GetDimensionOrDie("Time")
|
|
|
|
|
distance_dimension = routing.GetDimensionOrDie("Distance")
|
2018-11-28 10:37:45 +01:00
|
|
|
dropped = []
|
2020-12-03 00:46:02 +01:00
|
|
|
for order in range(6, routing.nodes()):
|
2018-11-28 10:37:45 +01:00
|
|
|
index = manager.NodeToIndex(order)
|
|
|
|
|
if assignment.Value(routing.NextVar(index)) == index:
|
|
|
|
|
dropped.append(order)
|
2025-01-29 13:25:44 +01:00
|
|
|
print(f"dropped orders: {dropped}")
|
|
|
|
|
dropped = []
|
2020-12-03 00:46:02 +01:00
|
|
|
for reload in range(1, 6):
|
|
|
|
|
index = manager.NodeToIndex(reload)
|
|
|
|
|
if assignment.Value(routing.NextVar(index)) == index:
|
|
|
|
|
dropped.append(reload)
|
2025-01-29 13:25:44 +01:00
|
|
|
print(f"dropped reload stations: {dropped}")
|
2018-11-28 10:37:45 +01:00
|
|
|
|
2025-01-29 13:25:44 +01:00
|
|
|
for vehicle_id in range(data["num_vehicles"]):
|
|
|
|
|
if not routing.IsVehicleUsed(assignment, vehicle_id):
|
|
|
|
|
continue
|
2018-11-28 10:37:45 +01:00
|
|
|
index = routing.Start(vehicle_id)
|
2025-01-29 13:25:44 +01:00
|
|
|
plan_output = f"Route for vehicle {vehicle_id}:\n"
|
2025-01-30 14:28:07 +01:00
|
|
|
load_value = 0
|
2018-11-28 10:37:45 +01:00
|
|
|
distance = 0
|
|
|
|
|
while not routing.IsEnd(index):
|
|
|
|
|
time_var = time_dimension.CumulVar(index)
|
2023-04-13 11:49:14 +02:00
|
|
|
plan_output += (
|
2025-01-29 13:25:44 +01:00
|
|
|
f" {manager.IndexToNode(index)} "
|
2025-01-30 14:28:07 +01:00
|
|
|
f"Load({assignment.Min(capacity_dimension.CumulVar(index))}) "
|
2025-01-29 13:25:44 +01:00
|
|
|
f"Time({assignment.Min(time_var)},{assignment.Max(time_var)}) ->"
|
2023-04-13 11:49:14 +02:00
|
|
|
)
|
2018-11-28 10:37:45 +01:00
|
|
|
previous_index = index
|
|
|
|
|
index = assignment.Value(routing.NextVar(index))
|
2025-01-29 13:25:44 +01:00
|
|
|
distance += distance_dimension.GetTransitValue(previous_index, index, vehicle_id)
|
2025-01-30 14:28:07 +01:00
|
|
|
# capacity dimension TransitVar is negative at reload stations during replenishment
|
|
|
|
|
# don't want to consider those values when calculating the total load of the route
|
|
|
|
|
# hence only considering the positive values
|
|
|
|
|
load_value += max(0, capacity_dimension.GetTransitValue(previous_index, index, vehicle_id))
|
2018-11-28 10:37:45 +01:00
|
|
|
time_var = time_dimension.CumulVar(index)
|
2023-04-13 11:49:14 +02:00
|
|
|
plan_output += (
|
2025-01-29 13:25:44 +01:00
|
|
|
f" {manager.IndexToNode(index)} "
|
2025-01-30 14:28:07 +01:00
|
|
|
f"Load({assignment.Min(capacity_dimension.CumulVar(index))}) "
|
2025-01-29 13:25:44 +01:00
|
|
|
f"Time({assignment.Min(time_var)},{assignment.Max(time_var)})\n"
|
|
|
|
|
)
|
|
|
|
|
plan_output += f"Distance of the route: {distance}m\n"
|
2025-01-30 14:28:07 +01:00
|
|
|
plan_output += f"Load of the route: {load_value}\n"
|
2025-01-29 13:25:44 +01:00
|
|
|
plan_output += f"Time of the route: {assignment.Min(time_var)}min\n"
|
2018-11-28 10:37:45 +01:00
|
|
|
print(plan_output)
|
|
|
|
|
total_distance += distance
|
2025-01-30 14:28:07 +01:00
|
|
|
total_load += load_value
|
2024-02-02 14:28:15 +01:00
|
|
|
total_time += assignment.Min(time_var)
|
2025-01-29 13:25:44 +01:00
|
|
|
print(f"Total Distance of all routes: {total_distance}m")
|
|
|
|
|
print(f"Total Load of all routes: {total_load}")
|
|
|
|
|
print(f"Total Time of all routes: {total_time}min")
|
2018-11-11 09:39:59 +01:00
|
|
|
|
2018-09-26 10:51:44 +02:00
|
|
|
|
|
|
|
|
########
|
|
|
|
|
# Main #
|
|
|
|
|
########
|
|
|
|
|
def main():
|
2018-11-28 10:37:45 +01:00
|
|
|
"""Entry point of the program"""
|
|
|
|
|
# Instantiate the data problem.
|
|
|
|
|
data = create_data_model()
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2018-11-28 10:37:45 +01:00
|
|
|
# Create the routing index manager
|
2025-01-29 13:25:44 +01:00
|
|
|
manager = pywrapcp.RoutingIndexManager(
|
|
|
|
|
data["num_locations"], data["num_vehicles"], data["depot"]
|
|
|
|
|
)
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2018-11-28 10:37:45 +01:00
|
|
|
# Create Routing Model
|
|
|
|
|
routing = pywrapcp.RoutingModel(manager)
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2018-11-28 10:37:45 +01:00
|
|
|
# Define weight of each edge
|
|
|
|
|
distance_evaluator_index = routing.RegisterTransitCallback(
|
2025-01-29 13:25:44 +01:00
|
|
|
partial(create_distance_evaluator(data), manager)
|
|
|
|
|
)
|
2018-11-28 10:37:45 +01:00
|
|
|
routing.SetArcCostEvaluatorOfAllVehicles(distance_evaluator_index)
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2018-11-28 10:37:45 +01:00
|
|
|
# Add Distance constraint to minimize the longuest route
|
2020-12-03 00:46:02 +01:00
|
|
|
add_distance_dimension(routing, manager, data, distance_evaluator_index)
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2018-11-28 10:37:45 +01:00
|
|
|
# Add Capacity constraint
|
|
|
|
|
demand_evaluator_index = routing.RegisterUnaryTransitCallback(
|
2025-01-29 13:25:44 +01:00
|
|
|
partial(create_demand_evaluator(data), manager)
|
|
|
|
|
)
|
2018-11-28 10:37:45 +01:00
|
|
|
add_capacity_constraints(routing, manager, data, demand_evaluator_index)
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2018-11-28 10:37:45 +01:00
|
|
|
# Add Time Window constraint
|
|
|
|
|
time_evaluator_index = routing.RegisterTransitCallback(
|
2025-01-29 13:25:44 +01:00
|
|
|
partial(create_time_evaluator(data), manager)
|
|
|
|
|
)
|
2018-11-28 10:37:45 +01:00
|
|
|
add_time_window_constraints(routing, manager, data, time_evaluator_index)
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2018-11-28 10:37:45 +01:00
|
|
|
# Setting first solution heuristic (cheapest addition).
|
|
|
|
|
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
|
|
|
|
|
search_parameters.first_solution_strategy = (
|
2025-01-29 13:25:44 +01:00
|
|
|
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC
|
|
|
|
|
) # pylint: disable=no-member
|
2020-12-03 00:18:05 +01:00
|
|
|
search_parameters.local_search_metaheuristic = (
|
2025-01-29 13:25:44 +01:00
|
|
|
routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH
|
|
|
|
|
)
|
2020-12-03 00:18:05 +01:00
|
|
|
search_parameters.time_limit.FromSeconds(3)
|
|
|
|
|
|
2018-11-28 10:37:45 +01:00
|
|
|
# Solve the problem.
|
2020-12-03 00:18:05 +01:00
|
|
|
solution = routing.SolveWithParameters(search_parameters)
|
|
|
|
|
if solution:
|
|
|
|
|
print_solution(data, manager, routing, solution)
|
|
|
|
|
else:
|
|
|
|
|
print("No solution found !")
|
2018-11-11 09:39:59 +01:00
|
|
|
|
2018-09-26 10:51:44 +02:00
|
|
|
|
2025-01-29 13:25:44 +01:00
|
|
|
if __name__ == "__main__":
|
2018-11-28 10:37:45 +01:00
|
|
|
main()
|