diff --git a/discrete_optimization/facility/solvers/toulbar.py b/discrete_optimization/facility/solvers/toulbar.py index 837e1693..826eccad 100644 --- a/discrete_optimization/facility/solvers/toulbar.py +++ b/discrete_optimization/facility/solvers/toulbar.py @@ -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 @@ -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 diff --git a/discrete_optimization/generic_tools/toulbar_tools.py b/discrete_optimization/generic_tools/toulbar_tools.py index 161bcf97..7fea5530 100644 --- a/discrete_optimization/generic_tools/toulbar_tools.py +++ b/discrete_optimization/generic_tools/toulbar_tools.py @@ -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, @@ -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]): diff --git a/discrete_optimization/rcpsp/solvers/toulbar.py b/discrete_optimization/rcpsp/solvers/toulbar.py index 3aa16c49..14dc55af 100644 --- a/discrete_optimization/rcpsp/solvers/toulbar.py +++ b/discrete_optimization/rcpsp/solvers/toulbar.py @@ -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