# MOI Count Repetitions of Integers

**URL:** <https://discourse.julialang.org/t/moi-count-repetitions-of-integers/93545>\
**Category:** Optimization (Mathematical)\
**Created:** [January 26, 2023, 12:49am UTC](https://discourse.julialang.org/t/moi-count-repetitions-of-integers/93545 "2023-01-26T00:49:20Z")\
**Posts on this page:** 7\
**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:** [January 26, 2023, 12:49am UTC](https://discourse.julialang.org/t/moi-count-repetitions-of-integers/93545/1 "2023-01-26T00:49:20Z")

</div>

Given a vector of integers, is there a MOI constraint that will count the number of repetitions of each distinct integer in the vector?

---

<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:** [January 26, 2023, 12:54am UTC](https://discourse.julialang.org/t/moi-count-repetitions-of-integers/93545/2 "2023-01-26T00:54:58Z")

</div>

You can use `MOI.CountBelongs`:

```julia
using JuMP, HiGHS
model = Model(HiGHS.Optimizer);
set_silent(model)
N = 3
l, u = 0, 3
@variable(model, l <= x[1:N] <= u, Int)
@variable(model, n[l:u], Int)
# n[i] is the count of how many `i` are in x
@constraint(model, [i=l:u], [n[i]; x] in MOI.CountBelongs(1+N, Set([i])))
optimize!(model)
value.(n), value.(x)

```

---

<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:** [January 26, 2023, 1:03am UTC](https://discourse.julialang.org/t/moi-count-repetitions-of-integers/93545/3 "2023-01-26T01:03:17Z")

</div>

Given your recent questions ([MathOptInterface Documentation - #8 by odow](https://discourse.julialang.org/t/mathoptinterface-documentation/93544/8)), I’ll add a page to the JuMP documentation with a bunch of these short snippets.

---

<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:** [January 26, 2023, 1:14am UTC](https://discourse.julialang.org/t/moi-count-repetitions-of-integers/93545/4 "2023-01-26T01:14:12Z")

</div>

Is there a way to first find the set S of distinct integers in the vector x, where S is a subset of l:u? If so, would this be more efficient? For example, what if the size of S were much smaller than the size of l:u (u-l+1)?  
@variable(model, n[S], Int)  
@constraint(model, [i=S], [n[i]; x] in MOI.CountBelongs(1+N, 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:** [January 26, 2023, 1:53am UTC](https://discourse.julialang.org/t/moi-count-repetitions-of-integers/93545/5 "2023-01-26T01:53:43Z")

</div>

Here’s the PR to improve the JuMP documentation: [[docs] add tutorial on constraint programming by odow · Pull Request #3202 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/pull/3202)

> Is there a way to first find the set S of distinct integers in the vector x

You want to dynamically choose `S` _and_ count them? I don’t think so. That seems like a hard problem.

---

<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:** [January 26, 2023, 4:58am UTC](https://discourse.julialang.org/t/moi-count-repetitions-of-integers/93545/6 "2023-01-26T04:58:55Z")

</div>

> @constraint(model, [i=l:u], [n[i]; x] in MOI.CountBelongs(1+N, Set([i])))

How would I find the set J = {j : n[j] = m} for a particular count m? Is it possible to sort J in increasing order?

---

<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:** [January 26, 2023, 7:43am UTC](https://discourse.julialang.org/t/moi-count-repetitions-of-integers/93545/7 "2023-01-26T07:43:05Z")

</div>

The new docs of what you can do in JuMP are here: [Constraint programming · JuMP](https://jump.dev/JuMP.jl/dev/tutorials/linear/constraint_programming/)

It’s not a fully fledged constraint programming system, so you’ll have to rewrite what you’re trying to do in terms of the available primitives. Things like `sort`, and `nnz` (from our emails) aren’t supported.
