# MethodError: no method matching zero(::Type{Vector{AffExpr}})

**URL:** <https://discourse.julialang.org/t/methoderror-no-method-matching-zero-type-vector-affexpr/105639>\
**Category:** Optimization (Mathematical)\
**Created:** [November 1, 2023, 2:31am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-zero-type-vector-affexpr/105639 "2023-11-01T02:31:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaiming\_Zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kaiming_zhang/32/202862_2.png) [@Kaiming\_Zhang](https://discourse.julialang.org/u/Kaiming_Zhang)\
**Post date:** [November 1, 2023, 2:31am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-zero-type-vector-affexpr/105639/1 "2023-11-01T02:31:39Z")

</div>

I met a method error when running code in Julia 1.9. The code was written in Julia 1.1. How should I modify the code so that it can work?

```julia
{
	"name": "MethodError",
	"message": "MethodError: no method matching zero(::Type{Vector{AffExpr}})

Closest candidates are:
  zero(!Matched::Union{Type{P}, P}) where P<:Dates.Period
   @ Dates C:\\Users\\MNRG\\AppData\\Local\\Programs\\Julia-1.9.3\\share\\julia\\stdlib\\v1.9\\Dates\\src\\periods.jl:51
  zero(!Matched::GenericQuadExpr)
   @ JuMP C:\\Users\\MNRG\\.julia\\packages\\JuMP\\mvUVO\\src\\quad_expr.jl:115
  zero(!Matched::ForwardDiff.Dual)
   @ ForwardDiff C:\\Users\\MNRG\\.julia\\packages\\ForwardDiff\\PcZ48\\src\\dual.jl:357
  ...
",
	"stack": "MethodError: no method matching zero(::Type{Vector{AffExpr}})

Closest candidates are:
  zero(!Matched::Union{Type{P}, P}) where P<:Dates.Period
   @ Dates C:\\Users\\MNRG\\AppData\\Local\\Programs\\Julia-1.9.3\\share\\julia\\stdlib\\v1.9\\Dates\\src\\periods.jl:51
  zero(!Matched::GenericQuadExpr)
   @ JuMP C:\\Users\\MNRG\\.julia\\packages\\JuMP\\mvUVO\\src\\quad_expr.jl:115
  zero(!Matched::ForwardDiff.Dual)
   @ ForwardDiff C:\\Users\\MNRG\\.julia\\packages\\ForwardDiff\\PcZ48\\src\\dual.jl:357
  ...

Stacktrace:
  [1] _instantiate_zero(#unused#::Type{Vector{AffExpr}})
    @ MutableArithmetics C:\\Users\\MNRG\\.julia\\packages\\MutableArithmetics\\cnvbo\\src\\interface.jl:28
  [2] promote_operation_fallback(op::typeof(+), #unused#::Type{Vector{AffExpr}}, #unused#::Type{VariableRef})
    @ MutableArithmetics C:\\Users\\MNRG\\.julia\\packages\\MutableArithmetics\\cnvbo\\src\\interface.jl:51
  [3] promote_operation(::typeof(+), ::Type, ::Type)
    @ MutableArithmetics C:\\Users\\MNRG\\.julia\\packages\\MutableArithmetics\\cnvbo\\src\\interface.jl:113
  [4] promote_operation_fallback(op::typeof(MutableArithmetics.add_mul), T::Type, args::Type)
    @ MutableArithmetics C:\\Users\\MNRG\\.julia\\packages\\MutableArithmetics\\cnvbo\\src\\interface.jl:99
  [5] promote_operation(::typeof(MutableArithmetics.add_mul), ::Type, ::Type)
    @ MutableArithmetics C:\\Users\\MNRG\\.julia\\packages\\MutableArithmetics\\cnvbo\\src\\interface.jl:113
  [6] mutability(::Type, ::Function, ::Type, ::Type)
    @ MutableArithmetics C:\\Users\\MNRG\\.julia\\packages\\MutableArithmetics\\cnvbo\\src\\interface.jl:266
  [7] mutability(::Vector{AffExpr}, ::Function, ::Vector{AffExpr}, ::VariableRef)
    @ MutableArithmetics C:\\Users\\MNRG\\.julia\\packages\\MutableArithmetics\\cnvbo\\src\\interface.jl:274
  [8] operate!!(op::typeof(MutableArithmetics.add_mul), x::Vector{AffExpr}, args::VariableRef)
    @ MutableArithmetics C:\\Users\\MNRG\\.julia\\packages\\MutableArithmetics\\cnvbo\\src\\rewrite.jl:93
  [9] macro expansion
    @ C:\\Users\\MNRG\\.julia\\packages\\MutableArithmetics\\cnvbo\\src\\rewrite.jl:326 [inlined]
 [10] macro expansion
    @ C:\\Users\\MNRG\\.julia\\packages\\JuMP\\mvUVO\\src\\macros.jl:1080 [inlined]
 [11] cvxrs(network_data::Dict{String, Any}, option::String, target_network_data::Nothing, phase_shift::Bool)
    @ Main c:\\Users\\MNRG\\Desktop\\PowerFlowCVXRS-master\\src\\main copy.jl:608
 [12] scrs(network_data::Dict{String, Any}, max_iter_SCRS::Int64, phase_shift::Bool)
    @ Main c:\\Users\\MNRG\\Desktop\\PowerFlowCVXRS-master\\src\\main copy.jl:824
 [13] scrs(network_data::Dict{String, Any}, max_iter_SCRS::Int64)
    @ Main c:\\Users\\MNRG\\Desktop\\PowerFlowCVXRS-master\\src\\main copy.jl:814
 [14] top-level scope
    @ c:\\Users\\MNRG\\Desktop\\PowerFlowCVXRS-master\\examples\\example.ipynb:11"
}

```

---

<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:** [November 1, 2023, 3:19am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-zero-type-vector-affexpr/105639/2 "2023-11-01T03:19:07Z")

</div>

Hi @Kaiming_Zhang, welcome to the forum!

Do you have a reproducible example of how you encountered this error?

---

<div class="post-metadata">

**Author:** ![Kaiming\_Zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kaiming_zhang/32/202862_2.png) [@Kaiming\_Zhang](https://discourse.julialang.org/u/Kaiming_Zhang)\
**Post date:** [November 1, 2023, 6:28am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-zero-type-vector-affexpr/105639/3 "2023-11-01T06:28:54Z")

</div>

The code is available at [GitHub - dclee131/PowerFlowCVXRS: Solving Optimal Power Flow with Convex Restriction](https://github.com/dclee131/PowerFlowCVXRS). And I met this problem when running the below code in **examples/example.ipynb**

```julia
network_data, result_cvxr = scrs(network_data, max_iter_SCRS); # run sequential convex restriction

```

 ![20231101143628](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed0f428d6bf0ac2afad77cf02aeb9bdacfa7f0da.jpeg)

---

<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:** [November 1, 2023, 6:49am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-zero-type-vector-affexpr/105639/4 "2023-11-01T06:49:19Z")

</div>

This code was written at least three years ago, and it doesn’t have a `Project.toml` that declares which version of JuMP it supports.

What is `import Pkg; Pkg.status()`?

You’ll need to make sure that you’re using the appropriate version of the required packages, but that’s quite hard (if not impossible) to figure out.

From your error, it looks like line 608 is the problem:

> <https://github.com/dclee131/PowerFlowCVXRS/blob/7f5bb658d6d648ec88e6d7fac1467c20fd86eb75/src/main.jl#L608>

You probably need something like:

```julia
@constraint(m_cvx, pg_opt .+ alpha0 .* deltaᵘ .+ slack_pg .<= pg_max)

```

Note the extra `.` before the `+` operators.

---

<div class="post-metadata">

**Author:** ![Kaiming\_Zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kaiming_zhang/32/202862_2.png) [@Kaiming\_Zhang](https://discourse.julialang.org/u/Kaiming_Zhang)\
**Post date:** [November 1, 2023, 7:23am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-zero-type-vector-affexpr/105639/5 "2023-11-01T07:23:50Z")

</div>

Thank you so much!

The package versions are listed as below:

```julia
julia> import Pkg; Pkg.status()
Status `C:\Users\MNRG\.julia\environments\v1.9\Project.toml`
  [336ed68f] CSV v0.10.11
⌃ [aaaa29a8] Clustering v0.15.4
  [a93c6f00] DataFrames v1.6.1
⌃ [2e9cd046] Gurobi v1.0.3
⌃ [b6b21f68] Ipopt v1.4.2
⌃ [4076af6c] JuMP v1.14.1
  [b8f27783] MathOptInterface v1.21.0
  [1ec41992] MosekTools v0.15.1
  [9c9e696b] PkgServerClient v0.1.3
  [91a5bcdd] Plots v1.39.0
  [c36e90e8] PowerModels v0.19.9
  [37e2e46d] LinearAlgebra
  [2f01184e] SparseArrays
  [10745b16] Statistics v1.9.0
Info Packages marked with ⌃ have new versions available and may be upgradable.

```

And I add extra `.` before every `+` and `-` operator. It finally works. Here are the results.

```julia
Initial OPF objective solution: 5298.33
Iteration 1: 5298.12 5298.216 OPTIMAL 0.0003 0.0027
Iteration 2: 5297.94 5298.028 OPTIMAL 0.0001 0.0022
Iteration 3: 5297.79 5297.862 OPTIMAL 0.0001 0.0019
Iteration 4: 5297.64 5297.711 OPTIMAL 0.0001 0.0017
Iteration 5: 5297.5 5297.567 OPTIMAL 0.0001 0.0016
Iteration 6: 5297.36 5297.43 OPTIMAL 0.0001 0.0015
Iteration 7: 5297.23 5297.295 OPTIMAL 0.0001 0.0014
Iteration 8: 5297.1 5297.163 OPTIMAL 0.0001 0.0014
Iteration 9: 5296.97 5297.033 OPTIMAL 0.0001 0.0013
Iteration 10: 5296.9 5296.923 OPTIMAL 0.0001 0.0012

```

But I still wonder why it works to add extra `.` before the `+` operators. Could you please give me an answer?

---

<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:** [November 1, 2023, 7:47am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-zero-type-vector-affexpr/105639/6 "2023-11-01T07:47:19Z")

</div>

> Could you please give me an answer?

You are using JuMP v1.14.1. The original authors can have been using at most v0.21.2. A lot of things changed between then and now, but the good news is that since JuMP v1.0 in March 2022, there are no longer any breaking changes.

I’ve opened an issue to provide a nicer error message in future: [Provide better error message for Vector+VariableRef · Issue #3554 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/issues/3554)
