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

Commit

Permalink
* Included tests for "InsertionAlgorithm". #41
Browse files Browse the repository at this point in the history
* Improved tests for all "Algorithm" heirs.
* Fixed bug related with the criterion settings.
  • Loading branch information
garciparedes committed Sep 1, 2019
1 parent d8b83c9 commit 8ed70fa
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 13 deletions.
4 changes: 2 additions & 2 deletions jinete/algorithms/metaheuristics/grasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def build_first_solution_algorithm(self, *args, **kwargs) -> Algorithm:
args = (*self.args, *args)
kwargs.update(self.kwargs)

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

return IterativeAlgorithm(*args, **kwargs)

Expand Down
14 changes: 9 additions & 5 deletions jinete/models/criterions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


class PlannedTripCriterion(ABC):
def __init__(self, name: str, direction: OptimizationDirection):
def __init__(self, name: str, direction: OptimizationDirection, *args, **kwargs):
self.name = name
self.direction = direction

Expand All @@ -48,10 +48,11 @@ def nbest(self, n: int, planned_trips: Iterable[PlannedTrip], inplace: bool = Fa

class ShortestTimePlannedTripCriterion(PlannedTripCriterion):

def __init__(self):
def __init__(self, *args, **kwargs):
super().__init__(
direction=OptimizationDirection.MINIMIZATION,
name='Shortest-Time',
*args, **kwargs,
)

def scoring(self, planned_trip: PlannedTrip) -> float:
Expand All @@ -63,10 +64,11 @@ def scoring(self, planned_trip: PlannedTrip) -> float:

class LongestTimePlannedTripCriterion(PlannedTripCriterion):

def __init__(self):
def __init__(self, *args, **kwargs):
super().__init__(
direction=OptimizationDirection.MAXIMIZATION,
name='Longest-Time',
*args, **kwargs,
)

def scoring(self, planned_trip: PlannedTrip) -> float:
Expand All @@ -78,10 +80,11 @@ def scoring(self, planned_trip: PlannedTrip) -> float:

class LongestUtilTimePlannedTripCriterion(PlannedTripCriterion):

def __init__(self):
def __init__(self, *args, **kwargs):
super().__init__(
direction=OptimizationDirection.MAXIMIZATION,
name='Longest-Time',
*args, **kwargs,
)

def scoring(self, planned_trip: PlannedTrip) -> float:
Expand All @@ -93,10 +96,11 @@ def scoring(self, planned_trip: PlannedTrip) -> float:

class HashCodePlannedTripCriterion(PlannedTripCriterion):

def __init__(self):
def __init__(self, *args, **kwargs):
super().__init__(
direction=OptimizationDirection.MAXIMIZATION,
name='Longest-Time',
*args, **kwargs,
)

def scoring(self, planned_trip: PlannedTrip) -> float:
Expand Down
1 change: 1 addition & 0 deletions tests/test_algorithms/test_heuristics/test_insertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def test_optimize(self):
)
result = algorithm.optimize()

# TODO: Properly validate behaviour of the provided "Result" object.
self.assertIsNotNone(result)
self.assertIsInstance(result, jit.Result)

Expand Down
2 changes: 2 additions & 0 deletions tests/test_algorithms/test_heuristics/test_local_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class TestLocalSearchAlgorithm(unittest.TestCase):
job: jit.Job
fleet: jit.Fleet
initial: jit.Result

@classmethod
def setUpClass(cls) -> None:
Expand Down Expand Up @@ -40,6 +41,7 @@ def test_optimize(self):
)
result = algorithm.optimize()

# TODO: Properly validate behaviour of the provided "Result" object.
self.assertIsNotNone(result)
self.assertIsInstance(result, jit.Result)

Expand Down
28 changes: 22 additions & 6 deletions tests/test_algorithms/test_metaheuristics/test_grasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,32 @@


class TestGraspAlgorithm(unittest.TestCase):
job: jit.Job
fleet: jit.Fleet

@classmethod
def setUpClass(cls) -> None:
cls.job = jit.Job(generate_trips(10), objective_cls=jit.DialARideObjective)
cls.fleet = jit.Fleet(generate_vehicles(10))

def test_creation(self):
job = jit.Job(generate_trips(10), objective_cls=jit.DialARideObjective)
fleet = jit.Fleet(generate_vehicles(10))
algorithm = jit.GraspAlgorithm(
job=job,
fleet=fleet,
job=self.job,
fleet=self.fleet,
)
self.assertEqual(algorithm.job, job)
self.assertEqual(algorithm.fleet, fleet)
self.assertEqual(algorithm.job, self.job)
self.assertEqual(algorithm.fleet, self.fleet)

def test_optimize(self):
algorithm = jit.GraspAlgorithm(
job=self.job,
fleet=self.fleet,
)
result = algorithm.optimize()

# TODO: Properly validate behaviour of the provided "Result" object.
self.assertIsNotNone(result)
self.assertIsInstance(result, jit.Result)


if __name__ == '__main__':
Expand Down
41 changes: 41 additions & 0 deletions tests/test_algorithms/test_metaheuristics/test_iterative.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest

import jinete as jit

from tests.utils import (
generate_vehicles,
generate_trips,
)


class TestIterativeAlgorithm(unittest.TestCase):
job: jit.Job
fleet: jit.Fleet

@classmethod
def setUpClass(cls) -> None:
cls.job = jit.Job(generate_trips(10), objective_cls=jit.DialARideObjective)
cls.fleet = jit.Fleet(generate_vehicles(10))

def test_creation(self):
algorithm = jit.IterativeAlgorithm(
job=self.job,
fleet=self.fleet,
)
self.assertEqual(algorithm.job, self.job)
self.assertEqual(algorithm.fleet, self.fleet)

def test_optimize(self):
algorithm = jit.IterativeAlgorithm(
job=self.job,
fleet=self.fleet,
)
result = algorithm.optimize()

# TODO: Properly validate behaviour of the provided "Result" object.
self.assertIsNotNone(result)
self.assertIsInstance(result, jit.Result)


if __name__ == '__main__':
unittest.main()

0 comments on commit 8ed70fa

Please sign in to comment.