Skip to content
This repository has been archived by the owner on Nov 23, 2018. It is now read-only.

Commit

Permalink
Rename Linesearch to Linesearcher
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-ch committed Jul 30, 2015
1 parent 50101bc commit 767212b
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 54 deletions.
17 changes: 9 additions & 8 deletions backtracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ const (
minimumBacktrackingStepSize = 1e-20
)

// Backtracking is a type that implements LinesearchMethod using a backtracking
// line search. A backtracking line search checks that the Armijo condition has
// been met with the given function constant. If the Armijo condition has not
// been met, the step size is decreased by a factor of Decrease.
// Backtracking is a Linesearcher that uses a backtracking to find a point that
// satisfies the Armijo condition with the given function constant FunConst. If
// the Armijo condition has not been met, the step size is decreased by a
// factor of Decrease.
//
// The Armijo conditions only require the gradient at the initial condition
// (not successive step locations), and so Backtracking may be a good linesearch
// method for functions with expensive gradients. Backtracking is not appropriate
// for optimizers that require the Wolfe conditions to be met, such as BFGS.
// The Armijo condition only requires the gradient at the beginning of each
// major iteration (not at successive step locations), and so Backtracking may
// be a good linesearch for functions with expensive gradients. Backtracking is
// not appropriate for optimizers that require the Wolfe conditions to be met,
// such as BFGS.
//
// Both FunConst and Decrease must be between zero and one, and Backtracking will
// panic otherwise. If either FunConst or Decrease are zero, it will be set to a
Expand Down
13 changes: 5 additions & 8 deletions bfgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
)

