我正在尝试运行 5 年前的 Julia 代码来计算线性回归。它包含以下约束:
# y::Vector{Float64} of size 122
# X::Matrix{Float64} of size 122x122
# beta::Vector{VariableRef} of size 122
# t::VariableRef
@constraint(m, norm(y - X * beta) <= t)
这段代码当时应该是正确的,因为它是在实际工作中用作示例的。但是,当我现在运行它时,
norm
函数返回以下错误:
ERROR: StackOverflowError:
Stacktrace:
[1] norm2(x::AffExpr)
@ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:558
[2] norm(itr::AffExpr, p::Int64)
@ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:627
[3] norm(itr::AffExpr)
@ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:625
[4] generic_normInf(x::AffExpr)
@ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:466
[5] normInf(x::AffExpr)
@ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:556
[6] generic_norm2(x::AffExpr)
@ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:497
--- the last 6 lines are repeated 13329 more times ---
[79981] norm2(x::AffExpr)
@ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:558
[79982] norm(itr::AffExpr, p::Int64)
@ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:627
[79983] norm(itr::AffExpr)
@ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:625
我认为该错误是由于过去 5 年中范数函数的变化造成的,但我不知道如何解决这个问题......你知道如何让它发挥作用吗?
编辑:当规范的输入不是
Number
(source)类型时,似乎会发生这种情况。但这并不能解决我的问题,因为我仍在寻找 @constraint
上下文中范数函数的替代品。
可以通过添加这个功能来完成:
function normP(v::Vector{AffExpr}; p::Int=2)
return sum(v[i]^p for i in 1:length(v))
end