Skip to content

Commit

Permalink
remove deprecated toulbarsolver adapted for LNS, better use the autom…
Browse files Browse the repository at this point in the history
…atic wrapped solver that you can build through 'to_lns_toulbar'
  • Loading branch information
g-poveda committed Oct 28, 2024
1 parent 8b34e70 commit dbaf7f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 107 deletions.
72 changes: 5 additions & 67 deletions discrete_optimization/coloring/solvers/toulbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@

from discrete_optimization.generic_tools.do_solver import SolverDO, WarmstartMixin
from discrete_optimization.generic_tools.lns_tools import ConstraintHandler
from discrete_optimization.generic_tools.toulbar_tools import ToulbarSolver
from discrete_optimization.generic_tools.toulbar_tools import (
ToulbarSolver,
to_lns_toulbar,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -272,72 +275,7 @@ def set_warm_start(self, solution: ColoringSolution) -> None:
self.model.CFN.wcsp.setBestValue(i, solution.colors[i - 1])


class ToulbarColoringSolverForLns(ToulbarColoringSolver):
depth: int = 0

def init_model(self, **kwargs: Any) -> None:
super().init_model(**kwargs)
self.model.SolveFirst()
self.depth = self.model.Depth()
self.model.Store()
self.initub = self.model.GetUB()

def solve_2(self, time_limit: Optional[int] = 20, **kwargs: Any) -> ResultStorage:
try:
solution = self.model.SolveNext(showSolutions=1, timeLimit=time_limit)
logger.info(f"=== Solution === \n {solution}")
logger.info(
f"Best solution = {solution[1]}, Bound = {self.model.GetDDualBound()}"
)
self.model.Restore(self.depth)
self.model.Store()
if solution is not None:
sol = ColoringSolution(
problem=self.problem,
colors=solution[0][1 : 1 + self.problem.number_of_nodes],
)
fit = self.aggreg_from_sol(sol)
return self.create_result_storage(
[(sol, fit)],
)
else:
return self.create_result_storage()
except:
self.model.Restore(self.depth)
self.model.Store()
logger.info("Solve failed in given time")
return self.create_result_storage()

def solve(self, time_limit: Optional[int] = 20, **kwargs: Any) -> ResultStorage:
try:
solution = self.model.SolveNext(showSolutions=1, timeLimit=time_limit)
logger.info(f"=== Solution === \n {solution}")
logger.info(
f"Best solution = {solution[1]}, Bound = {self.model.GetDDualBound()}"
)
# Reinit for next iteration.
self.model.Restore(self.depth)
self.model.Store()
self.model.SetUB(self.initub)
if solution is not None:
sol = ColoringSolution(
problem=self.problem,
colors=solution[0][1 : 1 + self.problem.number_of_nodes],
)
fit = self.aggreg_from_sol(sol)
return self.create_result_storage(
[(sol, fit)],
)
else:
self.model.ClearPropagationQueues()
return self.create_result_storage()
except:
self.model.ClearPropagationQueues()
self.model.Restore(self.depth)
self.model.Store()
self.model.SetUB(self.initub)
logger.info("Solve failed in given time")
return self.create_result_storage()
ToulbarColoringSolverForLns = to_lns_toulbar(ToulbarColoringSolver)


class ColoringConstraintHandlerToulbar(ConstraintHandler):
Expand Down
45 changes: 5 additions & 40 deletions discrete_optimization/maximum_independent_set/solvers/toulbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
from typing import Any, Iterable, Optional

from discrete_optimization.generic_tools.do_problem import Solution
from discrete_optimization.generic_tools.hyperparameters.hyperparameter import (
CategoricalHyperparameter,
)
from discrete_optimization.generic_tools.lns_tools import ConstraintHandler
from discrete_optimization.generic_tools.toulbar_tools import ToulbarSolver
from discrete_optimization.generic_tools.toulbar_tools import (
ToulbarSolver,
to_lns_toulbar,
)

try:
import pytoulbar2
Expand Down Expand Up @@ -71,42 +71,7 @@ def set_warm_start(self, solution: MisSolution) -> None:
self.model.CFN.wcsp.setBestValue(i, solution.chosen[i])


class ToulbarMisSolverForLns(ToulbarMisSolver):
depth: int = 0

def init_model(self, **kwargs: Any) -> None:
super().init_model(**kwargs)
self.model.SolveFirst()
self.depth = self.model.Depth()
self.model.Store()
self.initub = self.model.GetUB()

def solve(self, time_limit: Optional[int] = 20, **kwargs: Any) -> ResultStorage:
try:
solution = self.model.SolveNext(showSolutions=1, timeLimit=time_limit)
logger.info(f"=== Solution === \n {solution}")
logger.info(
f"Best solution = {solution[1]}, Bound = {self.model.GetDDualBound()}"
)
# Reinit for next iteration.
self.model.Restore(self.depth)
self.model.Store()
self.model.SetUB(self.initub)
if solution is not None:
sol = MisSolution(problem=self.problem, chosen=solution[0])
fit = self.aggreg_from_sol(sol)
return self.create_result_storage(
[(sol, fit)],
)
else:
return self.create_result_storage()
except:
self.model.ClearPropagationQueues()
self.model.Restore(self.depth)
self.model.Store()
self.model.SetUB(self.initub)
logger.info("Solve failed in given time")
return self.create_result_storage()
ToulbarMisSolverForLns = to_lns_toulbar(ToulbarMisSolver)


class MisConstraintHandlerToulbar(ConstraintHandler):
Expand Down

0 comments on commit dbaf7f0

Please sign in to comment.