Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing multiple depots #126

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/parse/test_parse_vrplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def test_parse_specification(line, key, value):
["demand", np.array([1.1, 2.2, 3.3])],
),
(
["DEPOT_SECTION", "1", "-1"],
["depot", np.array([0])],
["DEPOT_SECTION", "1", "2", "3", "-1"],
["depot", np.array([0, 1, 2])],
),
(
# No end token in DEPOT_SECTION
Expand Down
13 changes: 7 additions & 6 deletions vrplib/parse/parse_vrplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,21 @@ def parse_section(
Parses the data section lines.
"""
name = lines[0].strip().removesuffix("_SECTION").lower()
values = [[infer_type(n) for n in line.split()] for line in lines[1:]]
rows = [[infer_type(n) for n in line.split()] for line in lines[1:]]

if name == "edge_weight":
# Parse edge weights separately as it involves extra processing.
data = parse_distances(values, **instance) # type: ignore
data = parse_distances(rows, **instance) # type: ignore
elif name == "depot":
# Remove -1 end token and renormalize depots to start at zero.
data = np.array(values[0]) - 1
elif any(len(row) != len(values[0]) for row in values):
data = np.array(rows)
data = data[data != -1] - 1
elif any(len(row) != len(rows[0]) for row in rows):
# This is a ragged array, so we keep it as a nested list, but we
# remove the indices column.
data = [row[1:] for row in values]
data = [row[1:] for row in rows]
else:
data = np.array([row[1:] for row in values])
data = np.array([row[1:] for row in rows])

if data.ndim > 1 and data.shape[-1] == 1:
# Squeeze data lines that contain only one column.
Expand Down
Loading