forked from loggi/loggibud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request loggi#9 from loggi/fillipe/update-task1-tests
Add Great Circle distance to tests in task 1
- Loading branch information
Showing
5 changed files
with
214 additions
and
36 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 was deleted.
Oops, something went wrong.
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,79 @@ | ||
import json | ||
|
||
import pytest | ||
from dacite import from_dict | ||
from mock import patch | ||
|
||
from loggibud.v1.types import CVRPInstance | ||
from loggibud.v1.baselines.shared import ortools | ||
from loggibud.v1.baselines.task1 import lkh_3 | ||
from loggibud.v1.distances import ( | ||
calculate_distance_matrix_great_circle_m, | ||
calculate_route_distance_great_circle_m, | ||
) | ||
from loggibud.v1.eval.task1 import evaluate_solution | ||
|
||
|
||
@pytest.fixture | ||
def toy_cvrp_instance(): | ||
with open("tests/results/cvrp-instances/train/rj-0/cvrp-0-rj-0.json") as f: | ||
data = json.load(f) | ||
|
||
return from_dict(CVRPInstance, data) | ||
|
||
|
||
@pytest.fixture | ||
def mocked_ortools_osrm_distance_matrix(): | ||
"""Monkey-patch the OR-Tools OSRM distance matrix with the Great Circle""" | ||
|
||
with patch( | ||
"loggibud.v1.baselines.shared.ortools.calculate_distance_matrix_m", | ||
new=calculate_distance_matrix_great_circle_m, | ||
) as mock_osrm: | ||
yield mock_osrm | ||
|
||
|
||
@pytest.fixture | ||
def mocked_lkh_osrm_distance_matrix(): | ||
"""Monkey-patch the LKH OSRM distance matrix with the Great Circle""" | ||
|
||
with patch( | ||
"loggibud.v1.data_conversion.calculate_distance_matrix_m", | ||
new=calculate_distance_matrix_great_circle_m, | ||
) as mock_osrm: | ||
yield mock_osrm | ||
|
||
|
||
@pytest.fixture | ||
def mocked_osrm_route_distance(): | ||
"""Monkey-patch the OSRM route distance with the Great Circle""" | ||
|
||
with patch( | ||
"loggibud.v1.eval.task1.calculate_route_distance_m", | ||
new=calculate_route_distance_great_circle_m, | ||
) as mock_osrm: | ||
yield mock_osrm | ||
|
||
|
||
def test_ortools_solver( | ||
toy_cvrp_instance, | ||
mocked_ortools_osrm_distance_matrix, | ||
mocked_osrm_route_distance, | ||
): | ||
result = ortools.solve(toy_cvrp_instance) | ||
|
||
total_distance = evaluate_solution(toy_cvrp_instance, result) | ||
|
||
assert total_distance | ||
|
||
|
||
def test_lkh_solver( | ||
toy_cvrp_instance, | ||
mocked_lkh_osrm_distance_matrix, | ||
mocked_osrm_route_distance, | ||
): | ||
result = lkh_3.solve(toy_cvrp_instance) | ||
|
||
total_distance = evaluate_solution(toy_cvrp_instance, result) | ||
|
||
assert total_distance |
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,102 @@ | ||
import json | ||
|
||
import pytest | ||
from dacite import from_dict | ||
from mock import patch | ||
from pathlib import Path | ||
|
||
from loggibud.v1.types import CVRPInstance | ||
from loggibud.v1.baselines.shared.ortools import ORToolsParams | ||
from loggibud.v1.baselines.task2 import kmeans_greedy, qrp_sweep | ||
from loggibud.v1.distances import ( | ||
calculate_distance_matrix_great_circle_m, | ||
calculate_route_distance_great_circle_m, | ||
) | ||
from loggibud.v1.eval.task1 import evaluate_solution | ||
|
||
|
||
@pytest.fixture | ||
def train_instances(): | ||
file_instances = Path("tests/results/cvrp-instances/train/rj-0/").rglob( | ||
"*.json" | ||
) | ||
|
||
def load_instance(file_instance): | ||
with open(file_instance) as f: | ||
data = json.load(f) | ||
|
||
return from_dict(CVRPInstance, data) | ||
|
||
instances = [ | ||
load_instance(file_instance) for file_instance in file_instances | ||
] | ||
|
||
return instances | ||
|
||
|
||
@pytest.fixture | ||
def dev_instance(): | ||
with open("tests/results/cvrp-instances/dev/rj-0/cvrp-0-rj-3.json") as f: | ||
data = json.load(f) | ||
|
||
return from_dict(CVRPInstance, data) | ||
|
||
|
||
@pytest.fixture | ||
def mocked_ortools_osrm_distance_matrix(): | ||
"""Monkey-patch the OR-Tools OSRM distance matrix with the Great Circle""" | ||
|
||
with patch( | ||
"loggibud.v1.baselines.shared.ortools.calculate_distance_matrix_m", | ||
new=calculate_distance_matrix_great_circle_m, | ||
) as mock_osrm: | ||
yield mock_osrm | ||
|
||
|
||
@pytest.fixture | ||
def mocked_osrm_route_distance(): | ||
"""Monkey-patch the OSRM route distance with the Great Circle""" | ||
|
||
with patch( | ||
"loggibud.v1.eval.task1.calculate_route_distance_m", | ||
new=calculate_route_distance_great_circle_m, | ||
) as mock_osrm: | ||
yield mock_osrm | ||
|
||
|
||
def test_kmeans_greedy_solver( | ||
train_instances, | ||
dev_instance, | ||
mocked_osrm_route_distance, | ||
mocked_ortools_osrm_distance_matrix, | ||
): | ||
# Limit OR-Tools TSP solver to 100 ms (this is just a test, a good solution | ||
# is not required) | ||
params = kmeans_greedy.KMeansGreedyParams( | ||
ortools_tsp_params=ORToolsParams(time_limit_ms=100) | ||
) | ||
model = kmeans_greedy.pretrain(train_instances, params=params) | ||
result = kmeans_greedy.solve_instance(model, dev_instance) | ||
|
||
total_distance = evaluate_solution(dev_instance, result) | ||
|
||
assert total_distance | ||
|
||
|
||
def test_qrp_sweep_solver( | ||
train_instances, | ||
dev_instance, | ||
mocked_osrm_route_distance, | ||
mocked_ortools_osrm_distance_matrix, | ||
): | ||
# Limit OR-Tools TSP solver to 100 ms (this is just a test, a good solution | ||
# is not required) | ||
params = qrp_sweep.QRPParams( | ||
ortools_tsp_params=ORToolsParams(time_limit_ms=100) | ||
) | ||
model = qrp_sweep.pretrain(train_instances, params=params) | ||
result = qrp_sweep.solve_instance(model, dev_instance) | ||
|
||
total_distance = evaluate_solution(dev_instance, result) | ||
|
||
assert total_distance |