Skip to content

Commit

Permalink
Fix verification of node coords
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickOHara committed Jul 8, 2024
1 parent cb95c1e commit 2c1fcc0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
4 changes: 4 additions & 0 deletions tspwplib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
LondonaqLocationShort,
LondonaqTimestamp,
LondonaqTimestampId,
NodeCoords,
NodeCoordType,
OptimalSolutionTSP,
SimpleEdgeFunction,
SimpleEdgeList,
Expand Down Expand Up @@ -102,6 +104,8 @@
"LondonaqLocationShort",
"LondonaqTimestamp",
"LondonaqTimestampId",
"NodeCoords",
"NodeCoordType",
"NotSimpleException",
"NotSimpleCycleException",
"NotSimplePathException",
Expand Down
47 changes: 25 additions & 22 deletions tspwplib/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,29 +273,32 @@ def from_tsplib95(cls, problem: tsplib95.models.StandardProblem):
if problem.node_coord_type
else NodeCoordType.NO_COORDS
)

node_coords = {i: problem.node_coords.get(i) for i in problem.get_nodes()}
if len(node_coords) == 0:
node_coord_type == NodeCoordType.NO_COORDS
if node_coord_type == NodeCoordType.NO_COORDS:
node_coords = None
raise RuntimeWarning(f"Problem {problem.name} has no node co-ordinates.")
elif (
len(node_coords.get(1)) == 3
or node_coord_type == NodeCoordType.THREED_COORDS
):
raise NotImplementedError("3D coords not yet supported")
elif (
len(node_coords.get(1)) == 2
and node_coord_type == NodeCoordType.THREED_COORDS
) or (
len(node_coords.get(1)) == 3
and node_coord_type == NodeCoordType.TWOD_COORDS
):
raise ValueError(
f"Problem {problem.name} has NODE_COORD_TYPE {node_coord_type.value}, but the length of the co-ordinates for node 1 is {len(node_coords.get(1))}."
)
elif len(node_coords.get(1)) == 2:
node_coord_type = NodeCoordType.TWOD_COORDS
else:
# do verifcation on node coords
node_coords = {i: problem.node_coords.get(i) for i in problem.get_nodes()}
if len(node_coords) == 0:
node_coord_type == NodeCoordType.NO_COORDS
node_coords = None
raise RuntimeWarning(f"Problem {problem.name} has no node co-ordinates.")
elif (
len(node_coords.get(1)) == 3
or node_coord_type == NodeCoordType.THREED_COORDS
):
raise NotImplementedError("3D coords not yet supported")
elif (
len(node_coords.get(1)) == 2
and node_coord_type == NodeCoordType.THREED_COORDS
) or (
len(node_coords.get(1)) == 3
and node_coord_type == NodeCoordType.TWOD_COORDS
):
raise ValueError(
f"Problem {problem.name} has NODE_COORD_TYPE {node_coord_type.value}, but the length of the co-ordinates for node 1 is {len(node_coords.get(1))}."
)
elif len(node_coords.get(1)) == 2:
node_coord_type = NodeCoordType.TWOD_COORDS

return cls(
capacity=problem.capacity,
Expand Down

0 comments on commit 2c1fcc0

Please sign in to comment.