Skip to content

Commit

Permalink
[FileFormats.NL] fix reading file with no objective
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Jan 4, 2024
1 parent 9ae1a89 commit 82e706d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/FileFormats/NL/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,14 @@ function _to_model(data::_CacheModel; use_nlp_block::Bool)
end
end
MOI.set.(model, MOI.VariablePrimalStart(), x, data.variable_primal)
MOI.set(model, MOI.ObjectiveSense(), data.sense)
if data.objective != :()
MOI.set(model, MOI.ObjectiveSense(), data.sense)

Check warning on line 186 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L185-L186

Added lines #L185 - L186 were not covered by tests
end
if use_nlp_block
nlp = MOI.Nonlinear.Model()
MOI.Nonlinear.set_objective(nlp, data.objective)
if data.objective != :()
MOI.Nonlinear.set_objective(nlp, data.objective)

Check warning on line 191 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L190-L191

Added lines #L190 - L191 were not covered by tests
end
for (i, expr) in enumerate(data.constraints)
lb, ub = data.constraint_lower[i], data.constraint_upper[i]
if lb == ub
Expand All @@ -204,8 +208,10 @@ function _to_model(data::_CacheModel; use_nlp_block::Bool)
block = MOI.NLPBlockData(evaluator)
MOI.set(model, MOI.NLPBlock(), block)
else
obj = _to_scalar_nonlinear_function(data.objective)
MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)
if data.objective != :()
obj = _to_scalar_nonlinear_function(data.objective)
MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)

Check warning on line 213 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L211-L213

Added lines #L211 - L213 were not covered by tests
end
for (i, expr) in enumerate(data.constraints)
lb, ub = data.constraint_lower[i], data.constraint_upper[i]
f = _to_scalar_nonlinear_function(expr)::MOI.ScalarNonlinearFunction
Expand Down
60 changes: 60 additions & 0 deletions test/FileFormats/NL/data/hs071_no_objective.nl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
g3 1 1 0
4 2 0 0 1 0
2 0
0 0
4 0 0
0 0 0 1
0 0 0 2 0
8 0
0 0
0 0 0 0 0
C0
o2
v0
o2
v2
o2
v3
v1
C1
o54
4
o5
v0
n2
o5
v2
n2
o5
v3
n2
o5
v1
n2
x4
0 2.1
1 2.4
2 2.2
3 2.3
r
2 25
4 40
b
0 1.1 5.1
0 1.4 5.4
0 1.2 1
0 1.3 5.3
k3
2
4
6
J0 4
0 0
1 0
2 0
3 0
J1 4
0 0
1 0
2 0
3 0
10 changes: 10 additions & 0 deletions test/FileFormats/NL/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,16 @@ function test_parse_header_integrality_both_int()
return
end

function test_no_objective()
model = NL.Model()
open(joinpath(@__DIR__, "data", "hs071_no_objective.nl"), "r") do io
return read!(io, model)
end
@test MOI.get(model, MOI.NumberOfVariables()) == 4
@test MOI.get(model, MOi.ObjectiveSense()) == MOI.FEASIBILITY_SENSE
return
end

"""
test_mac_minlp()
Expand Down

0 comments on commit 82e706d

Please sign in to comment.