Skip to content

Refactor GenericNonlinearExpr and NonlinearOperator constructors#3489

Merged
odow merged 7 commits into
masterfrom
od/vector-valued-args
Sep 8, 2023
Merged

Refactor GenericNonlinearExpr and NonlinearOperator constructors#3489
odow merged 7 commits into
masterfrom
od/vector-valued-args

Conversation

@odow
Copy link
Copy Markdown
Member

@odow odow commented Sep 7, 2023

Alternative for #3451

If we get rid of the "let's check if we can infer the variable type" then a lot of things become much easier.

Comment thread src/nlp_expr.jl Outdated
Comment thread src/nlp_expr.jl Outdated
Comment thread src/nlp_expr.jl Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 7, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.01% ⚠️

Comparison is base (a157126) 98.11% compared to head (cdecbb3) 98.11%.
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3489      +/-   ##
==========================================
- Coverage   98.11%   98.11%   -0.01%     
==========================================
  Files          37       37              
  Lines        5531     5529       -2     
==========================================
- Hits         5427     5425       -2     
  Misses        104      104              
Files Changed Coverage Δ
src/nlp_expr.jl 99.51% <100.00%> (-0.01%) ⬇️
src/operators.jl 96.83% <100.00%> (-0.02%) ⬇️

☔ View full report in Codecov by Sentry.

📢 Have feedback on the report? Share it here.

Comment thread test/test_nlp_expr.jl Outdated
Comment thread src/nlp_expr.jl Outdated
Copy link
Copy Markdown
Member

@blegat blegat left a comment

Choose a reason for hiding this comment

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

I like this

Comment thread src/nlp_expr.jl Outdated
@odow
Copy link
Copy Markdown
Member Author

odow commented Sep 7, 2023

For future reference, here were the functions I removed

function variable_ref_type(
    ::Type{GenericNonlinearExpr},
    ::AbstractArray{T},
) where {T<:AbstractJuMPScalar}
    return variable_ref_type(T)
end

moi_function(x::AbstractArray) = moi_function.(x)

jump_function(model::GenericModel, x::AbstractArray) = jump_function.(model, x)

function test_nonlinear_operator_vector_args()
    model = Model()
    @variable(model, x[1:2, 1:2])
    op_det = NonlinearOperator(LinearAlgebra.det, :det)
    @inferred log(op_det(x))
    z = [2.0 1.0; 1.0 2.0]
    @inferred log(op_det(z))
    @objective(model, Min, log(op_det(x)))
    @test isequal_canonical(objective_function(model), log(op_det(x)))
    f = MOI.get(model, MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction}())
    g = MOI.ScalarNonlinearFunction(:det, Any[index.(x)])
    h = MOI.ScalarNonlinearFunction(:log, Any[g])
    @test f  h
    @test moi_function(log(op_det(x)))  h
    return
end

Comment thread src/nlp_expr.jl
Comment on lines -84 to -92
function GenericNonlinearExpr(head::Symbol, args::Vector{Any})
index = findfirst(Base.Fix2(isa, AbstractJuMPScalar), args)
if index === nothing
error(
"Unable to create a nonlinear expression because it did not " *
"contain any JuMP scalars. head = $head, args = $args.",
)
end
return new{variable_ref_type(args[index])}(head, args)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I understand the motivation for this. Unfortunately, it will break InfiniteOpt, but I can look into refactoring the workflow. If supporting array arguments necessitates this change, then I support it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We can probably just keep this method then. It's not necessary. Let me make some changes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Try this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I confirmed that this works with InfiniteOpt: infiniteopt/InfiniteOpt.jl#321

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, you beat me to it :)

@odow odow changed the title Add support for Array arguments in NonlinearOperator Refactor GenericNonlinearExpr and NonlinearOperator constructors Sep 8, 2023
@odow odow merged commit 0df25a9 into master Sep 8, 2023
@odow odow deleted the od/vector-valued-args branch September 8, 2023 03:37
@odow odow mentioned this pull request Sep 8, 2023
25 tasks
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.

3 participants