Skip to content

Conversation

andyfaff
Copy link
Contributor

Assesses whether constraints are feasible for a given solution vector (scipy.optimize._constraints.PreparedConstraint.is_feasible()), and also adds a method to calculate how much each of the constraints are exceeded by (scipy.optimize._constraints.PreparedConstraint.excess_violation())

@andyfaff
Copy link
Contributor Author

This PR is part of a roadmap to apply constraints to differential_evolution, and possibly other solvers.

@andyfaff andyfaff requested a review from mdhaber March 29, 2019 06:40
rlucas7 pushed a commit to rlucas7/scipy that referenced this pull request Apr 2, 2019
…inuous_basic.py.

Added test_nomodify_gh9900_regression() to simulate thread interleaving.
Added test_broadcast_gh9990_regression() to validate broadcast for
out-of-support arguments, and to check support endpoints.
pvanmulbregt added a commit to pvanmulbregt/scipy that referenced this pull request Apr 4, 2019
…inuous_basic.py.

Added test_nomodify_gh9900_regression() to simulate thread interleaving.
Added test_broadcast_gh9990_regression() to validate broadcast for
out-of-support arguments, and to check support endpoints.
@andyfaff
Copy link
Contributor Author

andyfaff commented Apr 5, 2019

test fail is a timeout.

@rgommers rgommers added the enhancement A new feature or improvement label Apr 14, 2019
@rgommers rgommers added this to the 1.3.0 milestone Apr 14, 2019
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks sensible to me. Only a few minor comments. The .fun.fun is a bit unfortunately, the first .fun could have been named better. But that can't be easily changed anymore.

I'm not very familiar with this code, so I'll let @mdhaber or someone else comment on whether the added methods fit the overall design here.

@mdhaber
Copy link
Contributor

mdhaber commented Apr 14, 2019

@antonior92 thoughts?
I only wrote the code to convert between old and new style constraints.
I'll try to take a look, though.

@mdhaber
Copy link
Contributor

mdhaber commented Apr 14, 2019

First thought - when constraints are active they are not always satisfied strictly, so I would suggest adding a tolerance.

@andyfaff
Copy link
Contributor Author

@mdhaber, @antonior92 I'm not familiar with how constraint tolerances are specified/used elsewhere in scipy.optimize; I wouldn't want to make such a change without being familiar with correct usage, otherwise it gets harder to change later. If someone could confirm the change needed [e.g. just add tolerance kwd to is_satisfied, then amend to feasible_lb = np.all(ev >= self.bounds[0] - tolerance)] then I can make the changes. Otherwise, I'd prefer to get this merged as is, someone else can make further changes later.

This PR is a prerequisite for #10002, where such tolerances aren't needed (not mentioned in the paper I'm basing the modifications on). In fact all that's needed from this PR is the excess_violation method.

@antonior92
Copy link
Member

antonior92 commented Apr 16, 2019

By what I understood we do not need is_satisfied for anything. Maybe this commit could add just excess_violation... And we add is_satisfiedwhen needed.

Besides that, is_satisfiedcan be easily implemented from excess_violation with the desired tolerance.

is_satisfied = constr.excess_violation > tolerance

@andyfaff
Copy link
Contributor Author

Based on feedback from @antonior92 the recent commit:

  • removes the is_satisfied method (can be implemented in the future using the violation method.
  • renames the excess_violation method to violation.

@antonior92
Copy link
Member

Great!

+1 for merging this and moving on to #10002...

Copy link
Contributor

@tylerjereddy tylerjereddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make a note to merge this soon--I added two minor comments.


assert_array_equal(pc.violation([0.5, 1]), [0., 0.])

np.testing.assert_almost_equal(pc.violation([0.5, 1.2]), [0., 0.1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with assert_array_equal, we should probably import assert_almost_equal at the top level. But, pretty minor point.

"""
with suppress_warnings() as sup:
sup.filter(UserWarning)
ev = self.fun.fun(np.asarray(x))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be harmful to pass subclasses through with np.asanyarray? Don't know if that ever happens in practice / is useful for optimize stuff, so maybe just confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The array-like is passed to a user defined function, so it would be up to that user defined function as to whether it was acceptable. Internally differential_evolution uses np.float64, so the array will be that dtype. However I think internally other minimizers can use other dtypes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just a little worried that we might clobber ndarray subclasses with the choice of asarray instead of asanyarray, with no strong reason to chose the former.

Anyway, the reviews here are positive and there's a +1 from expert reviewer so in it goes!

@tylerjereddy tylerjereddy merged commit fd3d85f into scipy:master Apr 18, 2019
@tylerjereddy
Copy link
Contributor

Not sure if the release notes need a sentence for this PR? Please add one, if appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement scipy.optimize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants