UndefVarError: t not defined

function DR_Model_MPEC(J,T,W,PS_tw,D_jtw,delta_jcmax,C_t,sigma_w,M_S,M_P,M_C,M_T)
    m = Model(Gurobi.Optimizer)
    
    # Variables definition
    #################################
    
   @variables(m, begin 
     Delta_jtw_c[j=1:J,t=1:T,w=1:W]  
     delta_tw[t=1:T,w=1:W]             
     P_t[t=1:T]                        
     y_tw[t=1:T,w=1:W]                 
     q_jtw[j=1:J,t=1:T,w=1:W]          
     qS_tw[t=1:T,w=1:W]                
     alpha_tw_p[t=1:T,w=1:W]>=0       
     alpha_tw_m[t=1:T,w=1:W]>=0       
     beta_tw[t=1:T,w=1:W]>=0          
     epsilon_tw[t=1:T,w=1:W]           
     lambda_w[w=1:W]                   
     gamma_t[t=1:T]>=0                 
     mu_tw[t=1:T,w=1:W]>=0            
     theta_tw[t=1:T,w=1:W]>=0         
     nu_tj_Cmax[j=1:J,t=1:T]>=0       
     nu_tj_Cmin[j=1:J,t=1:T]>=0        
     X_jw[j=1:J,w=1:W]                
    
     
    #Binary variables
    ##############################
    
      psi_tj_C_min[t=1:T,j=1:J], Bin
      psi_tj_C_max[t=1:T,j=1:J], Bin
      tau_tw_q[t=1:T,w=1:W], Bin
      tau_tw_y[t=1:T,w=1:W], Bin
      tau_tw_p[t=1:T,w=1:W], Bin  
      tau_tw_m[t=1:T,w=1:W], Bin
      tau_t_P[t=1:T], Bin
end)
    
    
  @objective(m, Max, sum(sigma_w[w]*(sum(X_jw[j,w] for j=1:J)-sum(PS_tw[t,w]*qS_tw[t,w]
                    for t=1:T)-C_t[t]*y_tw[t,w]) for w=1:W))
  @constraint(m, sum(q_jtw[j,t,w] for j=1:J)-qS_tw[t,w] == delta_tw[t,w])
  @constraint(m, delta_tw[t,w] <= y_tw[t,w])
  @constraint(m, -delta_tw[t,w] <= y_tw[t,w])
  @constraint(m, qS_tw[t,w][t,w] >= 0)
  @constraint(m, y_tw[t,w] >= 0)
  @constraint(m, P_t[t] >= 0)
  @constraint(m, P_t[t]- epsilon_tw[t,w]== 0)
  @constraint(m, -epsilon_tw[t,w]-nu_tj_Cmin[j,t] + nu_tj_Cmax[j,t]== 0)
  @constraint(m, q_jtw[j,t,w] + Delta_jtw_c[j,t,w]== D_jtw[j,t,w])
  @constraint(m, sum(Delta_jtw_c[j,t,w] for t=1:T)== 0)
  @constraint(m, X_jw[j,w]==sum(nu_tj_Cmax[j,t]*D_jtw[j,t,w] for t=1:T) -sum(nu_tj_Cmax[j,t] for t=1:T)*delta_jcmax[j] 
-sum(nu_tj_Cmin[j,t]*D_jtw[j,t,w] for t=1:T)+ sum(nu_tj_Cmax[j,t] for t=1:T)*delta_jcmax[j])
  #@constraint(m, X_jw[j,w]==sum(nu_tj_Cmax[j,t]*D_jtw[j,t,w] -nu_tj_Cmax[j,t]*delta_jcmax[j] -nu_tj_Cmin[j,t]*D_jtw[j,t,w]+ nu_tj_Cmax[j,t]*delta_jcmax[j]  for t=1:T))  
        
        # Linear expressions
        ##############################
   @constraint(m, Delta_jtw_c[j,t,w] + delta_jcmax[j] >= 0)
   @constraint(m, delta_jcmax[j]- Delta_jtw_c[j,t,w]  >= 0)
   @constraint(m, nu_tj_Cmin[j,t] >= 0)
   @constraint(m, nu_tj_Cmax[j,t] >= 0)
   @constraint(m, Delta_jtw_c[j,t,w] + delta_jcmax[j] <= (1 - psi_tj_C_min[j,t])*M_C)
   @constraint(m, delta_jcmax[j]- Delta_jtw_c[j,t,w]  <= (1 - psi_tj_C_max[j,t])*M_C)
   @constraint(m, nu_tj_Cmax[j,t] <= psi_tj_C_max[j,t]*M_P)
   @constraint(m, nu_tj_Cmin[j,t] <= psi_tj_C_min[j,t]*M_P)
    
    print(m)
        
     @time status = JuMP.optimize!(m)
    return JuMP.objective_value(m), JuMP.value.(Delta_jtw_c), JuMP.value.(delta_tw),JuMP.value.(P_t),JuMP.value.(y_tw),JuMP.value.(q_jtw),JuMP.value.(X_jw),JuMP.value.(qS_tw),status
    
    
end    
(obj,Delta_jtw_c,delta_tw,P_t,y_tw,q_jtw,X_jw,qS_tw,status) = DR_Model_MPEC(J,T,W,PS_tw,D_jtw,delta_jcmax,C_t,sigma_w,M_S,M_P,M_C,M_T)


UndefVarError: t not defined

Stacktrace:
 [1] macro expansion at ....
 [3] DR_Model_MPEC(::Int64, ::Int64, ::Int64, ::Array{Float64,2}, ::Array{Float64,3}, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Int64, ::Int64, ::Int64, ::Int64) at .\In[11]:40
 [4] top-level scope at In[17]:1
 [5] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
 [6] execute_code(::String, ::String) at C:\Users\Abate\.julia\packages\IJulia\rWZ9e\src\execute_request.jl:27
 [7] execute_request(::ZMQ.Socket, ::IJulia.Msg) at C:\Users\Abate\.julia\packages\IJulia\rWZ9e\src\execute_request.jl:86
 [8] #invokelatest#1 at .\essentials.jl:710 [inlined]
 [9] invokelatest at .\essentials.jl:709 [inlined]
 [10] eventloop(::ZMQ.Socket) at C:\Users\Abate\.julia\packages\IJulia\rWZ9e\src\eventloop.jl:8
 [11] (::IJulia.var"#15#18")() at .\task.jl:356

I try to understand why the error is in the objective function. But I do not get it. Anyone who can help me?
The parameters are defined in a separate cell.

It’s probably in the constraints

here, t and w are not defined:

@constraint(m, delta_tw[t,w] <= y_tw[t,w])

these will work:

@constraint(m, delta_tw .<= y_tw)
@constraint(m, delta_tw[1:T, 1:W] .<= y_tw[1:T, 1:W])
[@constraint(m, delta_tw[t,w] <= y_tw[t,w]) for t in 1:T for w in 1:W]
1 Like

A third option is

@constraint(m, [t=1:T, w=1:W], delta_tw[t, w] <= y_tw[t, w])

Documentation: https://jump.dev/JuMP.jl/stable/constraints/#Constraint-containers-1