# Efficiency of MOI.CountBelongs

**URL:** <https://discourse.julialang.org/t/efficiency-of-moi-countbelongs/94819>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [February 18, 2023, 3:03am UTC](https://discourse.julialang.org/t/efficiency-of-moi-countbelongs/94819 "2023-02-18T03:03:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stuart\_Rogers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stuart_rogers/32/10694_2.png) [@Stuart\_Rogers](https://discourse.julialang.org/u/Stuart_Rogers)\
**Post date:** [February 18, 2023, 3:03am UTC](https://discourse.julialang.org/t/efficiency-of-moi-countbelongs/94819/1 "2023-02-18T03:03:59Z")

</div>

In the JuMP code below, I want to count the number of occurrences of each possible integer in the variable matrix M. s is defined to be a Kx1 variable vector equal to M[:]. Is there any difference in efficiency between  
@constraint(model, [i=1:v-1], [n[i]; s] in MOI.CountBelongs(1+K, Set([i])))  
versus  
@constraint(model, [i=1:v-1], [n[i]; M[:]] in MOI.CountBelongs(1+K, Set([i])))

using JuMP  
a = 4  
b = 3  
K = a\*b  
v = 16  
model = Model()  
@variable(model, 1 \<= M[1:a,1:b] \<= v-1, Int)

@variable(model, 1 \<= s[1:K] \<= v-1, Int)  
@constraint(model, s .== M[:])

@variable(model, 0 \<= n[1:v-1] \<= K, Int)

# n[i] is the count of how many `i` are in s for 1 \<= i \<= v-1.

@constraint(model, [i=1:v-1], [n[i]; s] in MOI.CountBelongs(1+K, Set([i])))

vs

@constraint(model, [i=1:v-1], [n[i]; M[:]] in MOI.CountBelongs(1+K, Set([i])))

---

<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:** [February 19, 2023, 9:18pm UTC](https://discourse.julialang.org/t/efficiency-of-moi-countbelongs/94819/2 "2023-02-19T21:18:19Z")

</div>

As a general rule of thumb, more variables and more constraints = worse performance. (Lots of cases where this isn’t true though.)

You should probably just use `M` everywhere? I don’t understand the need to introduce `s`.

If you want a vector of the `M` variables, use `vec(M)`.

---

<div class="post-metadata">

**Author:** ![Stuart\_Rogers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stuart_rogers/32/10694_2.png) [@Stuart\_Rogers](https://discourse.julialang.org/u/Stuart_Rogers)\
**Post date:** [February 20, 2023, 12:53am UTC](https://discourse.julialang.org/t/efficiency-of-moi-countbelongs/94819/3 "2023-02-20T00:53:59Z")

</div>

Is there a difference between  
@constraint(model, [i=1:v-1], [n[i]; M[:]] in MOI.CountBelongs(1+K, Set([i])))  
and  
@constraint(model, [i=1:v-1], [n[i]; vec(M)] in MOI.CountBelongs(1+K, Set([i]))) ?

---

<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:** [February 20, 2023, 1:03am UTC](https://discourse.julialang.org/t/efficiency-of-moi-countbelongs/94819/4 "2023-02-20T01:03:54Z")

</div>

No. You should be able to verify by trying it out?
