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"
}
odow
November 1, 2023, 3:19am
2
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
odow
November 1, 2023, 6:49am
4
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:
@constraint(m_cvx, ψᵘ_vv.>=vᵘ.^2)
@constraint(m_cvx, ψᵘ_vv.>=vˡ.^2)
@constraint(m_cvx, ψˡ_vv.<=ψ_vv0+2*v0.*Δvᵘ)
@constraint(m_cvx, ψˡ_vv.<=ψ_vv0+2*v0.*Δvˡ)
@constraint(m_cvx, gᵘ_pinj.>=Cg*pg_opt-Cl*pl_opt)
@constraint(m_cvx, gˡ_pinj.<=Cg*pg_opt-Cl*pl_opt)
@constraint(m_cvx, alpha_opt.==alpha0)
@constraint(m_cvx,pg_opt+alpha0.*deltaᵘ+slack_pg.<=pg_max)
@constraint(m_cvx,pg_opt+alpha0.*deltaˡ-slack_pg.>=pg_min)
zeta=sqrt.(sum((Cl[idx_pvs,:]*Diagonal(power_factor)*Matrix(cholesky(Σ0).U)).^2,dims=2))
@constraint(m_cvx,M_ineq_minus*[zeros(num_bus); Cg*qg_max-Cl*ql_opt; ψᵘ_vvcos; ψᵘ_vvsin; ψᵘ_vv]+M_ineq_plus*[zeros(num_bus); Cg*qg_max-Cl*ql0; ψˡ_vvcos; ψˡ_vvsin; ψˡ_vv]-slack_qg-zeta.*γ_opt.>=0)
@constraint(m_cvx,M_ineq_plus*[zeros(num_bus); Cg*qg_min-Cl*ql_opt; ψᵘ_vvcos; ψᵘ_vvsin; ψᵘ_vv]+M_ineq_minus*[zeros(num_bus); Cg*qg_min-Cl*ql0; ψˡ_vvcos; ψˡ_vvsin; ψˡ_vv]+slack_qg+zeta.*γ_opt.<=0)
@constraint(m_cvx,M_line_plus*[zeros(2*num_bus); ψᵘ_vvcos; ψᵘ_vvsin; ψᵘ_vv]+M_line_minus*[zeros(2*num_bus); ψˡ_vvcos; ψˡ_vvsin; ψˡ_vv].<=[p_line_frᵘ; p_line_toᵘ; q_line_frᵘ; q_line_toᵘ])
@constraint(m_cvx,-M_line_minus*[zeros(2*num_bus); ψᵘ_vvcos; ψᵘ_vvsin; ψᵘ_vv]-M_line_plus*[zeros(2*num_bus); ψˡ_vvcos; ψˡ_vvsin; ψˡ_vv].<=[p_line_frᵘ; p_line_toᵘ; q_line_frᵘ; q_line_toᵘ])
@constraint(m_cvx,p_line_frᵘ.^2+q_line_frᵘ.^2+slack_line.<=s_line_max.^2);
@constraint(m_cvx,p_line_toᵘ.^2+q_line_toᵘ.^2+slack_line.<=s_line_max.^2);
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
odow
November 1, 2023, 7:47am
6
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