forked from HQU-gxy/CVTH3PE
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
This commit is contained in:
@ -7,9 +7,9 @@ import numpy as np
|
|||||||
|
|
||||||
from app._typing import NDArray
|
from app._typing import NDArray
|
||||||
|
|
||||||
from ._wrap import matrix, spmatrix
|
from ._cvxopt_wrap import matrix, spmatrix
|
||||||
from ._wrap.glpk import ilp
|
from ._cvxopt_wrap.glpk import ilp
|
||||||
from ._wrap.glpk import set_global_options as set_glpk_options
|
from ._cvxopt_wrap.glpk import set_global_options as set_glpk_options
|
||||||
|
|
||||||
set_glpk_options({"msg_lev": "GLP_MSG_ERR"})
|
set_glpk_options({"msg_lev": "GLP_MSG_ERR"})
|
||||||
|
|
||||||
@ -139,19 +139,19 @@ class _BIPSolver:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def solution_mat_clusters(solution_mat: NDArray):
|
def solution_mat_clusters(solution_mat: NDArray):
|
||||||
n = solution_mat.shape[0]
|
n: int = solution_mat.shape[0]
|
||||||
labels = np.arange(1, n + 1)
|
labels = np.arange(1, n + 1)
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
for j in range(i + 1, n):
|
for j in range(i + 1, n):
|
||||||
if solution_mat[i, j] > 0:
|
if solution_mat[i, j] > 0:
|
||||||
labels[j] = labels[i]
|
labels[j] = labels[i]
|
||||||
|
# a dictionary that with empty list as default value for non-existing keys
|
||||||
clusters = defaultdict(list)
|
clusters: dict[int, list[int]] = defaultdict(list)
|
||||||
for i, label in enumerate(labels):
|
for i, label in enumerate(labels):
|
||||||
clusters[label].append(i)
|
clusters[int(label)].append(i)
|
||||||
return list(clusters.values())
|
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]
|
n_nodes = affinity_matrix.shape[0]
|
||||||
if n_nodes <= 1:
|
if n_nodes <= 1:
|
||||||
solution_x, sol_matrix = (
|
solution_x, sol_matrix = (
|
||||||
|
|||||||
Reference in New Issue
Block a user