// BFGS implements the Method interface to perform the Broyden–Fletcher–Goldfarb–Shanno
// optimization method with the given linesearch method. If LinesearchMethod is nil,
// optimization method with the given linesearch method. If Linesearcher is nil,
// it will be set to a reasonable default.
//
// BFGS is a quasi-Newton method that performs successive rank-one updates to
// an estimate of the inverse-Hessian of the function. It exhibits super-linear
// convergence when in proximity to a local minimum. It has memory cost that is
// O(n^2) relative to the input dimension.
type BFGS struct {
Linesearch Linesearch
Linesearcher Linesearcher

ls *LinesearchHelper

Expand All @@ -38,17 +38,14 @@ type BFGS struct {
first bool // Is it the first iteration (used to set the scale of the initial hessian)
}

// NOTE: This method exists so that it's easier to use a bfgs algorithm because
// it implements Method

func (b *BFGS) Init(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
if b.Linesearch == nil {
b.Linesearch = &Bisection{}
if b.Linesearcher == nil {
b.Linesearcher = &Bisection{}
}
if b.ls == nil {
b.ls = &LinesearchHelper{}
}
b.ls.Linesearch = b.Linesearch
b.ls.Linesearcher = b.Linesearcher
b.ls.NextDirectioner = b

return b.ls.Init(loc, xNext)
Expand Down
2 changes: 1 addition & 1 deletion bisection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package optimize

import "math"

// Bisection is a LinesearchMethod that uses a bisection to find a point that
// Bisection is a Linesearcher that uses a bisection to find a point that
// satisfies the strong Wolfe conditions with the given gradient constant and
// function constant of zero. If GradConst is zero, it will be set to a reasonable
// value. Bisection will panic if GradConst is not between zero and one.
Expand Down
12 changes: 6 additions & 6 deletions cg.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ type CGVariant interface {
// gradient methods. Pacific Journal of Optimization, 2 (2006), pp. 35-58, and
// references therein.
type CG struct {
// Linesearch must satisfy the strong Wolfe conditions at every iteration.
// If Linesearch == nil, an appropriate default is chosen.
Linesearch Linesearch
// Linesearcher must satisfy the strong Wolfe conditions at every iteration.
// If Linesearcher == nil, an appropriate default is chosen.
Linesearcher Linesearcher
// Variant implements the particular CG formula for computing β_k.
// If Variant is nil, an appropriate default is chosen.
Variant CGVariant
Expand Down Expand Up @@ -102,8 +102,8 @@ func (cg *CG) Init(loc *Location, xNext []float64) (EvaluationType, IterationTyp
panic("cg: AngleRestartThreshold not in [-1, 0]")
}

if cg.Linesearch == nil {
cg.Linesearch = &Bisection{GradConst: 0.1}
if cg.Linesearcher == nil {
cg.Linesearcher = &Bisection{GradConst: 0.1}
}
if cg.Variant == nil {
cg.Variant = &HestenesStiefel{}
Expand All @@ -122,7 +122,7 @@ func (cg *CG) Init(loc *Location, xNext []float64) (EvaluationType, IterationTyp
if cg.ls == nil {
cg.ls = &LinesearchHelper{}
}
cg.ls.Linesearch = cg.Linesearch
cg.ls.Linesearcher = cg.Linesearcher
cg.ls.NextDirectioner = cg

return cg.ls.Init(loc, xNext)
Expand Down
14 changes: 7 additions & 7 deletions gradientdescent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import "github.com/gonum/floats"

// GradientDescent is a Method that performs gradient-based optimization.
// Gradient Descent performs successive steps along the direction of the
// gradient. The Linesearch specifies the kind of linesearch to be done, and
// gradient. The Linesearcher specifies the kind of linesearch to be done, and
// StepSizer determines the initial step size of each direction. If either
// Linesearch or StepSizer are nil, a reasonable value will be chosen.
// Linesearcher or StepSizer are nil, a reasonable value will be chosen.
type GradientDescent struct {
Linesearch Linesearch
StepSizer StepSizer
Linesearcher Linesearcher
StepSizer StepSizer

ls *LinesearchHelper
}
Expand All @@ -22,13 +22,13 @@ func (g *GradientDescent) Init(loc *Location, xNext []float64) (EvaluationType,
if g.StepSizer == nil {
g.StepSizer = &QuadraticStepSize{}
}
if g.Linesearch == nil {
g.Linesearch = &Backtracking{}
if g.Linesearcher == nil {
g.Linesearcher = &Backtracking{}
}
if g.ls == nil {
g.ls = &LinesearchHelper{}
}
g.ls.Linesearch = g.Linesearch
g.ls.Linesearcher = g.Linesearcher
g.ls.NextDirectioner = g

return g.ls.Init(loc, xNext)
Expand Down
4 changes: 2 additions & 2 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ type Method interface {
}
}

// Linesearch is a type that can perform a line search. It tries to find an
// Linesearcher is a type that can perform a line search. It tries to find an
// (approximate) minimum of the objective function along the search direction
// dir_k starting at the most recent location x_k, i.e., it tries to minimize a
// function
// φ(step) := f(x_k + step * dir_k), step > 0.
// Typically, these methods will not be called by the user directly, as they
// will be called by the LinesearchHelper struct.
type Linesearch interface {
type Linesearcher interface {
// Init initializes the linesearch method. Value and derivative contain
// φ(0) and φ'(0), respectively, and step contains the first trial step
// length as returned by the NextDirectioner.InitDirection(). It returns
Expand Down
14 changes: 7 additions & 7 deletions lbfgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
// better than BFGS for functions with Hessians that vary rapidly spatially.
//
// If Store is 0, Store is defaulted to 15.
// A Linesearch for LBFGS must satisfy the strong Wolfe conditions at every
// iteration. If Linesearch == nil, an appropriate default is chosen.
// A Linesearcher for LBFGS must satisfy the strong Wolfe conditions at every
// iteration. If Linesearcher == nil, an appropriate default is chosen.
type LBFGS struct {
Linesearch Linesearch
Store int // how many past iterations to store
Linesearcher Linesearcher
Store int // how many past iterations to store

ls *LinesearchHelper

Expand All @@ -39,13 +39,13 @@ type LBFGS struct {
}

func (l *LBFGS) Init(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
if l.Linesearch == nil {
l.Linesearch = &Bisection{}
if l.Linesearcher == nil {
l.Linesearcher = &Bisection{}
}
if l.ls == nil {
l.ls = &LinesearchHelper{}
}
l.ls.Linesearch = l.Linesearch
l.ls.Linesearcher = l.Linesearcher
l.ls.NextDirectioner = l
return l.ls.Init(loc, xNext)
}
Expand Down
10 changes: 5 additions & 5 deletions linesearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
// LinesearchHelper encapsulates the common functionality of gradient-based
// line-search optimization methods and serves as a helper struct for their
// implementation. It consists of a NextDirectioner, which specifies the search
// direction at each iteration, and a Linesearch which performs a linesearch
// direction at each iteration, and a Linesearcher which performs a linesearch
// along the search direction.
type LinesearchHelper struct {
NextDirectioner NextDirectioner
Linesearch Linesearch
Linesearcher Linesearcher

x []float64 // Starting point for the current iteration.
dir []float64 // Search direction for the current iteration.
Expand Down Expand Up @@ -62,7 +62,7 @@ func (ls *LinesearchHelper) Iterate(loc *Location, xNext []float64) (EvaluationT
}

projGrad := floats.Dot(loc.Gradient, ls.dir)
if ls.Linesearch.Finished(loc.F, projGrad) {
if ls.Linesearcher.Finished(loc.F, projGrad) {
copy(xNext, loc.X)
// Check if the last evaluation evaluated all fields of Location.
ls.evalType = complementEval(loc, ls.evalType)
Expand All @@ -77,7 +77,7 @@ func (ls *LinesearchHelper) Iterate(loc *Location, xNext []float64) (EvaluationT
}

// Line search not done, just iterate.
stepSize, evalType, err := ls.Linesearch.Iterate(loc.F, projGrad)
stepSize, evalType, err := ls.Linesearcher.Iterate(loc.F, projGrad)
if err != nil {
ls.evalType = NoEvaluation
ls.iterType = NoIteration
Expand Down Expand Up @@ -116,7 +116,7 @@ func (ls *LinesearchHelper) initNextLinesearch(loc *Location, xNext []float64) (
return ls.evalType, ls.iterType, ErrNonNegativeStepDirection
}

ls.evalType = ls.Linesearch.Init(loc.F, projGrad, stepSize)
ls.evalType = ls.Linesearcher.Init(loc.F, projGrad, stepSize)

floats.AddScaledTo(xNext, ls.x, stepSize, ls.dir)
// Compare the starting point for the current iteration with the next
Expand Down
14 changes: 7 additions & 7 deletions newton.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ const maxNewtonModifications = 20
// cost of its factorization is prohibitive, BFGS or L-BFGS quasi-Newton method
// can be used instead.
type Newton struct {
// Linesearch is a method used for selecting suitable steps along the
// descent direction d. Steps should satisfy at least one of the Wolfe,
// Linesearcher is used for selecting suitable steps along the descent
// direction d. Accepted steps should satisfy at least one of the Wolfe,
// Goldstein or Armijo conditions.
// If Linesearch == nil, an appropriate default is chosen.
Linesearch Linesearch
// If Linesearcher == nil, an appropriate default is chosen.
Linesearcher Linesearcher
// Increase is the factor by which a scalar tau is successively increased
// so that (H + tau*I) is positive definite. Larger values reduce the
// number of trial Hessian factorizations, but also reduce the second-order
Expand All @@ -61,13 +61,13 @@ func (n *Newton) Init(loc *Location, xNext []float64) (EvaluationType, Iteration
if n.Increase <= 1 {
panic("optimize: Newton.Increase must be greater than 1")
}
if n.Linesearch == nil {
n.Linesearch = &Bisection{}
if n.Linesearcher == nil {
n.Linesearcher = &Bisection{}
}
if n.ls == nil {
n.ls = &LinesearchHelper{}
}
n.ls.Linesearch = n.Linesearch
n.ls.Linesearcher = n.Linesearcher
n.ls.NextDirectioner = n

return n.ls.Init(loc, xNext)
Expand Down
6 changes: 3 additions & 3 deletions unconstrained_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,15 +1014,15 @@ func TestGradientDescent(t *testing.T) {

func TestGradientDescentBacktracking(t *testing.T) {
testLocal(t, gradientDescentTests, &GradientDescent{
Linesearch: &Backtracking{
Linesearcher: &Backtracking{
FunConst: 0.1,
},
})
}

func TestGradientDescentBisection(t *testing.T) {
testLocal(t, gradientDescentTests, &GradientDescent{
Linesearch: &Bisection{},
Linesearcher: &Bisection{},
})
}

Expand Down Expand Up @@ -1291,7 +1291,7 @@ func TestIssue76(t *testing.T) {
MajorIterations: 1000000,
}
m := &GradientDescent{
Linesearch: &Backtracking{},
Linesearcher: &Backtracking{},
}
// We are not interested in the error, only in the returned status.
r, _ := Local(p, x, s, m)
Expand Down

0 comments on commit 767212b

Please sign in to comment.