From 57e4a17955809a4570be0090d9e38fe8b301f3bf Mon Sep 17 00:00:00 2001 From: Stefano Date: Thu, 6 Apr 2023 14:25:47 +0200 Subject: [PATCH] Added possibility to set solver options and plugins at construction time. --- src/hippopt/base/opti_solver.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/hippopt/base/opti_solver.py b/src/hippopt/base/opti_solver.py index 014e3aed..d52632a9 100644 --- a/src/hippopt/base/opti_solver.py +++ b/src/hippopt/base/opti_solver.py @@ -24,6 +24,12 @@ class OptiSolver(OptimizationSolver): _options_plugin: dict[str, Any] = dataclasses.field(default_factory=dict) _options_solver: dict[str, Any] = dataclasses.field(default_factory=dict) + options_solver: dataclasses.InitVar[dict[str, Any]] = dataclasses.field( + default=None + ) + options_plugin: dataclasses.InitVar[dict[str, Any]] = dataclasses.field( + default=None + ) _cost: cs.MX = dataclasses.field(default=None) _solver: cs.Opti = dataclasses.field(default=None) @@ -36,8 +42,19 @@ class OptiSolver(OptimizationSolver): default=None ) - def __post_init__(self, problem_type: str) -> None: + def __post_init__( + self, + problem_type: str, + options_solver: dict[str, Any] = None, + options_plugin: dict[str, Any] = None, + ): self._solver = cs.Opti(problem_type) + self._options_solver = ( + options_solver if isinstance(options_solver, dict) else {} + ) + self._options_plugin = ( + options_plugin if isinstance(options_plugin, dict) else {} + ) self._solver.solver( self._inner_solver, self._options_plugin, self._options_solver ) @@ -364,8 +381,8 @@ def set_initial_guess( def set_opti_options( self, inner_solver: str = None, - options_plugin: dict[str, Any] = None, options_solver: dict[str, Any] = None, + options_plugin: dict[str, Any] = None, ) -> None: if inner_solver is not None: self._inner_solver = inner_solver