2018-04-04 11:26:38 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# 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.
|
|
|
|
|
"""Vehicle Routing Problem (VRP).
|
2018-06-11 11:51:18 +02:00
|
|
|
|
|
|
|
|
This is a sample using the routing library python wrapper to solve a VRP
|
|
|
|
|
problem.
|
2018-04-04 11:26:38 +02:00
|
|
|
A description of the problem can be found here:
|
|
|
|
|
http://en.wikipedia.org/wiki/Vehicle_routing_problem.
|
|
|
|
|
|
2018-06-29 15:35:48 +02:00
|
|
|
Distances are in meters.
|
2018-04-04 11:26:38 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
from six.moves import xrange
|
|
|
|
|
from ortools.constraint_solver import pywrapcp
|
|
|
|
|
from ortools.constraint_solver import routing_enums_pb2
|
|
|
|
|
|
|
|
|
|
###########################
|
|
|
|
|
# Problem Data Definition #
|
|
|
|
|
###########################
|
2018-09-27 10:36:00 +02:00
|
|
|
def create_data_model():
|
2018-06-11 11:51:18 +02:00
|
|
|
"""Stores the data for the problem"""
|
2018-09-27 10:36:00 +02:00
|
|
|
data = {}
|
|
|
|
|
# Locations in block unit
|
|
|
|
|
_locations = \
|
|
|
|
|
[(4, 4), # depot
|
|
|
|
|
(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), (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"
|
|
|
|
|
data["locations"] = [(l[0] * 114, l[1] * 80) for l in _locations]
|
|
|
|
|
data["num_locations"] = len(data["locations"])
|
|
|
|
|
data["num_vehicles"] = 4
|
|
|
|
|
data["depot"] = 0
|
|
|
|
|
return data
|
2018-04-04 11:26:38 +02:00
|
|
|
|
|
|
|
|
#######################
|
|
|
|
|
# Problem Constraints #
|
|
|
|
|
#######################
|
|
|
|
|
def manhattan_distance(position_1, position_2):
|
2018-06-11 11:51:18 +02:00
|
|
|
"""Computes the Manhattan distance between two points"""
|
|
|
|
|
return (
|
|
|
|
|
abs(position_1[0] - position_2[0]) + abs(position_1[1] - position_2[1]))
|
|
|
|
|
|
2018-09-27 10:36:00 +02:00
|
|
|
def create_distance_evaluator(data):
|
2018-06-11 11:51:18 +02:00
|
|
|
"""Creates callback to return distance between points."""
|
2018-09-27 10:36:00 +02:00
|
|
|
_distances = {}
|
|
|
|
|
# precompute distance between location to have distance callback in O(1)
|
|
|
|
|
for from_node in xrange(data["num_locations"]):
|
|
|
|
|
_distances[from_node] = {}
|
|
|
|
|
for to_node in xrange(data["num_locations"]):
|
|
|
|
|
if from_node == to_node:
|
|
|
|
|
_distances[from_node][to_node] = 0
|
|
|
|
|
else:
|
|
|
|
|
_distances[from_node][to_node] = (
|
|
|
|
|
manhattan_distance(data["locations"][from_node],
|
|
|
|
|
data["locations"][to_node]))
|
|
|
|
|
|
|
|
|
|
def distance_evaluator(from_node, to_node):
|
2018-06-11 11:51:18 +02:00
|
|
|
"""Returns the manhattan distance between the two nodes"""
|
2018-09-27 10:36:00 +02:00
|
|
|
return _distances[from_node][to_node]
|
|
|
|
|
|
|
|
|
|
return distance_evaluator
|
2018-06-11 11:51:18 +02:00
|
|
|
|
2018-04-04 11:26:38 +02:00
|
|
|
###########
|
|
|
|
|
# Printer #
|
|
|
|
|
###########
|
2018-06-29 15:35:48 +02:00
|
|
|
def print_solution(data, routing, assignment):
|
|
|
|
|
"""Prints assignment on console"""
|
|
|
|
|
print('Objective: {}'.format(assignment.ObjectiveValue()))
|
|
|
|
|
total_distance = 0
|
2018-09-27 10:36:00 +02:00
|
|
|
for vehicle_id in xrange(data["num_vehicles"]):
|
2018-06-29 15:35:48 +02:00
|
|
|
index = routing.Start(vehicle_id)
|
|
|
|
|
plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
|
|
|
|
|
distance = 0
|
|
|
|
|
while not routing.IsEnd(index):
|
2018-09-27 10:36:00 +02:00
|
|
|
plan_output += ' {} ->'.format(routing.IndexToNode(index))
|
2018-06-29 15:35:48 +02:00
|
|
|
previous_index = index
|
|
|
|
|
index = assignment.Value(routing.NextVar(index))
|
|
|
|
|
distance += routing.GetArcCostForVehicle(previous_index, index, vehicle_id)
|
|
|
|
|
plan_output += ' {}\n'.format(routing.IndexToNode(index))
|
|
|
|
|
plan_output += 'Distance of the route: {}m\n'.format(distance)
|
|
|
|
|
print(plan_output)
|
|
|
|
|
total_distance += distance
|
|
|
|
|
print('Total Distance of all routes: {}m'.format(total_distance))
|
2018-04-04 11:26:38 +02:00
|
|
|
|
|
|
|
|
########
|
|
|
|
|
# Main #
|
|
|
|
|
########
|
|
|
|
|
def main():
|
2018-06-11 11:51:18 +02:00
|
|
|
"""Entry point of the program"""
|
|
|
|
|
# Instantiate the data problem.
|
2018-09-27 10:36:00 +02:00
|
|
|
data = create_data_model()
|
2018-06-11 11:51:18 +02:00
|
|
|
|
|
|
|
|
# Create Routing Model
|
2018-09-27 10:36:00 +02:00
|
|
|
routing = pywrapcp.RoutingModel(
|
|
|
|
|
data["num_locations"],
|
|
|
|
|
data["num_vehicles"],
|
|
|
|
|
data["depot"])
|
2018-06-11 11:51:18 +02:00
|
|
|
# Define weight of each edge
|
2018-09-27 10:36:00 +02:00
|
|
|
distance_evaluator = create_distance_evaluator(data)
|
2018-06-11 11:51:18 +02:00
|
|
|
routing.SetArcCostEvaluatorOfAllVehicles(distance_evaluator)
|
|
|
|
|
|
|
|
|
|
# Setting first solution heuristic (cheapest addition).
|
|
|
|
|
search_parameters = pywrapcp.RoutingModel.DefaultSearchParameters()
|
|
|
|
|
search_parameters.first_solution_strategy = (
|
2018-06-29 15:35:48 +02:00
|
|
|
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC) # pylint: disable=no-member
|
2018-06-11 11:51:18 +02:00
|
|
|
# Solve the problem.
|
|
|
|
|
assignment = routing.SolveWithParameters(search_parameters)
|
2018-06-29 15:35:48 +02:00
|
|
|
print_solution(data, routing, assignment)
|
2018-04-04 11:26:38 +02:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-06-11 11:51:18 +02:00
|
|
|
main()
|