-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from PatrickOHara/oplib-graph
Profit Problem class
- Loading branch information
Showing
5 changed files
with
192 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
"""Test that all existing instances are complete graphs""" | ||
|
||
import networkx as nx | ||
from tspwplib.dataset import load_tsplib_dataset | ||
import tsplib95 | ||
from tspwplib.utils import build_path_to_tsplib_instance | ||
|
||
|
||
def test_tsplib_is_complete(tsplib_root, instance_name): | ||
"""Test each instance of TSPLIB95 is a complete graph""" | ||
problem = load_tsplib_dataset(tsplib_root, instance_name) | ||
filepath = build_path_to_tsplib_instance(tsplib_root, instance_name) | ||
problem = tsplib95.load(filepath) | ||
graph = problem.get_graph() | ||
assert nx.complete_graph(graph) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Test the TSP with Profits Problem class""" | ||
|
||
import math | ||
import networkx as nx | ||
from tspwplib.problem import ProfitsProblem | ||
from tspwplib.types import OptimalSolutionTSP | ||
from tspwplib.utils import build_path_to_oplib_instance | ||
|
||
|
||
def test_parse_profits_problem(oplib_root, generation, instance_name, alpha): | ||
"""Test an OP instance can be parsed""" | ||
filepath = build_path_to_oplib_instance( | ||
oplib_root, generation, instance_name, alpha=alpha | ||
) | ||
assert "COST_LIMIT" in ProfitsProblem.fields_by_keyword | ||
|
||
problem = ProfitsProblem.load(filepath) | ||
assert problem.is_complete() | ||
graph = problem.get_graph() | ||
assert nx.complete_graph(graph) | ||
|
||
|
||
def test_get_cost_limit(oplib_root, generation, instance_name, alpha): | ||
"""Test we can get the cost limit""" | ||
filepath = build_path_to_oplib_instance( | ||
oplib_root, generation, instance_name, alpha=alpha | ||
) | ||
problem = ProfitsProblem.load(filepath) | ||
expected_cost_limit = math.ceil( | ||
OptimalSolutionTSP[instance_name] * (alpha.value / 100.0) | ||
) | ||
assert problem.get_cost_limit() == expected_cost_limit |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Functions and classes for datasets""" | ||
|
||
from typing import Dict | ||
import tsplib95 | ||
|
||
|
||
class ProfitsProblem(tsplib95.models.StandardProblem): | ||
"""TSP with Profits Problem""" | ||
|
||
# Maximum distance of the total route in a OP. | ||
cost_limit = tsplib95.fields.IntegerField("COST_LIMIT") | ||
# The scores of the nodes of a OP are given in the form (per line) | ||
node_score = tsplib95.fields.DemandsField("NODE_SCORE_SECTION") | ||
# The optimal solution to the TSP | ||
tspsol = tsplib95.fields.IntegerField("TSPSOL") | ||
|
||
def get_cost_limit(self) -> int: | ||
"""Get the cost limit for a TSP with Profits problem | ||
Returns: | ||
Cost limit | ||
""" | ||
return self.cost_limit | ||
|
||
def get_node_score(self) -> Dict[int, int]: | ||
"""Get the node scores (profits) | ||
Returns: | ||
Mapping from node to node score (profit) | ||
""" | ||
|
||
def get_tsp_optimal_value(self) -> int: | ||
"""Get the value of the optimal solution to TSP | ||
Returns: | ||
TSP optimal value | ||
""" | ||
return self.tspsol |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters