Skip to content

Commit

Permalink
Merge pull request #12 from PatrickOHara/name_changes
Browse files Browse the repository at this point in the history
🚚  name changes
  • Loading branch information
PatrickOHara authored Jan 7, 2021
2 parents eb7549b + 1efced9 commit 46f2a29
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# python cache
*.egg-info
__pycache__
.eggs

# misc
.vscode
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ mkdocs-material>=6.0.2
mypy>=0.782
pylint>=2.6.0
pytest>=6.1.1
pytest-tspwplib>=0.2.0b1
pytest-tspwplib>=0.2.3
4 changes: 2 additions & 2 deletions tests/test_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def test_is_complete_with_self_loops():
assert is_complete_with_self_loops(graph)


def test_tsplib_is_complete(tsplib_root, instance_name):
def test_tsplib_is_complete(tsplib_root, graph_name):
"""Test each instance of TSPLIB95 is a complete graph"""
filepath = build_path_to_tsplib_instance(tsplib_root, instance_name)
filepath = build_path_to_tsplib_instance(tsplib_root, graph_name)
problem = tsplib95.load(filepath)
graph = problem.get_graph()
assert is_complete_with_self_loops(graph)
18 changes: 9 additions & 9 deletions tests/test_profits_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from tspwplib.utils import build_path_to_oplib_instance


def test_parse_profits_problem(oplib_root, generation, instance_name, alpha):
def test_parse_profits_problem(oplib_root, generation, graph_name, alpha):
"""Test an OP instance can be parsed"""
filepath = build_path_to_oplib_instance(
oplib_root, generation, instance_name, alpha=alpha
oplib_root, generation, graph_name, alpha=alpha
)
assert "COST_LIMIT" in ProfitsProblem.fields_by_keyword
assert "NODE_SCORE_SECTION" in ProfitsProblem.fields_by_keyword
Expand All @@ -19,22 +19,22 @@ def test_parse_profits_problem(oplib_root, generation, instance_name, alpha):
assert problem.is_complete()


def test_get_cost_limit(oplib_root, generation, instance_name, alpha):
def test_get_cost_limit(oplib_root, generation, graph_name, alpha):
"""Test we can get the cost limit"""
filepath = build_path_to_oplib_instance(
oplib_root, generation, instance_name, alpha=alpha
oplib_root, generation, graph_name, alpha=alpha
)
problem = ProfitsProblem.load(filepath)
expected_cost_limit = math.ceil(
OptimalSolutionTSP[instance_name] * (alpha.value / 100.0)
OptimalSolutionTSP[graph_name] * (alpha.value / 100.0)
)
assert problem.get_cost_limit() == expected_cost_limit


def test_get_node_score(oplib_root, generation, instance_name, alpha):
def test_get_node_score(oplib_root, generation, graph_name, alpha):
"""Test every node has a prize"""
filepath = build_path_to_oplib_instance(
oplib_root, generation, instance_name, alpha=alpha
oplib_root, generation, graph_name, alpha=alpha
)
problem = ProfitsProblem.load(filepath)
node_scores = problem.get_node_score()
Expand All @@ -45,10 +45,10 @@ def test_get_node_score(oplib_root, generation, instance_name, alpha):
assert value >= 0


def test_get_graph(oplib_root, generation, instance_name, alpha):
def test_get_graph(oplib_root, generation, graph_name, alpha):
"""Test we can load a graph with prizes"""
filepath = build_path_to_oplib_instance(
oplib_root, generation, instance_name, alpha=alpha
oplib_root, generation, graph_name, alpha=alpha
)
problem = ProfitsProblem.load(filepath)
graph = problem.get_graph()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_sparsity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@


def test_remove_random_edges_from_graph(
tsplib_root, instance_name, edge_removal_probability
tsplib_root, graph_name, edge_removal_probability
):
"""Test the right number of edges are removed"""
filepath = build_path_to_tsplib_instance(tsplib_root, instance_name)
filepath = build_path_to_tsplib_instance(tsplib_root, graph_name)
problem = StandardProblem.load(filepath)
complete_graph = problem.get_graph()
assert is_complete_with_self_loops(complete_graph)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from tspwplib.utils import build_path_to_oplib_instance, build_path_to_tsplib_instance


def test_build_path_to_oplib_instance(oplib_root, generation, instance_name, alpha):
def test_build_path_to_oplib_instance(oplib_root, generation, graph_name, alpha):
"""Test path building"""
assert build_path_to_oplib_instance(
oplib_root, generation, instance_name, alpha=alpha
oplib_root, generation, graph_name, alpha=alpha
).exists()


def test_build_path_to_tsplib_instance(tsplib_root, instance_name):
def test_build_path_to_tsplib_instance(tsplib_root, graph_name):
"""Test path building for tsplib"""
assert build_path_to_tsplib_instance(tsplib_root, instance_name).exists()
assert build_path_to_tsplib_instance(tsplib_root, graph_name).exists()
14 changes: 7 additions & 7 deletions tspwplib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EdgeFunctionName(str, Enum):
weight = "weight"


class InstanceName(str, Enum):
class GraphName(str, Enum):
"""Names of TSPlib instances"""

eil76 = "eil76"
Expand All @@ -26,16 +26,16 @@ class InstanceName(str, Enum):
vm1748 = "vm1748"


class Generation(Enum):
class Generation(str, Enum):
"""Generations of TSPwP problem instances"""

one = "gen1"
two = "gen2"
three = "gen3"
four = "gen4"
gen1 = "gen1"
gen2 = "gen2"
gen3 = "gen3"
gen4 = "gen4"


class Alpha(Enum):
class Alpha(IntEnum):
"""Ratio between profit/cost limit and profit/cost of TSP solution"""

fifty = 50
Expand Down
6 changes: 3 additions & 3 deletions tspwplib/utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
"""Useful functions for parsing"""

from pathlib import Path
from .types import Alpha, Generation, InstanceName
from .types import Alpha, Generation, GraphName


def build_path_to_oplib_instance(
oplib_root: Path,
generation: Generation,
name: InstanceName,
name: GraphName,
alpha: Alpha = Alpha.fifty,
) -> Path:
"""Build a filepath to a oplib instance"""
filename: str = name + "-" + generation.value + "-" + str(alpha.value) + ".oplib"
return oplib_root / "instances" / generation.value / filename


def build_path_to_tsplib_instance(tsplib_root: Path, name: InstanceName) -> Path:
def build_path_to_tsplib_instance(tsplib_root: Path, name: GraphName) -> Path:
"""Build a filepath to a tsplib instance"""
filename = name.value + ".tsp"
return tsplib_root / filename

0 comments on commit 46f2a29

Please sign in to comment.