-
-
Notifications
You must be signed in to change notification settings - Fork 398
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
Fix error message if NonlinearExpression is mixed with new NL API #3741
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #3741 +/- ##
==========================================
- Coverage 98.40% 98.37% -0.04%
==========================================
Files 43 43
Lines 5820 5835 +15
==========================================
+ Hits 5727 5740 +13
- Misses 93 95 +2 ☔ View full report in Codecov by Sentry. |
This PR addresses mixing using JuMP
m = Model()
@variable(m, x)
@NLexpression(m, nl_ex, x^3)
@objective(m, Min, nl_ex) ERROR: MethodError: no method matching check_belongs_to_model(::NonlinearExpression, ::Model)
Closest candidates are:
check_belongs_to_model(::ConstraintRef, ::AbstractModel)
@ JuMP ~\.julia\packages\JuMP\027Gt\src\constraints.jl:63
check_belongs_to_model(::ScalarConstraint, ::Any)
@ JuMP ~\.julia\packages\JuMP\027Gt\src\constraints.jl:614
check_belongs_to_model(::VectorConstraint, ::Any)
@ JuMP ~\.julia\packages\JuMP\027Gt\src\constraints.jl:674
...
Stacktrace:
[1] set_objective_function(model::Model, func::NonlinearExpression)
@ JuMP ~\.julia\packages\JuMP\027Gt\src\objective.jl:129
[2] set_objective(model::Model, sense::MathOptInterface.OptimizationSense, func::NonlinearExpression)
@ JuMP ~\.julia\packages\JuMP\027Gt\src\objective.jl:176
[3] macro expansion
@ ~\.julia\packages\JuMP\027Gt\src\macros\@objective.jl:70 [inlined]
[4] top-level scope
@ REPL[5]:1 This can be remedied by adding something like: function check_belongs_to_model(::NonlinearExpression, ::GenericModel)
error("Expressions made with `@NLexpression` are only compatible for use with `@NLobjective` and `@NLconstraint`")
end |
Hahah but whhyyyyy would you do this. I'll fix 😄 |
Never underestimate the power of typos and/or ignorance 😅 |
Okay. I've actually tweaked things slightly, because this has nothing to do with the new API, it would also have been a bug in older versions of JuMP. We now continue to play laissez-faire with expression construction--for better and worse--but we now error when we attempt to convert them to MOI functions. This provides a much more uniform error message for all the possible ways that they could end up being passed to the solver, and we can provide a nice error message. |
I understand the logic behind relying on Lines 374 to 438 in fbe96e5
Why would this be expected behavior that we have tests that ensure it remains? What is the purpose of these tests? |
Okay, how about now. |
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.
Looks good to me, thanks!
Closes #3740
TODO