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

Fix legacy NLP interface #340

Merged
merged 7 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions docs/src/guide/expression.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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.

Expand Down
10 changes: 8 additions & 2 deletions src/TranscriptionOpt/transcribe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion test/TranscriptionOpt/transcribe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions test/macro_expressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/optimizer_setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading