# No method matching error

**URL:** <https://discourse.julialang.org/t/no-method-matching-error/111044>\
**Category:** Optimization (Mathematical)\
**Created:** [March 2, 2024, 7:18am UTC](https://discourse.julialang.org/t/no-method-matching-error/111044 "2024-03-02T07:18:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IgorESMelo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/igoresmelo/32/204760_2.png) [@IgorESMelo](https://discourse.julialang.org/u/IgorESMelo)\
**Post date:** [March 2, 2024, 7:18am UTC](https://discourse.julialang.org/t/no-method-matching-error/111044/1 "2024-03-02T07:18:42Z")

</div>

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

---

<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:** [March 2, 2024, 7:31am UTC](https://discourse.julialang.org/t/no-method-matching-error/111044/2 "2024-03-02T07:31:11Z")

</div>

Take a look at your brackets:

```julia
@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 😄

```julia
@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

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

```

p.s., you can format your code with

````julia
```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.

---

<div class="post-metadata">

**Author:** ![IgorESMelo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/igoresmelo/32/204760_2.png) [@IgorESMelo](https://discourse.julialang.org/u/IgorESMelo)\
**Post date:** [March 2, 2024, 8:30am UTC](https://discourse.julialang.org/t/no-method-matching-error/111044/3 "2024-03-02T08:30:55Z")

</div>

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.
