Skip to content

Define output_dimension #392

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 4 commits into from
Jun 15, 2018
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
6 changes: 6 additions & 0 deletions docs/src/apireference.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ VectorQuadraticTerm
VectorQuadraticFunction
```

Functions for getting and setting properties of sets.

```@docs
output_dimension
```

List of function modifications.
```@docs
ScalarConstantChange
Expand Down
4 changes: 2 additions & 2 deletions src/Bridges/soctopsdbridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Builds a VectorAffineFunction representing the upper (or lower) triangular part
[ f[2:end] g * I ]
"""
function _SOCtoPSDCaff(f::MOI.VectorAffineFunction{T}, g::MOI.ScalarAffineFunction{T}) where T
dim = MOIU.moilength(f)
dim = MOI.output_dimension(f)
n = div(dim * (dim+1), 2)
# Needs to add t*I
N0 = length(f.terms)
Expand Down Expand Up @@ -125,7 +125,7 @@ end

_RSOCtoPSDCaff(f::MOI.VectorOfVariables, ::Type{T}) where T = _RSOCtoPSDCaff(MOI.VectorAffineFunction{T}(f), T)
function _RSOCtoPSDCaff(f::MOI.VectorAffineFunction, ::Type)
n = MOIU.moilength(f)
n = MOI.output_dimension(f)
g = mapcoefficient(c -> 2c, MOIU.eachscalar(f)[2])
_SOCtoPSDCaff(MOIU.eachscalar(f)[[1; 3:n]], g)
end
Expand Down
10 changes: 4 additions & 6 deletions src/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ mapvariables(varmap::Function, change::MOI.MultirowChange) = MOI.MultirowChange(
mapvariables(varmap, f::MOI.AbstractFunctionModification) = mapvariables(vi -> varmap[vi], f)

# Cat for MOI sets
moilength(f::Union{MOI.ScalarAffineFunction, MOI.ScalarQuadraticFunction}) = 1
moilength(f::Union{MOI.VectorAffineFunction, MOI.VectorQuadraticFunction}) = length(f.constants)
_constant(f::Union{MOI.ScalarAffineFunction, MOI.ScalarQuadraticFunction}) = f.constant
_constant(f::Union{MOI.VectorAffineFunction, MOI.VectorQuadraticFunction}) = f.constants
constant(f::Union{MOI.ScalarAffineFunction, MOI.ScalarQuadraticFunction}) = [f.constant]
Expand All @@ -66,7 +64,7 @@ offsetterm(t::MOI.VectorAffineTerm, offset::Int) = MOI.VectorAffineTerm(offset+t
offsetterms(f::Union{MOI.ScalarAffineFunction, MOI.VectorAffineFunction}, offset::Int) = offsetterm.(f.terms, offset)
function moivcat(f::Union{MOI.ScalarAffineFunction, MOI.VectorAffineFunction}...)
n = length(f)
offsets = cumsum(collect(moilength.(f)))
offsets = cumsum(collect(MOI.output_dimension.(f)))
offsets = [0; offsets[1:(n-1)]]
terms = vcat((offsetterms.(f, offsets))...)
cst = vcat(constant.(f)...)
Expand All @@ -91,7 +89,7 @@ Base.start(it::ScalarFunctionIterator) = 1
Base.done(it::ScalarFunctionIterator, state) = state > length(it)
Base.next(it::ScalarFunctionIterator, state) = (it[state], state+1)
Base.length(it::ScalarFunctionIterator{MOI.VectorOfVariables}) = length(it.f.variables)
Base.length(it::ScalarFunctionIterator{<:Union{MOI.VectorAffineFunction, MOI.VectorQuadraticFunction}}) = moilength(it.f)
Base.length(it::ScalarFunctionIterator{<:Union{MOI.VectorAffineFunction, MOI.VectorQuadraticFunction}}) = MOI.output_dimension(it.f)
Base.eltype(it::ScalarFunctionIterator{MOI.VectorOfVariables}) = MOI.SingleVariable
Base.eltype(it::ScalarFunctionIterator{MOI.VectorAffineFunction{T}}) where T = MOI.ScalarAffineFunction{T}
Base.eltype(it::ScalarFunctionIterator{MOI.VectorQuadraticFunction{T}}) where T = MOI.ScalarQuadraticFunction{T}
Expand Down Expand Up @@ -362,9 +360,9 @@ function _modifycoefficients(n, terms::Vector{<:MOI.VectorAffineTerm}, variable:
terms
end
function modifyfunction(f::MOI.VectorAffineFunction, change::MOI.MultirowChange)
MOI.VectorAffineFunction(_modifycoefficients(moilength(f), f.terms, change.variable, change.new_coefficients), f.constants)
MOI.VectorAffineFunction(_modifycoefficients(MOI.output_dimension(f), f.terms, change.variable, change.new_coefficients), f.constants)
end
function modifyfunction(f::MOI.VectorQuadraticFunction, change::MOI.MultirowChange)
MOI.VectorQuadraticFunction(_modifycoefficients(moilength(f), f.affine_terms, change.variable, change.new_coefficients),
MOI.VectorQuadraticFunction(_modifycoefficients(MOI.output_dimension(f), f.affine_terms, change.variable, change.new_coefficients),
f.quadratic_terms, f.constants)
end
12 changes: 12 additions & 0 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ Abstract supertype for function objects.
"""
abstract type AbstractFunction end

