# Parameterization of a set of constraints

**URL:** <https://discourse.julialang.org/t/parameterization-of-a-set-of-constraints/132885>\
**Category:** Optimization (Mathematical)\
**Created:** [October 5, 2025, 10:14am UTC](https://discourse.julialang.org/t/parameterization-of-a-set-of-constraints/132885 "2025-10-05T10:14:13Z")\
**Posts on this page:** 9\
**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:** [October 5, 2025, 10:14am UTC](https://discourse.julialang.org/t/parameterization-of-a-set-of-constraints/132885/1 "2025-10-05T10:14:13Z")

</div>

I would like, if possible, to parameterize the following set of constraints.  
I would like to replace the fixed values ​​4 in 4:ng; 3 in g-3:g; and 1 in  
… \<=1  
with parameters external to the function that define the model, so that I can more easily change them as needed.

```julia-auto
    #distance between N and N or NF >=4
    [r in 1:nr, g in 4:ng], sum(x[r,i,"N"]+x[r,i,"NF"] for i in g-3:g) <= 1

```

If I use the following expression, I get the following error

```julia-auto
funcion ()
...
     [r in 1:nr, g in Nel_Mese["min_ng_tra_notti"]:ng], sum(x[r,i,"N"]+x[r,i,"NF"] for i in g-Nel_Mese["min_ng_tra_notti"]+1:g) <= 1
...
end

...

Nel_Mese=Dict{String, Int}()
Nel_Mese["min_ng_tra_notti"]=4    

....

julia> x,nr,ng,turni,ops,res=sol_jump(mese,y, path*exc_input)
ERROR: At c:\Users\sprmn\.julia\envs\v1_10\J_turni_SG_10.jl:192: `@constraint(model, [r in 1:nr, g in Nel_Mese["min_ng_tra_notti"]:ng], sum((x[r, i, "N"] + x[r, i, "NF"] for i = (g - Nel_Mese["min_ng_tra_notti"]) + 1:g)) <= 1)`: unexpected error parsing reference set: Nel_Mese["min_ng_tra_notti"]:ng
Stacktrace:
 [1] error(::String, ::String, ::Expr)

```

---

<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:** [October 5, 2025, 8:21pm UTC](https://discourse.julialang.org/t/parameterization-of-a-set-of-constraints/132885/2 "2025-10-05T20:21:54Z")

</div>

Hi @rocco_sprmnt21,

I can’t reproduce this:

```Julia
julia> using JuMP

julia> model = Model()
A JuMP Model
├ solver: none
├ objective_sense: FEASIBILITY_SENSE
├ num_variables: 0
├ num_constraints: 0
└ Names registered in the model: none

julia> @variable(model, x[1:5])
5-element Vector{VariableRef}:
 x[1]
 x[2]
 x[3]
 x[4]
 x[5]

julia> Nel_Mese=Dict{String, Int}()
Dict{String, Int64}()

julia> Nel_Mese["min_ng_tra_notti"]=4
4

julia> nr, ng = 2, 5
(2, 5)

julia> @constraint(model, [r in 1:nr, g in Nel_Mese["min_ng_tra_notti"]:ng], x[g] <= r)
2-dimensional DenseAxisArray{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.LessThan{Float64}}, ScalarShape},2,...} with index sets:
    Dimension 1, Base.OneTo(2)
    Dimension 2, 4:5
And data, a 2×2 Matrix{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.LessThan{Float64}}, ScalarShape}}:
 x[4] ≤ 1 x[5] ≤ 1
 x[4] ≤ 2 x[5] ≤ 2

```

Can you show more of your code? What is the output of `Pkg.status()`?

---

<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:** [October 5, 2025, 8:27pm UTC](https://discourse.julialang.org/t/parameterization-of-a-set-of-constraints/132885/3 "2025-10-05T20:27:44Z")

</div>

```julia-auto

(v1_10) pkg> status
Status `C:\Users\sprmn\.julia\envs\v1_10\Project.toml`
  [39db22c5] ClipData v0.2.3
  [f65535da] Convex v0.16.4
  [c47d62df] DAQP v0.7.1
  [0c46a032] DifferentialEquations v7.16.1
  [e2685f51] ECOS v1.1.3
  [2e9cd046] Gurobi v1.7.6
  [87dc4568] HiGHS v1.19.0
  [4076af6c] JuMP v1.29.1
  [91a5bcdd] Plots v1.41.1
  [82193955] SCIP v0.12.7

julia> versioninfo()
Julia Version 1.11.6
Commit 9615af0f26 (2025-07-09 12:58 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 12 × 13th Gen Intel(R) Core(TM) i7-1355U
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, goldmont)
Threads: 1 default, 0 interactive, 1 GC (on 12 virtual cores)
Environment:
  JULIA_EDITOR = code
  JULIA_VSCODE_REPL = 1

```

As soon as I have time I’ll try to produce a M(not)WE

I don’t know if it makes a difference, but the constraint is inside the block.

```julia-auto
@constraints(model, begin

...

end

```

---

<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:** [October 5, 2025, 8:34pm UTC](https://discourse.julialang.org/t/parameterization-of-a-set-of-constraints/132885/4 "2025-10-05T20:34:20Z")

</div>

Even with an example that is more like yours I can’t reproduce:

```Julia
julia> using JuMP

julia> begin
           nr, ng = 2, 5
           Nel_Mese = Dict{String, Int}("min_ng_tra_notti" => 4)
           model = Model()
           @variable(model, x[1:nr, 1:ng, ["N", "NF"]])
           @constraint(
               model,
               [r in 1:nr, g in Nel_Mese["min_ng_tra_notti"]:ng],
               sum(x[r,i,"N"]+x[r,i,"NF"] for i in g-Nel_Mese["min_ng_tra_notti"]+1:g) <= 1
           )
       end
2-dimensional DenseAxisArray{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.LessThan{Float64}}, ScalarShape},2,...} with index sets:
    Dimension 1, Base.OneTo(2)
    Dimension 2, 4:5
And data, a 2×2 Matrix{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.LessThan{Float64}}, ScalarShape}}:
 x[1,1,N] + x[1,2,N] + x[1,3,N] + x[1,4,N] + x[1,1,NF] + x[1,2,NF] + x[1,3,NF] + x[1,4,NF] ≤ 1 … x[1,2,N] + x[1,3,N] + x[1,4,N] + x[1,5,N] + x[1,2,NF] + x[1,3,NF] + x[1,4,NF] + x[1,5,NF] ≤ 1
 x[2,1,N] + x[2,2,N] + x[2,3,N] + x[2,4,N] + x[2,1,NF] + x[2,2,NF] + x[2,3,NF] + x[2,4,NF] ≤ 1 x[2,2,N] + x[2,3,N] + x[2,4,N] + x[2,5,N] + x[2,2,NF] + x[2,3,NF] + x[2,4,NF] + x[2,5,NF] ≤ 1

(jmp) pkg> st
Status `/private/tmp/jmp/Project.toml`
  [4076af6c] JuMP v1.29.1

```

You must have something else in your code.

What was the full stack trace of the error? Did it have “caused by: …” further down?

---

<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:** [October 5, 2025, 8:47pm UTC](https://discourse.julialang.org/t/parameterization-of-a-set-of-constraints/132885/5 "2025-10-05T20:47:28Z")

</div>

I tried again just now with a clean session and I don’t get that error anymore.  
Sorry.  
If I get it again, I’ll try to pay more attention to the context.

---

<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:** [October 5, 2025, 9:03pm UTC](https://discourse.julialang.org/t/parameterization-of-a-set-of-constraints/132885/6 "2025-10-05T21:03:39Z")

</div>

> I tried again just now with a clean session and I don’t get that error anymore.

Great! It’s likely that you had some mistake, where either `Nel_Mese` or `ng` wasn’t defined properly. The full error message would have told you what the problem is.

---

<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:** [October 5, 2025, 9:06pm UTC](https://discourse.julialang.org/t/parameterization-of-a-set-of-constraints/132885/7 "2025-10-05T21:06:18Z")

</div>

is there a daily log in vscode by any chance?

---

<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:** [October 5, 2025, 9:19pm UTC](https://discourse.julialang.org/t/parameterization-of-a-set-of-constraints/132885/8 "2025-10-05T21:19:35Z")

</div>

Not sure sorry. I’d guess not. If it works now I wouldnt worry about spending time trying to figure out what went wrong. Just wait until it happens again

---

<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:** [October 5, 2025, 9:21pm UTC](https://discourse.julialang.org/t/parameterization-of-a-set-of-constraints/132885/9 "2025-10-05T21:21:42Z")

</div>

In any case, I learned, for future use, that I need to save the complete log, in case of errors. 😄
