Skip to content

Commit

Permalink
Changes to coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickOHara committed Aug 22, 2024
1 parent 2c1fcc0 commit 062f7f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
54 changes: 29 additions & 25 deletions tspwplib/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,32 +273,36 @@ def from_tsplib95(cls, problem: tsplib95.models.StandardProblem):
if problem.node_coord_type
else NodeCoordType.NO_COORDS
)
if node_coord_type == NodeCoordType.NO_COORDS:
# do verifcation on node coords
node_coords = {i: problem.node_coords.get(i) for i in problem.get_nodes()}
key_error = False
no_coords = False
try:
coord = node_coords.get(1)
if not coord:
no_coords = True
except KeyError:
key_error = True
if key_error or len(node_coords) == 0 or no_coords:
node_coord_type == NodeCoordType.NO_COORDS
node_coords = None
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
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
13 changes: 8 additions & 5 deletions tspwplib/sparse.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
"""Functions for creating a sparse graph from a complete graph"""

from typing import Optional
from copy import deepcopy
import random
import networkx as nx
import numpy as np
from .types import Vertex


def sparsify_uid(
G: nx.Graph,
kappa: int,
remove_self_loops: bool = False,
seed: int = 0,
generator: Optional[np.random.Generator] = None,
) -> nx.Graph:
"""Remove edges with uniform and independent (uid) probability until
the number of edges equals kappa * number of nodes
Expand All @@ -19,23 +21,24 @@ def sparsify_uid(
G: Graph
kappa: Parameter independent of the input size
remove_self_loops: Should self loops have a change of being removed?
seed: Set the random seed
generator: Pass an optional random generator
Returns:
Graph with kappa * V edges where V is the number of nodes
Notes:
A copy of the graph is made and returned. The original graph is unedited.
"""
random.seed(seed)
if not generator:
generator = np.random.default_rng()
graph_copy = deepcopy(G)
vertex_list = list(graph_copy.nodes())
while graph_copy.number_of_edges() > graph_copy.number_of_nodes() * kappa:
# choose vertex randomly
u = random.choice(vertex_list)
u = generator.choice(vertex_list)
if graph_copy.degree(u) > 0:
# choose a neighbor randomly
v = random.choice(list(graph_copy.neighbors(u)))
v = generator.choice(list(graph_copy.neighbors(u)))
# remove edge if it is not a self loop
if remove_self_loops or u != v:
graph_copy.remove_edge(u, v)
Expand Down

0 comments on commit 062f7f5

Please sign in to comment.