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

Commit

Permalink
* Now using "self.__iter__(...)" instead of "self.as_dict()" method. #49
Browse files Browse the repository at this point in the history
  • Loading branch information
garciparedes committed Oct 25, 2019
1 parent a1ca6c3 commit 03deb58
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 116 deletions.
2 changes: 1 addition & 1 deletion jinete/algorithms/utils/breeders/flips.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class FlipBreeder(Breeder):

def improve(self) -> Result:
for route in self.planning:
for route in self.planning.routes:
cost = self.objective.optimization_function(route)

for i in range(1, len(route.stops) - 1):
Expand Down
18 changes: 15 additions & 3 deletions jinete/models/abc.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Any, Dict
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import (
Any,
Generator,
Tuple,
)


class Model(ABC):
def __repr__(self):
return self._print(self.as_dict())
return self._print(dict(self))

def _print(self, values):
values = ', '.join(f'{key}={value}' for key, value in values.items())
return f'{self.__class__.__name__}({values})'

def _print_key_value(self, key: str, value: Any):
return f'{key}={value}'

@abstractmethod
def as_dict(self) -> Dict[str, Any]:
def __iter__(self) -> Generator[Tuple[str, Any], None, None]:
pass
18 changes: 7 additions & 11 deletions jinete/models/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from typing import (
Set,
Any,
Dict,
Generator,
Tuple,
Type,
Optional,
)
Expand Down Expand Up @@ -45,16 +46,11 @@ def objective(self) -> Objective:
self._objective = self.objective_cls(*self.args, **self.kwargs)
return self._objective

def __iter__(self):
yield from self.trips

def __deepcopy__(self, memo) -> Job:
return self

def as_dict(self) -> Dict[str, Any]:
trips_str = ', '.join(str(trip) for trip in self.trips)
dict_values = {
'trips': f'{{{trips_str}}}',
'objective': self.objective,
}
return dict_values
def __iter__(self) -> Generator[Tuple[str, Any], None, None]:
yield from (
('trip_identifiers', tuple(trip.identifier for trip in self.trips)),
('objective', self.objective),
)
2 changes: 1 addition & 1 deletion jinete/models/objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def _result_optimization_function(self, result: Result) -> float:

def _planning_optimization_function(self, planning: Planning) -> float:
scoring = 0.0
for route in planning:
for route in planning.routes:
scoring += self._route_optimization_function(route)
return scoring

Expand Down
21 changes: 11 additions & 10 deletions jinete/models/planned_trips.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

