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

Extend add_to_expression! for NonlinearExpr #3505

Closed
hdavid16 opened this issue Sep 13, 2023 · 6 comments · Fixed by #3506
Closed

Extend add_to_expression! for NonlinearExpr #3505

hdavid16 opened this issue Sep 13, 2023 · 6 comments · Fixed by #3506
Labels
Category: Nonlinear Related to nonlinear programming Type: Feature request

Comments

@hdavid16
Copy link
Contributor

Hi, it would be nice to use add_to_expression! with NonlinearExpr. The following currently errors on master:

(@v1.9) pkg> st JuMP
Status `~/.julia/environments/v1.9/Project.toml`
  [4076af6c] JuMP v1.14.1 `https://github.com/jump-dev/JuMP.jl.git#master`

using JuMP
m=Model(); @variable(m,x);
ex = zeros(NonlinearExpr)
add_to_expression!(ex, exp(x))
ERROR: MethodError: no method matching add_to_expression!(::Array{NonlinearExpr, 0}, ::NonlinearExpr)

Closest candidates are:
  add_to_expression!(::GenericQuadExpr{C, V}, ::Union{GenericAffExpr{C, V}, V}, ::Union{Number, LinearAlgebra.UniformScaling}) where {C, V}
   @ JuMP ~/.julia/packages/JuMP/8MuvO/src/quad_expr.jl:364
  add_to_expression!(::GenericQuadExpr{C, V}, ::V) where {C, V}
   @ JuMP ~/.julia/packages/JuMP/8MuvO/src/quad_expr.jl:323
  add_to_expression!(::GenericQuadExpr{C, V}, ::V, ::GenericAffExpr{C, V}) where {C, V}
   @ JuMP ~/.julia/packages/JuMP/8MuvO/src/quad_expr.jl:417
  ...

@odow
Copy link
Member

odow commented Sep 13, 2023

So one problem with this is that add_to_expression! modifies in-place. So this only makes sense if the expression is a :+ node.

Should we try and throw an error if not?

When is add_to_expression! useful for you?

@odow odow added Category: Nonlinear Related to nonlinear programming Type: Feature request labels Sep 13, 2023
@hdavid16
Copy link
Contributor Author

I see. I am incrementally building a nonlinear expression to reformulate a disjunct constraint in DisjunctiveProgramming.jl. I notice that when I initialize the expression with zero(NonlinearExpr), the resulting expression has :+ as the head. Would you suggest I just do expr += ...? Or would it make sense to extend add_to_expression! for the case when :+ is the head so it can be modified in place and throw an error when the head is different?

@odow
Copy link
Member

odow commented Sep 14, 2023

Are you just adding a sum of terms? += will work. Do have a larger example?

I'd just do:

julia> model = Model();

julia> @variable(model, x)
x

julia> y = 0
0

julia> for _ in 1:3
           y += exp(x)
       end

julia> y
((0.0 + exp(x)) + exp(x)) + exp(x)

julia> flatten!(y)
0.0 + exp(x) + exp(x) + exp(x)

@hdavid16
Copy link
Contributor Author

Yes, I'm currently using += and will continue doing so then. Thanks.

Perhaps a better error message could be provided telling the user that the in-place modification is not supported for nonlinear expressions if you want. However, the stack trace points to the issue anyways.

@odow
Copy link
Member

odow commented Sep 14, 2023

See #3506

@blegat
Copy link
Member

blegat commented Sep 14, 2023

I think MA.add_mul!! does work though. It will modify in-place if the head is :+ and return a new instance otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Nonlinear Related to nonlinear programming Type: Feature request
Development

Successfully merging a pull request may close this issue.

3 participants