Parameterization of a set of constraints

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.

    #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

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)

Hi @rocco_sprmnt21,

I can’t reproduce this:

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()?


(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.

@constraints(model, begin

...

end

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

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?

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.

1 Like

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.

is there a daily log in vscode by any chance?

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

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

1 Like