From 2c1fcc047c40131be4debb0d0bf954a4f9551681 Mon Sep 17 00:00:00 2001 From: PatrickOHara Date: Mon, 8 Jul 2024 14:20:22 +0100 Subject: [PATCH] Fix verification of node coords --- tspwplib/__init__.py | 4 ++++ tspwplib/problem.py | 47 +++++++++++++++++++++++--------------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/tspwplib/__init__.py b/tspwplib/__init__.py index 348a3a6..f5a32a5 100644 --- a/tspwplib/__init__.py +++ b/tspwplib/__init__.py @@ -59,6 +59,8 @@ LondonaqLocationShort, LondonaqTimestamp, LondonaqTimestampId, + NodeCoords, + NodeCoordType, OptimalSolutionTSP, SimpleEdgeFunction, SimpleEdgeList, @@ -102,6 +104,8 @@ "LondonaqLocationShort", "LondonaqTimestamp", "LondonaqTimestampId", + "NodeCoords", + "NodeCoordType", "NotSimpleException", "NotSimpleCycleException", "NotSimplePathException", diff --git a/tspwplib/problem.py b/tspwplib/problem.py index 40ef395..492a97a 100644 --- a/tspwplib/problem.py +++ b/tspwplib/problem.py @@ -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,