# JuMP: constraint to use a value only \`n\` times

**URL:** <https://discourse.julialang.org/t/jump-constraint-to-use-a-value-only-n-times/73930>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [January 2, 2022, 3:20pm UTC](https://discourse.julialang.org/t/jump-constraint-to-use-a-value-only-n-times/73930 "2022-01-02T15:20:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sushant-Padha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushant-padha/32/32367_2.png) [@Sushant-Padha](https://discourse.julialang.org/u/Sushant-Padha)\
**Post date:** [January 2, 2022, 3:20pm UTC](https://discourse.julialang.org/t/jump-constraint-to-use-a-value-only-n-times/73930/1 "2022-01-02T15:20:11Z")

</div>

I am trying to solve a problem using [ConstraintSolver.jl](https://github.com/Wikunia/ConstraintSolver.jl) which uses JuMP. I want to define a constraint for a group of variables, such that certain values are only chosen certain times.

For eg: if the variables are `1 <= x[1:10] <= 3`, I want it such that value `1` is only chosen 2 times, value `2` is only chosen 5 times and value `3` is chosen 2 times.

Can this be achieved using the constraints defined in JuMP and ConstraintSolver ([listed here](https://wikunia.github.io/ConstraintSolver.jl/stable/supported/#Supported-constraints))?

---

<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 2, 2022, 8:47pm UTC](https://discourse.julialang.org/t/jump-constraint-to-use-a-value-only-n-times/73930/2 "2022-01-02T20:47:50Z")

</div>

This is generally achieved using a binary expansion:

```julia
model = Model()
n = [2, 5, 3]
@variable(model, y[1:10, 1:3], Bin)
@constraint(model, [i=1:10], sum(y[i, :]) == 1)
@constraint(model, [j=1:3], sum(y[:, j]) == n[j])
@expression(model, x[i=1:10], sum(j * y[i, j] for j in 1:3))

```

---

<div class="post-metadata">

**Author:** ![Sushant-Padha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushant-padha/32/32367_2.png) [@Sushant-Padha](https://discourse.julialang.org/u/Sushant-Padha)\
**Post date:** [January 3, 2022, 5:06am UTC](https://discourse.julialang.org/t/jump-constraint-to-use-a-value-only-n-times/73930/3 "2022-01-03T05:06:28Z")

</div>

Thanks a lot! I have been looking for a solution for a very long time.

Just making sure, if my values are not in a range like instead of `1,2,3` which can be written as `1:3`, if my possible values are `1,3,4` can I still make it work by using range `1:4` but not defining any constraint for value `2`?

---

<div class="post-metadata">

**Author:** ![Hakan\_Kjellerstrand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hakan_kjellerstrand/32/8447_2.png) [@Hakan\_Kjellerstrand](https://discourse.julialang.org/u/Hakan_Kjellerstrand)\
**Post date:** [January 3, 2022, 5:47am UTC](https://discourse.julialang.org/t/jump-constraint-to-use-a-value-only-n-times/73930/4 "2022-01-03T05:47:30Z")

</div>

The standard name in the constraint programming community for this constraint is “Global Cardinality Count” (aka “GCC”). I have defined a decomposition for ConstraintSolver.jl in [http://hakank.org/julia/constraints/constraints\_utils.jl](http://hakank.org/julia/constraints/constraints_utils.jl) (as `global_cardinality_count(model, a, gcc)`), with a small example of the usage here: [http://hakank.org/julia/constraints/clobal\_cardinality\_count.jl](http://hakank.org/julia/constraints/clobal_cardinality_count.jl) .

The array`a` is the array of decision variables and `gcc` contains the occurrences for each value in `a` (including number of occurrences for the value of 0). It use another decomposition `count_ctr` for doing the counts.

If you want to constrain that one value in `a` should not be present, simply set the corresponding value in `gcc` to 0.

---

<div class="post-metadata">

**Author:** ![Sushant-Padha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushant-padha/32/32367_2.png) [@Sushant-Padha](https://discourse.julialang.org/u/Sushant-Padha)\
**Post date:** [January 3, 2022, 3:05pm UTC](https://discourse.julialang.org/t/jump-constraint-to-use-a-value-only-n-times/73930/5 "2022-01-03T15:05:28Z")

</div>

This is a very concise solution to define such a constraint. However, I think I will use the more performant solution for my use case; I think that the `global_cardinality_count` function will be slower because of the mutiple binary variables and sum constraints.

Based on my limited testing ([gcc\_vs\_binexpr\_test.jl](https://gist.github.com/Sushant-Padha/6210c782eceb57da9331e4d7dc54b167#file-gcc_vs_binexpr_test-jl)) with some changed code, it seems that the original solution, though a bit more verbose, is faster ([output.txt](https://gist.github.com/Sushant-Padha/6210c782eceb57da9331e4d7dc54b167#file-output-txt)).

---

<div class="post-metadata">

**Author:** ![Hakan\_Kjellerstrand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hakan_kjellerstrand/32/8447_2.png) [@Hakan\_Kjellerstrand](https://discourse.julialang.org/u/Hakan_Kjellerstrand)\
**Post date:** [January 3, 2022, 7:38pm UTC](https://discourse.julialang.org/t/jump-constraint-to-use-a-value-only-n-times/73930/6 "2022-01-03T19:38:41Z")

</div>

I definitely agree that your implementation is faster than mine. On my computer your approach is even more performant than mine than what the output.txt indicates.

A comment: My original implementation of `global_cardinality_count` also support cases when the `gcc` array (the count array) is an array of decision variables, which might be handy in some cases, for example if one want to add some constraints about the occurrences, such that the occurrences should be distinct.

But perhaps that variant is not relevant for your use case.
