Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backtracking line search #2

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ uuid = "87fe0de2-c867-4266-b59a-2f0a94fc965b"
authors = ["Vaibhav Dixit <[email protected]> and contributors"]
version = "1.0.0-DEV"

[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
julia = "1.9"

Expand Down
58 changes: 58 additions & 0 deletions src/Backtracking.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#=
Backtracking line search implementation
=#

#=
Input parameters:
- x: current iterate
- g: gradient
- α: initial step size
- p: descent direction
- f: function evaluation
=#

mutable struct Backtracking{X, F, A}
x::X
g::X
p::X
f::F
α::A
end

# Determine step size by backtracking line search
function Backtracking(backtrack::Backtracking)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function Backtracking(backtrack::Backtracking)
function Backtracking(;x, g, α, p , f)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this should be a constructor for the method

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel confused about this comment. In the function Backtracking, there are five arguments including f. Here f is a function. A function cannot be an argument of a function. That is where I feel confused.

# Setup (Function information)
x = backtrack.x
g = backtrack.g
α = backtrack.α
p = backtrack.p
f = backtrack.f

# Setup (Parameters)
c1 = 1e-4
β = 0.5
iterations = 1_000

ϕ_0 = f(x)
ϕ_α = f(x + α*p)
iteration = 0

# Backtracking line search
while ϕ_α > ϕ_0 + c1 * α * g * p
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this oput to a separate function that takes as argument the function and a Backtracking object

# Increment the number of steps we've had to perform
iteration += 1

# Ensure termination
if iteration > iterations
throw(LineSearchException("Linesearch failed to converge, reached maximum iterations $(iterations).", α))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't throw an error, return NaN

end

# Decrease the step-size
α = β * α

# Update function value at new iterate
ϕ_α = f(x + α*p)
end

return α
end
6 changes: 5 additions & 1 deletion src/LineSearch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module LineSearch

# Write your package code here.

end
include("Backtracking.jl")

export Backtracking

end
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,21 @@ using Test

@testset "LineSearch.jl" begin
# Write your tests here.

# Test1: Quadratic function
f(u) = u*u
x = 1
g = 2
p = -g
α = 1

# Create a Backtracking object
my_b = Backtracking(x, g, p, f, α)
α = Backtracking(my_b)

print(α)

# Test alpha
@test α == 0.5
@test 0 == 0
end
Loading