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

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?

{
	"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"
}

Hi @Kaiming_Zhang, welcome to the forum!

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

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

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

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:

You probably need something like:

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

Note the extra . before the + operators.

1 Like

Thank you so much!

The package versions are listed as below:

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.

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?

1 Like

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

1 Like