Skip to content

Commit

Permalink
fix bugs for MC/MIS/MCl/MVC solver
Browse files Browse the repository at this point in the history
  • Loading branch information
heatingma committed Oct 31, 2024
1 parent 6399008 commit 461c003
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions ml4co_kit/solver/mc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
11 changes: 7 additions & 4 deletions ml4co_kit/solver/mc/gurobi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import uuid
import numpy as np
import gurobipy as gp
from typing import List
Expand All @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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])
Expand Down
1 change: 1 addition & 0 deletions ml4co_kit/solver/mcl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
9 changes: 6 additions & 3 deletions ml4co_kit/solver/mcl/gurobi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import uuid
import numpy as np
import gurobipy as gp
from typing import List
Expand All @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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])
Expand Down
1 change: 1 addition & 0 deletions ml4co_kit/solver/mis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
9 changes: 6 additions & 3 deletions ml4co_kit/solver/mis/gurobi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import uuid
import numpy as np
import gurobipy as gp
from typing import List
Expand All @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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])
Expand Down
1 change: 1 addition & 0 deletions ml4co_kit/solver/mvc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
9 changes: 6 additions & 3 deletions ml4co_kit/solver/mvc/gurobi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import uuid
import numpy as np
import gurobipy as gp
from typing import List
Expand All @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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])
Expand Down

0 comments on commit 461c003

Please sign in to comment.