Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] wrapping cost function to return best value explored #33

Merged
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8d8cf99
[feature] wrapping cost function to return best value explored
VolodyaCO Jan 2, 2023
ff18230
[fix] complying with minimize signature
VolodyaCO Jan 2, 2023
4ce8bd7
[feature] saving best value and params only if params are bounded and…
VolodyaCO Jan 3, 2023
0a5ce54
Merge branch 'main' into fix/#29/wrap-cost-function-to-return-best-va…
VolodyaCO Jan 3, 2023
29fa0a3
[fix] correct way of checking bounds
VolodyaCO Jan 3, 2023
03e016f
[test] testing edge case of the langermann function
VolodyaCO Jan 3, 2023
0bb0216
[fix] fixes test
VolodyaCO Jan 3, 2023
a7bfb2b
[fix] inequality constraint fails if function is less than 0 only
VolodyaCO Jan 3, 2023
7026892
[style] length of comment
VolodyaCO Jan 3, 2023
c2311d0
[feature] expose argument to control if best parameters are stored
VolodyaCO Jan 16, 2023
3c84347
[feature] typing
VolodyaCO Jan 16, 2023
fa7da9b
[feature] wrapping cost function to return best value explored
VolodyaCO Jan 2, 2023
718322c
[fix] complying with minimize signature
VolodyaCO Jan 2, 2023
b015210
[feature] saving best value and params only if params are bounded and…
VolodyaCO Jan 3, 2023
f0afb09
[fix] correct way of checking bounds
VolodyaCO Jan 3, 2023
a4ff2a7
[test] testing edge case of the langermann function
VolodyaCO Jan 3, 2023
f17b02d
[fix] fixes test
VolodyaCO Jan 3, 2023
de20411
[fix] inequality constraint fails if function is less than 0 only
VolodyaCO Jan 3, 2023
e46d423
[style] length of comment
VolodyaCO Jan 3, 2023
c878851
[feature] expose argument to control if best parameters are stored
VolodyaCO Jan 16, 2023
9818a7a
[feature] typing
VolodyaCO Jan 16, 2023
3ee4bad
[test] asserting that the opt value is the minimum of the history
VolodyaCO Jun 2, 2023
3905209
Merge branch 'fix/#29/wrap-cost-function-to-return-best-val-scipy-opt…
VolodyaCO Jun 2, 2023
8192721
[style] mypy corrections
VolodyaCO Jun 2, 2023
6e6a611
[fix] seeding ttopt
VolodyaCO Jun 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[feature] expose argument to control if best parameters are stored
  • Loading branch information
VolodyaCO committed Jan 16, 2023
commit c2311d05e564649b4ebabefb3a88900e2513239b
36 changes: 30 additions & 6 deletions src/orquestra/opt/optimizers/scipy_optimizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
################################################################################
# © Copyright 2022 Zapata Computing Inc.
################################################################################
import warnings
from typing import Callable, Dict, Optional, Sequence, Tuple, Union

import numpy as np
Expand Down Expand Up @@ -46,6 +47,17 @@ def __init__(
# Inherit all attributes from the cost function
for attr in dir(cost_function):
if not attr.startswith("__"):
if attr in [
"best_value",
"best_params",
"_are_params_bounded",
"_are_params_constrained",
]:
warnings.warn(
f"Attribute {attr} of the cost function is being overwritten "
"by the optimizer.",
UserWarning,
)
setattr(self, attr, getattr(cost_function, attr))
self.best_params.fill(np.nan)
self.constraints = constraints
Expand Down Expand Up @@ -99,6 +111,7 @@ def __init__(
] = None,
options: Optional[Dict] = None,
recorder: RecorderFactory = _recorder,
store_best_parameters: bool = True,
):
"""
Integration with scipy optimizers. Documentation for this module is minimal,
Expand All @@ -111,6 +124,8 @@ def __init__(
options: dictionary with additional options for the optimizer.
recorder: recorder object which defines how to store
the optimization history.
store_best_parameters: whether to wrap the cost function to store the best
parameters the optimizer has seen so far.

""" # noqa: E501
super().__init__(recorder=recorder)
Expand All @@ -120,11 +135,17 @@ def __init__(
self.options = options
self.constraints = [] if constraints is None else constraints
self.bounds = bounds
self.store_best_parameters = store_best_parameters

def _preprocess_cost_function(
self, cost_function: Union[CallableWithGradient, Callable]
) -> _CostFunctionWithBestValue:
return _CostFunctionWithBestValue(cost_function, self.constraints, self.bounds)
) -> Union[CallableWithGradient, Callable, _CostFunctionWithBestValue]:
if self.store_best_parameters:
return _CostFunctionWithBestValue(
cost_function, self.constraints, self.bounds
)
else:
return cost_function

def _minimize(
self,
Expand All @@ -142,7 +163,6 @@ def _minimize(
evaluations should be recorded.

"""
assert isinstance(cost_function, _CostFunctionWithBestValue)
jacobian = None
if isinstance(cost_function, CallableWithGradient) and callable(
getattr(cost_function, "gradient")
Expand All @@ -158,8 +178,12 @@ def _minimize(
bounds=self.bounds,
jac=jacobian,
)
opt_value = cost_function.best_value
opt_params = cost_function.best_params
if isinstance(cost_function, _CostFunctionWithBestValue):
opt_value = cost_function.best_value
opt_params = cost_function.best_params
else:
opt_value = result.fun
opt_params = result.x

nit = result.get("nit", None)
nfev = result.get("nfev", None)
Expand All @@ -169,5 +193,5 @@ def _minimize(
opt_params=opt_params,
nit=nit,
nfev=nfev,
**construct_history_info(cost_function, keep_history) # type: ignore
**construct_history_info(cost_function, keep_history), # type: ignore
)