Skip to content

Commit

Permalink
Fixed setting of cost function when using to_function
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Dafarra committed Apr 11, 2024
1 parent d807aac commit c6a88d2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 17 deletions.
13 changes: 2 additions & 11 deletions src/hippopt/base/opti_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ def __init__(self, message: Exception):
)


class ToFunctionSolveNotCalled(Exception):
def __init__(self):
super().__init__(
"The solve() method must be called before converting to function."
)


@dataclasses.dataclass
class OptiSolver(OptimizationSolver):
DefaultSolverType: ClassVar[str] = "ipopt"
Expand Down Expand Up @@ -591,10 +584,8 @@ def get_free_parameters_names(self) -> list[str]:
def to_function(
self, name: str = "opti_function", options: dict = None
) -> cs.Function:
if self._output_solution is None:
# to_function does not seem to work without calling solve() first
raise ToFunctionSolveNotCalled

self._cost = self._cost if self._cost is not None else cs.MX(0)
self._solver.minimize(self._cost)
variables_names = list(self._objects_dict.keys())
# Prepend guess to the variable names
guess_names = ["guess." + name for name in variables_names]
Expand Down
6 changes: 0 additions & 6 deletions test/test_optimization_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,6 @@ def test_opti_to_function():
),
)

problem.solver().set_initial_guess(initial_guess=initial_guess)
problem.solve()

c = 20.0 * np.random.rand(3, 1) - 10.0
initial_guess.parameter = c

opti_function = opti_solver.to_function()
output_dict = opti_function(**initial_guess.to_dict(prefix="guess."))
output = MyTestVarAndPar()
Expand Down

0 comments on commit c6a88d2

Please sign in to comment.