JuMP.all_constraints not listing GenericQuadExpr constraints

I have a quadratic program, and was trying to list all the constraints. However, all_constraints, wouldn’t list any of the constraints of type GenericQuadExpr. Below is a small example exhibiting the problem.

m = Model(optimizer_with_attributes(Gurobi.Optimizer, "OutputFlag" => 1))
@variable(m, x[1:2]>=0)
@objective(m, Min, x[1]+x[2])
@constraint(m, x[1]>=x[2]^2)
@constraint(m, x[2]>=3)
println(all_constraints(m))

The output is Any[x[2] >= 3.0, x[1] >= 0.0, x[2] >= 0.0]

However, if I run:

println(list_of_constraint_types(m))

The output is [(GenericAffExpr{Float64,VariableRef}, MathOptInterface.GreaterThan{Float64}), (GenericQuadExpr{Float64,VariableRef}, MathOptInterface.GreaterThan{Float64}), (VariableRef, MathOptInterface.GreaterThan{Float64})]

So I can run:

println(all_constraints(m,list_of_constraint_types(m)[2][1],list_of_constraint_types(m)[2][2])[1])

in order to get the quadratic constraint: -x[2]² + x[1] >= 0.0

Is this the intended method to extract these constraints?

Thanks,
Tony.

What version of JuMP are you on? The signature all_constraints(m) does not exist in the reference (I can’t find it anyway…).
To your point, though, yes I think that is the intended use of the all_constraints + list_of_constraint_types functions. If you want to print all of the constraints, you can do something like:

for T in list_of_constraint_types(m)
    cs = all_constraints(m, T...)
    println(T)
    println.("\t", cs)
end
1 Like

Tony, here’s the documentation: Constraints · JuMP

Thanks.

I had found that documentation, which is how I am now am using all_constraints. However, I must have inferred the single argument usage from the way all_variables(m) works.

I guess my concern is that all_constraints(m) works without any warning or error (I’ve looked at the code, and I’m not sure how that is the case, since the other two arguments aren’t optional).

I think that either all_variables(m) shouldn’t work at all (throwing an error), or it should enumerate all constraint types.

Thanks,
Tony.

all_constraints(model) doesn’t work…

julia> all_constraints(model)
ERROR: MethodError: no method matching all_constraints(::Model)
Closest candidates are:
  all_constraints(::Model, ::Type{#s94} where #s94<:Union{AbstractJuMPScalar, Array{#s93,1} where #s93<:AbstractJuMPScalar}, ::Type{#s92} where #s92<:MathOptInterface.AbstractSet) at /Users/oscar/.julia/packages/JuMP/YXK4e/src/constraints.jl:852
Stacktrace:
 [1] top-level scope at REPL[230]:100

What does it return if it doesn’t throw an error? Do you have a reproducible example? What is the output of ] status?

The example I put in my question is what I get: an Any array of all the linear constraints.

My version of JuMP was v0.21.2, but I just updated to JuMP v0.21.3, and it’s doing the same thing. (I am using Julia 1.3, however.)

What is @which all_constraints(m)?

I’ll send you an email.

Tony was using a package which defined JuMP.all_constraints(::Model), thus breaking the rule: Style Guide · The Julia Language

1 Like

Apologies all.

I had imported a package that clearly violated all the Julia rules.

Tony.