Skip to content

Commit 0c88fac

Browse files
authored
[FileFormats.MOF] fix reading nonlinear objectives only (#2068)
1 parent e8a4f57 commit 0c88fac

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/FileFormats/MOF/read.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function Base.read!(io::IO, model::Model)
3030
end
3131

3232
function _convert_to_nlpblock(model::Model)
33-
has_constraints = false
33+
needs_nlp_block = false
3434
nlp_model = MOI.Nonlinear.Model()
3535
for S in (
3636
MOI.LessThan{Float64},
@@ -44,7 +44,7 @@ function _convert_to_nlpblock(model::Model)
4444
MOI.Nonlinear.add_constraint(nlp_model, f.expr, set)
4545
# We don't need this in `model` any more.
4646
MOI.delete(model, ci)
47-
has_constraints = true
47+
needs_nlp_block = true
4848
end
4949
end
5050
if MOI.get(model, MOI.ObjectiveFunctionType()) == Nonlinear
@@ -58,8 +58,9 @@ function _convert_to_nlpblock(model::Model)
5858
0.0,
5959
),
6060
)
61+
needs_nlp_block = true
6162
end
62-
if has_constraints
63+
if needs_nlp_block
6364
options = get_options(model)
6465
evaluator = MOI.Nonlinear.Evaluator(
6566
nlp_model,

test/FileFormats/MOF/MOF.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,35 @@ function test_parse_constraintname_variable()
12011201
return
12021202
end
12031203

1204+
function test_parse_nonlinear_objective_only()
1205+
io = IOBuffer()
1206+
print(
1207+
io,
1208+
"""{
1209+
"version": {"major": 1, "minor": 2},
1210+
"variables": [{"name": "x"}],
1211+
"objective": {
1212+
"sense": "min",
1213+
"function": {
1214+
"type": "ScalarNonlinearFunction",
1215+
"root": {"type": "node", "index": 1},
1216+
"node_list": [{"type": "sin", "args": [{"type": "variable", "name": "x"}]}]
1217+
}
1218+
},
1219+
"constraints": []
1220+
}""",
1221+
)
1222+
seekstart(io)
1223+
model = MOF.Model()
1224+
read!(io, model)
1225+
block = MOI.get(model, MOI.NLPBlock())
1226+
@test block isa MOI.NLPBlockData
1227+
@test block.has_objective
1228+
MOI.initialize(block.evaluator, Symbol[])
1229+
@test MOI.eval_objective(block.evaluator, [2.0]) sin(2.0)
1230+
return
1231+
end
1232+
12041233
function runtests()
12051234
for name in names(@__MODULE__, all = true)
12061235
if startswith("$(name)", "test_")

0 commit comments

Comments
 (0)