From 15e873e75edf91504ecf5abec4b13bdf469bc02f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 24 Nov 2024 16:58:12 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20pre-commit=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mqt/qao/karp/karp_graphs.py | 30 +++++++++++++------------- src/mqt/qao/karp/karp_sets.py | 37 ++++++++++++++------------------- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/mqt/qao/karp/karp_graphs.py b/src/mqt/qao/karp/karp_graphs.py index d8e2905..c1f3e28 100644 --- a/src/mqt/qao/karp/karp_graphs.py +++ b/src/mqt/qao/karp/karp_graphs.py @@ -375,7 +375,7 @@ def graph_coloring( edges = list(graph.edges()) unique_vertices = set(graph.nodes()) - unique_vertices = set(sorted(unique_vertices)) + unique_vertices = set(unique_vertices) {vertex: idx for idx, vertex in enumerate(unique_vertices, 1)} problem = Problem() @@ -592,7 +592,7 @@ def vertex_cover( edges = list(graph.edges()) unique_vertices = set(graph.nodes()) - unique_vertices = set(sorted(unique_vertices)) + unique_vertices = set(unique_vertices) problem = Problem() variables = Variables() @@ -760,7 +760,7 @@ def clique( edges = list(graph.edges()) unique_vertices = set(graph.nodes()) - unique_vertices = set(sorted(unique_vertices)) + unique_vertices = set(unique_vertices) {vertex: idx for idx, vertex in enumerate(unique_vertices, 1)} problem = Problem() @@ -954,7 +954,7 @@ def hamiltonian_path( edges = set(graph.edges()) unique_vertices = set(graph.nodes()) - unique_vertices = set(sorted(unique_vertices)) + unique_vertices = set(unique_vertices) problem = Problem() variables = Variables() @@ -1089,7 +1089,9 @@ def check_hamiltonian_path_solution(graph: nx.Graph, solution: list[int]) -> dic return {"Valid Solution": True} @staticmethod - def _hamiltonian_path_tsp(filename: str, cycle: bool = False, a: float = 1, b: float = 1) -> tuple[Any, Any, Any, Any]: + def _hamiltonian_path_tsp( + filename: str, cycle: bool = False, a: float = 1, b: float = 1 + ) -> tuple[Any, Any, Any, Any]: """Sets up the Hamiltonian path or Travelling Salesman Problem (TSP) as an optimization problem. Args: @@ -1129,7 +1131,7 @@ def _hamiltonian_path_tsp(filename: str, cycle: bool = False, a: float = 1, b: f edges.add((u, v)) edges.add((v, u)) - unique_vertices = set(sorted(unique_vertices)) + unique_vertices = set(unique_vertices) variables = Variables() @@ -1236,7 +1238,7 @@ def travelling_salesman( max_weight = max(weights) a = max_weight * b + 1 - unique_vertices = set(sorted(unique_vertices)) + unique_vertices = set(unique_vertices) variables = Variables() @@ -1439,7 +1441,7 @@ def independent_set( edges = list(graph.edges()) unique_vertices = set(graph.nodes()) - unique_vertices = set(sorted(unique_vertices)) + unique_vertices = set(unique_vertices) {vertex: idx for idx, vertex in enumerate(unique_vertices, 1)} a = b + 1 @@ -1653,7 +1655,7 @@ def max_cut( edges = list(graph.edges()) unique_vertices = set(graph.nodes()) - unique_vertices = set(sorted(unique_vertices)) + unique_vertices = set(unique_vertices) problem = Problem() variables = Variables() @@ -1751,8 +1753,8 @@ def directed_feedback_vertex_set( msg = "'solve' must be True if 'solver_method', 'read_solution', 'solver_params', or 'visualize' are provided." raise ValueError(msg) - unique_vertices:set[Any] = set() - edges:list[Any] = [] + unique_vertices: set[Any] = set() + edges: list[Any] = [] if isinstance(input_data, str): filename = input_data @@ -1783,7 +1785,7 @@ def directed_feedback_vertex_set( a = b * 3.0 - unique_vertices = set(sorted(unique_vertices)) + unique_vertices = set(unique_vertices) variables = Variables() y_vars = {v: variables.add_binary_variable(f"y_{v}") for v in unique_vertices} @@ -2078,9 +2080,7 @@ def directed_feedback_edge_set( return feedback_edge_set @staticmethod - def check_directed_feedback_edge_set_solution( - graph: nx.DiGraph, solution: list[tuple[int, int]] - ) -> dict[Any, Any]: + def check_directed_feedback_edge_set_solution(graph: nx.DiGraph, solution: list[tuple[int, int]]) -> dict[Any, Any]: """Validates a solution for the directed feedback edge set problem, ensuring the removal. of specified edges results in an acyclic directed graph. diff --git a/src/mqt/qao/karp/karp_sets.py b/src/mqt/qao/karp/karp_sets.py index e8c7271..00991b1 100644 --- a/src/mqt/qao/karp/karp_sets.py +++ b/src/mqt/qao/karp/karp_sets.py @@ -98,7 +98,7 @@ def set_cover( unique_elements.update(elements) len(unique_elements) - unique_elements = set(sorted(unique_elements)) + unique_elements = set(unique_elements) problem = Problem() variables = Variables() @@ -179,9 +179,7 @@ def set_cover( return solution_sets @staticmethod - def check_set_cover_solution( - all_sets: Any, solution: Any - ) -> dict[Any, Any]: + def check_set_cover_solution(all_sets: Any, solution: Any) -> dict[Any, Any]: """Validates a set cover solution by checking if all elements are covered. Args: @@ -344,9 +342,7 @@ def set_packing( return solution_sets @staticmethod - def check_set_packing_solution( - all_sets: Any, solution: Any - ) -> dict[Any, Any]: + def check_set_packing_solution(all_sets: Any, solution: Any) -> dict[Any, Any]: """Validates a set packing solution by ensuring no sets overlap. Args: @@ -710,9 +706,7 @@ def three_d_matching( pass @staticmethod - def check_three_d_matching( - x: list[int], y: list[int], z: list[int], solution: Any - ) -> dict[Any, Any]: + def check_three_d_matching(x: list[int], y: list[int], z: list[int], solution: Any) -> dict[Any, Any]: """Validates a 3D matching solution by ensuring each element in the solution matches exactly one element from each of the sets x, y, and z, and that there are no repeated or mismatched elements. @@ -735,11 +729,14 @@ def check_three_d_matching( """ used_elements: dict[Any, Any] = {} - errors: dict[Any, Any] = {"Invalid Triplets": [], "Non-matching Elements": [], "Repeated Elements": [], "Unused Elements": []} + errors: dict[Any, Any] = { + "Invalid Triplets": [], + "Non-matching Elements": [], + "Repeated Elements": [], + "Unused Elements": [], + } for triplet in solution: - - x1, y1, z1 = triplet if x1 not in x or y1 not in y or z1 not in z: @@ -830,7 +827,7 @@ def exact_cover( unique_elements.update(elements) len(unique_elements) - unique_elements = set(sorted(unique_elements)) + unique_elements = set(unique_elements) problem = Problem() variables = Variables() @@ -898,9 +895,7 @@ def exact_cover( return solution_sets @staticmethod - def check_exact_cover_solution( - sets: Any, solution: Any - ) -> dict[Any, Any]: + def check_exact_cover_solution(sets: Any, solution: Any) -> dict[Any, Any]: """Validates an exact cover solution by ensuring each element is covered exactly once and that there are no overlapping or missing elements. @@ -984,7 +979,7 @@ def set_to_string(sets_list: list[tuple[int, list[int]]], weighted: bool) -> str def print_solution( problem_name: str = "", file_name: str = "", - solution: str = "", + solution: str = "", summary: str = "", ) -> None: """Prints the formatted solution of a problem to the console. @@ -1007,7 +1002,7 @@ def save_solution( problem_name: str = "", file_name: str = "", solution: str = "", - summary: str = "", + summary: str = "", txt_outputname: str = "", ) -> None: """Saves the formatted solution of a problem to a specified file. @@ -1029,7 +1024,7 @@ def save_solution( print(f"Solution written to {txt_outputname}") @staticmethod - def convert_dict_to_string(dictionary: dict [Any, Any]) -> str: + def convert_dict_to_string(dictionary: dict[Any, Any]) -> str: """Converts a dictionary of solution validation results into a readable string format. Args: @@ -1046,4 +1041,4 @@ def convert_dict_to_string(dictionary: dict [Any, Any]) -> str: for sub_key, sub_value in value.items(): if sub_value: result += f"\n'{sub_key}': {sub_value}" - return result \ No newline at end of file + return result