# Compact form of a set of constraints

**URL:** <https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220>\
**Category:** Optimization (Mathematical)\
**Created:** [May 21, 2025, 1:43pm UTC](https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220 "2025-05-21T13:43:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 21, 2025, 1:43pm UTC](https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220/1 "2025-05-21T13:43:09Z")

</div>

I have the following script that works for the case N=4.

```julia
using JuMP 
import HiGHS
m = Model(HiGHS.Optimizer);
@variable(m, x[1:4,1:8], Bin)
@variable(m, y[1:4,1:6], Bin)

@constraints(m, begin
    [j in 1:8], sum(x[r,j] for r in 1:4)==1

    [i in 1:3], y[4,i]-->{x[4,i]+x[4,i+5]==2}
       sum(y[4,j] for j in 1:3)==1
    [i in 1:4], y[3,i]-->{x[3,i]+x[3,i+4]==2}
       sum(y[3,j] for j in 1:4)==1
    [i in 1:5], y[2,i]-->{x[2,i]+x[2,i+3]==2}
       sum(y[2,j] for j in 1:5)==1
    [i in 1:6], y[1,i]-->{x[1,i]+x[1,i+2]==2}
       sum(y[1,j] for j in 1:6)==1
    end)
optimize!(m)
julia> value.(x)
4×8 Matrix{Float64}:
 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 1.0 0.0 0.0 1.0
 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0
 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0

```

Can I get a compact form that allows me to do tests for higher values ​​of N, without having to hand write all the constraints, case by case?

Is something like this, with the appropriate and correct syntax possible?

```julia

N=4
m = Model(HiGHS.Optimizer);
@variable(m, x[1:N,1:2N], Bin)
@variable(m, y[1:N,1:2(N-1)], Bin)

@constraints(m, begin
    [j in 1:2N], sum(x[r,j] for r in 1:N)==1

    for k in N-1:-1:1
    [i in 1:k], y[k+1,i]-->{x[k1,i]+x[k+1,i+k+2]==2}
       sum(y[k+1,j] for j in 1:k)==1
    end

    end)
optimize!(m)

value.(x)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 21, 2025, 2:01pm UTC](https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220/2 "2025-05-21T14:01:08Z")

</div>

maybe like this?

```julia
@constraints(m, begin
    [j in 1:2N], sum(x[r,j] for r in 1:N)==1
    [k in N-1:-1:1, i in 1:k], y[k+1,i]-->{x[k+1,i]+x[k+1,i+k+2]==2}
    [k in N-1:-1:1], sum(y[k+1,j] for j in 1:k)==1

    end)
optimize!(m)

```

---

<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:** [May 21, 2025, 9:29pm UTC](https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220/3 "2025-05-21T21:29:22Z")

</div>

Your second comment looks like it’s on the right track, but you might want to double check your logic.

I’d probably write your model as:

```Julia
using JuMP 
import HiGHS
function main(N)
    model = Model(HiGHS.Optimizer)
    @variable(model, x[1:N, 1:2N], Bin)
    @variable(model, y[1:N, 1:2(N-1)], Bin)
    @constraints(model, begin
        [j in 1:2N], sum(x[:,j]) == 1
        [i in 1:N], sum(y[i,1:(2N-1-i)]) == 1
        [k in 1:N, i in 1:(2N-1-k)], y[k,i] --> {x[k,i] + x[k,i+k+1] == 2}
    end)
    optimize!(model)
    assert_is_solved_and_feasible(model)
    return value.(x), value.(y)
end

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 22, 2025, 4:54am UTC](https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220/4 "2025-05-22T04:54:51Z")

</div>

Yes, of course. That was to confirm that the syntax was valid.  
I put it that way.  
Why does the solution x (declared Bin) have real values ​​and not integers?

