From 6034175a10ca5992faab064cd1ed5a9b8357980b Mon Sep 17 00:00:00 2001 From: crosstyan Date: Mon, 3 Mar 2025 17:38:24 +0800 Subject: [PATCH] refactor: Migrate solver module to use CVXOPT wrapper - Rename `_wrap` to `_cvx_opt_wrap` for consistency - Update import paths in solver module - Add type hints to `solution_mat_clusters` method - Improve type annotations in `solve` method --- app/solver/__init__.py | 16 ++++++++-------- app/solver/{_wrap => _cvxopt_wrap}/__init__.py | 0 app/solver/{_wrap => _cvxopt_wrap}/glpk.py | 0 3 files changed, 8 insertions(+), 8 deletions(-) rename app/solver/{_wrap => _cvxopt_wrap}/__init__.py (100%) rename app/solver/{_wrap => _cvxopt_wrap}/glpk.py (100%) diff --git a/app/solver/__init__.py b/app/solver/__init__.py index 5193d53..c40d95c 100644 --- a/app/solver/__init__.py +++ b/app/solver/__init__.py @@ -7,9 +7,9 @@ import numpy as np from app._typing import NDArray -from ._wrap import matrix, spmatrix -from ._wrap.glpk import ilp -from ._wrap.glpk import set_global_options as set_glpk_options +from ._cvxopt_wrap import matrix, spmatrix +from ._cvxopt_wrap.glpk import ilp +from ._cvxopt_wrap.glpk import set_global_options as set_glpk_options set_glpk_options({"msg_lev": "GLP_MSG_ERR"}) @@ -139,19 +139,19 @@ class _BIPSolver: @staticmethod def solution_mat_clusters(solution_mat: NDArray): - n = solution_mat.shape[0] + n: int = solution_mat.shape[0] labels = np.arange(1, n + 1) for i in range(n): for j in range(i + 1, n): if solution_mat[i, j] > 0: labels[j] = labels[i] - - clusters = defaultdict(list) + # a dictionary that with empty list as default value for non-existing keys + clusters: dict[int, list[int]] = defaultdict(list) for i, label in enumerate(labels): - clusters[label].append(i) + clusters[int(label)].append(i) return list(clusters.values()) - def solve(self, affinity_matrix, rtn_matrix=False): + def solve(self, affinity_matrix: NDArray, rtn_matrix=False): n_nodes = affinity_matrix.shape[0] if n_nodes <= 1: solution_x, sol_matrix = ( diff --git a/app/solver/_wrap/__init__.py b/app/solver/_cvxopt_wrap/__init__.py similarity index 100% rename from app/solver/_wrap/__init__.py rename to app/solver/_cvxopt_wrap/__init__.py diff --git a/app/solver/_wrap/glpk.py b/app/solver/_cvxopt_wrap/glpk.py similarity index 100% rename from app/solver/_wrap/glpk.py rename to app/solver/_cvxopt_wrap/glpk.py