Skip to content

Commit

Permalink
Add isconstant field to Constraint; use to skip updating constant con…
Browse files Browse the repository at this point in the history
…straints.
  • Loading branch information
tkoolen committed Dec 5, 2018
1 parent 2518951 commit f88f384
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
36 changes: 18 additions & 18 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,9 @@ $(METHODLIST)
function scale! end

function scale!(
dest::AbstractVector{<:LinearTerm},
x::Number,
y::AbstractVector{Variable})
dest::AbstractVector{<:LinearTerm},
x::Number,
y::AbstractVector{Variable})
@boundscheck axes(dest) == axes(y) || throw(DimensionMismatch())
@inbounds for i in eachindex(dest)
dest[i] = x * y[i]
Expand All @@ -844,9 +844,9 @@ function scale!(
end

function scale!(
dest::AbstractVector{<:LinearTerm},
x::AbstractVector{Variable},
y::Number)
dest::AbstractVector{<:LinearTerm},
x::AbstractVector{Variable},
y::Number)
@boundscheck axes(dest) == axes(x) || throw(DimensionMismatch())
@inbounds for i in eachindex(dest)
dest[i] = x[i] * y
Expand All @@ -855,9 +855,9 @@ function scale!(
end

function scale!(
dest::AbstractVector{<:AffineFunction},
x::Number,
y::AbstractVector{<:AffineFunction})
dest::AbstractVector{<:AffineFunction},
x::Number,
y::AbstractVector{<:AffineFunction})
@boundscheck axes(dest) == axes(y) || throw(DimensionMismatch())
@inbounds for i in eachindex(dest)
mul!(dest[i], x, y[i])
Expand All @@ -866,9 +866,9 @@ function scale!(
end

function scale!(
dest::AbstractVector{<:AffineFunction},
x::AbstractVector{<:AffineFunction},
y::Number)
dest::AbstractVector{<:AffineFunction},
x::AbstractVector{<:AffineFunction},
y::Number)
@boundscheck axes(dest) == axes(x) || throw(DimensionMismatch())
@inbounds for i in eachindex(dest)
mul!(dest[i], x[i], y)
Expand Down Expand Up @@ -928,16 +928,16 @@ $(METHODLIST)
"""
function vcat! end

function _vcat!(dest::AbstractVector{<:AffineFunction},
i::Integer)
function _vcat!(dest::AbstractVector{<:AffineFunction}, i::Integer)
@boundscheck i == lastindex(dest) + 1 || throw(DimensionMismatch())
dest
end

function _vcat!(dest::AbstractVector{<:AffineFunction},
i::Integer,
source::AbstractVector{<:AffineFunction},
remaining::Vararg{<:AbstractVector{<:AffineFunction}, N}) where {N}
function _vcat!(
dest::AbstractVector{<:AffineFunction},
i::Integer,
source::AbstractVector{<:AffineFunction},
remaining::Vararg{<:AbstractVector{<:AffineFunction}, N}) where {N}
@boundscheck i >= firstindex(dest) && (i + length(source) - 1) <= lastindex(dest) || throw(DimensionMismatch())
@inbounds for s in source
copyto!(dest[i], s)
Expand Down
22 changes: 15 additions & 7 deletions src/moi_interop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,26 @@ moi_to_native_type(::Type{MOI.SingleVariable}) = Nothing
struct Objective{E, F}
expr::WrappedExpression{E}
f::F
isconstant::Bool
end

function Objective(::Type{T}, expr) where T
val = evalarg(expr)
E = canonical_function_type(typeof(val), T)
converted = @expression convert(E, expr)
isconstant = converted isa E # i.e., it's just a value; not a LazyExpression or a Parameter
wrapped = convert(WrappedExpression{E}, converted)
f = make_moi_equivalent(E)
F = typeof(f)
update!(f, wrapped())
Objective{E, F}(wrapped, f)
Objective{E, F}(wrapped, f, isconstant)
end

function update!(objective::Objective{E, F}, optimizer::MOI.AbstractOptimizer, varmap) where {E, F}
update!(objective.f, objective.expr(), varmap)
MOI.set(optimizer, MOI.ObjectiveFunction{F}(), objective.f)
if !objective.isconstant
update!(objective.f, objective.expr(), varmap)
MOI.set(optimizer, MOI.ObjectiveFunction{F}(), objective.f)
end
nothing
end

Expand All @@ -137,30 +141,34 @@ mutable struct Constraint{E, F<:MOI.AbstractFunction, S<:MOI.AbstractSet}
expr::WrappedExpression{E}
f::F
set::S
isconstant::Bool
modelindex::MOI.ConstraintIndex{F, S}
optimizerindex::MOI.ConstraintIndex{F, S}

function Constraint(::Type{T}, expr, set::S) where {T, S<:MOI.AbstractSet}
val = evalarg(expr)
E = canonical_function_type(typeof(val), T)
converted = @expression convert(E, expr)
isconstant = converted isa E # i.e., it's just a value; not a LazyExpression or a Parameter
wrapped = convert(WrappedExpression{E}, converted)
f = make_moi_equivalent(E)
F = typeof(f)
update!(f, wrapped())
new{E, F, S}(wrapped, f, set)
new{E, F, S}(wrapped, f, set, isconstant)
end

function Constraint(f::F, set::S) where {F<:MOI.AbstractFunction, S<:MOI.AbstractSet}
E = Nothing
wrapped = convert(WrappedExpression{E}, @expression nothing)
new{E, F, S}(wrapped, f, set)
new{E, F, S}(wrapped, f, set, true)
end
end

function update!(constraint::Constraint, optimizer::MOI.AbstractOptimizer, varmap)
update!(constraint.f, constraint.expr(), varmap)
MOI.set(optimizer, MOI.ConstraintFunction(), constraint.optimizerindex, constraint.f)
if !constraint.isconstant
update!(constraint.f, constraint.expr(), varmap)
MOI.set(optimizer, MOI.ConstraintFunction(), constraint.optimizerindex, constraint.f)
end
nothing
end
update!(constraint::Constraint{Nothing}, optimizer::MOI.AbstractOptimizer, varmap) = nothing
Expand Down

0 comments on commit f88f384

Please sign in to comment.