if TYPE_CHECKING:
from typing import (
Dict,
Any,
Optional,
Generator,
Tuple,
)
from uuid import (
UUID,
Expand Down Expand Up @@ -113,15 +114,15 @@ def feasible(self) -> bool:
def empty(self) -> bool:
return self.trip.empty

def as_dict(self) -> Dict[str, Any]:
return {
'route_uuid': self.route_uuid,
'trip_identifier': self.trip_identifier,
'pickup': self.pickup,
'delivery': self.delivery,
'down_time': self.down_time,
'feasible': self.feasible,
}
def __iter__(self) -> Generator[Tuple[str, Any], None, None]:
yield from (
('route_uuid', self.route_uuid),
('trip_identifier', self.trip_identifier),
('pickup', self.pickup),
('delivery', self.delivery),
('down_time', self.down_time),
('feasible', self.feasible),
)

def flush(self) -> None:
self._feasible = None
Expand Down
14 changes: 7 additions & 7 deletions jinete/models/plannings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
Dict,
Any,
Iterator,
Generator,
Tuple,
)
from .routes import (
Route,
Expand Down Expand Up @@ -46,9 +48,6 @@ def __init__(self, routes: Set[Route] = None, uuid: UUID = None):
self.routes = routes
self.uuid = uuid

def __iter__(self):
yield from self.routes

@property
def loaded_routes(self):
return set(route for route in self.routes if route.loaded)
Expand All @@ -62,10 +61,11 @@ def planned_trips(self) -> Iterator[PlannedTrip]:
def trips(self) -> Iterator[Trip]:
yield from (planned_trip.trip for planned_trip in self.planned_trips)

def as_dict(self) -> Dict[str, Any]:
return {
'uuid': self.uuid,
}
def __iter__(self) -> Generator[Tuple[str, Any], None, None]:
yield from (
('uuid', self.uuid),
('route_uuids', tuple(route.uuid for route in self.routes))
)

def __deepcopy__(self, memo: Dict[int, Any]) -> Planning:
planning = Planning()
Expand Down
23 changes: 18 additions & 5 deletions jinete/models/positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
import logging
from abc import (
ABC,
abstractmethod,
)
from typing import (
TYPE_CHECKING,
)

from .abc import (
Model,
)

if TYPE_CHECKING:
from typing import (
Tuple,
Sequence,
Generator,
Dict,
Any,
)
Expand All @@ -22,16 +28,18 @@
logger = logging.getLogger(__name__)


class Position(ABC):
class Position(Model, ABC):
def __init__(self, surface: Surface):
self.surface = surface

def __eq__(self, other):
return hash(self) == hash(other)

def distance_to(self, other: Position) -> float:
return self.surface.distance(self, other)

@property
@abstractmethod
def coordinates(self) -> Tuple[Any]:
pass

def time_to(self, other: Position, now: float = None) -> float:
return self.surface.time(self, other, now=now)

Expand Down Expand Up @@ -59,8 +67,13 @@ def __eq__(self, other) -> bool:
def __ne__(self, other) -> bool:
return self.coordinates != other.coordinates

def __iter__(self) -> Generator[Tuple[str, Any], None, None]:
yield from (
('coordinates', self.coordinates),
)

def __str__(self):
c = ",".join(f"{x:07.3f}" for x in self)
c = ",".join(f"{x:07.3f}" for x in self.coordinates)
return f'({c})'

def __getitem__(self, item):
Expand Down
22 changes: 11 additions & 11 deletions jinete/models/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
Dict,
Optional,
Iterable,
Generator,
Tuple,
)
from uuid import (
UUID,
Expand Down Expand Up @@ -64,9 +66,6 @@ def __init__(self, vehicle: Vehicle, stops: List[Stop] = None, uuid: UUID = None
else:
self.stops = list(stops)

def __iter__(self):
yield from self.planned_trips

def __deepcopy__(self, memo: Dict[int, Any]) -> Route:
vehicle = deepcopy(self.vehicle, memo)

Expand Down Expand Up @@ -174,16 +173,17 @@ def last_stop(self) -> Stop:
return stop

@property
def vehicle_uuid(self) -> Optional[UUID]:
def vehicle_identifier(self) -> Optional[str]:
if self.vehicle is None:
return None
return self.vehicle.uuid

def as_dict(self) -> Dict[str, Any]:
return {
'uuid': self.uuid,
'vehicle_uuid': self.vehicle_uuid,
}
return self.vehicle.identifier

def __iter__(self) -> Generator[Tuple[str, Any], None, None]:
yield from (
('uuid', self.uuid),
('vehicle_identifier', self.vehicle_identifier),
('trip_identifiers', tuple(trip.identifier for trip in self.trips))
)

def conjecture_trip(self, trip: Trip) -> PlannedTrip:
pickup = Stop(self, trip.origin_position, self.last_stop)
Expand Down
7 changes: 3 additions & 4 deletions jinete/models/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from typing import (
Dict,
Any,
Generator,
Tuple,
)
from .positions import (
Position,
Expand Down Expand Up @@ -50,17 +52,14 @@ def __eq__(self, other: Service) -> bool:
def __hash__(self):
return hash(tuple(self))

def __iter__(self):
def __iter__(self) -> Generator[Tuple[str, Any], None, None]:
yield from (
('position', self.position),
('earliest', self.earliest),
('latest', self.latest),
('duration', self.duration),
)

def as_dict(self) -> Dict[str, Any]:
return dict(self)

def distance_to(self, other: Service) -> float:
return self.position.distance_to(other.position)

Expand Down
21 changes: 7 additions & 14 deletions jinete/models/stops.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
)

if TYPE_CHECKING:
from uuid import (
UUID,
)
from typing import (
Dict,
Generator,
Tuple,
Any,
Optional,
Iterable,
Tuple,
List,
)
from .positions import (
Expand Down Expand Up @@ -131,10 +128,6 @@ def load_time(self) -> float:
def vehicle(self) -> Vehicle:
return self.route.vehicle

@property
def vehicle_uuid(self) -> UUID:
return self.vehicle.uuid

@property
def stops(self) -> List[Stop]:
return self.route.stops
Expand Down Expand Up @@ -180,11 +173,11 @@ def arrival_time(self):
def departure_time(self) -> float:
return self.arrival_time + self.load_time

def as_dict(self) -> Dict[str, Any]:
return {
'vehicle_uuid': self.vehicle_uuid,
'position': self.position,
}
def __iter__(self) -> Generator[Tuple[str, Any], None, None]:
yield from (
('route_uuid', self.route.uuid),
('position', self.position),
)

def flush(self) -> None:
self._down_time = None
Expand Down
14 changes: 7 additions & 7 deletions jinete/models/surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
Set,
Any,
Dict,
Generator,
Tuple,
)
from uuid import (
UUID,
Expand Down Expand Up @@ -69,12 +71,10 @@ def distance(self, position_a: Position, position_b: Position) -> float:
def time(self, position_a: Position, position_b: Position, **kwargs) -> float:
pass

def as_dict(self) -> Dict[str, Any]:
positions_str = ', '.join(str(position) for position in self.positions)
dict_values = {
'positions': f'{{{positions_str}}}'
}
return dict_values
def __iter__(self) -> Generator[Tuple[str, Any], None, None]:
yield from (
('position_coordinates', tuple(position.coordinates for position in self.positions)),
)


class GeometricSurface(Surface):
Expand All @@ -93,7 +93,7 @@ def distance(self, position_a: Position, position_b: Position) -> float:
try:
distance = self.cached_distance[position_a][position_b]
except KeyError:
distance = self.metric(position_a, position_b)
distance = self.metric(position_a.coordinates, position_b.coordinates)
self.cached_distance[position_a][position_b] = distance

return distance
Expand Down
Loading

0 comments on commit 03deb58

Please sign in to comment.