# Count Binary Variables in JuMP Model

**URL:** https://discourse.julialang.org/t/count-binary-variables-in-jump-model/87232
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [September 13, 2022, 8:44pm UTC](https://discourse.julialang.org/t/count-binary-variables-in-jump-model/87232 "2022-09-13T20:44:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![chelseas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chelseas/32/12148_2.png) [@chelseas](https://discourse.julialang.org/u/chelseas)
#### Post date: [September 13, 2022, 8:44pm UTC](https://discourse.julialang.org/t/count-binary-variables-in-jump-model/87232/1 "2022-09-13T20:44:12Z")

</div>

What is the proper way to count binary variables in my jump model?  
I have been using:

```julia
MathOptInterface = MOI
    const_types = list_of_constraint_types(model)
    n_bin = 0
    n_lin = 0

    for i = 1:length(const_types)
        var = const_types[i][1]
        const_type = const_types[i][2]
        l = num_constraints(model, var, const_type)
        if const_type == MathOptInterface.ZeroOne
            n_bin += l
        else
            n_lin += l
        end
    end

```

And then we have number of binary variables is `n_bin` and number of real variables (Assuming there are only two types) is `num_variables(model) - n_bin` and number of constraints (all are linear) is `n_lin`. Does this seem right?

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [September 13, 2022, 9:39pm UTC](https://discourse.julialang.org/t/count-binary-variables-in-jump-model/87232/2 "2022-09-13T21:39:54Z")

</div>

Probably just `num_constraints(model, VariableRef, MOI.ZeroOne)`?

Your count of the number of linear constraints includes things like variable bounds. Do instead:

```nohighlight
n_bin, n_lin = 0, 0
for (F, S) in list_of_constraint_types(model)
    n = num_constraints(model, F, S)
    if S == MOI.ZeroOne
        n_bin += n
    end
    if F == AffExpr
        n_lin += n
    end
end

```

---

<div class="post-metadata">

### Author: ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)
#### Post date: [April 11, 2024, 8:43pm UTC](https://discourse.julialang.org/t/count-binary-variables-in-jump-model/87232/3 "2024-04-11T20:43:00Z")

</div>

> [@odow](#):
>
> `num_constraints(model, VariableRef, MOI.ZeroOne)`?

Hi @odow, I have a follow-up (more like confirming if what I usually do is right or not):

Assuming the model has only continuous (both with bounds and unrestricted) and binary variables:

For continuous variables only: `count(v -> !is_binary(v), all_variables(model))`

For binary variables `count(is_binary, all_variables(model))`

For all constraints, including all bounds on variables:  
`num_constraints(model; count_variable_in_set_constraints = true)`

Do these all look right? Thanks!

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [April 11, 2024, 11:42pm UTC](https://discourse.julialang.org/t/count-binary-variables-in-jump-model/87232/4 "2024-04-11T23:42:29Z")

</div>

Yes, that’s one way to do this.

You could simplify to `count(!is_binary, all_variables(model))`.

---

<div class="post-metadata">

### Author: ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)
#### Post date: [April 12, 2024, 12:30am UTC](https://discourse.julialang.org/t/count-binary-variables-in-jump-model/87232/5 "2024-04-12T00:30:24Z")

</div>

Thanks!