"""
output_dimension(f::AbstractFunction)

Return 1 if `f` has a scalar output and the number of output components if `f`
has a vector output.
"""
function output_dimension end

"""
AbstractScalarFunction

Abstract supertype for scalar-valued function objects.
"""
abstract type AbstractScalarFunction <: AbstractFunction end
output_dimension(::AbstractScalarFunction) = 1

"""
AbstractVectorFunction
Expand Down Expand Up @@ -41,6 +50,7 @@ This function is naturally be used for constraints that apply to groups of varia
struct VectorOfVariables <: AbstractVectorFunction
variables::Vector{VariableIndex}
end
output_dimension(f::VectorOfVariables) = length(f.variables)

"""
struct ScalarAffineTerm{T}
Expand Down Expand Up @@ -110,6 +120,7 @@ struct VectorAffineFunction{T} <: AbstractVectorFunction
terms::Vector{VectorAffineTerm{T}}
constants::Vector{T}
end
output_dimension(f::VectorAffineFunction) = length(f.constants)

"""
struct ScalarQuadraticTerm{T}
Expand Down Expand Up @@ -193,6 +204,7 @@ struct VectorQuadraticFunction{T} <: AbstractVectorFunction
quadratic_terms::Vector{VectorQuadraticTerm{T}}
constants::Vector{T}
end
output_dimension(f::VectorQuadraticFunction) = length(f.constants)

# Function modifications

Expand Down
2 changes: 1 addition & 1 deletion src/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract type AbstractSet end
"""
dimension(s::AbstractSet)

Return the output dimension that an [`AbstractFunction`](@ref) should have to be used with the set `s`.
Return the [`output_dimension`](@ref) that an [`AbstractFunction`](@ref) should have to be used with the set `s`.

### Examples

