-
-
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
Add subexpression kwarg to expression macro #3878
Conversation
y = @variable(model) | ||
set_name(y, name) | ||
@constraint(model, y == expr) | ||
return y |
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.
return y | |
return y, expr |
This would enable
julia> model = Model()
A JuMP Model
├ solver: none
├ objective_sense: FEASIBILITY_SENSE
├ num_variables: 0
├ num_constraints: 0
└ Names registered in the model: none
julia> @variable(model, x)
x
julia> @expression(model, ex, sin(x), subexpression = true)
(ex, sin(x))
julia> ex
(ex, sin(x))
julia> model[:ex]
(ex, sin(x))
julia> model = Model()
A JuMP Model
├ solver: none
├ objective_sense: FEASIBILITY_SENSE
├ num_variables: 0
├ num_constraints: 0
└ Names registered in the model: none
julia> @variable(model, x[1:2])
2-element Vector{VariableRef}:
x[1]
x[2]
julia> @expression(model, ex[i in 1:2], sin(x[i]), subexpression = true)
2-element Vector{Tuple{VariableRef, NonlinearExpr}}:
(ex[1], sin(x[1]))
(ex[2], sin(x[2]))
julia> first.(ex)
2-element Vector{VariableRef}:
ex[1]
ex[2]
julia> last.(ex)
2-element Vector{NonlinearExpr}:
sin(x[1])
sin(x[2])
which seems quite nice.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #3878 +/- ##
=======================================
Coverage 99.58% 99.58%
=======================================
Files 43 43
Lines 6028 6044 +16
=======================================
+ Hits 6003 6019 +16
Misses 25 25 ☔ View full report in Codecov by Sentry. |
I don't like |
Having it in the docs (as is) seems to be a better way. |
Closing as won't-fix. |
Trying out an idea from #3738. We don't necessarily need to merge this. Open to thoughts/feedback.
The benefit of this approach is simplicity.
The downsides are that it doesn't solve the issue in MOI, it doesn't utilise the subexpression support in
MOI.Nonlinear
, and we don't have a way for the user to query the actual subexpression.Perhaps we should return a tuple of the
VariableRef
and the expression, so people could use either-or?