discrete_optimization.generic_tools.callbacks package

Submodules

discrete_optimization.generic_tools.callbacks.backup module

class discrete_optimization.generic_tools.callbacks.backup.PickleBestSolutionBackup(save_nb_steps: int, backup_path: str = 'debug.pkl')[source]

Bases: Callback

on_step_end(step: int, res: ResultStorage, solver: SolverDO) bool | None[source]

Called at the end of an optimization step.

Parameters:
  • step – index of step

  • res – current result storage

  • solver – solvers using the callback

Returns:

If True, the optimization process is stopped, else it goes on.

discrete_optimization.generic_tools.callbacks.callback module

class discrete_optimization.generic_tools.callbacks.callback.Callback[source]

Bases: object

Base class used to build new callbacks.

Callbacks can be passed to solvers solve() in order to hook into the various stages of the solve.

To create a custom callback, subclass discrete_optimization.generic_tools.callbacks.Callback and override the method associated with the stage of interest.

on_solve_end(res: ResultStorage, solver: SolverDO)[source]

Called at the end of solve.

Parameters:
  • res – current result storage

  • solver – solvers using the callback

on_solve_start(solver: SolverDO)[source]

Called at the start of solve.

Parameters:

solver – solvers using the callback

on_step_end(step: int, res: ResultStorage, solver: SolverDO) bool | None[source]

Called at the end of an optimization step.

Parameters:
  • step – index of step

  • res – current result storage

  • solver – solvers using the callback

Returns:

If True, the optimization process is stopped, else it goes on.

set_params(params)[source]
class discrete_optimization.generic_tools.callbacks.callback.CallbackList(callbacks=None, **params)[source]

Bases: Callback

Container abstracting a list of callbacks.

append(callback)[source]
on_solve_end(res: ResultStorage, solver: SolverDO)[source]

Called at the end of solve.

Parameters:
  • res – current result storage

  • solver – solvers using the callback

on_solve_start(solver: SolverDO)[source]

Called at the start of solve.

Parameters:

solver – solvers using the callback

on_step_end(step: int, res: ResultStorage, solver: SolverDO) bool | None[source]

Called at the end of an optimization step.

Parameters:
  • step – index of step

  • res – current result storage

  • solver – solvers using the callback

Returns:

If True, the optimization process is stopped, else it goes on.

set_params(params)[source]

discrete_optimization.generic_tools.callbacks.early_stoppers module

class discrete_optimization.generic_tools.callbacks.early_stoppers.NbIterationStopper(nb_iteration_max: int)[source]

Bases: Callback

Callback to stop the optimization when a given number of solutions are found.

on_step_end(step: int, res: ResultStorage, solver: SolverDO) bool | None[source]

Called at the end of an optimization step.

Parameters:
  • step – index of step

  • res – current result storage

  • solver – solvers using the callback

Returns:

If True, the optimization process is stopped, else it goes on.

class discrete_optimization.generic_tools.callbacks.early_stoppers.TimerStopper(total_seconds: int, check_nb_steps: int = 1)[source]

Bases: Callback

Callback to stop the optimization after a given time.

Stops the optimization process if a limit training time has been elapsed. This time is checked after each check_nb_steps steps.

on_solve_start(solver: SolverDO)[source]

Called at the start of solve.

Parameters:

solver – solvers using the callback

on_step_end(step: int, res: ResultStorage, solver: SolverDO) bool | None[source]

Called at the end of an optimization step.

Parameters:
  • step – index of step

  • res – current result storage

  • solver – solvers using the callback

Returns:

If True, the optimization process is stopped, else it goes on.

discrete_optimization.generic_tools.callbacks.loggers module

class discrete_optimization.generic_tools.callbacks.loggers.NbIterationTracker(step_verbosity_level: int = 10, end_verbosity_level: int = 20)[source]

Bases: Callback

Log the number of iteration of a given solver

on_solve_end(res: ResultStorage, solver: SolverDO)[source]

Called at the end of solve.

Parameters:
  • res – current result storage

  • solver – solvers using the callback

on_step_end(step: int, res: ResultStorage, solver: SolverDO) bool | None[source]

Called at the end of an optimization step.

Parameters:
  • step – index of step

  • res – current result storage

  • solver – solvers using the callback

Returns:

If True, the optimization process is stopped, else it goes on.

class discrete_optimization.generic_tools.callbacks.loggers.ObjectiveLogger(step_verbosity_level: int = 10, end_verbosity_level: int = 20)[source]

Bases: Callback

Log the current best objective function at every iteration/new solution found by the solver

on_solve_end(res: ResultStorage, solver: SolverDO)[source]

Called at the end of solve.

Parameters:
  • res – current result storage

  • solver – solvers using the callback

on_step_end(step: int, res: ResultStorage, solver: SolverDO) bool | None[source]

Called at the end of an optimization step.

Parameters:
  • step – index of step

  • res – current result storage

  • solver – solvers using the callback

Returns:

If True, the optimization process is stopped, else it goes on.

discrete_optimization.generic_tools.callbacks.optuna module

class discrete_optimization.generic_tools.callbacks.optuna.OptunaCallback(trial: optuna.trial.Trial, optuna_report_nb_steps: int = 1, starting_time: float | None = None, elapsed_time_attr: str = 'elapsed_time', report_time: bool = False, pruning: bool = True, **kwargs)[source]

Bases: Callback

Callback reporting intermediate values to prune unpromising trials during Optuna hyperparameters tuning.

Adapted to single objective optimization (res.fit is a float)

The callback report to optuna intermediate fitness with the corresponding step number or elapsed time since starting time. It also updates the user attribute used to store computing time, so that pruned or failed trials will still have the user attribute updated. If the optuna pruner see that the trial should be pruned, raise the appropriate TrialPruned exception.

Parameters:
  • trial – A optuna.trial.Trial corresponding to the current evaluation of the objective function.

  • optuna_report_nb_steps – report intermediate result every optuna_report_nb_steps steps when the number of iterations is high, setting this to 1 could slow too much run of a single trial

  • starting_time – float representing the start time of the solving process. Should be the result of a call to time.perf_counter(). Default to time.perf_counter() called by on_solve_start(). Useful to be on par with a clock set outside the callback.

  • elapsed_time_attr – key of trial user attribute used to store the elapsed time at each step

  • report_time – if True, report to optuna intermediate fitness with elapsed time instead of step

  • pruning – if True, use the optuna pruner to decide if we the trial should be pruned. Else never try to prune.

on_solve_start(solver: SolverDO)[source]

Called at the start of solve.

Parameters:

solver – solvers using the callback

on_step_end(step: int, res: ResultStorage, solver: SolverDO) bool | None[source]

Called at the end of an optimization step.

Parameters:
  • step – index of step

  • res – current result storage

  • solver – solvers using the callback

Returns:

If True, the optimization process is stopped, else it goes on.

Module contents