Skip to content

Commit 4cf625e

Browse files
authored
[FileFormats.SDPA] add coefficient_type kwarg and improve tests (#2772)
1 parent bba59d2 commit 4cf625e

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/FileFormats/SDPA/SDPA.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ struct Options end
5555
get_options(m::Model) = get(m.ext, :SDPA_OPTIONS, Options())
5656

5757
"""
58-
Model(; number_type::Type = Float64)
58+
Model(; coefficient_type::Type{T} = Float64) where {T}
5959
60-
Create an empty instance of `FileFormats.SDPA.Model{number_type}`.
60+
Create an empty instance of `FileFormats.SDPA.Model{T}`.
6161
6262
It is important to be aware that the SDPA file format is interpreted in
6363
*geometric* form and not *standard conic* form.
@@ -122,8 +122,11 @@ and affine constraints will be bridged into equality constraints
122122
by creating a slack variable by the
123123
[`MOI.Bridges.Constraint.VectorSlackBridge`](@ref).
124124
"""
125-
function Model(; number_type::Type = Float64)
126-
model = Model{number_type}()
125+
function Model(;
126+
number_type::Type{T} = Float64,
127+
coefficient_type::Type{S} = number_type,
128+
) where {T,S}
129+
model = Model{S}()
127130
model.ext[:SDPA_OPTIONS] = Options()
128131
return model
129132
end

test/FileFormats/SDPA/SDPA.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ function test_objective()
154154
model,
155155
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}}(),
156156
)
157+
model = SDPA.Model(; coefficient_type = T)
158+
@test !MOI.supports(model, MOI.ObjectiveFunction{MOI.VariableIndex}())
159+
@test !MOI.supports(
160+
model,
161+
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}}(),
162+
)
157163
end
158164
end
159165

@@ -373,6 +379,29 @@ function test_unsupported_variable_types()
373379
return
374380
end
375381

382+
function test_example_A_integer_coefficients()
383+
filename = joinpath(SDPA_MODELS_DIR, "example_A_int.dat-s")
384+
model = MOI.FileFormats.SDPA.Model(; coefficient_type = Int)
385+
MOI.read_from_file(model, filename)
386+
for xi in MOI.get(model, MOI.ListOfVariableIndices())
387+
MOI.set(model, MOI.VariableName(), xi, "v$(xi.value)")
388+
end
389+
F, S = MOI.VectorAffineFunction{Int}, MOI.PositiveSemidefiniteConeTriangle
390+
for ci in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
391+
MOI.set(model, MOI.ConstraintName(), ci, "c$(ci.value)")
392+
end
393+
input = """
394+
variables: v1, v2
395+
minobjective::Int: 10v1 + 20v2
396+
c1::Int: [v1 + -1, 0, v1 + v2 + -2] in PositiveSemidefiniteConeTriangle(2)
397+
c2::Int: [5v2 + -3, 2v2, 6v2 + -4] in PositiveSemidefiniteConeTriangle(2)
398+
"""
399+
target = MOI.Utilities.Model{Int}()
400+
MOI.Utilities.loadfromstring!(target, input)
401+
MOI.Test.util_test_models_equal(model, target, ["v1", "v2"], ["c1", "c2"])
402+
return
403+
end
404+
376405
end # module
377406

378407
TestSDPA.runtests()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"Example from http://plato.asu.edu/ftp/sdpa_format.txt
2+
"A sample problem.
3+
2 =mdim
4+
2 =nblocks
5+
2 2
6+
10 20
7+
0 1 1 1 1
8+
0 1 2 2 2
9+
0 2 1 1 3
10+
0 2 2 2 4
11+
1 1 1 1 1
12+
1 1 2 2 1
13+
2 1 2 2 1
14+
2 2 1 1 5
15+
2 2 1 2 2
16+
2 2 2 2 6

0 commit comments

Comments
 (0)