gpt4 book ai didi

julia - 精确线搜索算法

转载 作者:行者123 更新时间:2023-12-04 07:37:48 24 4
gpt4 key购买 nike

我正在尝试在 Julia 中实现一个简单的行搜索算法。我是 Julia 编程的新手,所以我正在学习它。如果可能的话,我想寻求一些帮助来纠正运行代码时的错误。
源代码 .

using LinearAlgebra

function bracket_minimum(f, x = 0, s = 1e-2, k = 2.0)
a, fa = x, f(x)
b, fb = x + s, f(x + s)

if(fb > fa)
a, b = b, a
fa, fb = fb, fa
s = -s
end

while(true)
c, fc = b + s, f(b + s)
if(fb < fc)
return a < c ? (a, c) : (c, a)
else
a, fa, b, fb = b, fb, c, fc
s *= k
end
end
end

function bisection(f, a₀, b₀, ϵ)

function D(f,a)
# Approximate the first derivative using central differences
h = 0.001
return (f(a + h) - f(a - h))/(2 * h)
end

a = a₀
b = b₀

while((b - a) > ϵ)
c = (a + b)/2.0

if D(f,c) > 0
b = c
else
a = c
end
end

return (a,b)
end

function line_search(f::Function, x::Vector{Float64}, d::Vector{Float64})
println("Hello")
objective = α -> f(x + α*d)
a, b = bracket_minimum(objective)
α = bisection(objective, a, b, 1e-5)
return α, x + α*d
end

f(x) = sin(x[1] * x[2]) + exp(x[2] + x[3]) - x[3]

x = [1,2,3]
d = [0, -1, -1]
α, x_min = line_search(f, x, d)
我得到一个线性代数错误,所以我想我一定没有正确传递向量,或者我没有正确地进行标量向量乘法。但是,我很难弄清楚。如果我单步执行代码,它会在函数调用 line_search(f,x,d) 上失败甚至不进入函数体内部。
错误描述。
ERROR: MethodError: no method matching *(::Tuple{Float64,Float64}, ::Array{Int64,1})
Closest candidates are:
*(::Any, ::Any, ::Any, ::Any...) at operators.jl:538
*(::Adjoint{var"#s828",var"#s8281"} where var"#s8281"<:(AbstractArray{T,1} where T) where var"#s828"<:Number, ::AbstractArray{var"#s827",1} where var"#s827"<:Number) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\adjtrans.jl:283
*(::Transpose{T,var"#s828"} where var"#s828"<:(AbstractArray{T,1} where T), ::AbstractArray{T,1}) where T<:Real at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\adjtrans.jl:284

最佳答案

这是代码中的一个修复程序(我已经清理了几个文体,但关键问题是您的 bisection 返回了一个元组而不是值 - 我已将其更改为返回括号间隔的中心):

function bracket_minimum(f, x = 0.0, s = 1e-2, k = 2.0)
a, fa = x, f(x)
b, fb = x + s, f(x + s)

if fb > fa
a, b = b, a
fa, fb = fb, fa
s = -s
end

while true
s *= k
c, fc = b + s, f(b + s)
if fb < fc
return minmax(a, c)
else
a, fa, b, fb = b, fb, c, fc
end
end
end

function bisection(f, a₀, b₀, ϵ)

function D(f, a)
# Approximate the first derivative using central differences
h = 0.001
return (f(a + h) - f(a - h)) / (2 * h)
end

a = a₀
b = b₀

while (b - a) > ϵ
c = (a + b) / 2.0

if D(f, c) > 0
b = c
else
a = c
end
end

return (a + b) / 2 # this was changed
end

function line_search(f::Function, x::Vector{Float64}, d::Vector{Float64})
@assert length(x) == length(d)
objective(α) = f(x .+ α .* d)
a, b = bracket_minimum(objective)
α = bisection(objective, a, b, 1e-5)
return α, x .+ α .* d
end

f(x) = sin(x[1] * x[2]) + exp(x[2] + x[3]) - x[3]

x = [1.0, 2.0, 3.0]
d = [0.0, -1.0, -1.0]
α, x_min = line_search(f, x, d)
我不是在评论算法,因为我假设您将其作为编程练习来编写,并且您不是在尝试编写最快和最健壮的算法。

关于julia - 精确线搜索算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67647216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com