Skip to content

Commit c6ca08d

Browse files
authored
[FileFormats.NL] fix reading file with no objective (#2391)
1 parent 9ae1a89 commit c6ca08d

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

src/FileFormats/NL/read.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,14 @@ function _to_model(data::_CacheModel; use_nlp_block::Bool)
182182
end
183183
end
184184
MOI.set.(model, MOI.VariablePrimalStart(), x, data.variable_primal)
185-
MOI.set(model, MOI.ObjectiveSense(), data.sense)
185+
if data.objective != :()
186+
MOI.set(model, MOI.ObjectiveSense(), data.sense)
187+
end
186188
if use_nlp_block
187189
nlp = MOI.Nonlinear.Model()
188-
MOI.Nonlinear.set_objective(nlp, data.objective)
190+
if data.objective != :()
191+
MOI.Nonlinear.set_objective(nlp, data.objective)
192+
end
189193
for (i, expr) in enumerate(data.constraints)
190194
lb, ub = data.constraint_lower[i], data.constraint_upper[i]
191195
if lb == ub
@@ -204,8 +208,10 @@ function _to_model(data::_CacheModel; use_nlp_block::Bool)
204208
block = MOI.NLPBlockData(evaluator)
205209
MOI.set(model, MOI.NLPBlock(), block)
206210
else
207-
obj = _to_scalar_nonlinear_function(data.objective)
208-
MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)
211+
if data.objective != :()
212+
obj = _to_scalar_nonlinear_function(data.objective)
213+
MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)
214+
end
209215
for (i, expr) in enumerate(data.constraints)
210216
lb, ub = data.constraint_lower[i], data.constraint_upper[i]
211217
f = _to_scalar_nonlinear_function(expr)::MOI.ScalarNonlinearFunction
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
g3 1 1 0
2+
4 2 0 0 1 0
3+
2 0
4+
0 0
5+
4 0 0
6+
0 0 0 1
7+
0 0 0 2 0
8+
8 0
9+
0 0
10+
0 0 0 0 0
11+
C0
12+
o2
13+
v0
14+
o2
15+
v2
16+
o2
17+
v3
18+
v1
19+
C1
20+
o54
21+
4
22+
o5
23+
v0
24+
n2
25+
o5
26+
v2
27+
n2
28+
o5
29+
v3
30+
n2
31+
o5
32+
v1
33+
n2
34+
x4
35+
0 2.1
36+
1 2.4
37+
2 2.2
38+
3 2.3
39+
r
40+
2 25
41+
4 40
42+
b
43+
0 1.1 5.1
44+
0 1.4 5.4
45+
0 1.2 1
46+
0 1.3 5.3
47+
k3
48+
2
49+
4
50+
6
51+
J0 4
52+
0 0
53+
1 0
54+
2 0
55+
3 0
56+
J1 4
57+
0 0
58+
1 0
59+
2 0
60+
3 0

test/FileFormats/NL/read.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,16 @@ function test_parse_header_integrality_both_int()
638638
return
639639
end
640640

641+
function test_no_objective()
642+
model = NL.Model()
643+
open(joinpath(@__DIR__, "data", "hs071_no_objective.nl"), "r") do io
644+
return read!(io, model)
645+
end
646+
@test MOI.get(model, MOI.NumberOfVariables()) == 4
647+
@test MOI.get(model, MOI.ObjectiveSense()) == MOI.FEASIBILITY_SENSE
648+
return
649+
end
650+
641651
"""
642652
test_mac_minlp()
643653

0 commit comments

Comments
 (0)