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

Support the JuMP macro operator #197

Merged
merged 4 commits into from
Sep 6, 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
1 change: 1 addition & 0 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function MOI.supports(
MOI.ObjectiveSense,
MOI.ObjectiveFunction{<:Union{LinQuad, MOI.ScalarNonlinearFunction}},
MOI.NLPBlock,
MOI.UserDefinedFunction,
},
)
return true
Expand Down
37 changes: 21 additions & 16 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -426,41 +426,46 @@ function parser_MOI(moimodel, index_map, nvar)
end

# Affine or quadratic, nothing to do
function _nlp_model(model::Union{Nothing, MOI.Nonlinear.Model}, ::MOI.ModelLike, ::Type, ::Type)
return model
end
_nlp_model(::MOI.Nonlinear.Model, ::MOI.ModelLike, ::Type, ::Type) = false

function _nlp_model(
dest::Union{Nothing, MOI.Nonlinear.Model},
dest::MOI.Nonlinear.Model,
src::MOI.ModelLike,
F::Type{<:Union{MOI.ScalarNonlinearFunction, MOI.VectorNonlinearFunction}},
S::Type,
)
has_nonlinear = false
for ci in MOI.get(src, MOI.ListOfConstraintIndices{F, S}())
if isnothing(dest)
dest = MOI.Nonlinear.Model()
end
MOI.Nonlinear.add_constraint(
dest,
MOI.get(src, MOI.ConstraintFunction(), ci),
MOI.get(src, MOI.ConstraintSet(), ci),
)
has_nonlinear = true
end
return dest
return has_nonlinear
end

function _nlp_model(model::MOI.ModelLike)
nlp_model = nothing
function _nlp_model(model::MOI.ModelLike)::Union{Nothing,MOI.Nonlinear.Model}
nlp_model = MOI.Nonlinear.Model()
has_nonlinear = false
for attr in MOI.get(model, MOI.ListOfModelAttributesSet())
if attr isa MOI.UserDefinedFunction
has_nonlinear = true
args = MOI.get(model, attr)
MOI.Nonlinear.register_operator(nlp_model, attr.name, attr.arity, args...)
end
end
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
nlp_model = _nlp_model(nlp_model, model, F, S)
has_nonlinear |= _nlp_model(nlp_model, model, F, S)
end
F = MOI.get(model, MOI.ObjectiveFunctionType())
if F <: MOI.ScalarNonlinearFunction
if isnothing(nlp_model)
nlp_model = MOI.Nonlinear.Model()
end
attr = MOI.ObjectiveFunction{F}()
MOI.Nonlinear.set_objective(nlp_model, MOI.get(model, attr))
MOI.Nonlinear.set_objective(nlp_model, MOI.get(model, MOI.ObjectiveFunction{F}()))
has_nonlinear = true
end
if !has_nonlinear
return nothing
end
return nlp_model
end
Expand Down
2 changes: 2 additions & 0 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function test_runtests()
r"test_nonlinear_hs071_no_hessian$",
r"test_nonlinear_hs071_hessian_vector_product$",
r"test_nonlinear_hs071$",
# Require hessian = false in the constructor of MathOptNLPModel
r"test_nonlinear_expression_multivariate_function$",
# FIXME We should look over all attributes set and error
# for unknown ones instead of just getting the ones we know we support
# and ignoring the rest
Expand Down
47 changes: 47 additions & 0 deletions test/nlp_problems/operatorspb.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
function operatorspb()
nlp = Model()
x0 = [390, 1000, 419.5, 340.5, 198.175, 0.5]
lvar = [0, 0, 340, 340, -1000, 0]
uvar = [400, 1000, 420, 420, 10000, 0.5236]
@variable(nlp, lvar[i] <= x[i = 1:6] <= uvar[i], start = x0[i])

a = 131078 // 1000
b = 148577 // 100000
ci = 90798 // 100000
d = cos(147588 // 100000)
e = sin(147588 // 100000)

@constraint(nlp, 300 - x[1] - 1 / a * x[3] * x[4] * cos(b - x[6]) + ci / a * d * x[3] == 0)
@constraint(nlp, -x[2] - 1 / a * x[3] * x[4] * cos(b + x[6]) + ci / a * d * x[4]^2 == 0)
@constraint(nlp, -x[5] - 1 / a * x[3] * x[4] * cos(b + x[6]) + ci / a * e * x[4]^2 == 0)
@constraint(nlp, 200 - 1 / a * x[3] * x[4] * sin(b - x[6]) + ci / a * e * x[3]^2 == 0)

function f1(t)
return if 0 <= t <= 300
30 * t
elseif 300 <= t <= 400
31 * t
else
eltype(x)(Inf)
end
end

function f2(t)
return if 0 <= t <= 100
28 * t
elseif 100 <= t <= 200
29 * t
elseif 200 <= t <= 1000
30 * t
else
eltype(t)(Inf)
end
end
@operator(nlp, op_f1, 1, f1)
@expression(nlp, op_f1)
@operator(nlp, op_f2, 1, f2)
@expression(nlp, op_f2)
@objective(nlp, Min, op_f1(x[1]) + op_f2(x[2]))

return nlp
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using Test, Printf
nlp_problems = setdiff(NLPModelsTest.nlp_problems, ["MGH01Feas"])
nls_problems = NLPModelsTest.nls_problems

extra_nlp_problems = ["nohesspb", "hs61", "hs100", "hs219", "quadcon"]
extra_nlp_problems = ["nohesspb", "hs61", "hs100", "hs219", "quadcon", "operatorspb"]
extra_nls_problems = ["nlsnohesspb", "HS30", "HS43", "MGH07", "nlsqc"]

for problem in lowercase.(nlp_problems ∪ extra_nlp_problems)
Expand Down
Loading