Skip to content

Commit

Permalink
Or-Tools Solution Analysis
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Monteiro <dfamonteiro@users.noreply.github.com>
Co-authored-by: Gonçalo Pascoal <goncalojpascoal@gmail.com>
  • Loading branch information
3 people committed Jun 18, 2022
1 parent 6b1c184 commit 0478fdc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
49 changes: 49 additions & 0 deletions scripts/analysis2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import time
from typing import Tuple, List

from or_tools_solution import cvrp

class Solver:
def solve(distance_matrix_path: str, cvrp_problem_path: str, **args) -> Tuple[float, List[List[int]], float]:
"""Returns the time it took to solve (in seconds), list of paths, total travelled distance"""
raise Exception("Not implemented")


class OrToolsSolver(Solver):
def solve(distance_matrix_path: str, cvrp_problem_path: str) -> Tuple[float, List[List[int]], float]:
start_time = time.time()
(data, manager, routing, solution) = cvrp(distance_matrix_path, cvrp_problem_path, 1)
end_time = time.time()
total_distance = 0
routes = []
for vehicle_id in range(len(data['distance_matrix'])):
route = []
index = routing.Start(vehicle_id)
while not routing.IsEnd(index):
node_index = manager.IndexToNode(index)
route.append(node_index)
previous_index = index
index = solution.Value(routing.NextVar(index))
total_distance += routing.GetArcCostForVehicle(
previous_index, index, vehicle_id) / 100
routes.append(route)
return (end_time - start_time, routes, total_distance)

class SimulatedAnnealing(Solver):
def solve(distance_matrix_path: str, cvrp_problem_path: str) -> Tuple[float, List[List[int]], float]:
pass

class Greedy(Solver):
def solve(distance_matrix_path: str, cvrp_problem_path: str) -> Tuple[float, List[List[int]], float]:
pass

class TabuSearch(Solver):
def solve(distance_matrix_path: str, cvrp_problem_path: str) -> Tuple[float, List[List[int]], float]:
pass

class AntColony(Solver):
def solve(distance_matrix_path: str, cvrp_problem_path: str) -> Tuple[float, List[List[int]], float]:
pass

if __name__=="__main__":
pass
22 changes: 13 additions & 9 deletions scripts/or_tools_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import argparse
import json

def create_data_model(args):
def create_data_model(distance_matrix_path, cvrp_problem_path):
"""Stores the data for the problem."""
data = {}

lines = []
with open(args.dist_filename) as file:
with open(distance_matrix_path) as file:
lines = file.readlines()
data['distance_matrix'] = [[int(round(float(element) * 100, 0)) for element in line.split(' ')[:-1]] for line in lines]

with open(args.cvrp_filename) as file:
with open(cvrp_problem_path) as file:
cvrp_info = json.load(file)
data['demands'] = [0] + [delivery['size'] for delivery in cvrp_info['deliveries']]
data['vehicle_capacity'] = cvrp_info['vehicle_capacity']
Expand Down Expand Up @@ -62,9 +62,16 @@ def main():
parser.add_argument('dist_filename', type=str, help='path for the file with the distance matrix')

args = parser.parse_args()
(data, manager, routing, solution) = cvrp(args.dist_filename, args.cvrp_filename)

# Print solution on console.
if solution:
print_solution(data, manager, routing, solution)


def cvrp(distance_matrix_path, cvrp_problem_path, time_limit = 1):
# Instantiate the data problem.
data = create_data_model(args)
data = create_data_model(distance_matrix_path, cvrp_problem_path)

# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']), len(data['distance_matrix']), 0)
Expand Down Expand Up @@ -106,14 +113,11 @@ def demand_callback(from_index):
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
search_parameters.local_search_metaheuristic = (routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
search_parameters.time_limit.FromSeconds(1)
search_parameters.time_limit.FromSeconds(time_limit)

# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)

# Print solution on console.
if solution:
print_solution(data, manager, routing, solution)
return (data, manager, routing, solution)


if __name__ == '__main__':
Expand Down

0 comments on commit 0478fdc

Please sign in to comment.