-
Notifications
You must be signed in to change notification settings - Fork 109
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
Added linear constraint check and updated documentation #410
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc08914
updated linear constraint guide
kanekosh 46b2a31
Raise error if user is returning linear constraint values
A-CGray 70d01eb
minor edits
kanekosh 1fd9532
modified some tests and added a test
kanekosh 1e3ce7c
format
kanekosh 89e4935
Merge branch 'main' into lincon_docs
marcomangano 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
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
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Tests that pyOptSparse raises an error when a user-defined obj/con function returns a linear constraint value | ||
(which should not because linear constraint is defined exclusively by jac and bounds) | ||
""" | ||
|
||
# Standard Python modules | ||
import unittest | ||
|
||
# First party modules | ||
from pyoptsparse import SLSQP, Optimization | ||
from pyoptsparse.pyOpt_error import Error | ||
|
||
|
||
def objfunc(xdict): | ||
"""Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3""" | ||
x = xdict["x"] | ||
funcs = {} | ||
|
||
funcs["obj"] = x**2 | ||
funcs["con"] = x - 1 # falsely return a linear constraint value | ||
|
||
fail = False | ||
return funcs, fail | ||
|
||
|
||
class TestLinearConstraintCheck(unittest.TestCase): | ||
def test(self): | ||
# define an optimization problem with a linear constraint | ||
optProb = Optimization("test", objfunc) | ||
optProb.addVarGroup("x", 1, value=1) | ||
optProb.addObj("obj") | ||
optProb.addConGroup("con", 1, lower=1.0, linear=True, wrt=["x"], jac={"x": [1.0]}) | ||
|
||
opt = SLSQP() | ||
with self.assertRaises(Error) as context: | ||
opt(optProb, sens="FD") | ||
|
||
# check if we get the expected error message | ||
err_msg = ( | ||
"Value for linear constraint returned from user obj function. Linear constraints " | ||
+ "are evaluated internally and should not be returned from the user's function." | ||
) | ||
self.assertEqual(err_msg, str(context.exception)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
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
Oops, something went wrong.
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.
Nice catch, I assume the new check had these tests failing right away, right?
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.
Yes you're right