diff --git a/src/Utilities/product_of_sets.jl b/src/Utilities/product_of_sets.jl index ae8bf3a3b1..7cebdc10b1 100644 --- a/src/Utilities/product_of_sets.jl +++ b/src/Utilities/product_of_sets.jl @@ -209,7 +209,7 @@ function rows( ) where {T,S} @assert sets.final_touch i = set_index(sets, S) - return (i == 1 ? 0 : sets.num_rows[i-1]) + ci.value + 1 + return (i == 1 ? 0 : sets.num_rows[i-1]) + ci.value end function rows( @@ -219,18 +219,18 @@ function rows( @assert sets.final_touch i = set_index(sets, S) offset = i == 1 ? 0 : sets.num_rows[i-1] - return (ci.value + offset) .+ (1:sets.dimension[(i, ci.value)]) + return (offset + ci.value - 1) .+ (1:sets.dimension[(i, ci.value)]) end function add_set(sets::OrderedProductOfSets, i) @assert !sets.final_touch sets.num_rows[i] += 1 - return sets.num_rows[i] - 1 + return sets.num_rows[i] end function add_set(sets::OrderedProductOfSets, i, dim) @assert !sets.final_touch - ci = sets.num_rows[i] + ci = sets.num_rows[i] + 1 sets.dimension[(i, ci)] = dim sets.num_rows[i] += dim return ci @@ -275,14 +275,14 @@ end Base.IteratorSize(::_UnevenIterator) = Base.SizeUnknown() function Base.iterate(it::_UnevenIterator, cur = it.start) - if cur >= it.stop + if cur > it.stop return nothing end return (cur, cur + it.dimension[(it.i, cur)]) end function Base.in(x::Int64, it::_UnevenIterator) - return it.start <= x < it.stop && haskey(it.dimension, (it.i, x)) + return it.start <= x <= it.stop && haskey(it.dimension, (it.i, x)) end function _range_iterator( @@ -292,7 +292,7 @@ function _range_iterator( stop::Int, ::Type{MOI.ScalarAffineFunction{T}}, ) where {T} - return start:(stop-1) + return start:stop end function _range_iterator( @@ -314,7 +314,7 @@ function _range_iterator( if i === nothing || F != _affine_function_type(T, S) return end - return _range_iterator(sets, i, 0, _num_rows(sets, S), F) + return _range_iterator(sets, i, 1, _num_rows(sets, S), F) end _length(::Nothing) = 0 diff --git a/src/Utilities/sparse_matrix.jl b/src/Utilities/sparse_matrix.jl index 420815950d..a4d058aff5 100644 --- a/src/Utilities/sparse_matrix.jl +++ b/src/Utilities/sparse_matrix.jl @@ -70,19 +70,19 @@ mutable struct MutableSparseMatrixCSC{Tv,Ti<:Integer,I<:AbstractIndexing} end end -function MOI.empty!(A::MutableSparseMatrixCSC) +function MOI.empty!(A::MutableSparseMatrixCSC{Tv,Ti}) where {Tv,Ti} A.m = 0 A.n = 0 resize!(A.colptr, 1) - A.colptr[1] = 0 + A.colptr[1] = zero(Ti) empty!(A.rowval) empty!(A.nzval) return end -function add_column(A::MutableSparseMatrixCSC) +function add_column(A::MutableSparseMatrixCSC{Tv,Ti}) where {Tv,Ti} A.n += 1 - push!(A.colptr, 0) + push!(A.colptr, zero(Ti)) return end diff --git a/test/Utilities/matrix_of_constraints.jl b/test/Utilities/matrix_of_constraints.jl index df704ffe11..cb83a2b063 100644 --- a/test/Utilities/matrix_of_constraints.jl +++ b/test/Utilities/matrix_of_constraints.jl @@ -40,15 +40,8 @@ function _test( bridged::Bool, Indexing, ) - optimizer = MOIU.GenericOptimizer{ - Float64, - MOIU.MatrixOfConstraints{ - Float64, - MOIU.MutableSparseMatrixCSC{Float64,Int,Indexing}, - ConstantsType, - ProductOfSetsType, - }, - }() + optimizer = + matrix_instance(Float64, ConstantsType, ProductOfSetsType, Indexing) _inner(model::MOI.Bridges.LazyBridgeOptimizer) = _inner(model.model) _inner(model::MOI.Utilities.CachingOptimizer) = _inner(model.optimizer) _inner(model::MOI.Utilities.MockOptimizer) = _inner(model.inner_model) @@ -146,17 +139,21 @@ function _lp(model, ::MOI.Test.Config{T}) where {T} return MOI.add_constraint(model, 5fx - 4fy, MOI.Interval(T(6), T(7))) end -function matrix_lp(T::Type, ProductOfSetsType::Type) - optimizer = MOIU.GenericOptimizer{ +function matrix_instance( + T::Type, + ConstantsType, + ProductOfSetsType::Type, + Indexing, +) + return MOIU.GenericOptimizer{ T, MOIU.MatrixOfConstraints{ T, MOIU.MutableSparseMatrixCSC{T,Int,Indexing}, - MOI.Utilities.Box{T}, + ConstantsType, ProductOfSetsType, }, }() - return MOI.Utilities.final_touch(optimizer) end MOIU.@mix_of_scalar_sets( @@ -345,3 +342,31 @@ MOIU.@product_of_sets(NonnegNonpos, MOI.Nonnegatives, MOI.Nonpositives) return _lin3_query(optimizer, [(F, MOI.Nonnegatives)]) end end + +function test_get_by_name(T::Type, SetsType::Type) + model = matrix_instance( + T, + MOI.Utilities.Box{T}, + SetsType, + MOI.Utilities.OneBasedIndexing, + ) + MOI.empty!(model) + x = MOI.add_variable(model) + fx = MOI.SingleVariable(x) + c = MOI.add_constraint(model, one(T) * fx, MOI.EqualTo(one(T))) + MOI.set(model, MOI.ConstraintName(), c, "c") + @test "c" == MOI.get(model, MOI.ConstraintName(), c) + @test c == MOI.get(model, MOI.ConstraintIndex, "c") + @test c == MOI.get(model, typeof(c), "c") +end +function test_get_by_name() + for T in [Int, Float64] + for SetsType in [MixLP{T}, OrdLP{T}] + test_get_by_name(T, SetsType) + end + end +end + +@testset "Get constraint by name" begin + test_get_by_name() +end diff --git a/test/Utilities/product_of_sets.jl b/test/Utilities/product_of_sets.jl index 82a288bd51..45b6319c64 100644 --- a/test/Utilities/product_of_sets.jl +++ b/test/Utilities/product_of_sets.jl @@ -262,13 +262,13 @@ function test_vector_ListOfConstraintIndices() VAF = MOI.VectorAffineFunction{Float64} @test MOI.get(sets, MOI.ListOfConstraintIndices{VAF,MOI.Zeros}()) == MOI.ConstraintIndex{VAF,MOI.Zeros}[] - for (x, S) in zip([[0], [0, 2]], MOI.Utilities.set_types(sets)[1:2]) + for (x, S) in zip([[1], [1, 3]], MOI.Utilities.set_types(sets)[1:2]) ci = MOI.get(sets, MOI.ListOfConstraintIndices{VAF,S}()) @test ci == MOI.ConstraintIndex{VAF,S}.(x) end F, S = MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64} @test MOI.get(sets, MOI.ListOfConstraintIndices{F,S}()) == - [MOI.ConstraintIndex{F,S}(0)] + [MOI.ConstraintIndex{F,S}(1)] return end @@ -288,7 +288,7 @@ function test_vector_ListOfConstraintIndices2() S = MOI.Utilities.set_types(sets)[2] VAF = MOI.VectorAffineFunction{Float64} indices = MOI.get(sets, MOI.ListOfConstraintIndices{VAF,S}()) - @test indices == MOI.ConstraintIndex{VAF,S}.([0, 2, 5, 7]) + @test indices == MOI.ConstraintIndex{VAF,S}.([1, 3, 6, 8]) end end