Expand Down
10 changes: 10 additions & 0 deletions test/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,29 @@
# We do tests twice to make sure the function is not modified
vals = Dict(w=>0, x=>3, y=>1, z=>5)
fsv = MOI.SingleVariable(z)
@test MOI.output_dimension(fsv) == 1
@test MOIU.evalvariables(vi -> vals[vi], fsv) ≈ 5
@test MOIU.evalvariables(vi -> vals[vi], fsv) ≈ 5
fvv = MOI.VectorOfVariables([x, z, y])
@test MOI.output_dimension(fvv) == 3
@test MOIU.evalvariables(vi -> vals[vi], fvv) ≈ [3, 5, 1]
@test MOIU.evalvariables(vi -> vals[vi], fvv) ≈ [3, 5, 1]
fsa = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x), MOI.ScalarAffineTerm(3.0, z), MOI.ScalarAffineTerm(2.0, y)], 2.0)
@test MOI.output_dimension(fsa) == 1
@test MOIU.evalvariables(vi -> vals[vi], fsa) ≈ 22
@test MOIU.evalvariables(vi -> vals[vi], fsa) ≈ 22
fva = MOI.VectorAffineFunction(MOI.VectorAffineTerm.([2, 1, 2], MOI.ScalarAffineTerm.([1.0, 3.0, 2.0], [x, z, y])), [-3.0, 2.0])
@test MOI.output_dimension(fva) == 2
@test MOIU.evalvariables(vi -> vals[vi], fva) ≈ [12, 7]
@test MOIU.evalvariables(vi -> vals[vi], fva) ≈ [12, 7]
fsq = MOI.ScalarQuadraticFunction(MOI.ScalarAffineTerm.(1.0, [x, y]),
MOI.ScalarQuadraticTerm.(1.0, [x, w, w], [z, z, y]), -3.0)
@test MOI.output_dimension(fsq) == 1
@test MOIU.evalvariables(vi -> vals[vi], fsq) ≈ 16
@test MOIU.evalvariables(vi -> vals[vi], fsq) ≈ 16
fvq = MOI.VectorQuadraticFunction(MOI.VectorAffineTerm.([2, 1], MOI.ScalarAffineTerm.(1.0, [x, y])),
MOI.VectorQuadraticTerm.([1, 2, 2], MOI.ScalarQuadraticTerm.(1.0, [x, w, w], [z, z, y])), [-3.0, -2.0])
@test MOI.output_dimension(fvq) == 2
@test MOIU.evalvariables(vi -> vals[vi], fvq) ≈ [13, 1]
@test MOIU.evalvariables(vi -> vals[vi], fvq) ≈ [13, 1]
end
Expand Down Expand Up @@ -127,6 +133,7 @@
@test MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1, 1], [x, z]), 1) ≈ MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1, 1e-7, 1], [x, y, z]), 1.0) atol=1e-6
@test MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x), MOI.ScalarAffineTerm(1e-7, y)], 1.0) ≈ MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1, x)], 1) atol=1e-6
f = MOIU.canonical(MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([2, 1, 3, -2, -3], [y, x, z, x, z]), 5))
@test MOI.output_dimension(f) == 1
@test f.terms == MOI.ScalarAffineTerm.([-1, 2], [x, y])
@test f.constant == 5
f = MOIU.canonical(MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1, 3, 1, 2, -3, 2, -1, -2, -2, 3, 2],
Expand All @@ -146,6 +153,7 @@
end
@testset "Quadratic" begin
f = MOI.ScalarQuadraticFunction(MOI.ScalarAffineTerm.([3], [x]), MOI.ScalarQuadraticTerm.([1, 2, 3], [x, y, x], [x, y, y]), 7)
@test MOI.output_dimension(f) == 1
f = MOIU.modifyfunction(f, MOI.ScalarConstantChange(9))
@test f.constant == 9
f = MOIU.modifyfunction(f, MOI.ScalarCoefficientChange(y, 0))
Expand All @@ -163,6 +171,7 @@
f = MOIU.canonical(MOI.VectorAffineFunction(MOI.VectorAffineTerm.([2, 1, 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 1, 2],
MOI.ScalarAffineTerm.([3, 2, 3, -3, -1, -2, 3, -2, 1, 3, 5, -2, 0, -1],
[x, x, z, y, y, x, y, z, x, y, y, x, x, z])), [5, 7]))
@test MOI.output_dimension(f) == 2
@test f.terms == MOI.VectorAffineTerm.([1, 1, 2], MOI.ScalarAffineTerm.([2, 4, 3], [x, y, y]))
@test MOIU.constant(f) == [5, 7]
f = MOIU.modifyfunction(f, MOI.VectorConstantChange([6, 8]))
Expand All @@ -178,6 +187,7 @@
end
@testset "Quadratic" begin
f = MOI.VectorQuadraticFunction(MOI.VectorAffineTerm.([1, 2, 2], MOI.ScalarAffineTerm.([3, 1, 2], [x, x, y])), MOI.VectorQuadraticTerm.([1, 1, 2], MOI.ScalarQuadraticTerm.([1, 2, 3], [x, y, x], [x, y, y])), [7, 3, 4])
@test MOI.output_dimension(f) == 3
f = MOIU.modifyfunction(f, MOI.VectorConstantChange([10, 11, 12]))
@test MOIU.constant(f) == [10, 11, 12]
f = MOIU.modifyfunction(f, MOI.MultirowChange(y, [(2, 0), (1, 1)]))
Expand Down