Skip to content

Commit

Permalink
Set OptimizerResult.optimizer in Optimizer.minimize
Browse files Browse the repository at this point in the history
  • Loading branch information
dweindl committed Nov 27, 2024
1 parent 6eb4245 commit 31654ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
1 change: 0 additions & 1 deletion pypesto/optimize/ess/ess.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ def _create_result(self) -> pypesto.Result:
for i, optimizer_result in enumerate(self.local_solutions):
i_result += 1
optimizer_result.id = f"Local solution {i}"
optimizer_result.optimizer = str(self.local_optimizer)
result.optimize_result.append(optimizer_result)

if self._result_includes_refset:
Expand Down
37 changes: 25 additions & 12 deletions pypesto/optimize/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, optimizer: str):
def hierarchical_decorator(minimize):
"""Add inner parameters to the optimizer result.
Default decorator for the minimize() method.
Default decorator for the :meth:`Optimizer.minimize` method.
"""

@wraps(minimize)
Expand Down Expand Up @@ -81,7 +81,7 @@ def wrapped_minimize(
def history_decorator(minimize):
"""Initialize and extract information stored in the history.
Default decorator for the minimize() method.
Default decorator for the :meth:`Optimizer.minimize` method.
"""

@wraps(minimize)
Expand Down Expand Up @@ -140,7 +140,11 @@ def wrapped_minimize(

logger.error(f"start {id} failed:\n{trace}")
result = OptimizerResult(
x0=x0, exitflag=-1, message=str(err), id=id
x0=x0,
exitflag=-1,
message=str(err),
id=id,
optimizer=str(self),
)
else:
raise
Expand All @@ -163,7 +167,7 @@ def wrapped_minimize(
def time_decorator(minimize):
"""Measure time of optimization.
Default decorator for the minimize() method to take time.
Default decorator for the :meth:`Optimizer.minimize` method to take time.
Currently, the method time.time() is used, which measures
the wall-clock time.
"""
Expand Down Expand Up @@ -196,8 +200,8 @@ def wrapped_minimize(
def fix_decorator(minimize):
"""Include also fixed parameters in the result arrays of minimize().
Default decorator for the minimize() method (nans will be inserted in the
derivatives).
Default decorator for the :meth:`Optimizer.minimize` method (nans will be
inserted in the derivatives).
"""

@wraps(minimize)
Expand Down Expand Up @@ -523,6 +527,7 @@ def fun(x):
hess=getattr(res, "hess", None),
exitflag=res.status,
message=res.message,
optimizer=str(self),
)

return optimizer_result
Expand Down Expand Up @@ -612,7 +617,10 @@ def minimize(

# the ipopt return object is a scipy.optimize.OptimizeResult
return OptimizerResult(
x=ret.x, exitflag=ret.status, message=ret.message
x=ret.x,
exitflag=ret.status,
message=ret.message,
optimizer=str(self),
)

def is_least_squares(self):
Expand All @@ -630,7 +638,7 @@ def __init__(self, options: dict = None):
if self.options is None:
self.options = DlibOptimizer.get_default_options(self)
elif "maxiter" not in self.options:
raise KeyError("Dlib options are missing the key word " "maxiter.")
raise KeyError("Dlib options are missing the keyword maxiter.")

def __repr__(self) -> str:
rep = f"<{self.__class__.__name__}"
Expand Down Expand Up @@ -677,7 +685,7 @@ def get_fval_vararg(*x):
0.002,
)

optimizer_result = OptimizerResult()
optimizer_result = OptimizerResult(optimizer=str(self))

return optimizer_result

Expand Down Expand Up @@ -737,7 +745,9 @@ def minimize(
problem.objective.get_fval, lb, ub, **self.options
)

optimizer_result = OptimizerResult(x=np.array(xopt), fval=fopt)
optimizer_result = OptimizerResult(
x=np.array(xopt), fval=fopt, optimizer=str(self)
)

return optimizer_result

Expand Down Expand Up @@ -821,7 +831,7 @@ def minimize(
)

optimizer_result = OptimizerResult(
x=np.array(result[0]), fval=result[1]
x=np.array(result[0]), fval=result[1], optimizer=str(self)
)

return optimizer_result
Expand Down Expand Up @@ -901,7 +911,7 @@ def minimize(
)

optimizer_result = OptimizerResult(
x=np.array(result.x), fval=result.fun
x=np.array(result.x), fval=result.fun, optimizer=str(self)
)

return optimizer_result
Expand Down Expand Up @@ -1019,6 +1029,7 @@ def successively_working_fval(swarm: np.ndarray) -> np.ndarray:
optimizer_result = OptimizerResult(
x=pos,
fval=float(cost),
optimizer=str(self),
)

return optimizer_result
Expand Down Expand Up @@ -1249,6 +1260,7 @@ def nlopt_objective(x, grad):
fval=opt.last_optimum_value(),
message=msg,
exitflag=opt.last_optimize_result(),
optimizer=str(self),
)

return optimizer_result
Expand Down Expand Up @@ -1433,6 +1445,7 @@ def minimize(
hess=opt.hess,
message=msg,
exitflag=opt.exitflag,
optimizer=str(self),
)

return optimizer_result
Expand Down
1 change: 0 additions & 1 deletion pypesto/optimize/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def execute(self) -> OptimizerResult:
history_options=self.history_options,
optimize_options=self.optimize_options,
)
optimizer_result.optimizer = str(self.optimizer)

if not self.optimize_options.report_hess:
optimizer_result.hess = None
Expand Down
1 change: 1 addition & 0 deletions test/optimize/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def check_minimize(problem, library, solver, allow_failed_starts=False):
]:
assert np.isfinite(result.optimize_result.list[0]["fval"])
assert result.optimize_result.list[0]["x"] is not None
assert result.optimize_result.list[0]["optimizer"] is not None


def test_trim_results(problem):
Expand Down

0 comments on commit 31654ae

Please sign in to comment.