```julia
m = Model(HiGHS.Optimizer);
@variable(m, x[1:N,1:2N], Bin)
@variable(m, y[1:N,1:2(N-1)], Bin)

@constraints(m, begin
   [k in 1:N, i in 1:2N-k-1], y[k,i]-->{x[k,i]+x[k,i+k+1]==2}
   [k in 1:N], sum(y[k,i] for i in 1:2(N-1)-k+1)==1
   [j in 1:2N], sum(x[r,j] for r in 1:N)==1
    end)
optimize!(m)

round.(Int,value.(x))

```

What role does this expression have?

```julia
assert_is_solved_and_feasible(model)

```

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [May 22, 2025, 5:14am UTC](https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220/5 "2025-05-22T05:14:52Z")

</div>

> [@rocco\_sprmnt21](#):
>
> Why does the solution x (declared Bin) have real values ​​and not integers?

Because Optimizers work with float64.

> [@rocco\_sprmnt21](#):
>
> What role does this expression have?

A normality check. See the docstring.

```julia
help?> JuMP.is_solved_and_feasible
  is_solved_and_feasible(
      model::GenericModel;
      allow_local::Bool = true,
      allow_almost::Bool = false,
      dual::Bool = false,
      result::Int = 1,
  )

.....

    • the primal_status of the result index result is FEASIBLE_POINT.

  This function is conservative, in that it returns false for situations like the solver terminating with a feasible solution due to a time      
  limit.

  If this function returns false, use termination_status, result_count, primal_status and dual_status to understand what solutions are
  available (if any).

  See also: assert_is_solved_and_feasible.

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 22, 2025, 5:22am UTC](https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220/6 "2025-05-22T05:22:28Z")

</div>

in my system it is not defined (I probably need to update)

```julia
ERROR: UndefVarError: `assert_is_solved_and_feasible` not defined

```

I provide some more information on the problem.  
I was trying to understand for which values ​​of N it is possible to find a sequence of 2N numbers, containing all the pairs (1,1) (2,2) …(N,N) positioned at a distance equal to the number. For example (1,x,1), (3,x,x,x,3) …  
in the case N=11

```julia
[1:11...]'*main(11)
...
 6 8 10 11 1 2 1 6 2 7 8 9 5 10 4 11 3 7 5 4 3 9

```

---

<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:** [May 22, 2025, 5:33am UTC](https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220/7 "2025-05-22T05:33:36Z")

</div>

> [@rocco\_sprmnt21](#):
>
> Why does the solution x (declared Bin) have real values ​​and not integers

Take a read of [Tolerances and numerical issues · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/tolerances/)

And yes, you must have an old version of JuMP. It should work after updating.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [September 21, 2025, 1:37pm UTC](https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220/8 "2025-09-21T13:37:12Z")

</div>

I tried to rewrite the set of constraints differently, but equivalently.  
The following attempt shows that the equivalence is not true.  
But I don’t know how to interpret the second rows of the y and x matrices.  
x[2,14]=1 =\> y[2,14]+y[2,17]==2  
An error should be reported or this configuration should be considered outside the region defined by the constraints.  
What is the reality in this case?

```julia-auto
function main(N)
    model = Model(HiGHS.Optimizer)
    set_silent(model)
    @variable(model, x[1:N, 1:2N], Bin)
    @variable(model, y[1:N, 1:2(N-1)], Bin)
    @constraints(model, begin
        [j in 1:2N], sum(x[:,j]) == 1
        [j in 1:2N-2], sum(y[:,j]) <= 1
        [i in 1:N], sum(x[i,:]) == 2
#[i in 1:N], sum(y[i,1:(2N-1-i)]) == 1
        [i in 1:N], sum(y[i,:]) == 1
        [k in 1:N, i in 1:(2N-1-k)], y[k,i] --> {x[k,i] + x[k,i+k+1] == 2}
    end)
    optimize!(model)
    assert_is_solved_and_feasible(model)
    #return value.(x), value.(y)
    round.(Int,value.(x)), round.(Int,value.(y))
end
# l'insieme di vincoli di questa versione è APPARENTEMENTE equivalente alla versione precedente
# ma il risultato che segue è un controesempio di questa congettura
#
# julia> x
# 8×16 Matrix{Int64}:
# 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0
# 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
# 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0
# 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
# 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0
# 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0
# 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
# 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0

# julia> y
# 8×14 Matrix{Int64}:
# 0 0 0 0 0 0 0 0 0 1 0 0 0 0
# 0 0 0 0 0 0 0 0 0 0 0 0 0 1
# 0 0 0 0 0 0 0 0 0 0 0 0 1 0
# 0 0 0 0 0 0 0 0 0 0 0 1 0 0
# 0 0 0 0 0 0 0 0 0 0 1 0 0 0
# 0 1 0 0 0 0 0 0 0 0 0 0 0 0
# 0 0 0 0 0 0 0 0 1 0 0 0 0 0
# 0 0 0 0 0 0 0 1 0 0 0 0 0 0

```

PS

How is the implication y → {x1+x2 ==2} translated into terms of linear constraints?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [September 21, 2025, 2:08pm UTC](https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220/9 "2025-09-21T14:08:03Z")

</div>

I’ll answer myself, maybe.  
The constraint y[2,14+]–\> {x + x} is not actually defined.  
So there is no implication

Adding this constraint  
`[i in 1:N], sum(y[i,2N-i:2N-2]) == 0`  
seems to work.

```julia-auto
           model = Model(HiGHS.Optimizer)
           set_silent(model)
           @variable(model, x[1:N, 1:2N], Bin)
           @variable(model, y[1:N, 1:2(N-1)], Bin)
           @constraints(model, begin
               [j in 1:2N], sum(x[:,j]) == 1
               [j in 1:2N-2], sum(y[:,j]) <= 1
               [i in 1:N], sum(x[i,:]) == 2
       #[i in 1:N], sum(y[i,1:(2N-1-i)]) == 1
               [i in 1:N], sum(y[i,:]) == 1
               [i in 1:N], sum(y[i,2N-i:2N-2]) == 0
               [k in 1:N, i in 1:(2N-1-k)], y[k,i] --> {x[k,i] + x[k,i+k+1] == 2} 
           end)
           optimize!(model)
           assert_is_solved_and_feasible(model)
           #return value.(x), value.(y)
           round.(Int,value.(x)), round.(Int,value.(y))
       end
main (generic function with 1 method)

julia> 

julia> 

julia> x,y = main(N)
([0 0 … 0 0; 0 0 … 0 0; … ; 0 0 … 0 0; 0 0 … 1 0], [0 0 … 0 0; 0 0 … 0 0; … ; 0 0 … 0 0; 0 0 … 0 0])

julia> 

julia> 

julia> 

julia> x
8×16 Matrix{Int64}:
 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0
 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0
 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0
 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1
 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0
 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0

julia> y
8×14 Matrix{Int64}:
 0 0 0 0 0 0 0 0 0 0 0 1 0 0
 0 0 0 1 0 0 0 0 0 0 0 0 0 0
 1 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 1 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 1 0 0 0 0
 0 1 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 1 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 0 0 0 0 0 0 0 0

```

---

<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:** [September 21, 2025, 9:31pm UTC](https://discourse.julialang.org/t/compact-form-of-a-set-of-constraints/129220/10 "2025-09-21T21:31:31Z")

</div>

Is there still a question? Or did you figure it out?

If `N = 8` and `k = 2`, then `i = 1:13`, so yeah, there is no implication for `y[2,14]`.

> How is the implication y → {x1+x2 ==2} translated into terms of linear constraints?

It depends on the solver. But in this specific case:

```julia
x1 + x2 + s == 2
s >= 0
s + 2y <= 2

```

If `y = 1`, then `s = 0`. If `y = 0`, then `0 <= s <= 2`, and so the `== 2` constraint is satisfiable for any combination of `x1` and `x2`.
