Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashank-1234 committed Oct 5, 2024
1 parent 10a0a8d commit 5df86d3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion tests/parse/test_parse_vrplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def test_parse_vrplib_with_edge_duration_section():
"EOF",
]
)

actual = parse_vrplib(instance)

# Expected results
Expand Down
8 changes: 2 additions & 6 deletions tests/write/test_write_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,7 @@ def test_duration_section(tmp_path):
name = "duration_test"
instance = {
"NAME": "duration_test_instance",
"EDGE_DURATION_SECTION": [
[0, 5, 8],
[5, 0, 6],
[8, 6, 0]
]
"EDGE_DURATION_SECTION": [[0, 5, 8], [5, 0, 6], [8, 6, 0]],
}

write_instance(tmp_path / name, instance)
Expand All @@ -181,7 +177,7 @@ def test_duration_section(tmp_path):
"5\t0\t6",
"8\t6\t0",
"EOF",
""
"",
]
)

Expand Down
21 changes: 15 additions & 6 deletions vrplib/parse/parse_durations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from typing import Optional, Union

import numpy as np
from typing import Union, Optional


def parse_durations(
data: list[list[float]],
data: list[list[Union[float, int, str]]],
edge_duration_type: str = "EXPLICIT", # Default to 'EXPLICIT'
edge_duration_format: Optional[str] = "FULL_MATRIX", # Default to 'FULL_MATRIX'
edge_duration_format: Optional[
str
] = "FULL_MATRIX", # Default to 'FULL_MATRIX'
**kwargs: Union[float, str, np.ndarray], # Optional keyword arguments
) -> np.ndarray:
"""
Expand Down Expand Up @@ -32,7 +36,12 @@ def parse_durations(
ValueError
If the edge_durations_type or edge_duration_format is unsupported.
"""
if edge_duration_type == "EXPLICIT" and edge_duration_format == "FULL_MATRIX":
if (
edge_duration_type == "EXPLICIT"
and edge_duration_format == "FULL_MATRIX"
):
return np.array(data)

raise ValueError(f"Unsupported durations_weight_type: {edge_duration_type} or format: {edge_duration_format}.")

raise ValueError(
f"Unsupported durations_weight_type: {edge_duration_type} or format: {edge_duration_format}."
)
2 changes: 1 addition & 1 deletion vrplib/parse/parse_vrplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def parse_section(
data = parse_distances(values, **instance) # type: ignore
elif name == "edge_duration":
# Parse duration weights separately as it involves extra processing.
data = parse_durations(values, **instance)
data = parse_durations(values, **instance)
elif name == "depot":
# Remove -1 end token and renormalize depots to start at zero.
data = np.array(values[0]) - 1
Expand Down
6 changes: 5 additions & 1 deletion vrplib/write/write_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ def _format_section(name: str, data: _ArrayLike) -> str:
A VRPLIB-formatted data section.
"""
section = [name]
include_idx = name not in ["EDGE_WEIGHT_SECTION", "DEPOT_SECTION", "EDGE_DURATION_SECTION"]
include_idx = name not in [
"EDGE_WEIGHT_SECTION",
"DEPOT_SECTION",
"EDGE_DURATION_SECTION",
]

if _is_one_dimensional(data):
# Treat 1D arrays as column vectors, so each element is a row.
Expand Down

0 comments on commit 5df86d3

Please sign in to comment.