Skip to content

Commit

Permalink
Add support to MOI 0.10 (#28)
Browse files Browse the repository at this point in the history
* add support to MOI 0.10

* handle properly names in tests
  • Loading branch information
frapac authored Sep 8, 2021
1 parent 4652d04 commit 21e8c35
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
MathOptInterface = "0.9"
MathOptInterface = "0.10"
julia = "1"

[extras]
Expand Down
10 changes: 5 additions & 5 deletions src/conic_form.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ end
function MOI.set(model::GeometricConicForm, ::MOI.ObjectiveSense, sense::MOI.OptimizationSense)
model.sense = sense
end
variable_index_value(t::MOI.ScalarAffineTerm) = t.variable_index.value
variable_index_value(t::MOI.ScalarAffineTerm) = t.variable.value
variable_index_value(t::MOI.VectorAffineTerm) = variable_index_value(t.scalar_term)
function MOI.set(model::GeometricConicForm{T}, ::MOI.ObjectiveFunction,
f::MOI.ScalarAffineFunction{T}) where {T}
Expand Down Expand Up @@ -141,14 +141,14 @@ 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) where T
MOI.empty!(dest)

vis_src = MOI.get(src, MOI.ListOfVariableIndices())
idxmap = MOIU.IndexMap()

has_constraints = BitSet()
for (F, S) in MOI.get(src, MOI.ListOfConstraints())
for (F, S) in MOI.get(src, MOI.ListOfConstraintTypesPresent())
i = get(dest.cone_types_dict, S, nothing)
if i === nothing || F != MOI.VectorAffineFunction{T}
throw(MOI.UnsupportedConstraint{F, S}())
Expand All @@ -167,10 +167,10 @@ function MOI.copy_to(dest::GeometricConicForm{T}, src::MOI.ModelLike; copy_names
_load_variables(dest, length(vis_src))

# Set variable attributes
MOIU.pass_attributes(dest, src, copy_names, idxmap, vis_src)
MOIU.pass_attributes(dest, src, idxmap, vis_src)

# Set model attributes
MOIU.pass_attributes(dest, src, copy_names, idxmap)
MOIU.pass_attributes(dest, src, idxmap)

# Load constraints
offset = 0
Expand Down
16 changes: 8 additions & 8 deletions src/matrix_input.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct LPStandardForm{T, AT<:AbstractMatrix{T}, VT <: AbstractVector{T}} <: Abst
b::VT
end

function MOI.get(::LPStandardForm{T}, ::MOI.ListOfConstraints) where T
function MOI.get(::LPStandardForm{T}, ::MOI.ListOfConstraintTypesPresent) where T
return [(MOI.ScalarAffineFunction{T}, MOI.EqualTo{T}),
(MOI.VectorOfVariables, MOI.Nonnegatives)]
end
Expand Down Expand Up @@ -117,7 +117,7 @@ struct LPGeometricForm{T, AT<:AbstractMatrix{T}, VT <: AbstractVector{T}} <: Abs
b::VT
end

function MOI.get(::LPGeometricForm{T}, ::MOI.ListOfConstraints) where T
function MOI.get(::LPGeometricForm{T}, ::MOI.ListOfConstraintTypesPresent) where T
return [(MOI.ScalarAffineFunction{T}, MOI.LessThan{T})]
end
const LT{T} = MOI.ConstraintIndex{MOI.ScalarAffineFunction{T}, MOI.LessThan{T}}
Expand All @@ -139,10 +139,10 @@ end

abstract type LPMixedForm{T} <: AbstractLPForm{T} end

function MOI.get(model::LPMixedForm{T}, ::MOI.ListOfConstraints) where T
function MOI.get(model::LPMixedForm{T}, ::MOI.ListOfConstraintTypesPresent) where T
list = Tuple{DataType, DataType}[]
for S in [MOI.EqualTo{T}, MOI.Interval{T}, MOI.GreaterThan{T}, MOI.LessThan{T}]
for F in [MOI.SingleVariable, MOI.ScalarAffineFunction{T}]
for F in [MOI.VariableIndex, MOI.ScalarAffineFunction{T}]
if !iszero(MOI.get(model, MOI.NumberOfConstraints{F, S}()))
push!(list, (F, S))
end
Expand Down Expand Up @@ -176,14 +176,14 @@ function MOI.get(model::LPMixedForm{T}, ::MOI.ListOfConstraintIndices{MOI.Scalar
))
end

const VBOUND{S} = MOI.ConstraintIndex{MOI.SingleVariable, S}
function MOI.get(model::LPMixedForm{T}, ::MOI.NumberOfConstraints{MOI.SingleVariable, S}) where {T, S <: LinearBounds{T}}
const VBOUND{S} = MOI.ConstraintIndex{MOI.VariableIndex, S}
function MOI.get(model::LPMixedForm{T}, ::MOI.NumberOfConstraints{MOI.VariableIndex, S}) where {T, S <: LinearBounds{T}}
s = _sense(S)
return count(MOI.get(model, MOI.ListOfVariableIndices())) do vi
MOI.get(model, BoundSense(), vi) == s
end
end
function MOI.get(model::LPMixedForm{T}, ::MOI.ListOfConstraintIndices{MOI.SingleVariable, S}) where {T, S <: LinearBounds{T}}
function MOI.get(model::LPMixedForm{T}, ::MOI.ListOfConstraintIndices{MOI.VariableIndex, S}) where {T, S <: LinearBounds{T}}
s = _sense(S)
return collect(MOIU.LazyMap{VBOUND{S}}(
Base.Iterators.Filter(MOI.get(model, MOI.ListOfVariableIndices())) do vi
Expand All @@ -194,7 +194,7 @@ function MOI.get(model::LPMixedForm{T}, ::MOI.ListOfConstraintIndices{MOI.Single
end)
end
function MOI.get(::LPMixedForm, ::MOI.ConstraintFunction, ci::VBOUND)
return MOI.SingleVariable(MOI.VariableIndex(ci.value))
return MOI.VariableIndex(ci.value)
end
function MOI.get(model::LPMixedForm, ::MOI.ConstraintSet, ci::VBOUND)
return _bound_set(model.v_lb[ci.value], model.v_ub[ci.value])
Expand Down
4 changes: 2 additions & 2 deletions src/sparse_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ function final_touch(A::SparseMatrixCSRtoCSC)
end
function _allocate_terms(colptr, indexmap, terms)
for term in terms
colptr[indexmap[term.scalar_term.variable_index].value + 1] += 1
colptr[indexmap[term.scalar_term.variable].value + 1] += 1
end
end
function allocate_terms(A::SparseMatrixCSRtoCSC, indexmap, func)
_allocate_terms(A.colptr, indexmap, func.terms)
end
function _load_terms(colptr, rowval, nzval, indexmap, terms, offset)
for term in terms
ptr = colptr[indexmap[term.scalar_term.variable_index].value] += 1
ptr = colptr[indexmap[term.scalar_term.variable].value] += 1
rowval[ptr] = offset + term.output_index
nzval[ptr] = -term.scalar_term.coefficient
end
Expand Down
10 changes: 5 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const MatOI = MatrixOptInterface
const MOI = MatOI.MOI
const MOIU = MatOI.MOIU
const MOIB = MOI.Bridges
const MOIT = MOI.Test


const ATOL = 1e-4
Expand Down Expand Up @@ -45,11 +46,10 @@ const dense_A = [1.0 2.0
v_ub = [Inf, Inf]

function test_expected(form)
MOI.copy_to(MOI.Bridges.Constraint.Scalarize{Float64}(model), form, copy_names = false)
MOI.copy_to(MOI.Bridges.Constraint.Scalarize{Float64}(model), form)
MOI.set(model, MOI.VariableName(), MOI.VariableIndex.(1:2), var_names)
MOI.set(model, MOI.ConstraintName(), MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}}.(1:2), con_names)
MOI.set(model, MOI.ConstraintName(), MOI.ConstraintIndex{MOI.SingleVariable, MOI.GreaterThan{Float64}}.(1:2), vcon_names)
MOIU.test_models_equal(model, expected, var_names, [con_names; vcon_names])
MOIT.util_test_models_equal(model, expected, var_names, con_names)
end

@testset "change $(typeof(lp))" for lp in [
Expand Down Expand Up @@ -92,10 +92,10 @@ const dense_A = [1.0 2.0
v_ub = [Inf, Inf]

function test_expected(form)
MOI.copy_to(model, form, copy_names = false)
MOI.copy_to(model, form)
MOI.set(model, MOI.VariableName(), MOI.VariableIndex.(1:2), var_names)
MOI.set(model, MOI.ConstraintName(), MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}}.(1:2), con_names)
MOIU.test_models_equal(model, expected, var_names, con_names)
MOIT.util_test_models_equal(model, expected, var_names, con_names)
end

@testset "change $(typeof(lp))" for lp in [
Expand Down

0 comments on commit 21e8c35

Please sign in to comment.