Skip to content

Commit

Permalink
compute penalty for rejecting requests (#14047)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritzerp committed Jan 2, 2024
1 parent 7cbdaa3 commit 39293bc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
55 changes: 48 additions & 7 deletions tools/drt/drtOrtools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
# Copyright (C) 2021-2023 German Aerospace Center (DLR) and others.
Expand All @@ -25,6 +25,7 @@
from enum import Enum

import os
import pathlib
import sys

import numpy as np
Expand All @@ -38,19 +39,21 @@
import sumolib # noqa
import traci # noqa

SPEED_DEFAULT = 20 # default vehicle speed in m/s
PENALTY_FACTOR = 5 # factor on penalty for rejecting requests

class CostType(Enum):
DISTANCE = 1
TIME = 2


def dispatch(reservations, fleet, time_limit, cost_type, drf, waiting_time, end,
fix_allocation, solution_requests, verbose):
fix_allocation, solution_requests, penalty, verbose):
"""Dispatch using ortools."""
if verbose:
print('Start creating the model.')
data = create_data_model(reservations, fleet, cost_type, drf, waiting_time, end,
fix_allocation, solution_requests, verbose)
fix_allocation, solution_requests, penalty, verbose)
if verbose:
print('Start solving the problem.')
solution_ortools = ortools_pdp.main(data, time_limit, verbose)
Expand All @@ -61,7 +64,7 @@ def dispatch(reservations, fleet, time_limit, cost_type, drf, waiting_time, end,


def create_data_model(reservations, fleet, cost_type, drf, waiting_time, end,
fix_allocation, solution_requests, verbose):
fix_allocation, solution_requests, penalty, verbose):
"""Creates the data for the problem."""
n_vehicles = len(fleet)
# use only reservations that haven't been picked up yet; reservation.state!=8 (not picked up)
Expand Down Expand Up @@ -209,8 +212,38 @@ def create_data_model(reservations, fleet, cost_type, drf, waiting_time, end,
data['fix_allocation'] = fix_allocation
data['max_time'] = end
data['initial_routes'] = solution_requests
data['penalty'] = int(penalty)
return data

def get_network_path(sumo_config):
"""Get path to SUMO network from config file."""
sumo_config = pathlib.Path(sumo_config)
net_file = list(sumolib.xml.parse(sumo_config, "net-file"))
net_filename = net_file[0].getAttribute("value")
net_path = pathlib.Path(net_filename)
if net_path.is_absolute():
return net_path
else:
return sumo_config.parent / net_path

def get_network_dimension(sumo_config, cost_type):
"""Get the rough network dimension."""
net_path = get_network_path(sumo_config)
net = sumolib.net.readNet(net_path)
# diameter of bounding box
diameter = net.getBBoxDiameter()
if cost_type.name == "DISTANCE":
dimension = diameter
if cost_type.name == "TIME":
# convert distance to time assuming default speed
dimension = diameter / SPEED_DEFAULT
return dimension

def get_penalty(sumo_config, cost_type, penalty_factor=PENALTY_FACTOR):
"""Define penalty for rejecting requests."""
dimension = get_network_dimension(sumo_config, cost_type)
penalty = dimension * penalty_factor
return int(penalty)

def get_time_windows(reservations, fleet, end):
"""returns a list of pairs with earliest and latest time"""
Expand Down Expand Up @@ -348,13 +381,15 @@ def solution_by_requests(solution_ortools, reservations, data, verbose=False):
return solution_requests


def run(end=None, interval=30, time_limit=10, cost_type='distance', drf=1.5, waiting_time=900,
def run(penalty, end=None, interval=30, time_limit=10, cost_type='distance', drf=1.5, waiting_time=900,
fix_allocation=False, verbose=False):
"""
Execute the TraCI control loop and run the scenario.
Parameters
----------
penalty: int
Penalty for rejecting requests.
end : int, optional
Final time step of simulation. The default is 90000.
This option can be ignored by giving a negative value.
Expand Down Expand Up @@ -455,7 +490,7 @@ def run(end=None, interval=30, time_limit=10, cost_type='distance', drf=1.5, wai
if verbose:
print("Solve CPDP")
solution_requests = dispatch(reservations_all, fleet, time_limit, cost_type, drf, waiting_time, int(end),
fix_allocation, solution_requests, verbose)
fix_allocation, solution_requests, penalty, verbose)
if solution_requests is not None:
for index_vehicle, vehicle_requests in solution_requests.items(): # for each vehicle
id_vehicle = fleet[index_vehicle]
Expand Down Expand Up @@ -502,6 +537,8 @@ def get_arguments():
"does not change anymore")
ap.add_argument("-w", "--waiting-time", type=ap.time, default=900,
help="maximum waiting time to serve a request in s")
ap.add_argument("-p", "--penalty-factor", type=float, default=PENALTY_FACTOR,
help="factor on penalty for rejecting requests")
ap.add_argument("--trace-file", type=ap.file,
help="log file for TraCI debugging")
return ap.parse_args()
Expand Down Expand Up @@ -540,5 +577,9 @@ def check_set_arguments(arguments):
# this is the normal way of using traci. sumo is started as a
# subprocess and then the python script connects and runs
traci.start([arguments.sumoBinary, "-c", arguments.sumo_config], traceFile=arguments.trace_file)
run(arguments.end, arguments.interval, arguments.time_limit, arguments.cost_type, arguments.drf,

# get penalty
penalty = get_penalty(arguments.sumo_config, arguments.cost_type, arguments.penalty_factor)

run(penalty, arguments.end, arguments.interval, arguments.time_limit, arguments.cost_type, arguments.drf,
arguments.waiting_time, arguments.fix_allocation, arguments.verbose)
2 changes: 1 addition & 1 deletion tools/drt/ortools_pdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def add_transportation_requests_constraint(data, routing, manager, solver, dista
# allows to reject the order but gives penalty
if verbose:
print('allow to reject new reservation %s' % (request.id))
routing.AddDisjunction([pickup_index, delivery_index], 10000, 2)
routing.AddDisjunction([pickup_index, delivery_index], data['penalty'], 2)


def add_direct_route_factor_constraint(data, routing, manager, solver, distance_dimension, verbose):
Expand Down

0 comments on commit 39293bc

Please sign in to comment.