From bdb5560a2bc6d978afae1a5ef9a028e9828bb0ec Mon Sep 17 00:00:00 2001 From: Joshua Pulsipher <33848311+pulsipher@users.noreply.github.com> Date: Wed, 1 May 2024 15:23:49 -0400 Subject: [PATCH] Fix legacy NLP interface (#340) * Fix legacy NLP interface * bug fix * minor fix * Updates tests and compat * bug fix * minor patch updates * test update --- Project.toml | 6 +++--- docs/src/guide/expression.md | 4 ++-- src/TranscriptionOpt/transcribe.jl | 10 ++++++++-- test/TranscriptionOpt/transcribe.jl | 2 +- test/macro_expressions.jl | 20 ++++++++++---------- test/optimizer_setup.jl | 2 +- test/show.jl | 2 +- 7 files changed, 26 insertions(+), 20 deletions(-) diff --git a/Project.toml b/Project.toml index 3931da940..d75b01dda 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "InfiniteOpt" uuid = "20393b10-9daf-11e9-18c9-8db751c92c57" authors = ["Joshua Pulsipher and Weiqi Zhang"] -version = "0.5.8" +version = "0.5.9" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" @@ -19,8 +19,8 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" AbstractTrees = "0.4" DataStructures = "0.14.2 - 0.18" Distributions = "0.19 - 0.25" -FastGaussQuadrature = "0.3.2 - 0.4, 0.5" -JuMP = "1.2" +FastGaussQuadrature = "0.3.2 - 0.4, 0.5, 1" +JuMP = "1.15" LeftChildRightSiblingTrees = "0.2" MutableArithmetics = "1" Reexport = "0.2, 1" diff --git a/docs/src/guide/expression.md b/docs/src/guide/expression.md index 41a2e26ec..55bb22259 100644 --- a/docs/src/guide/expression.md +++ b/docs/src/guide/expression.md @@ -206,7 +206,7 @@ julia> expr = 2y^2 - z * y + 42t - 3 2 y(t)² - z*y(t) + 42 t - 3 julia> expr = @expression(model, 2y^2 - z * y + 42t - 3) -2 y(t)² - y(t)*z + 42 t - 3 +2 y(t)² - z*y(t) + 42 t - 3 julia> typeof(expr) GenericQuadExpr{Float64, GeneralVariableRef} @@ -233,7 +233,7 @@ GenericAffExpr{Float64, GeneralVariableRef} julia> expr.terms OrderedCollections.OrderedDict{UnorderedPair{GeneralVariableRef}, Float64} with 2 entries: UnorderedPair{GeneralVariableRef}(y(t), y(t)) => 2.0 - UnorderedPair{GeneralVariableRef}(y(t), z) => -1.0 + UnorderedPair{GeneralVariableRef}(z, y(t)) => -1.0 ``` Notice again that the ordered dictionary preserves the order. diff --git a/src/TranscriptionOpt/transcribe.jl b/src/TranscriptionOpt/transcribe.jl index 9e03b36ce..72cc466cf 100644 --- a/src/TranscriptionOpt/transcribe.jl +++ b/src/TranscriptionOpt/transcribe.jl @@ -487,11 +487,17 @@ function transcription_expression( expr::Union{JuMP.GenericAffExpr, JuMP.GenericQuadExpr}, support::Vector{Float64} ) - # TODO fix this temporary hack (need to handle NLP expressions better) try - return InfiniteOpt.map_expression( + new_expr = InfiniteOpt.map_expression( v -> transcription_expression(trans_model, v, support), expr) + if new_expr isa JuMP.GenericNonlinearExpr + return transcription_expression(trans_model, + convert(InfiniteOpt.NLPExpr, expr), + support) + else + return new_expr + end catch return transcription_expression(trans_model, convert(InfiniteOpt.NLPExpr, expr), diff --git a/test/TranscriptionOpt/transcribe.jl b/test/TranscriptionOpt/transcribe.jl index 59de8b25f..b09af3740 100644 --- a/test/TranscriptionOpt/transcribe.jl +++ b/test/TranscriptionOpt/transcribe.jl @@ -358,7 +358,7 @@ end @test length(transcription_constraint(LowerBoundRef(x))) == 5 @test transcription_constraint(FixRef(x0)) == FixRef(x0t) @test transcription_constraint(BinaryRef(x0)) == BinaryRef(x0t) - @test transcription_constraint(FixRef(y)) == FixRef.(yt)[1:2] + @test transcription_constraint(FixRef(y)) == FixRef.(yt[1:2]) @test transcription_constraint(UpperBoundRef(yf)) == UpperBoundRef(yft) @test transcription_constraint(BinaryRef(z)) == BinaryRef(zt) # test constraint transcriptions diff --git a/test/macro_expressions.jl b/test/macro_expressions.jl index 6162a090b..20bbf27e9 100644 --- a/test/macro_expressions.jl +++ b/test/macro_expressions.jl @@ -79,7 +79,7 @@ end # test nonlinear operations @testset "Nonlinear" begin - @test isequal(@expression(m, pt / inf), pt * (1 / inf)) + @test isequal(@expression(m, pt / inf), pt / inf) @test isequal(@expression(m, pt ^ inf), pt ^ inf) @test isequal(@expression(m, 2 ^ inf), 2 ^ inf) @test isequal(@expression(m, abs(pt)), abs(pt)) @@ -168,7 +168,7 @@ end end # test nonlinear operations @testset "Nonlinear" begin - @test isequal(@expression(m, pt / aff1), pt * (1 / aff1)) + @test isequal(@expression(m, pt / aff1), pt / aff1) @test isequal(@expression(m, pt ^ aff1), pt ^ aff1) @test isequal(@expression(m, abs(aff1)), abs(aff1)) end @@ -256,7 +256,7 @@ end end # test nonlinear operations @testset "Nonlinear" begin - @test isequal(@expression(m, aff1 / pt), aff1 * (1 / pt)) + @test isequal(@expression(m, aff1 / pt), aff1 / pt) @test isequal(@expression(m, aff1 ^ pt), aff1 ^ pt) end end @@ -405,7 +405,7 @@ end end # test nonlinear operations @testset "Nonlinear" begin - @test isequal(@expression(m, aff1 / aff1), aff1 * (1 / aff1)) + @test isequal(@expression(m, aff1 / aff1), aff1 / aff1) @test isequal(@expression(m, aff1 ^ aff1), aff1 ^ aff1) end end @@ -512,7 +512,7 @@ end # test nonlinear operations @testset "Nonlinear" begin @test isequal(@expression(m, quad1 * pt), quad1 * pt) - @test isequal(@expression(m, quad1 / pt), quad1 * (1 / pt)) + @test isequal(@expression(m, quad1 / pt), quad1 / pt) @test isequal(@expression(m, quad1 ^ pt), quad1 ^ pt) @test isequal(@expression(m, abs(quad1)), abs(quad1)) end @@ -619,7 +619,7 @@ end end @testset "Nonlinear" begin @test isequal(@expression(m, pt * quad1), pt * quad1) - @test isequal(@expression(m, pt / quad1), pt * (1 / quad1)) + @test isequal(@expression(m, pt / quad1), pt / quad1) @test isequal(@expression(m, pt ^ quad1), pt ^ quad1) end end @@ -737,7 +737,7 @@ end end @testset "Nonlinear" begin @test isequal(@expression(m, aff1 * quad1), aff1 * quad1) - @test isequal(@expression(m, aff1 / quad1), aff1 * (1 / quad1)) + @test isequal(@expression(m, aff1 / quad1), aff1 / quad1) @test isequal(@expression(m, aff1 ^ quad1), aff1 ^ quad1) end end @@ -856,7 +856,7 @@ end # test nonlinear operations @testset "Nonlinear" begin @test isequal(@expression(m, quad1 * aff1), quad1 * aff1) - @test isequal(@expression(m, quad1 / aff1), quad1 * (1 / aff1)) + @test isequal(@expression(m, quad1 / aff1), quad1 / aff1) @test isequal(@expression(m, quad1 ^ aff1), quad1 ^ aff1) end end @@ -961,7 +961,7 @@ end # test nonlinear operations @testset "Nonlinear" begin @test isequal(@expression(m, quad1 * quad1), quad1 * quad1) - @test isequal(@expression(m, quad1 / quad1), quad1 * (1 / quad1)) + @test isequal(@expression(m, quad1 / quad1), quad1 / quad1) @test isequal(@expression(m, quad1 ^ quad1), quad1 ^ quad1) end end @@ -986,7 +986,7 @@ end @test isequal(@expression(m, nlp ^ quad), nlp ^ quad) @test isequal(@expression(m, 2 ^ quad), 2 ^ quad) @test isequal(@expression(m, 2 ^ nlp), 2 ^ nlp) - @test isequal(@expression(m, nlp / y), nlp * (1 / y)) + @test isequal(@expression(m, nlp / y), nlp / y) @test isequal(@expression(m, nlp + aff), nlp + aff) end # test function calls diff --git a/test/optimizer_setup.jl b/test/optimizer_setup.jl index d38249b55..c9fdc8296 100644 --- a/test/optimizer_setup.jl +++ b/test/optimizer_setup.jl @@ -44,7 +44,7 @@ end # test without inheritance @test isa(set_optimizer_model(m, Model(), inherit_optimizer = false), Nothing) @test length(optimizer_model(m).ext) == 0 - @test_throws ErrorException time_limit_sec(optimizer_model(m)) + @test_throws Union{ErrorException, MOI.GetAttributeNotAllowed{MOI.TimeLimitSec}} time_limit_sec(optimizer_model(m)) @test_throws NoOptimizer optimize!(optimizer_model(m)) end # optimizer_model_key (optimizer models) diff --git a/test/show.jl b/test/show.jl index 6fdaea7e2..6bca82467 100644 --- a/test/show.jl +++ b/test/show.jl @@ -717,7 +717,7 @@ end end # test Base.show (GeneralVariableRef in IJulia) @testset "Base.show (IJulia GeneralVariableRef)" begin - show_test(MIME("text/latex"), y, "\$\$ y \$\$") + show_test(MIME("text/latex"), y, "\$ y \$") end # test Base.show (GeneralVariableRef in REPL) @testset "Base.show (REPL GeneralVariableRef)" begin