# \`num\_constraints\` to return the total number of constraints

**URL:** https://discourse.julialang.org/t/num-constraints-to-return-the-total-number-of-constraints/65488
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [July 29, 2021, 10:26am UTC](https://discourse.julialang.org/t/num-constraints-to-return-the-total-number-of-constraints/65488 "2021-07-29T10:26:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![raphaelsaavedra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raphaelsaavedra/32/27631_2.png) [@raphaelsaavedra](https://discourse.julialang.org/u/raphaelsaavedra)
#### Post date: [July 29, 2021, 10:26am UTC](https://discourse.julialang.org/t/num-constraints-to-return-the-total-number-of-constraints/65488/1 "2021-07-29T10:26:40Z")

</div>

Apparently `num_constraints` only accepts concrete types, which means it’s not straightforward to return the total number of constraints in the model. Is there a straightforward way of doing this, or do we need to resort to something like

```julia
l = list_of_constraint_types(m)
fun_types = first.(l)
con_types = last.(l)
sum(num_constraints.(m, fun_types, con_types))

```

If there is no one-line way of doing this, I think it would be interesting to have a method for that (e.g. just `num_constraints(m)` without passing any types)

---

<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: [July 29, 2021, 11:02am UTC](https://discourse.julialang.org/t/num-constraints-to-return-the-total-number-of-constraints/65488/2 "2021-07-29T11:02:19Z")

</div>

> If there is no one-line way of doing this

It’s trivial to write:

```julia
sum(num_constraints(model, F, S) for (F, S) in list_of_constraint_types(model))

```

You have to be a little careful with this, because it’s going to return an answer you might not expect. in particular, it will also count things like variable bounds and integrality as constraints, not just affine rows in the constraint matrix.

---

<div class="post-metadata">

### Author: ![raphaelsaavedra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raphaelsaavedra/32/27631_2.png) [@raphaelsaavedra](https://discourse.julialang.org/u/raphaelsaavedra)
#### Post date: [July 29, 2021, 11:08am UTC](https://discourse.julialang.org/t/num-constraints-to-return-the-total-number-of-constraints/65488/3 "2021-07-29T11:08:37Z")

</div>

Thanks, that’s definitely better than what I was doing

> it will also count things like variable bounds and integrality as constraints

Yep, that’s expected and it makes sense to me that it does.
