Skip to content

MOI.is_valid can be falsely positive for bridged constraints #2696

Description

@odow

Found as part of #2695. But it does't need to hold that up merging.

The last MOI.is_valid should return false:

julia> import MathOptInterface as MOI

julia> include("test/Bridges/sdpa_models.jl")

julia> model = MOI.instantiate(StandardSDPAModel{Float64}; with_bridge_type = Float64);

julia> x = MOI.add_variables(model, 2);

julia> c = MOI.add_constraint(model, MOI.VectorOfVariables(x), MOI.Nonpositives(2));

julia> MOI.is_valid(model, c)
true

julia> MOI.is_valid(model, typeof(c)(c.value + 1))
true

See
https://github.com/jump-dev/MathOptInterface.jl/pull/2695/files#diff-87c703874f7251d45788070ce6ab50219cfe8b7561d08e6f6148d44afba24ccbR249-R251

Activity

  1. added theissue type on Mar 25, 2025
  2. odow commented on Apr 2, 2025

    @odow
    MemberAuthor
    julia> using Revise
    
    julia> import MathOptInterface as MOI
    
    julia> MOI.Utilities.@model(
               Model,
               (), (MOI.EqualTo,), (MOI.Nonnegatives,), (),
               (), (MOI.ScalarAffineFunction,), (MOI.VectorOfVariables,), (),
           )
    MathOptInterface.Utilities.GenericModel{T, MathOptInterface.Utilities.ObjectiveContainer{T}, MathOptInterface.Utilities.VariablesContainer{T}, ModelFunctionConstraints{T}} where T
    
    julia> MOI.supports_constraint(::Model, ::Type{MOI.VectorOfVariables}, ::Type{MOI.Reals}) = false
    
    julia> MOI.supports_add_constrained_variables(::Model, ::Type{MOI.Nonnegatives}) = true
    
    julia> MOI.supports_add_constrained_variables(::Model, ::Type{MOI.Reals}) = false
    
    julia> model = MOI.instantiate(Model{Float64}; with_bridge_type = Float64);
    
    julia> x = MOI.add_variables(model, 2);
    
    julia> c = MOI.add_constraint(model, MOI.VectorOfVariables(x), MOI.Nonpositives(2));
    
    julia> MOI.is_valid(model, c)
    true
    
    julia> ci = typeof(c)(c.value + 1)
    MathOptInterface.ConstraintIndex{MathOptInterface.VectorOfVariables, MathOptInterface.Nonpositives}(-2)
    
    julia> MOI.is_valid(model, ci)
    true
  3. m-bossart commented on Nov 4, 2026

    @m-bossart

    Here is a case where the false positive silently corrupts a model.

    With a solver that supports Integer but not ZeroOne (MosekTools, for example), ZeroOneBridge adds an internal VariableIndex-in-Integer constraint.
    is_valid reports that constraint as a user constraint, and delete removes it.
    The ZeroOne constraint stays valid, but the solver no longer has integrality on the variable.

    The delete checks added in #2818 cover deleting variables, not deleting the constraint directly.

    import MathOptInterface as MOI
    
    MOI.Utilities.@model(
        IntegerOnlyModel,
        (MOI.Integer,),
        (MOI.EqualTo, MOI.GreaterThan, MOI.LessThan, MOI.Interval),
        (),
        (),
        (),
        (MOI.ScalarAffineFunction,),
        (),
        (),
    )
    function MOI.supports_constraint(
        ::IntegerOnlyModel,
        ::Type{MOI.VariableIndex},
        ::Type{MOI.ZeroOne},
    )
        return false
    end
    
    model = MOI.instantiate(IntegerOnlyModel{Float64}; with_bridge_type = Float64)
    x = MOI.add_variable(model)
    c = MOI.add_constraint(model, x, MOI.ZeroOne())
    ci = MOI.ConstraintIndex{MOI.VariableIndex,MOI.Integer}(x.value)
    @show MOI.get(model, MOI.ListOfConstraintIndices{MOI.VariableIndex,MOI.Integer}())
    @show MOI.is_valid(model, ci)
    MOI.delete(model, ci)
    @show MOI.is_valid(model, c)
    @show MOI.get(model.model, MOI.ListOfConstraintTypesPresent())
    MOI.get(model, MOI.ListOfConstraintIndices{MOI.VariableIndex, MOI.Integer}()) = MOI.ConstraintIndex{MOI.VariableIndex, MOI.Integer}[]
    MOI.is_valid(model, ci) = true
    MOI.is_valid(model, c) = true
    MOI.get(model.model, MOI.ListOfConstraintTypesPresent()) = Tuple{Type, Type}[(MOI.ScalarAffineFunction{Float64}, MOI.Interval{Float64})]
    

    The comment on is_valid says users won't have the internal index "except by luck/enumeration", but JuMP builds a VariableIndex constraint's index from the variable's .value, so JuMP hits this through ordinary calls.
    is_integer(b) returns true for a variable declared only with @variable(model, b, Bin).
    fix_discrete_variables then calls unset_integer(b), which runs the delete above, and its undo function throws:

    ERROR: Cannot set variable b to integer because it is already binary.
    

    Found while computing MILP duals with MosekTools in direct_model (jump-dev/MosekTools.jl#222).

    MathOptInterface v1.54.0, JuMP v1.32.1, Julia 1.13.0.

  4. blegat commented on Nov 5, 2026

    @blegat
    Member

    Actually, #3013 fixes the first issue @odow reported in this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions