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

Commit

Permalink
* Merged master on to "issue-41". #41
Browse files Browse the repository at this point in the history
  • Loading branch information
garciparedes committed Aug 31, 2019
1 parent b2648a2 commit a8b7298
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 18 deletions.
2 changes: 2 additions & 0 deletions jinete/algorithms/heuristics/local_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ def _optimize(self) -> Planning:
best = self.objective.best(best, current)
again = best == current

assert best is not None

return best.planning
16 changes: 7 additions & 9 deletions jinete/algorithms/metaheuristics/grasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,18 @@ def build_first_solution_algorithm(self, *args, **kwargs) -> Algorithm:
args = (*self.args, *args)
kwargs.update(self.kwargs)

return IterativeAlgorithm(
episodes=self.first_solution_episodes,
seed=self.random.randint(0, MAX_INT),
*args, **kwargs,
)
kwargs['episodes'] = self.first_solution_episodes,
kwargs['seed'] = self.random.randint(0, MAX_INT),

return IterativeAlgorithm(*args, **kwargs)

def build_local_search_algorithm(self, *args, **kwargs) -> Algorithm:
args = (*self.args, *args)
kwargs.update(self.kwargs)

return LocalSearchAlgorithm(
seed=self.random.randint(0, MAX_INT),
*args, **kwargs,
)
kwargs['seed'] = self.random.randint(0, MAX_INT),

return LocalSearchAlgorithm(*args, **kwargs)

def again(self, episode_count: int, *args, **kwargs):
return episode_count < self.first_solution_episodes
Expand Down
13 changes: 11 additions & 2 deletions jinete/algorithms/metaheuristics/iterative.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
if TYPE_CHECKING:
from typing import (
Type,
Optional,
)
from ...models import (
Result,
)

logger = logging.getLogger(__name__)
Expand All @@ -37,12 +41,17 @@ def __init__(self, episodes: int = 100, algorithm_cls: Type[Algorithm] = None, s
self.kwargs = kwargs

def build_algorithm(self, *args, **kwargs) -> Algorithm:
return self.algorithm_cls(*self.args, *args, **self.kwargs, **kwargs)
args = (*self.args, *args)
kwargs.update(self.kwargs)

return self.algorithm_cls(*args, **kwargs)

def _optimize(self) -> Planning:
best = None
best: Optional[Result] = None
for i in range(self.episodes):
seed = self.random.randint(0, MAX_INT)
current = self.build_algorithm(seed=seed).optimize()
best = self.objective.best(best, current)

assert best is not None
return best.planning
2 changes: 1 addition & 1 deletion jinete/models/criterions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, name: str, direction: OptimizationDirection):
def scoring(self, planned_trip: PlannedTrip) -> float:
pass

def best(self, *args: Optional[PlannedTrip]) -> Optional[PlannedTrip]:
def best(self, *args: Optional[PlannedTrip]) -> PlannedTrip:
return self.direction(
(arg for arg in args if arg is not None),
key=self.scoring,
Expand Down
2 changes: 1 addition & 1 deletion jinete/models/objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, name: str, direction: OptimizationDirection):
self.name = name
self.direction = direction

def best(self, *args: Optional[Optimizable]) -> Optional[Optimizable]:
def best(self, *args: Optional[Optimizable]) -> Optimizable:
return self.direction(
(arg for arg in args if arg is not None),
key=self.optimization_function,
Expand Down
2 changes: 2 additions & 0 deletions jinete/models/plannings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Planning(Model):
def __init__(self, routes: Set[Route] = None, uuid: UUID = None):
if uuid is None:
uuid = uuid4()
if routes is None:
routes = set()

self.routes = routes
self.uuid = uuid
Expand Down
2 changes: 1 addition & 1 deletion jinete/models/stops.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Stop(Model):
_previous_departure_time: Optional[float]

def __init__(self, route: Route, position: Position, previous: Optional[Stop], following: Optional[Stop] = None,
pickups: Tuple[PlannedTrip] = tuple(), deliveries: Tuple[PlannedTrip] = tuple()):
pickups: Tuple[PlannedTrip, ...] = tuple(), deliveries: Tuple[PlannedTrip, ...] = tuple()):

self.route = route
self.position = position
Expand Down
2 changes: 0 additions & 2 deletions tests/test_algorithms/test_metaheuristics/test_grasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ def test_creation(self):
job = jit.Job(generate_trips(10), objective_cls=jit.DialARideObjective)
fleet = jit.Fleet(generate_vehicles(10))
algorithm = jit.GraspAlgorithm(
algorithm_cls=jit.InsertionAlgorithm,
job=job,
fleet=fleet,
)
self.assertEqual(algorithm.algorithm_cls, jit.InsertionAlgorithm)
self.assertEqual(algorithm.job, job)
self.assertEqual(algorithm.fleet, fleet)

Expand Down
13 changes: 11 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from random import uniform, randint
from typing import Set

import jinete as jit

if TYPE_CHECKING:
from typing import (
Set,
Type,
Optional,
)


def generate_one_surface(*args, **kwargs) -> jit.Surface:
kwargs['metric'] = jit.DistanceMetric.MANHATTAN
Expand Down Expand Up @@ -63,7 +72,7 @@ def generate_trips(n: int, *args, **kwargs) -> Set[jit.Trip]:


def generate_one_job(trips_count: int = None, trips_count_min: int = 1, trips_count_max: int = 100,
objective_cls: jit.Objective = None, *args, **kwargs) -> jit.Job:
objective_cls: Optional[Type[jit.Objective]] = None, *args, **kwargs) -> jit.Job:
if trips_count is None:
trips_count = randint(trips_count_min, trips_count_max)
if objective_cls is None:
Expand Down

0 comments on commit a8b7298

Please sign in to comment.