From 461c0037fb1ea306b7ca979320c7eb2f087b7942 Mon Sep 17 00:00:00 2001 From: heatingma Date: Thu, 31 Oct 2024 13:28:44 +0800 Subject: [PATCH] fix bugs for MC/MIS/MCl/MVC solver --- ml4co_kit/solver/mc/base.py | 1 + ml4co_kit/solver/mc/gurobi.py | 11 +++++++---- ml4co_kit/solver/mcl/base.py | 1 + ml4co_kit/solver/mcl/gurobi.py | 9 ++++++--- ml4co_kit/solver/mis/base.py | 1 + ml4co_kit/solver/mis/gurobi.py | 9 ++++++--- ml4co_kit/solver/mvc/base.py | 1 + ml4co_kit/solver/mvc/gurobi.py | 9 ++++++--- 8 files changed, 29 insertions(+), 13 deletions(-) diff --git a/ml4co_kit/solver/mc/base.py b/ml4co_kit/solver/mc/base.py index fc709bf..0a9183d 100644 --- a/ml4co_kit/solver/mc/base.py +++ b/ml4co_kit/solver/mc/base.py @@ -338,6 +338,7 @@ def from_adj_matrix( self.graph_data[idx] = graph def from_nx_graph(self, nx_graphs: List[nx.Graph]): + self.graph_data = list() for idx in range(len(nx_graphs)): graph = MCGraphData() graph.from_nx_graph(nx_graphs[idx]) diff --git a/ml4co_kit/solver/mc/gurobi.py b/ml4co_kit/solver/mc/gurobi.py index 106bb74..7eea646 100644 --- a/ml4co_kit/solver/mc/gurobi.py +++ b/ml4co_kit/solver/mc/gurobi.py @@ -1,4 +1,5 @@ import os +import uuid import numpy as np import gurobipy as gp from typing import List @@ -17,7 +18,8 @@ def __init__( solver_type=SOLVER_TYPE.GUROBI, weighted=weighted, time_limit=time_limit ) self.licence_path = licence_path - + self.tmp_name = None + def solve( self, graph_data: List[MCGraphData] = None, @@ -29,6 +31,7 @@ def solve( self.graph_data = graph_data timer = Timer(apply=show_time) timer.start() + self.tmp_name = uuid.uuid4().hex[:9] # solve solutions = list() @@ -71,7 +74,7 @@ def _solve(self, idx: int) -> np.ndarray: mc_graph.check_edge_attr() # create gurobi model - model = gp.Model(f"MC-{idx}") + model = gp.Model(f"MC-{self.tmp_name}-{idx}") model.setParam("OutputFlag", 0) model.setParam("TimeLimit", self.time_limit) model.setParam("Threads", 1) @@ -90,9 +93,9 @@ def _solve(self, idx: int) -> np.ndarray: model.setObjective(object, gp.GRB.MINIMIZE) # Solve - model.write(f"MC-{idx}.lp") + model.write(f"MC-{self.tmp_name}-{idx}.lp") model.optimize() - os.remove(f"MC-{idx}.lp") + os.remove(f"MC-{self.tmp_name}-{idx}.lp") # return return np.array([int(var_dict[key].X) for key in var_dict]) diff --git a/ml4co_kit/solver/mcl/base.py b/ml4co_kit/solver/mcl/base.py index c027fcc..c7a63b4 100644 --- a/ml4co_kit/solver/mcl/base.py +++ b/ml4co_kit/solver/mcl/base.py @@ -359,6 +359,7 @@ def from_adj_matrix( self.graph_data[idx] = graph def from_nx_graph(self, nx_graphs: List[nx.Graph]): + self.graph_data = list() for idx in range(len(nx_graphs)): graph = MClGraphData() graph.from_nx_graph(nx_graphs[idx]) diff --git a/ml4co_kit/solver/mcl/gurobi.py b/ml4co_kit/solver/mcl/gurobi.py index 5447d3b..db3edfc 100644 --- a/ml4co_kit/solver/mcl/gurobi.py +++ b/ml4co_kit/solver/mcl/gurobi.py @@ -1,4 +1,5 @@ import os +import uuid import numpy as np import gurobipy as gp from typing import List @@ -17,6 +18,7 @@ def __init__( solver_type=SOLVER_TYPE.GUROBI, weighted=weighted, time_limit=time_limit ) self.licence_path = licence_path + self.tmp_name = None def solve( self, @@ -29,6 +31,7 @@ def solve( self.graph_data = graph_data timer = Timer(apply=show_time) timer.start() + self.tmp_name = uuid.uuid4().hex[:9] # solve solutions = list() @@ -72,7 +75,7 @@ def _solve(self, idx: int) -> np.ndarray: mcl_graph.remove_self_loop() # create gurobi model - model = gp.Model(f"MCL-{idx}") + model = gp.Model(f"MCl-{self.tmp_name}-{idx}") model.setParam("OutputFlag", 0) model.setParam("TimeLimit", self.time_limit) model.setParam("Threads", 1) @@ -95,9 +98,9 @@ def _solve(self, idx: int) -> np.ndarray: model.setObjective(object, gp.GRB.MINIMIZE) # Solve - model.write(f"MCL-{idx}.lp") + model.write(f"MCl-{self.tmp_name}-{self.tmp_name}-{idx}.lp") model.optimize() - os.remove(f"MCL-{idx}.lp") + os.remove(f"MCl-{self.tmp_name}-{self.tmp_name}-{idx}.lp") # return return np.array([int(var_dict[key].X) for key in var_dict]) diff --git a/ml4co_kit/solver/mis/base.py b/ml4co_kit/solver/mis/base.py index 6cebd40..4c605f3 100644 --- a/ml4co_kit/solver/mis/base.py +++ b/ml4co_kit/solver/mis/base.py @@ -359,6 +359,7 @@ def from_adj_matrix( self.graph_data[idx] = graph def from_nx_graph(self, nx_graphs: List[nx.Graph]): + self.graph_data = list() for idx in range(len(nx_graphs)): graph = MISGraphData() graph.from_nx_graph(nx_graphs[idx]) diff --git a/ml4co_kit/solver/mis/gurobi.py b/ml4co_kit/solver/mis/gurobi.py index fc429e3..bec1755 100644 --- a/ml4co_kit/solver/mis/gurobi.py +++ b/ml4co_kit/solver/mis/gurobi.py @@ -1,4 +1,5 @@ import os +import uuid import numpy as np import gurobipy as gp from typing import List @@ -17,6 +18,7 @@ def __init__( solver_type=SOLVER_TYPE.GUROBI, weighted=weighted, time_limit=time_limit ) self.licence_path = licence_path + self.tmp_name = None def solve( self, @@ -29,6 +31,7 @@ def solve( self.graph_data = graph_data timer = Timer(apply=show_time) timer.start() + self.tmp_name = uuid.uuid4().hex[:9] # solve solutions = list() @@ -71,7 +74,7 @@ def _solve(self, idx: int) -> np.ndarray: mis_graph.remove_self_loop() # create gurobi model - model = gp.Model(f"MIS-{idx}") + model = gp.Model(f"MIS-{self.tmp_name}-{idx}") model.setParam("OutputFlag", 0) model.setParam("TimeLimit", self.time_limit) model.setParam("Threads", 1) @@ -94,9 +97,9 @@ def _solve(self, idx: int) -> np.ndarray: model.setObjective(object, gp.GRB.MINIMIZE) # Solve - model.write(f"MIS-{idx}.lp") + model.write(f"MIS-{self.tmp_name}-{idx}.lp") model.optimize() - os.remove(f"MIS-{idx}.lp") + os.remove(f"MIS-{self.tmp_name}-{idx}.lp") # return return np.array([int(var_dict[key].X) for key in var_dict]) diff --git a/ml4co_kit/solver/mvc/base.py b/ml4co_kit/solver/mvc/base.py index 4953252..e8928a4 100644 --- a/ml4co_kit/solver/mvc/base.py +++ b/ml4co_kit/solver/mvc/base.py @@ -359,6 +359,7 @@ def from_adj_matrix( self.graph_data[idx] = graph def from_nx_graph(self, nx_graphs: List[nx.Graph]): + self.graph_data = list() for idx in range(len(nx_graphs)): graph = MVCGraphData() graph.from_nx_graph(nx_graphs[idx]) diff --git a/ml4co_kit/solver/mvc/gurobi.py b/ml4co_kit/solver/mvc/gurobi.py index b663b9e..de7451f 100644 --- a/ml4co_kit/solver/mvc/gurobi.py +++ b/ml4co_kit/solver/mvc/gurobi.py @@ -1,4 +1,5 @@ import os +import uuid import numpy as np import gurobipy as gp from typing import List @@ -17,6 +18,7 @@ def __init__( solver_type=SOLVER_TYPE.GUROBI, weighted=weighted, time_limit=time_limit ) self.licence_path = licence_path + self.tmp_name = None def solve( self, @@ -29,6 +31,7 @@ def solve( self.graph_data = graph_data timer = Timer(apply=show_time) timer.start() + self.tmp_name = uuid.uuid4().hex[:9] # solve solutions = list() @@ -71,7 +74,7 @@ def _solve(self, idx: int) -> np.ndarray: mvc_graph.remove_self_loop() # create gurobi model - model = gp.Model(f"MVC-{idx}") + model = gp.Model(f"MVC-{self.tmp_name}-{idx}") model.setParam("OutputFlag", 0) model.setParam("TimeLimit", self.time_limit) model.setParam("Threads", 1) @@ -94,9 +97,9 @@ def _solve(self, idx: int) -> np.ndarray: model.setObjective(object, gp.GRB.MINIMIZE) # Solve - model.write(f"MVC-{idx}.lp") + model.write(f"MVC-{self.tmp_name}-{idx}.lp") model.optimize() - os.remove(f"MVC-{idx}.lp") + os.remove(f"MVC-{self.tmp_name}-{idx}.lp") # return return np.array([int(var_dict[key].x) for key in var_dict])