Skip to content

add quadratic term in objective of conic term #27

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 26 additions & 6 deletions src/conic_form.jl
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@

"""
GeometricConicForm{T, AT, VT, C} <: MOI.ModelLike
GeometricConicForm{T, AT, VB, VC, QT, C} <: MOI.ModelLike

Represents an optimization model of the form:
```
sense ⟨c, x⟩ + c0
sense ⟨c, x⟩ + ⟨Qx, x⟩ + c0
s.t. b_i - A_i x ∈ C_i ∀ i
```
with each `C_i` a cone defined in MOI.
"""
mutable struct GeometricConicForm{T, AT, VB, VC, C} <: MOI.ModelLike
mutable struct GeometricConicForm{T, AT, VB, VC, QT, C} <: MOI.ModelLike
num_rows::Vector{Int}
dimension::Dict{Int, Int}
sense::MOI.OptimizationSense
objective_constant::T # The objective
A::Union{Nothing, AT} # The constraints
b::VB # `b - Ax in cones`
c::VC # `sense c'x + objective_constant`
Q::Union{QT, Nothing}
cone_types::C
cone_types_dict::Dict{DataType, Int}

function GeometricConicForm{T, AT, VB, VC}(cone_types) where {T, AT, VB, VC}
model = new{T, AT, VB, VC, typeof(cone_types)}()
function GeometricConicForm{T, AT, VB, VC, QT}(cone_types) where {T, AT, VB, VC, QT}
model = new{T, AT, VB, VC, QT, typeof(cone_types)}()
model.cone_types = cone_types
model.cone_types_dict = Dict{DataType, Int}(
s => i for (i, s) in enumerate(cone_types)
)
model.num_rows = zeros(Int, length(cone_types))
model.dimension = Dict{Int, Int}()
model.A = nothing
model.Q = nothing
return model
end
end

function GeometricConicForm{T, AT, VB, VC}(cone_types) where {T, AT, VB, VC}
return GeometricConicForm{T, AT, VB, VC, SparseMatrixCSC{T,Int}}(cone_types)
end

function GeometricConicForm{T, AT, VT}(cone_types) where {T, AT, VT}
return GeometricConicForm{T, AT, VT, VT}(cone_types)
end
Expand Down Expand Up @@ -105,6 +111,20 @@ function MOI.set(model::GeometricConicForm{T}, ::MOI.ObjectiveFunction,
return nothing
end

function MOI.set(model::GeometricConicForm{T, AT, VB, VC, QT}, ::MOI.ObjectiveFunction,
f::MOI.ScalarQuadraticFunction{T}) where {T, AT, VB, VC, QT}
c = Vector(sparsevec(variable_index_value.(f.affine_terms), MOI.coefficient.(f.affine_terms),
model.A.n))
model.objective_constant = f.constant
model.c = c
n = length(c)
Q = convert(QT, spzeros(T, n, n))
for term in f.quadratic_terms
Q[term.variable_index_1.value, term.variable_index_2.value] = term.coefficient
end
return nothing
end

function _allocate_constraint(model::GeometricConicForm, src, indexmap, cone_id, ci)
# TODO use `CanonicalConstraintFunction`
func = MOI.get(src, MOI.ConstraintFunction(), ci)
Expand Down Expand Up @@ -141,7 +161,7 @@ function _load_constraints(model::GeometricConicForm, src, indexmap, cone_offset
end
end

function MOI.copy_to(dest::GeometricConicForm{T}, src::MOI.ModelLike; copy_names::Bool=true) where T
function MOI.copy_to(dest::GeometricConicForm{T}, src::MOI.ModelLike; copy_names::Bool=true) where {T}
MOI.empty!(dest)

vis_src = MOI.get(src, MOI.ListOfVariableIndices())
Expand Down
18 changes: 17 additions & 1 deletion test/conic_form.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ function psd1(::Type{T}, ::Type{I}) where {T, I}
T[-1, -1, -1, -1, -2, -1, -1, -1, -1, -2, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1],
)
)
@test conic_form.Q === nothing

conic_form2 = MatOI.GeometricConicForm{T, MatOI.SparseMatrixCSRtoCSC{T, Int, I}, Vector{T}, Vector{T}, SparseMatrixCSC{T, Int}}([MOI.PositiveSemidefiniteConeTriangle, MOI.SecondOrderCone, MOI.Zeros])

MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}}(),
MOI.ScalarQuadraticFunction(
MOI.ScalarAffineTerm.([objXcoefs; one(T)], [X[objXidx]; x[1]]),
[MOI.ScalarQuadraticTerm(0.5, x[1], x[1])],
zero(T),
)
)
index_map = MOI.copy_to(conic_form2, model)
@test conic_form.Q !== nothing
@test sum(conic_form.Q) ≈ 0.5
end

# Taken from `MOI.Test.psdt2test`.
Expand Down Expand Up @@ -147,7 +163,7 @@ function psd2(::Type{T}, ::Type{I}, η::T = T(10), α::T = T(4)/T(5), δ::T = T(
)
end

@testset "PSD $T, $I" for T in [Float64, BigFloat], I in [MatOI.ZeroBasedIndexing, MatOI.OneBasedIndexing]
@testset "PSD $T, $I" for T in (Float64, BigFloat), I in (MatOI.ZeroBasedIndexing, MatOI.OneBasedIndexing)
psd1(T, I)
psd2(T, I)
end