Say I define a constraint with JuMP:

```
using JuMP
model = Model();
@variable(model, x[1:3]);
constraint = @constraint(model, c1, sum(x) <= 1)
```

How can I now, only using the variable `constraint`

, obtain an iterable of variables present in this constraint?

odow
December 13, 2023, 7:40pm
2
Hi @BdeKoning , welcome to the forum,

There is no explicit function you can call, but if the constraint is linear (JuMP represents linear constraints as an affine function with type `AffExpr`

), you can use:

```
julia> using JuMP
julia> model = Model();
julia> @variable(model, x[1:3]);
julia> constraint = @constraint(model, c1, sum(x) <= 1)
c1 : x[1] + x[2] + x[3] ≤ 1
julia> object = constraint_object(constraint)
ScalarConstraint{AffExpr, MathOptInterface.LessThan{Float64}}(x[1] + x[2] + x[3], MathOptInterface.LessThan{Float64}(1.0))
julia> object.func
x[1] + x[2] + x[3]
julia> typeof(object.func)
AffExpr (alias for GenericAffExpr{Float64, GenericVariableRef{Float64}})
julia> variables = [k for k in keys(object.func.terms)]
3-element Vector{VariableRef}:
x[1]
x[2]
x[3]
```

2 Likes