Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Fix some num vehicles things
Browse files Browse the repository at this point in the history
  • Loading branch information
leonlan committed Sep 29, 2023
1 parent e936a52 commit 541addb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
3 changes: 2 additions & 1 deletion ddwp/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ def get_hindsight_problem(self) -> VrpInstance:
vehicle_types = []

if self.num_vehicles_per_epoch is None:
vehicle_types.append(VehicleType(capacity, customer_idx.size))
max_vehicles = VehicleType(capacity, self.instance.num_requests)
vehicle_types.append(max_vehicles)
else:
for epoch, num_primary in enumerate(self.num_vehicles_per_epoch):
departure = epoch * self.epoch_duration + self.dispatch_margin
Expand Down
30 changes: 21 additions & 9 deletions ddwp/agents/IterativeConditionalDispatch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import partial

import numpy as np
from pyvrp import VehicleType

from ddwp.Environment import State, StaticInfo
from ddwp.sampling import SamplingMethod
Expand Down Expand Up @@ -75,16 +76,28 @@ def act(self, info: StaticInfo, obs: State) -> list[list[int]]:
First determines the dispatch decisions for the current epoch, then
solves the instance of dispatched requests.
"""
epoch_instance = obs.epoch_instance
to_dispatch = self._determine_dispatch(info, obs)
dispatch_instance = epoch_instance.filter(to_dispatch)
dispatch_inst = obs.epoch_instance.filter(to_dispatch)

# BUG: If the number of vehicles per epoch is not fixed, we need to
# correct the number of vehicles in the dispatch instance to match
# the number of requests. This is a "bug" in how random solutions
# are generated in PyVRP.
if info.num_vehicles_per_epoch is None:
capacity = dispatch_inst.capacity
num_vehicles = dispatch_inst.num_requests
vehicle_types = [VehicleType(capacity, num_vehicles)]
dispatch_inst.replace(vehicle_types=vehicle_types)

res = default_solver(
dispatch_instance, self.seed, self.dispatch_time_limit
dispatch_inst, self.seed, self.dispatch_time_limit
)
routes = [route.visits() for route in res.best.get_routes()]

return [dispatch_instance.request_idx[r].tolist() for r in routes]
assert res.best.is_feasible(), "Infeasible dispatch solution!"

# Convert the solution to request indices.
routes = [route.visits() for route in res.best.get_routes()]
return [dispatch_inst.request_idx[r].tolist() for r in routes]

def _determine_dispatch(self, info: StaticInfo, obs: State) -> np.ndarray:
"""
Expand Down Expand Up @@ -138,9 +151,8 @@ def _solve_scenario(self, instance: VrpInstance) -> list[list[int]]:
"""
Solves a single scenario instance, returning the solution.
"""
result = scenario_solver(instance, self.seed, self.scenario_time_limit)
res = scenario_solver(instance, self.seed, self.scenario_time_limit)

if not result.best.is_feasible():
raise RuntimeError("Infeasible scenario solution!")
assert res.best.is_feasible(), "Infeasible scenario solution!"

return [route.visits() for route in result.best.get_routes()]
return [route.visits() for route in res.best.get_routes()]

0 comments on commit 541addb

Please sign in to comment.