Skip to content

Commit

Permalink
update toulbar solvers: callback well called at start end end of solv…
Browse files Browse the repository at this point in the history
…er calls
  • Loading branch information
g-poveda committed Nov 29, 2024
1 parent cfe0e63 commit fba9a40
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
10 changes: 8 additions & 2 deletions discrete_optimization/facility/solvers/toulbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ def init_model_boolean_variable(self, **kwargs):
"""
nb_facilities = self.problem.facility_count
nb_customers = self.problem.customer_count
model = pytoulbar2.CFN(kwargs.get("upper_bound", 10e8))
if "vns" in kwargs:
model = pytoulbar2.CFN(kwargs.get("upper_bound", 10e8), vns=kwargs["vns"])
else:
model = pytoulbar2.CFN(kwargs.get("upper_bound", 10e8))
x: Dict[Tuple[int, int], Union[int, Any]] = {}
key_to_index = {}
index = 0
Expand Down Expand Up @@ -133,7 +136,10 @@ def init_model_integer_variable(self, **kwargs):
"""
nb_facilities = self.problem.facility_count
nb_customers = self.problem.customer_count
model = pytoulbar2.CFN(kwargs.get("upper_bound", 10e8))
if "vns" in kwargs:
model = pytoulbar2.CFN(kwargs.get("upper_bound", 10e8), vns=kwargs["vns"])
else:
model = pytoulbar2.CFN(kwargs.get("upper_bound", 10e8))
index = 0
matrix_fc_indicator, matrix_length = prune_search_space(
self.problem, n_cheapest=nb_facilities, n_shortest=nb_facilities
Expand Down
7 changes: 6 additions & 1 deletion discrete_optimization/generic_tools/toulbar_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import logging

from discrete_optimization.generic_tools.callbacks.callback import CallbackList
from discrete_optimization.generic_tools.do_solver import SolverDO
from discrete_optimization.generic_tools.hyperparameters.hyperparameter import (
CategoricalHyperparameter,
Expand Down Expand Up @@ -53,12 +54,16 @@ def solve(
) -> ResultStorage:
if self.model is None:
self.init_model(**kwargs)
callback = CallbackList(callbacks)
callback.on_solve_start(solver=self)
solution = self.model.Solve(showSolutions=1, timeLimit=int(time_limit))
logger.info(
f"Solution value = {solution[1]}, bound={self.model.GetDDualBound()}"
)
sol = self.retrieve_solution(solution)
return self.create_result_storage([(sol, self.aggreg_from_sol(sol))])
res = self.create_result_storage([(sol, self.aggreg_from_sol(sol))])
callback.on_solve_end(res=res, solver=self)
return res


def to_lns_toulbar(cls: Type[ToulbarSolver]):
Expand Down
6 changes: 4 additions & 2 deletions discrete_optimization/rcpsp/solvers/toulbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ def init_model(self, **kwargs: Any) -> None:
)
raise exc
if "vns" in kwargs:
model = pytoulbar2.CFN(ubinit=self.problem.horizon, vns=kwargs["vns"])
model = pytoulbar2.CFN(
ubinit=kwargs.get("ub", self.problem.horizon), vns=kwargs["vns"]
)
else:
model = pytoulbar2.CFN(ubinit=self.problem.horizon)
model = pytoulbar2.CFN(ubinit=kwargs.get("ub", self.problem.horizon))
n_jobs = self.problem.n_jobs
horizon = self.problem.horizon
index_var = 0
Expand Down

0 comments on commit fba9a40

Please sign in to comment.