Skip to content

[FileFormats.MOF] make use_nlp_block=false the default if SNF in model #2688

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

Merged
merged 2 commits into from
Mar 11, 2025
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 src/FileFormats/MOF/MOF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ struct Options
print_compact::Bool
warn::Bool
differentiation_backend::MOI.Nonlinear.AbstractAutomaticDifferentiation
use_nlp_block::Bool
use_nlp_block::Union{Bool,Nothing}
end

function get_options(m::Model)
return get(
m.model.ext,
:MOF_OPTIONS,
Options(false, false, MOI.Nonlinear.SparseReverseMode(), true),
Options(false, false, MOI.Nonlinear.SparseReverseMode(), nothing),
)
end

Expand All @@ -144,7 +144,7 @@ function Model(;
print_compact::Bool = false,
warn::Bool = false,
differentiation_backend::MOI.Nonlinear.AbstractAutomaticDifferentiation = MOI.Nonlinear.SparseReverseMode(),
use_nlp_block::Bool = true,
use_nlp_block::Union{Bool,Nothing} = nothing,
)
model = MOI.Utilities.UniversalFallback(InnerModel{Float64}())
model.model.ext[:MOF_OPTIONS] =
Expand Down
10 changes: 9 additions & 1 deletion src/FileFormats/MOF/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ function Base.read!(io::IO, model::Model)
read_objective(model, object, name_map)
read_constraints(model, object, name_map)
options = get_options(model)
if options.use_nlp_block
# We should convert to NLPBlock if...
# | options.use_nlp_block
# | true false nothing
# object["has_scalar_nonlinear"] = false | 1 0 1
# = true | 1 0 0
if something(
options.use_nlp_block,
!get(object, "has_scalar_nonlinear", false),
)
_convert_to_nlpblock(model)
end
return
Expand Down
17 changes: 12 additions & 5 deletions src/FileFormats/MOF/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ function Base.write(io::IO, model::Model)
variables, constraints = NamedTuple[], NamedTuple[]
name_map = _write_variables(variables, model)
objective = _write_nlpblock(constraints, model, name_map)
has_scalar_nonlinear = false
if objective === nothing
objective = _write_objective(model, name_map)
objective, has_scalar_nonlinear = _write_objective(model, name_map)
end
_write_constraints(constraints, model, name_map)
has_scalar_nonlinear |= _write_constraints(constraints, model, name_map)
object = (;
name = "MathOptFormat Model",
version = (
Expand All @@ -29,6 +30,9 @@ function Base.write(io::IO, model::Model)
objective = objective,
constraints = constraints,
)
if has_scalar_nonlinear
object = (; has_scalar_nonlinear = true, object...)
end
JSON3.write(io, object)
return
end
Expand Down Expand Up @@ -122,27 +126,30 @@ function _write_objective(
)
sense = MOI.get(model, MOI.ObjectiveSense())
if sense == MOI.FEASIBILITY_SENSE
return (; :sense => moi_to_object(sense))
return (; :sense => moi_to_object(sense)), false
end
F = MOI.get(model, MOI.ObjectiveFunctionType())
objective_function = MOI.get(model, MOI.ObjectiveFunction{F}())
return (;
object = (;
:sense => moi_to_object(sense),
:function => moi_to_object(objective_function, name_map),
)
return object, (F == MOI.ScalarNonlinearFunction)
end

function _write_constraints(
constraints::Vector{NamedTuple},
model::Model,
name_map::Dict{MOI.VariableIndex,String},
)
has_scalar_nonlinear = false
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
has_scalar_nonlinear |= (F == MOI.ScalarNonlinearFunction)
for index in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
push!(constraints, moi_to_object(index, model, name_map))
end
end
return
return has_scalar_nonlinear
end

"""
Expand Down
45 changes: 41 additions & 4 deletions test/FileFormats/MOF/MOF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,7 @@ variables: x
minobjective: ScalarNonlinearFunction(exp(x))
""",
["x"],
String[];
use_nlp_block = false,
String[],
)
end

Expand All @@ -779,8 +778,7 @@ variables: x
c1: ScalarNonlinearFunction(exp(x)^2) <= 1.0
""",
["x"],
["c1"];
use_nlp_block = false,
["c1"],
)
end

Expand Down Expand Up @@ -1553,6 +1551,45 @@ function test_write_NLPBlock_no_objective()
return
end

function test_use_nlp_block()
model = MOF.Model()
x = MOI.add_variable(model)
MOI.set(model, MOI.VariableName(), x, "x")
f = MOI.ScalarNonlinearFunction(:log, Any[x])
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
io = IOBuffer()
write(io, model)
seekstart(io)
object = JSON3.read(io, Dict{String,Any})
@test object["has_scalar_nonlinear"] == true
@test JSONSchema.validate(SCHEMA, object) === nothing
# Test (; use_nlp_block = nothing)
seekstart(io)
model = MOF.Model()
read!(io, model)
@test MOI.get(model, MOI.NLPBlock()) === nothing
@test MOI.get(model, MOI.ObjectiveFunctionType()) ==
MOI.ScalarNonlinearFunction
# Test (; use_nlp_block = false)
seekstart(io)
model = MOF.Model(; use_nlp_block = false)
read!(io, model)
@test MOI.get(model, MOI.NLPBlock()) === nothing
@test MOI.get(model, MOI.ObjectiveFunctionType()) ==
MOI.ScalarNonlinearFunction
# Test (; use_nlp_block = true)
seekstart(io)
model = MOF.Model(; use_nlp_block = true)
read!(io, model)
block = MOI.get(model, MOI.NLPBlock())
@test block isa MOI.NLPBlockData
@test block.has_objective == true
@test MOI.get(model, MOI.ObjectiveFunctionType()) ==
MOI.ScalarAffineFunction{Float64}
return
end

end

TestMOF.runtests()
Loading