Skip to content
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

Closed
wants to merge 4 commits into from
Closed

Conversation

odow
Copy link
Member

@odow odow commented Nov 12, 2024

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?

y = @variable(model)
set_name(y, name)
@constraint(model, y == expr)
return y
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
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.

test/test_macros.jl Outdated Show resolved Hide resolved
Copy link

codecov bot commented Nov 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.58%. Comparing base (5e4273d) to head (5b7cc3a).
Report is 4 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

@joaquimg
Copy link
Member

I don't like @expression adding variables and constraints. I would force users to do both steps or create a new macro @subexpression (which still seems to be a very suboptimal solution, rethinking the name might be needed).

@joaquimg
Copy link
Member

Having it in the docs (as is) seems to be a better way.

@odow
Copy link
Member Author

odow commented Nov 17, 2024

Closing as won't-fix.

@odow odow closed this Nov 17, 2024
@odow odow deleted the od/subexpression branch November 17, 2024 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants