Skip to content
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
21 changes: 4 additions & 17 deletions src/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1742,23 +1742,10 @@ function bridged_constraint_function(
if !Variable.has_bridges(Variable.bridges(b))
return func, set
end
if func isa MOI.AbstractScalarFunction
constant = MOI.constant(func)
if !iszero(constant)
# We use the fact that the initial function constant was zero to
# implement getters for `MOI.ConstraintFunction` and
# `MOI.ConstraintSet`. See `unbridged_constraint_function`.
throw(
MOI.ScalarFunctionConstantNotZero{
typeof(constant),
typeof(func),
typeof(set),
}(
constant,
),
)
end
end
# We use the fact that the initial function constant was zero to
# implement getters for `MOI.ConstraintFunction` and
# `MOI.ConstraintSet`. See `unbridged_constraint_function`.
MOI.throw_if_scalar_and_constant_not_zero(func, typeof(set))
f = bridged_function(b, func)::typeof(func)
return MOIU.normalize_constant(f, set)
end
Expand Down
29 changes: 29 additions & 0 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,35 @@ function Base.showerror(
)
end

"""
throw_if_scalar_and_constant_not_zero(func, S::Type)

Throw a `ScalarFunctionConstantNotZero(index)` error `func` is a scalar function
whose constant is not zero.
"""
function throw_if_scalar_and_constant_not_zero(
func::AbstractScalarFunction,
::Type{S},
) where {S<:AbstractScalarSet}
cst = constant(func)
if !iszero(cst)
throw(ScalarFunctionConstantNotZero{typeof(cst),typeof(func),S}(cst))
end
return
end
function throw_if_scalar_and_constant_not_zero(
::SingleVariable,
::Type{S},
) where {S<:AbstractScalarSet}
return
end
function throw_if_scalar_and_constant_not_zero(
::AbstractVectorFunction,
::Type{S},
) where {S<:AbstractVectorSet}
return
end

"""
add_constraint(model::ModelLike, func::F, set::S)::ConstraintIndex{F,S} where {F,S}

Expand Down
23 changes: 23 additions & 0 deletions test/constraints.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Test
using MathOptInterface
const MOI = MathOptInterface

function constant_not_zero_test(::Type{T}) where {T}
S = MOI.EqualTo{T}
x = MOI.VariableIndex(1)
fx = MOI.SingleVariable(x)
@test nothing === MOI.throw_if_scalar_and_constant_not_zero(fx, S)
func1 = one(T) * fx + one(T)
@test_throws MOI.ScalarFunctionConstantNotZero begin
MOI.throw_if_scalar_and_constant_not_zero(func1, S)
end
func2 = one(T) * fx
@test nothing === MOI.throw_if_scalar_and_constant_not_zero(func2, S)
func = MOI.Utilities.operate(vcat, T, func1, func2)
@test nothing === MOI.throw_if_scalar_and_constant_not_zero(func, MOI.Zeros)
end

@testset "Constant not zero" begin
constant_not_zero_test(Int)
constant_not_zero_test(Float64)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ include("dummy.jl")
include("functions.jl")
include("sets.jl")
include("attributes.jl")
include("constraints.jl")
include("instantiate.jl")
include("deprecate.jl")
end
Expand Down