-
Notifications
You must be signed in to change notification settings - Fork 56
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
MAINT: minimize_cyipopt: add input validation #206
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,9 +107,26 @@ def __init__(self, | |
raise ImportError() | ||
self.obj_hess = None | ||
self.last_x = None | ||
|
||
# Input validation of user-provided arguments | ||
if fun is not None and not callable(fun): | ||
raise ValueError('`fun` must be callable.') | ||
if not isinstance(args, tuple): | ||
args = (args,) | ||
kwargs = dict() if kwargs is None else kwargs | ||
if not isinstance(kwargs, dict): | ||
raise ValueError('`kwargs` must be a dictionary.') | ||
if jac is not None and jac not in {True, False} and not callable(jac): | ||
raise ValueError('`jac` must be callable or boolean.') | ||
if hess is not None and not callable(hess): | ||
raise ValueError('`hess` must be callable.') | ||
if hessp is not None: | ||
msg = 'Using hessian matrix times an arbitrary vector is not yet implemented!' | ||
raise NotImplementedError(msg) | ||
raise NotImplementedError( | ||
'`hessp` is not yet supported by Ipopt.`') | ||
# TODO: add input validation for `constraints` when adding | ||
# support for instances of new-style constraints (e.g. | ||
# `NonlinearConstraint`) and sequences of constraints. | ||
|
||
if hess is not None: | ||
self.obj_hess = hess | ||
if jac is None: | ||
|
@@ -118,8 +135,7 @@ def __init__(self, | |
elif jac is True: | ||
fun = MemoizeJac(fun) | ||
jac = fun.derivative | ||
elif not callable(jac): | ||
raise NotImplementedError('jac has to be bool or a function') | ||
|
||
self.fun = fun | ||
self.jac = jac | ||
self.args = args | ||
|
@@ -465,6 +481,11 @@ def minimize_ipopt(fun, | |
msg = 'Install SciPy to use the `minimize_ipopt` function.' | ||
raise ImportError(msg) | ||
|
||
res = _minimize_ipopt_iv(fun, x0, args, kwargs, method, jac, hess, hessp, | ||
bounds, constraints, tol, callback, options) | ||
(fun, x0, args, kwargs, method, jac, hess, hessp, | ||
bounds, constraints, tol, callback, options) = res | ||
|
||
_x0 = np.atleast_1d(x0) | ||
|
||
lb, ub = get_bounds(bounds) | ||
|
@@ -533,3 +554,33 @@ def minimize_ipopt(fun, | |
nfev=problem.nfev, | ||
njev=problem.njev, | ||
nit=problem.nit) | ||
|
||
def _minimize_ipopt_iv(fun, x0, args, kwargs, method, jac, hess, hessp, | ||
bounds, constraints, tol, callback, options): | ||
# basic input validation for minimize_ipopt that is not included in | ||
# IpoptProblemWrapper | ||
|
||
x0 = np.asarray(x0)[()] | ||
if not np.issubdtype(x0.dtype, np.number): | ||
raise ValueError('`x0` must be a numeric array.') | ||
|
||
if method is not None: # this will be updated when gh-200 is merged | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and gh-200 can be merged in either order. I'll make sure the other gets updated accordingly. |
||
raise NotImplementedError('`method` is not yet supported.`') | ||
|
||
# TODO: add input validation for `bounds` when adding | ||
# support for instances of new-style constraints (e.g. `Bounds`) | ||
|
||
if callback is not None: | ||
raise NotImplementedError('`callback` is not yet supported by Ipopt.`') | ||
|
||
if tol is not None: | ||
tol = np.asarray(tol)[()] | ||
if tol.ndim != 0 or not np.issubdtype(tol.dtype, np.number) or tol <= 0: | ||
raise ValueError('`tol` must be a positive scalar.') | ||
|
||
options = dict() if options is None else options | ||
if not isinstance(options, dict): | ||
raise ValueError('`options` must be a dictionary.') | ||
|
||
return (fun, x0, args, kwargs, method, jac, hess, hessp, | ||
bounds, constraints, tol, callback, options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have pointed out that I changed this to a
ValueError
above because I'm not sure if there is anything specific to be implemented. (hessp
is different because there is something specific we have in mind.) Hope that's OK. If not, I can change it back in the next PR (in which I'll add support forBounds
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be a rare case that someone relied on this as a feature, so we can go with your change.