Skip to content

Commit 9417b6e

Browse files
authored
[FileFormats] fix and test MOI.supports of MOI.ObjectiveFunction (#2814)
1 parent bb949d9 commit 9417b6e

File tree

10 files changed

+139
-12
lines changed

10 files changed

+139
-12
lines changed

src/FileFormats/CBF/CBF.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ function MOI.supports_constraint(
4747
return true
4848
end
4949

50+
MOI.supports(::Model, ::MOI.ObjectiveFunction) = false
51+
5052
function MOI.supports(
51-
::Model,
52-
::MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{Float64}},
53-
)
54-
return false
53+
::Model{T},
54+
::MOI.ObjectiveFunction{
55+
<:Union{MOI.VariableIndex,MOI.ScalarAffineFunction{T}},
56+
},
57+
) where {T}
58+
return true
5559
end
5660

5761
"""

src/FileFormats/LP/LP.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ function MOI.supports_constraint(
7272
return false
7373
end
7474

75+
MOI.supports(::Model, ::MOI.ObjectiveFunction) = false
76+
77+
function MOI.supports(
78+
::Model{T},
79+
::MOI.ObjectiveFunction{
80+
<:Union{
81+
MOI.VariableIndex,
82+
MOI.ScalarAffineFunction{T},
83+
MOI.ScalarQuadraticFunction{T},
84+
},
85+
},
86+
) where {T}
87+
return true
88+
end
89+
7590
struct Options
7691
maximum_length::Int
7792
warn::Bool

src/FileFormats/MPS/MPS.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ function MOI.supports_constraint(
9999
return false
100100
end
101101

102+
MOI.supports(::Model, ::MOI.ObjectiveFunction) = false
103+
104+
function MOI.supports(
105+
::Model{T},
106+
::MOI.ObjectiveFunction{
107+
<:Union{
108+
MOI.VariableIndex,
109+
MOI.ScalarAffineFunction{T},
110+
MOI.ScalarQuadraticFunction{T},
111+
},
112+
},
113+
) where {T}
114+
return true
115+
end
116+
102117
@enum(
103118
QuadraticFormat,
104119
kQuadraticFormatCPLEX,

src/FileFormats/SDPA/SDPA.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ function MOI.supports_constraint(
3939
return true
4040
end
4141

42-
function MOI.supports(::Model, ::MOI.ObjectiveFunction{MOI.VariableIndex})
43-
return false
44-
end
42+
MOI.supports(::Model, ::MOI.ObjectiveFunction) = false
4543

4644
function MOI.supports(
4745
::Model{T},
48-
::MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}},
46+
::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}},
4947
) where {T}
50-
return false
48+
return true
5149
end
5250

5351
struct Options end

test/FileFormats/CBF/CBF.jl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,10 +692,20 @@ function test_roundtrip_DualExponentialCone()
692692
return
693693
end
694694

695-
function test_supports_quadratic_objective()
695+
function test_unsupported_objectives()
696696
model = CBF.Model()
697-
F = MOI.ScalarQuadraticFunction{Float64}
698-
@test !MOI.supports(model, MOI.ObjectiveFunction{F}())
697+
for (F, ret) in [
698+
MOI.VariableIndex => true,
699+
MOI.ScalarAffineFunction{Float64} => true,
700+
MOI.ScalarQuadraticFunction{Float64} => false,
701+
MOI.ScalarNonlinearFunction => false,
702+
MOI.VectorOfVariables => false,
703+
MOI.VectorAffineFunction{Float64} => false,
704+
MOI.VectorQuadraticFunction{Float64} => false,
705+
MOI.VectorNonlinearFunction => false,
706+
]
707+
@test MOI.supports(model, MOI.ObjectiveFunction{F}()) == ret
708+
end
699709
return
700710
end
701711

test/FileFormats/LP/LP.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,23 @@ function test_int_round_trip()
11891189
return
11901190
end
11911191

1192+
function test_unsupported_objectives()
1193+
model = LP.Model()
1194+
for (F, ret) in [
1195+
MOI.VariableIndex => true,
1196+
MOI.ScalarAffineFunction{Float64} => true,
1197+
MOI.ScalarQuadraticFunction{Float64} => true,
1198+
MOI.ScalarNonlinearFunction => false,
1199+
MOI.VectorOfVariables => false,
1200+
MOI.VectorAffineFunction{Float64} => false,
1201+
MOI.VectorQuadraticFunction{Float64} => false,
1202+
MOI.VectorNonlinearFunction => false,
1203+
]
1204+
@test MOI.supports(model, MOI.ObjectiveFunction{F}()) == ret
1205+
end
1206+
return
1207+
end
1208+
11921209
end # module
11931210

11941211
TestLP.runtests()

test/FileFormats/MOF/MOF.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,23 @@ function test_AAA_int()
16101610
return
16111611
end
16121612

1613+
function test_unsupported_objectives()
1614+
model = MOF.Model()
1615+
for (F, ret) in [
1616+
MOI.VariableIndex => true,
1617+
MOI.ScalarAffineFunction{Float64} => true,
1618+
MOI.ScalarQuadraticFunction{Float64} => true,
1619+
MOI.ScalarNonlinearFunction => true,
1620+
MOI.VectorOfVariables => true,
1621+
MOI.VectorAffineFunction{Float64} => true,
1622+
MOI.VectorQuadraticFunction{Float64} => true,
1623+
MOI.VectorNonlinearFunction => true,
1624+
]
1625+
@test MOI.supports(model, MOI.ObjectiveFunction{F}()) == ret
1626+
end
1627+
return
1628+
end
1629+
16131630
end
16141631

16151632
TestMOF.runtests()

test/FileFormats/MPS/MPS.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,23 @@ function test_issue_2797_tab()
17131713
return
17141714
end
17151715

1716+
function test_unsupported_objectives()
1717+
model = MPS.Model()
1718+
for (F, ret) in [
1719+
MOI.VariableIndex => true,
1720+
MOI.ScalarAffineFunction{Float64} => true,
1721+
MOI.ScalarQuadraticFunction{Float64} => true,
1722+
MOI.ScalarNonlinearFunction => false,
1723+
MOI.VectorOfVariables => false,
1724+
MOI.VectorAffineFunction{Float64} => false,
1725+
MOI.VectorQuadraticFunction{Float64} => false,
1726+
MOI.VectorNonlinearFunction => false,
1727+
]
1728+
@test MOI.supports(model, MOI.ObjectiveFunction{F}()) == ret
1729+
end
1730+
return
1731+
end
1732+
17161733
end # TestMPS
17171734

17181735
TestMPS.runtests()

test/FileFormats/NL/NL.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,23 @@ function test_unsupported_variable_types()
13771377
return
13781378
end
13791379

1380+
function test_unsupported_objectives()
1381+
model = NL.Model()
1382+
for (F, ret) in [
1383+
MOI.VariableIndex => true,
1384+
MOI.ScalarAffineFunction{Float64} => true,
1385+
MOI.ScalarQuadraticFunction{Float64} => true,
1386+
MOI.ScalarNonlinearFunction => true,
1387+
MOI.VectorOfVariables => false,
1388+
MOI.VectorAffineFunction{Float64} => false,
1389+
MOI.VectorQuadraticFunction{Float64} => false,
1390+
MOI.VectorNonlinearFunction => false,
1391+
]
1392+
@test MOI.supports(model, MOI.ObjectiveFunction{F}()) == ret
1393+
end
1394+
return
1395+
end
1396+
13801397
end
13811398

13821399
TestNLModel.runtests()

test/FileFormats/SDPA/SDPA.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,23 @@ function test_example_A_integer_coefficients()
402402
return
403403
end
404404

405+
function test_unsupported_objectives()
406+
model = SDPA.Model()
407+
for (F, ret) in [
408+
MOI.VariableIndex => false,
409+
MOI.ScalarAffineFunction{Float64} => true,
410+
MOI.ScalarQuadraticFunction{Float64} => false,
411+
MOI.ScalarNonlinearFunction => false,
412+
MOI.VectorOfVariables => false,
413+
MOI.VectorAffineFunction{Float64} => false,
414+
MOI.VectorQuadraticFunction{Float64} => false,
415+
MOI.VectorNonlinearFunction => false,
416+
]
417+
@test MOI.supports(model, MOI.ObjectiveFunction{F}()) == ret
418+
end
419+
return
420+
end
421+
405422
end # module
406423

407424
TestSDPA.runtests()

0 commit comments

Comments
 (0)