Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
* Now most of the properties generated on "Route" are computed as Ite…
Browse files Browse the repository at this point in the history
…rators. #12
  • Loading branch information
garciparedes committed Aug 24, 2019
1 parent 457c336 commit e0dcdad
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 47 deletions.
4 changes: 2 additions & 2 deletions jinete/models/criterions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def scoring(self, planned_trip: PlannedTrip) -> float:
if not planned_trip.feasible:
return MAX_FLOAT

return planned_trip.delivery_time - planned_trip.route.last_time
return planned_trip.delivery_time - planned_trip.route.latest


class LongestTimePlannedTripCriterion(PlannedTripCriterion):
Expand All @@ -70,7 +70,7 @@ def scoring(self, planned_trip: PlannedTrip) -> float:
if not planned_trip.feasible:
return MIN_FLOAT

return planned_trip.delivery_time - planned_trip.route.last_time
return planned_trip.delivery_time - planned_trip.route.latest


class LongestUtilTimePlannedTripCriterion(PlannedTripCriterion):
Expand Down
51 changes: 9 additions & 42 deletions jinete/models/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __iter__(self):

@property
def planned_trips(self) -> Iterator[PlannedTrip]:
return iter(
yield from (
stop_cause.planned_trip
for stop_cause in self.stop_causes
if stop_cause.kind == StopKind.DELIVERY
Expand All @@ -92,7 +92,7 @@ def feasible(self) -> bool:
return False
if not self.last_position == self.vehicle.final:
return False
if not self.last_planned_trip.delivery_time <= self.vehicle.latest:
if not self.latest <= self.vehicle.latest:
return False

for planned_trip in self.planned_trips:
Expand All @@ -105,38 +105,16 @@ def loaded(self):
return any(self.planned_trips)

@property
def trips(self) -> Tuple[Trip]:
return tuple(planned_trip.trip for planned_trip in self.planned_trips)
def trips(self) -> Iterator[Trip]:
yield from (planned_trip.trip for planned_trip in self.planned_trips)

@property
def loaded_planned_trips(self) -> Tuple[PlannedTrip]:
return tuple(planned_trip for planned_trip in self.planned_trips if not planned_trip.empty)
def loaded_planned_trips(self) -> Iterator[PlannedTrip]:
yield from (planned_trip for planned_trip in self.planned_trips if not planned_trip.empty)

@property
def loaded_trips(self) -> Tuple[Trip]:
return tuple(planned_trip.trip for planned_trip in self.planned_trips if not planned_trip.empty)

@property
def first_planned_trip(self) -> Optional[PlannedTrip]:
if any(self.planned_trips):
return None
# return min(self.planned_trips, key=lambda pt: pt.pickup_time)
return next(self.planned_trips)

@property
def first_trip(self) -> Trip:
return self.first_planned_trip.trip

@property
def last_planned_trip(self) -> Optional[PlannedTrip]:
if not any(self.planned_trips):
return None
# return max(self.planned_trips, key=lambda pt: pt.delivery_time)
return deque(self.planned_trips, maxlen=1).pop()

@property
def last_trip(self) -> Trip:
return self.last_planned_trip.trip
def loaded_trips(self) -> Iterator[Trip]:
yield from (planned_trip.trip for planned_trip in self.loaded_planned_trips)

@property
def earliest(self) -> float:
Expand All @@ -162,17 +140,6 @@ def first_stop(self) -> Stop:
def last_stop(self) -> Stop:
return self.stops[-1]

@property
def last_time(self) -> float:
if not any(self.planned_trips):
return self.vehicle.earliest
return self.last_planned_trip.delivery_time

def time_at(self, idx: int) -> float:
if idx < 0:
return self.vehicle.earliest
return self.loaded_planned_trips[idx].delivery_time

@property
def vehicle_uuid(self) -> Optional[UUID]:
if self.vehicle is None:
Expand Down Expand Up @@ -224,7 +191,7 @@ def append_planned_trip(self, planned_trip: PlannedTrip):
assert planned_trip.delivery_stop.previous is not None
if len(self.stops) > 1:
assert planned_trip.pickup_stop.previous is not None
assert self.last_planned_trip.delivery_time <= planned_trip.pickup_time
assert self.latest <= planned_trip.pickup_time
assert planned_trip.pickup_stop == planned_trip.delivery_stop.previous
assert planned_trip.pickup_stop.latest <= planned_trip.delivery_stop.earliest
assert isnan(planned_trip.duration) or planned_trip.duration > 0
Expand Down
5 changes: 2 additions & 3 deletions jinete/storers/formatters/hashcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ def __init__(self, remove_empty_routs: bool = True, *args, **kwargs):

@staticmethod
def route_to_str(route) -> str:
loaded_trips = route.loaded_trips
trips_str = ' '.join(trip.identifier for trip in loaded_trips)
return f'{len(loaded_trips)} {trips_str}'
trips_str = ' '.join(trip.identifier for trip in route.loaded_trips)
return f'{len(route.loaded_trips)} {trips_str}'

def format(self) -> str:
result = str()
Expand Down

0 comments on commit e0dcdad

Please sign in to comment.