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

Commit

Permalink
* Working on type hint improvements. #41
Browse files Browse the repository at this point in the history
  • Loading branch information
garciparedes committed Aug 31, 2019
1 parent c4950a2 commit 7f7ef87
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 20 deletions.
9 changes: 7 additions & 2 deletions jinete/models/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Any,
Dict,
Type,
Optional,
)
from .trips import (
Trip,
Expand All @@ -24,7 +25,8 @@

class Job(Model):
trips: Set[Trip]
objective: Objective
objective_cls: Type[Objective]
_objective: Optional[Objective]

def __init__(self, trips: Set[Trip], objective_cls: Type[Objective], *args, **kwargs):
if objective_cls is None:
Expand All @@ -34,10 +36,13 @@ def __init__(self, trips: Set[Trip], objective_cls: Type[Objective], *args, **kw
self.objective_cls = objective_cls
self._objective = None

self.args = args
self.kwargs = kwargs

@property
def objective(self) -> Objective:
if self._objective is None:
self._objective = self.objective_cls()
self._objective = self.objective_cls(*self.args, **self.kwargs)
return self._objective

def __iter__(self):
Expand Down
2 changes: 1 addition & 1 deletion jinete/models/objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _route_optimization_function(self, route: Route) -> float:
def _planned_trip_optimization_function(self, planned_trip: PlannedTrip) -> float:
scoring = 0.0
current = planned_trip.pickup
while current != planned_trip.delivery:
while current != planned_trip.delivery and current.following is not None:
current = current.following
scoring += current.distance
return scoring
Expand Down
3 changes: 3 additions & 0 deletions jinete/models/planned_trips.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import (
Dict,
Any,
Optional,
)
from uuid import (
UUID,
Expand Down Expand Up @@ -47,6 +48,8 @@ class PlannedTrip(Model):
trip: Trip
down_time: float

_feasible: Optional[bool]

def __init__(self, route: Route, trip: Trip, pickup: Stop, delivery: Stop, down_time: float = 0.0):
self.route = route
self.trip = trip
Expand Down
2 changes: 1 addition & 1 deletion jinete/models/positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class GeometricPosition(Position):
'coordinates',
)

coordinates: Tuple[float]
coordinates: Tuple[float, ...]

def __init__(self, coordinates: Sequence[float], *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion jinete/models/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def routes(self) -> Set[Route]:

@property
def completed_trips(self) -> Set[Trip]:
trips = set()
trips: Set[Trip] = set()
for route in self.routes:
trips |= set(route.loaded_trips)
return trips
Expand Down
8 changes: 4 additions & 4 deletions jinete/models/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@


class Route(Model):
planned_trips: List[PlannedTrip]
vehicle: Vehicle
uuid: UUID
stops: List[Stop]
Expand All @@ -59,10 +58,11 @@ def __init__(self, vehicle: Vehicle, stops: List[Stop] = None,
self.vehicle = vehicle

if stops is None:
stops = (
self.stops = [
Stop(self, self.vehicle.initial, None),
)
self.stops = list(stops)
]
else:
self.stops = list(stops)

def __iter__(self):
yield from self.planned_trips
Expand Down
26 changes: 20 additions & 6 deletions jinete/models/stops.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ class Stop(Model):
]
route: Route
position: Position
pickups: Tuple[PlannedTrip]
deliveries: Tuple[PlannedTrip]
pickups: Tuple[PlannedTrip, ...]
deliveries: Tuple[PlannedTrip, ...]

_down_time: Optional[float]
_load_time: Optional[float]
_earliest: Optional[float]
_arrival_time: Optional[float]
_previous_departure_time: Optional[float]

def __init__(self, route: Route, position: Position, previous: Optional[Stop], following: Optional[Stop] = None):

Expand All @@ -66,7 +72,6 @@ def __init__(self, route: Route, position: Position, previous: Optional[Stop], f
self.following = following

self._previous_departure_time = None

self._down_time = None
self._load_time = None
self._earliest = None
Expand All @@ -87,19 +92,28 @@ def extend_deliveries(self, iterable: Iterable[PlannedTrip]) -> None:
@property
def down_time(self) -> float:
if self._down_time is None:
self._down_time = max((pt.down_time for pt in self.pickups), default=0.0)
if not any(self.pickups):
self._down_time = 0.0
else:
self._down_time = max((pt.down_time for pt in self.pickups))
return self._down_time

@property
def earliest(self):
if self._earliest is None:
self._earliest = max((pt.trip.earliest for pt in self.pickups), default=0.0)
if not any(self.pickups):
self._earliest = 0.0
else:
self._earliest = max((pt.trip.earliest for pt in self.pickups))
return self._earliest

@property
def load_time(self) -> float:
if self._load_time is None:
self._load_time = max((pt.trip.load_time for pt in self.pickups), default=0.0)
if not any(self.pickups):
self._load_time = 0.0
else:
self._load_time = max((pt.trip.load_time for pt in self.pickups))
return self._load_time

@property
Expand Down
6 changes: 3 additions & 3 deletions jinete/models/surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

class Surface(Model, ABC):
uuid: UUID
positions: Set[Position]

def __init__(self, positions: Set[Position] = None, uuid: UUID = None, *args, **kwargs):
if uuid is None:
Expand Down Expand Up @@ -77,7 +78,6 @@ def as_dict(self) -> Dict[str, Any]:


class GeometricSurface(Surface):
positions: Set[GeometricPosition]
cached_distance: Dict[Position, Dict[Position, float]]

def __init__(self, metric: DistanceMetric, *args, **kwargs):
Expand All @@ -89,7 +89,7 @@ def __init__(self, metric: DistanceMetric, *args, **kwargs):
def _build_position(self, *args, **kwargs):
return GeometricPosition(surface=self, *args, **kwargs)

def distance(self, position_a: GeometricPosition, position_b: GeometricPosition) -> float:
def distance(self, position_a: Position, position_b: Position) -> float:
try:
distance = self.cached_distance[position_a][position_b]
except KeyError:
Expand All @@ -98,5 +98,5 @@ def distance(self, position_a: GeometricPosition, position_b: GeometricPosition)

return distance

def time(self, position_a: GeometricPosition, position_b: GeometricPosition, now: float) -> float:
def time(self, position_a: Position, position_b: Position, now: float) -> float:
return self.distance(position_a, position_b)
4 changes: 2 additions & 2 deletions jinete/models/vehicles.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def __init__(self, identifier: str, initial: Position, final: Position = None, c

if uuid is None:
uuid = uuid4()
if timeout is None:
timeout = MAX_FLOAT

self.identifier = identifier
self.initial = initial
Expand All @@ -56,8 +58,6 @@ def __init__(self, identifier: str, initial: Position, final: Position = None, c

@property
def latest(self) -> float:
if self.timeout is None:
return MAX_FLOAT
return self.earliest + self.timeout

@property
Expand Down

0 comments on commit 7f7ef87

Please sign in to comment.