`num_constraints` to return the total number of constraints

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

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)

If there is no one-line way of doing this

It’s trivial to write:

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.

1 Like

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.

1 Like