No method matching error

Hello everyone,

I’m trying to build a mathematical model to minimize the variance of 4 variables. However, when I try to compute the variance julia have returned the follow error:

ERROR: MethodError: no method matching /(::Base.Generator{Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}, UnitRange{Int64}}}, var"#354#361"{AffExpr, Array{VariableRef, 3}}}, ::Int64)

Closest candidates are:
/(::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, ::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8})
@ Base int.jl:97
/(::StridedArray{P}, ::Real) where P<:Dates.Period
@ Dates C:\Julia-1.9.1\share\julia\stdlib\v1.9\Dates\src\deprecated.jl:44
/(::Union{SparseArrays.AbstractCompressedVector{Tv, Ti}, SubArray{Tv, 1, <:SparseArrays.AbstractSparseMatrixCSC{Tv, Ti}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, false}, SubArray{Tv, 1, <:SparseArrays.AbstractSparseVector{Tv, Ti}, Tuple{Base.Slice{Base.OneTo{Int64}}}, false}} where {Tv, Ti}, ::Number)
@ SparseArrays C:\Julia-1.9.1\share\julia\stdlib\v1.9\SparseArrays\src\sparsevector.jl:1654

Can anyone help me, please?

===================
Code:

using JuMP, Gurobi
import DelimitedFiles

function solve()
#Parametros
members = 30 #Team Members
hours = 24 #Hours of the day
days = 7 #Days of the week
weeks = 4 #Weeks of the month
shifts = 13 #Shifts and Shifts Names
shifts_name = [“RRTWD_1”, “RRTWD_2”, “RRTWD_3”, “RRTWE_1”, “RRTWE_2”,
“MOCWD_1”, “MOCWD_2”, “MOCWD_3”, “MOCWE_1”, “MOCWE_2”,
“NSWD_1”, “NSWE_1”, “NSWE_2”]
resilienceScore = [ 2334.94, 4071.01, 2187.28, 465.52, 1294.25, 1804.09, 6783.05,
2656.37, 2142.94, 433.23, 1403.59, 93.33, 148.42, 174.89, 431.78,
212.36, 107.98, 433.23, 87.26, 240.4, 5785.67, 702.58, 589.05,
364.28, 825.32, 1078.85, 647.19, 1064.08, 1048.19, 483.3 ]
#Setup Model
model = Model(Gurobi.Optimizer)
set_optimizer_attribute(model, “TimeLimit”, 3600)
set_optimizer_attribute(model, “Presolve”, 1)
set_optimizer_attribute(model, “IntFeasTol”, 1e-6)
#set_optimizer_attribute(model, “LogFile”, “logGUROBI-“sheetname”.txt”)

#Decision variables
@variable(model, x[m = 1:members, s = 1:shifts, w = 1:weeks, d = 1:days], Bin)
@variable(model, avg_1[w = 1:weeks, d = 1:days, h = 1:hours] >= 0)
@variable(model, avg_2[w = 1:weeks, d = 1:days, h = 1:hours] >= 0)
@variable(model, avg_3[w = 1:weeks, d = 1:days, h = 1:hours] >= 0)
@variable(model, grand_avg[w = 1:weeks, d = 1:days, h = 1:hours] >= 0)

#Mean and Variance
@expression(model, mean_avg_1, sum(avg_1[w,d,h] for w = 1:4, d = 1:7, h = 1:24)/(4+7+24))
@expression(model, mean_avg_2, sum(avg_2[w,d,h] for w = 1:4, d = 1:7, h = 1:24)/(4+7+24))
@expression(model, mean_avg_3, sum(avg_3[w,d,h] for w = 1:4, d = 1:7, h = 1:24)/(4+7+24))
@expression(model, mean_grand_avg, sum(grand_avg[w,d,h] for w = 1:4, d = 1:7, h = 1:24)/(4+7+24))
@expression(model, variance_avg_1, ((sum((avg_1[w,d,h] - mean_avg_1)^2) for w = 1:4, d = 1:7, h = 1:24)/(4+7+23)))
@expression(model, variance_avg_2, ((sum((avg_2[w,d,h] - mean_avg_2)^2) for w = 1:4, d = 1:7, h = 1:24)/(4+7+23)))
@expression(model, variance_avg_3, ((sum((avg_3[w,d,h] - mean_avg_3)^2) for w = 1:4, d = 1:7, h = 1:24)/(4+7+23)))
@expression(model, variance_grand_avg, ((sum((grand_avg[w,d,h] - mean_grand_avg)^2) for w = 1:4, d = 1:7, h = 1:24)/(4+7+23)))

#Objective
@objective(model, Min, variance_avg_1 + variance_avg_2 + variance_avg_3 + variance_grand_avg)
                      

#Print Model
open("model.txt", "w") do f
    println(f, model)
end

#-----------------------------------------------------
println("solving mathematical model...")
optimize!(model)
status = termination_status(model)

end

Thank you

1 Like

Take a look at your brackets:

@expression(
    model,
    variance_avg_1, 
    (
        (
            sum(
                (avg_1[w,d,h] - mean_avg_1)^2
            ) for w = 1:4, d = 1:7, h = 1:24
        ) / (4+7+23)
    )
)

I’m guessing this isn’t what you want :smile:

@expression(
    model,
    variance_avg_1, 
    sum(
        (avg_1[w,d,h] - mean_avg_1)^2 for w = 1:4, d = 1:7, h = 1:24
    ) / (4+7+23)
)

You could simplify to

@expression(model, variance_avg_1, sum((avg_1 .- mean_avg_1).^2) / (4+7+23))

p.s., you can format your code with

```Julia
code
```

It might also help your debugging to try and find a minimal example that reproduces the error, which line is the problem, etc.

Lol, sorry for my mistake. Thank you very much for the answer.

Using dots before the operators is an easy way to involve all the elements of the array, isn’t it? I didn’t know this feature, thanks for the tip.

1 Like