This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implemented "PlannedTripCriterion" object to decide what selection …
…criterion to use simply picking a class. #24
- Loading branch information
1 parent
fab046d
commit 3208479
Showing
15 changed files
with
132 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from enum import ( | ||
Enum, | ||
unique, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from typing import ( | ||
Callable, | ||
Iterable, | ||
TypeVar, | ||
) | ||
|
||
T = TypeVar('T') | ||
|
||
|
||
@unique | ||
class OptimizationDirection(Enum): | ||
MAXIMIZATION = max | ||
MINIMIZATION = min | ||
|
||
@property | ||
def fn(self) -> Callable[[Iterable[T]], T]: | ||
return self.value | ||
|
||
def __str__(self): | ||
return f'{self.name.capitalize()}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import TYPE_CHECKING | ||
|
||
from .constants import ( | ||
OptimizationDirection, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from typing import ( | ||
Iterable, | ||
Optional, | ||
List, | ||
) | ||
from .planned_trips import ( | ||
PlannedTrip, | ||
) | ||
|
||
|
||
class PlannedTripCriterion(ABC): | ||
def __init__(self, name: str, direction: OptimizationDirection): | ||
self.name = name | ||
self.direction = direction | ||
|
||
@abstractmethod | ||
def scoring(self, planned_trip: PlannedTrip) -> float: | ||
pass | ||
|
||
def best(self, *args: PlannedTrip) -> Optional[PlannedTrip]: | ||
return self.direction.fn( | ||
(arg for arg in args if arg is not None), | ||
key=self.scoring, | ||
default=None, | ||
) | ||
|
||
def sorted(self, arr: List[PlannedTrip], inplace: bool = False) -> List[PlannedTrip]: | ||
if inplace: | ||
arr.sort(key=self.scoring) | ||
else: | ||
arr = sorted(arr, key=self.scoring) | ||
return arr | ||
|
||
|
||
class ShortestTimePlannedTripCriterion(PlannedTripCriterion): | ||
|
||
def __init__(self): | ||
super().__init__( | ||
direction=OptimizationDirection.MAXIMIZATION, | ||
name='Shortest-Time', | ||
) | ||
|
||
def scoring(self, planned_trip: PlannedTrip) -> float: | ||
return planned_trip.collection_time - planned_trip.route.last_time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters