Skip to content

Commit

Permalink
Add more parse tests (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonlan authored Feb 13, 2023
1 parent b82ac1b commit ff6ecb5
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
56 changes: 55 additions & 1 deletion tests/parse/test_parse_distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,45 @@


@pytest.mark.parametrize("edge_weight_type", ["EUC_4D", "MANHATTAN", "Lnorm"])
def test_invalid_edge_weight_type(edge_weight_type):
def test_unknown_edge_weight_type(edge_weight_type):
instance = {"edge_weight_type": edge_weight_type}
with pytest.raises(ValueError):
parse_distances([], **instance)


@pytest.mark.parametrize("edge_weight_format", ["", "empty", "HALF_MATRIX"])
def test_unknown_explicit_edge_weight_format(edge_weight_format):
"""
Tests if an error is raised when an unknown edge weight format is specified
together with an ``EXPLICIT`` edge weight type.
"""
instance = {
"edge_weight_type": "EXPLICIT",
"edge_weight_format": edge_weight_format,
}
with pytest.raises(ValueError):
parse_distances([], **instance)


@pytest.mark.parametrize(
"edge_weight_type, edge_weight_format",
[("", ""), ("IMPLICIT", "LOWER_ROW"), ("TEST", "ABCD")],
)
def test_unknown_edge_weight_type_and_format(
edge_weight_type, edge_weight_format
):
"""
Tests if an error is raised when an unknown edge weight type and edge
weight format are specified.
"""
instance = {
"edge_weight_type": edge_weight_type,
"edge_weight_format": edge_weight_format,
}
with pytest.raises(ValueError):
parse_distances([], **instance)


@pytest.mark.parametrize(
"edge_weight_type", ["EUC_2D", "FLOOR_2D", "EXACT_2D"]
)
Expand Down Expand Up @@ -49,6 +82,27 @@ def test_parse_euclidean_distances(edge_weight_type, desired):
assert_almost_equal(actual, desired)


@pytest.mark.parametrize(
"comment, func", [("Eilon", from_eilon), (None, from_lower_row)]
)
def test_parse_lower_row(comment, func):
"""
Tests if a ``LOWER ROW`` instance is parsed as Eilon instance or regular
instance. Eilon instances do not contain a proper lower row matrix, but
a lower column matrix instead. The current way of detecting an Eilon
instance is by means of the ``COMMENT`` field, which is checked for
including "Eilon".
"""
instance = {
"data": np.array([[1], [2, 3], [4, 5, 6]], dtype=object),
"edge_weight_type": "EXPLICIT",
"edge_weight_format": "LOWER_ROW",
"comment": comment,
}

assert_equal(parse_distances(**instance), func(instance["data"]))


def test_from_lower_row():
"""
Tests that a lower row triangular matrix is correctly transformed into a
Expand Down
5 changes: 5 additions & 0 deletions tests/parse/test_parse_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@pytest.mark.parametrize(
"text, data",
[
("", {"routes": []}), # empty solution
(
"Route #1: 1 2 3\n Route #2: 5 6\n COST: 10",
{"routes": [[1, 2, 3], [5, 6]], "cost": 10},
Expand All @@ -19,6 +20,10 @@
"Route #1: 1 \n Route #2: 6\n time 180.23",
{"routes": [[1], [6]], "time": 180.23},
),
( # skip lines without : or space
"Route #1: 1 \n Route #2: 6\n ABCDE",
{"routes": [[1], [6]]},
),
],
)
def test_parse_solution(text, data):
Expand Down
35 changes: 35 additions & 0 deletions tests/parse/test_parse_vrplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,41 @@ def test_parse_vrplib():
assert_equal(actual, desired)


def test_parse_vrplib_no_explicit_edge_weights():
"""
Tests if the edge weight section is calculated when the instance does not
contain an explicit section.
"""
instance = "\n".join(
[
"NAME: VRPLIB",
"EDGE_WEIGHT_TYPE: FLOOR_2D",
"NODE_COORD_SECTION",
"1 0 1",
"2 1 0",
"SERVICE_TIME_SECTION",
"1 1",
"2 1",
"TIME_WINDOW_SECTION",
"1 1 2",
"2 1 2",
"EOF",
]
)
actual = parse_vrplib(instance)

desired = {
"name": "VRPLIB",
"edge_weight_type": "FLOOR_2D",
"edge_weight": np.array([[0, 1], [1, 0]]),
"node_coord": np.array([[0, 1], [1, 0]]),
"service_time": np.array([1, 1]),
"time_window": np.array([[1, 2], [1, 2]]),
}

assert_equal(actual, desired)


def test_empty_text():
"""
Tests if an empty text file is still read correctly.
Expand Down
4 changes: 2 additions & 2 deletions vrplib/parse/parse_distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def parse_distances(
raise ValueError("Edge weight format unknown.")

if edge_weight_format == "LOWER_ROW":
# Eilon instances edge weight specifications are incorrect.
# TODO Find a better way to identify Eilon instances?
# TODO Eilon instances edge weight specifications are incorrect in
# (C)VRPLIB format. Find a better way to identify Eilon instances.
if comment is not None and "Eilon" in comment:
return from_eilon(data)
else:
Expand Down

0 comments on commit ff6ecb5

Please sign in to comment.