Skip to content

Feature to read/write integer constraints #1079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/FileFormats/SDPA/SDPA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function MOI.supports_constraint(

return false
end
function MOI.supports_constraint(
::Model, ::Type{MOI.SingleVariable}, ::Type{MOI.Integer})
return true
end

function MOI.supports(
::Model,
Expand Down Expand Up @@ -202,6 +206,16 @@ function Base.write(io::IO, model::Model{T}) where {T}
for i in eachindex(psd)
_print_constraint(length(nonneg) + i, true, psd[i])
end

# Integrality constraints.
# Based on the extension: http://www.opt.tu-darmstadt.de/scipsdp/downloads/data_format.txt
integer_cons = model_cons(MOI.SingleVariable, MOI.Integer)
if length(integer_cons) > 0
println(io, "*INTEGER")
for con_idx in integer_cons
println(io, "*$(con_function(con_idx).variable.value)")
end
end
return
end

Expand Down Expand Up @@ -242,6 +256,9 @@ function Base.read!(io::IO, model::Model{T}) where T
end
end
objective_read = false
integer_read = false
scalar_vars = nothing
intvar_idx = Int[]
c = nothing
funcs = nothing
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
Expand All @@ -251,14 +268,28 @@ function Base.read!(io::IO, model::Model{T}) where T
if startswith(line, '"')
continue
end
# The lines starting with * should also be skipped
# according to http://plato.asu.edu/ftp/sdpa_format.txt.
if startswith(line, '*')
# Exceptions for integer variables
if startswith(line, "*INTEGER")
integer_read = true
elseif integer_read
if !num_variables_read
error("The number of variables should be given before *INTEGER section.")
end
push!(intvar_idx, parse(Int, strip(line[2:end])))
end
continue
end
if !num_variables_read
if isempty(line)
continue
end
num_variables_read = true
# According to http://plato.asu.edu/ftp/sdpa_format.txt,
# additional text after the number of variables should be ignored.
MOI.add_variables(model, parse(Int, split(line)[1]))
scalar_vars = MOI.add_variables(model, parse(Int, split(line)[1]))
elseif num_blocks === nothing
if isempty(line)
continue
Expand Down Expand Up @@ -288,7 +319,7 @@ function Base.read!(io::IO, model::Model{T}) where T
obj = zero(MOI.ScalarAffineFunction{T})
for i in eachindex(c)
if !iszero(c[i])
push!(obj.terms, MOI.ScalarAffineTerm(c[i], MOI.VariableIndex(i)))
push!(obj.terms, MOI.ScalarAffineTerm(c[i], scalar_vars[i]))
end
end
MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)
Expand Down Expand Up @@ -320,14 +351,17 @@ function Base.read!(io::IO, model::Model{T}) where T
else
if !iszero(coef)
push!(funcs[block].terms, MOI.VectorAffineTerm(k,
MOI.ScalarAffineTerm(coef, MOI.VariableIndex(matrix))))
MOI.ScalarAffineTerm(coef, scalar_vars[matrix])))
end
end
end
end
for block in 1:num_blocks
MOI.add_constraint(model, funcs[block], block_sets[block])
end
for var_idx in intvar_idx
MOI.add_constraint(model, MOI.SingleVariable(scalar_vars[var_idx]), MOI.Integer())
end
return
end

Expand Down
12 changes: 11 additions & 1 deletion test/FileFormats/SDPA/SDPA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function set_var_and_con_names(model::MOI.ModelLike)
idx = 0
constraint_names = String[]
for i in Iterators.flatten((
MOI.get(model, MOI.ListOfConstraintIndices{MOI.SingleVariable, MOI.Integer}()),
MOI.get(model, MOI.ListOfConstraintIndices{MOI.VectorAffineFunction{Float64}, MOI.Nonnegatives}()),
MOI.get(model, MOI.ListOfConstraintIndices{MOI.VectorAffineFunction{Float64}, MOI.PositiveSemidefiniteConeTriangle}())))
idx += 1
Expand Down Expand Up @@ -70,7 +71,6 @@ end
MOI.Interval(1.0, 2.0),
MOI.Semiinteger(1.0, 2.0),
MOI.Semicontinuous(1.0, 2.0),
MOI.Integer(),
MOI.ZeroOne()
]
model_string = """
Expand Down Expand Up @@ -201,6 +201,16 @@ example_models = [
minobjective: 1x
c1: [0, 1x + -1, 0] in PositiveSemidefiniteConeTriangle(2)
"""),
("example_integer.sdpa", """
variables: x, y, z
minobjective: 1x + -2y + -1z
c1: [1x, 1y, 1z] in PositiveSemidefiniteConeTriangle(2)
c2: [1z, 1x, 2.1] in PositiveSemidefiniteConeTriangle(2)
c3: [1x + 1y + 1z + -1, -1x + -1y + -1z + 8] in Nonnegatives(2)
c4: x in Integer()
c5: y in Integer()
c6: z in Integer()
"""),
]
@testset "Read and write/read $model_name" for (model_name, model_string) in example_models
test_read(joinpath(SDPA_MODELS_DIR, model_name), model_string)
Expand Down
25 changes: 25 additions & 0 deletions test/FileFormats/SDPA/models/example_integer.sdpa
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
* Example from http://www.opt.tu-darmstadt.de/scipsdp/downloads/data_format.txt
3
3
2 2 -2
* the next line gives the objective values in the order of the variables
1 -2 -1
* the remaining lines give the nonzeroes of the constraints with variable (0 meaning the constant part) block row column value
1 1 1 1 1
2 1 1 2 1
3 1 2 2 1
1 2 1 2 1
3 2 1 1 1
0 2 2 2 -2.1
1 3 1 1 1
2 3 1 1 1
3 3 1 1 1
0 3 1 1 1
1 3 2 2 -1
2 3 2 2 -1
3 3 2 2 -1
0 3 2 2 -8
*INTEGER
*1
*2
*3