Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/web/docs/Tools/Drt.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ If you don't need to simulate a shared service, you can directly use the taxi de
in sumo and don't need this tool. See [Taxi](../Simulation/Taxi.md) and the
respective [Taxi service tutorial ](../Tutorials/TaxiService.md).

The tool requires python3 and the python LP (Linear Programming) modeler
[PuLP](https://coin-or.github.io/pulp/) at least in version 2.0.
Please do not install the python3-pulp package on ubuntu 20.04 (or earlier)
use pip instead.
The tool requires Python 3 and [PuLP](https://coin-or.github.io/pulp/) version 2.0 or later.
The dispatcher uses the [CBC solver](https://www.coin-or.org/pulp/guides/how_to_configure_solvers.html#coin-or-cbc-coin-cmd) for its integer-programming step.
Install current PuLP with CBC using `python -m pip install "pulp[cbc]"`.
You can also install CBC separately and add its executable to your `PATH`.

The minimal call is:

Expand Down
Empty file.
11 changes: 11 additions & 0 deletions tests/tools/drt/drtOnline/cbc_dispatch/input_routes.rou.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<routes>
<vType id="taxi" vClass="taxi">
<param key="has.taxi.device" value="true"/>
</vType>
<trip id="taxiA" type="taxi" depart="0" from="A0B0" to="B0C0">
<stop lane="A0B0_1" triggered="person"/>
</trip>
<trip id="taxiB" type="taxi" depart="0" from="A2B2" to="B2C2">
<stop lane="A2B2_1" triggered="person"/>
</trip>
</routes>
5 changes: 5 additions & 0 deletions tests/tools/drt/drtOnline/cbc_dispatch/input_routes2.rou.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<routes>
<person id="rider" depart="0">
<ride from="B0C0" to="C0C1" lines="taxi"/>
</person>
</routes>
1 change: 1 addition & 0 deletions tests/tools/drt/drtOnline/cbc_dispatch/options.tools
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--end-time 200
10 changes: 10 additions & 0 deletions tests/tools/drt/drtOnline/cbc_dispatch/output.tools
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

Run dispatcher
New reservations: ['0']
Pending reservations: []
Solve DARP with exhaustive_search
Solve ILP
Dispatch: taxiA_0y_0z

Exact solution found.
DRT simulation ended
3 changes: 3 additions & 0 deletions tests/tools/drt/drtOnline/testsuite.tools
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ customized_times

# test time out to process a reservation
max_processing

# one request with a unique choice of taxi in the CBC model
cbc_dispatch
68 changes: 41 additions & 27 deletions tools/drt/drtOnline.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,49 +159,59 @@ def ilp_solve(options, veh_num, res_num, costs, veh_constraints,
# req_costs = [reservation.cost for reservation in reservations] # TODO to implement req with diff costs/priorities
order_trips = costs.keys()

ILP_result = []

# Create the 'prob' variable to contain the problem data
prob = pl.LpProblem("DARP", pl.LpMinimize)

# 'Trips_vars' dict with the referenced Variables (all possible trips)
Trips_vars = pl.LpVariable.dicts("Trip", order_trips, cat='Binary')
if hasattr(prob, "add_variable_dict"):
trip_vars = prob.add_variable_dict("Trip", order_trips, cat=pl.LpBinary)
else:
trip_vars = pl.LpVariable.dicts("Trip", order_trips, cat=pl.LpBinary)

# add objective function
# solution cost = sum(cost of each trip) -
# sum(reservation rejection cost * reservations served in trip)
prob += pl.lpSum([costs[i] * Trips_vars[i] for i in order_trips]) - \
options.c_ko * pl.lpSum([sum(res_constraints[i]) * Trips_vars[i]
prob += pl.lpSum([costs[i] * trip_vars[i] for i in order_trips]) - \
options.c_ko * pl.lpSum([sum(res_constraints[i]) * trip_vars[i]
for i in order_trips]), "Trips_travel_time"

# add constraints
for index in range(veh_num):
prob += pl.lpSum([veh_constraints[i][index] * Trips_vars[i]
prob += pl.lpSum([veh_constraints[i][index] * trip_vars[i]
for i in order_trips]) <= 1, "Max_1_trip_per_vehicle_%s" % index

for index in range(res_num):
prob += pl.lpSum([res_constraints[i][index] * Trips_vars[i]
prob += pl.lpSum([res_constraints[i][index] * trip_vars[i]
for i in order_trips]) <= 1, "Max_1_trip_per_reservation_%s" % index

prob += pl.lpSum([sum(veh_constraints[i]) * Trips_vars[i]
prob += pl.lpSum([sum(veh_constraints[i]) * trip_vars[i]
for i in order_trips]) >= 1, "Assing_at_least_one_vehicle"

# The problem is solved using PuLP's Solver choice
cbc_opts = ["RandomS %s" % options.seed] if options.seed else None
try:
prob.solve(pl.PULP_CBC_CMD(msg=0, timeLimit=options.ilp_time, options=cbc_opts))
except pl.apis.core.PulpSolverError:
prob.solve(pl.COIN_CMD(msg=0, timeLimit=options.ilp_time, path="/usr/bin/cbc", options=cbc_opts))

if pl.LpStatus[prob.status] != 'Optimal':
sys.exit("No optimal solution found: %s" % pl.LpStatus[prob.status])
else: # if optimal solution was found
for variable in prob.variables():
if variable.varValue == 1:
result = variable.name.split("Trip_")[1]
ILP_result.append(result)

return ILP_result
cbc_options = {"msg": False, "timeLimit": options.ilp_time,
"options": ["RandomS %s" % options.seed] if options.seed else None}
solve_result = None
bundled_cbc = getattr(pl, "PULP_CBC_CMD", None)
if bundled_cbc is not None:
try:
solve_result = prob.solve(bundled_cbc(**cbc_options))
except pl.PulpSolverError:
pass # Legacy packages may ship PuLP without the bundled CBC executable.
if solve_result is None:
solver = pl.COIN_CMD(**cbc_options)
if not solver.available():
sys.exit('CBC solver unavailable. Install it with python -m pip install "pulp[cbc]" '
'or add the CBC executable to PATH.')
solve_result = prob.solve(solver)

if hasattr(solve_result, "status"):
status = solve_result.status.name
# Older CBC wrappers reported these feasible results as Optimal.
usable = solve_result.has_solution and status in ("Optimal", "GapLimit", "TimeLimit")
else:
status = pl.LpStatus[solve_result]
usable = status == "Optimal"
if not usable:
sys.exit("No optimal solution found: %s" % status)
return [variable.name.split("Trip_")[1] for variable in prob.variables() if variable.value() == 1]


def main():
Expand Down Expand Up @@ -450,8 +460,12 @@ def main():

if options.verbose:
print('Solve ILP')
ilp_result = ilp_solve(options, len(fleet), len(ilp_res_cons),
costs, veh_constraints, res_constraints)
try:
ilp_result = ilp_solve(options, len(fleet), len(ilp_res_cons),
costs, veh_constraints, res_constraints)
except SystemExit:
traci.close()
raise

# parse ILP result
best_routes = [trips[int(route_index)]
Expand Down
Loading