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

Commit

Permalink
Merge IterationType and EvaluationType, simplify Method interface
Browse files Browse the repository at this point in the history
Resolves #103
  • Loading branch information
vladimir-ch committed Sep 14, 2015
1 parent 0c9c657 commit 37b6118
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 254 deletions.
6 changes: 3 additions & 3 deletions backtracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Backtracking struct {
initG float64
}

func (b *Backtracking) Init(f, g float64, step float64) EvaluationType {
func (b *Backtracking) Init(f, g float64, step float64) RequestType {
if step <= 0 {
panic("backtracking: bad step size")
}
Expand Down Expand Up @@ -64,10 +64,10 @@ func (b *Backtracking) Finished(f, _ float64) bool {
return ArmijoConditionMet(f, b.initF, b.initG, b.stepSize, b.FuncConst)
}

func (b *Backtracking) Iterate(_, _ float64) (float64, EvaluationType, error) {
func (b *Backtracking) Iterate(_, _ float64) (float64, RequestType, error) {
b.stepSize *= b.Decrease
if b.stepSize < minimumBacktrackingStepSize {
return 0, NoEvaluation, ErrLinesearchFailure
return 0, NoRequest, ErrLinesearchFailure
}
return b.stepSize, FuncEvaluation, nil
}
8 changes: 4 additions & 4 deletions bfgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type BFGS struct {
first bool // Is it the first iteration (used to set the scale of the initial hessian)
}

func (b *BFGS) Init(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
func (b *BFGS) Init(loc *Location) (RequestType, error) {
if b.Linesearcher == nil {
b.Linesearcher = &Bisection{}
}
Expand All @@ -49,11 +49,11 @@ func (b *BFGS) Init(loc *Location, xNext []float64) (EvaluationType, IterationTy
b.ls.Linesearcher = b.Linesearcher
b.ls.NextDirectioner = b

return b.ls.Init(loc, xNext)
return b.ls.Init(loc)
}

func (b *BFGS) Iterate(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
return b.ls.Iterate(loc, xNext)
func (b *BFGS) Iterate(loc *Location) (RequestType, error) {
return b.ls.Iterate(loc)
}

func (b *BFGS) InitDirection(loc *Location, dir []float64) (stepSize float64) {
Expand Down
10 changes: 5 additions & 5 deletions bisection.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Bisection struct {
maxGrad float64
}

func (b *Bisection) Init(f, g float64, step float64) EvaluationType {
func (b *Bisection) Init(f, g float64, step float64) RequestType {
if step <= 0 {
panic("bisection: bad step size")
}
Expand Down Expand Up @@ -73,7 +73,7 @@ func (b *Bisection) Finished(f, g float64) bool {
return false
}

func (b *Bisection) Iterate(f, g float64) (float64, EvaluationType, error) {
func (b *Bisection) Iterate(f, g float64) (float64, RequestType, error) {
// Deciding on the next step size
if math.IsInf(b.maxStep, 1) {
// Have not yet bounded the minimum
Expand Down Expand Up @@ -135,10 +135,10 @@ func (b *Bisection) Iterate(f, g float64) (float64, EvaluationType, error) {
// both of which indicate the minimization must stop. If the steps are different,
// it sets the new step size and returns the step and evaluation type. If the steps
// are the same, it returns an error.
func (b *Bisection) checkStepEqual(newStep float64, e EvaluationType) (float64, EvaluationType, error) {
func (b *Bisection) checkStepEqual(newStep float64, r RequestType) (float64, RequestType, error) {
if b.currStep == newStep {
return b.currStep, NoEvaluation, ErrLinesearchFailure
return b.currStep, NoRequest, ErrLinesearchFailure
}
b.currStep = newStep
return newStep, e, nil
return newStep, r, nil
}
8 changes: 4 additions & 4 deletions cg.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type CG struct {
gradPrevNorm float64
}

func (cg *CG) Init(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
func (cg *CG) Init(loc *Location) (RequestType, error) {
if cg.IterationRestartFactor < 0 {
panic("cg: IterationRestartFactor is negative")
}
Expand Down Expand Up @@ -125,11 +125,11 @@ func (cg *CG) Init(loc *Location, xNext []float64) (EvaluationType, IterationTyp
cg.ls.Linesearcher = cg.Linesearcher
cg.ls.NextDirectioner = cg

return cg.ls.Init(loc, xNext)
return cg.ls.Init(loc)
}

func (cg *CG) Iterate(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
return cg.ls.Iterate(loc, xNext)
func (cg *CG) Iterate(loc *Location) (RequestType, error) {
return cg.ls.Iterate(loc)
}

func (cg *CG) InitDirection(loc *Location, dir []float64) (stepSize float64) {
Expand Down
8 changes: 4 additions & 4 deletions gradientdescent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type GradientDescent struct {
ls *LinesearchMethod
}

func (g *GradientDescent) Init(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
func (g *GradientDescent) Init(loc *Location) (RequestType, error) {
if g.StepSizer == nil {
g.StepSizer = &QuadraticStepSize{}
}
Expand All @@ -31,11 +31,11 @@ func (g *GradientDescent) Init(loc *Location, xNext []float64) (EvaluationType,
g.ls.Linesearcher = g.Linesearcher
g.ls.NextDirectioner = g

return g.ls.Init(loc, xNext)
return g.ls.Init(loc)
}

func (g *GradientDescent) Iterate(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
return g.ls.Iterate(loc, xNext)
func (g *GradientDescent) Iterate(loc *Location) (RequestType, error) {
return g.ls.Iterate(loc)
}

func (g *GradientDescent) InitDirection(loc *Location, dir []float64) (stepSize float64) {
Expand Down
10 changes: 5 additions & 5 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ package optimize
type Method interface {
// Init initializes the method and stores the first location to evaluate
// in xNext.
Init(loc *Location, xNext []float64) (EvaluationType, IterationType, error)
Init(loc *Location) (RequestType, error)

// Iterate performs one iteration of the method and stores the next
// location to evaluate in xNext.
Iterate(loc *Location, xNext []float64) (EvaluationType, IterationType, error)
Iterate(loc *Location) (RequestType, error)

// Needs specifies information about the objective function needed by the
// optimizer beyond just the function value. The information is used
Expand All @@ -36,7 +36,7 @@ type Linesearcher interface {
// φ(0) and φ'(0), respectively, and step contains the first trial step
// length. It returns the type of evaluation to be performed at
// x_0 + step * dir_0.
Init(value, derivative float64, step float64) EvaluationType
Init(value, derivative float64, step float64) RequestType

// Finished takes in the values of φ and φ' evaluated at the previous step,
// and returns whether a sufficiently accurate minimum of φ has been found.
Expand All @@ -45,7 +45,7 @@ type Linesearcher interface {
// Iterate takes in the values of φ and φ' evaluated at the previous step
// and returns the next step size and the type of evaluation to be
// performed at x_k + step * dir_k.
Iterate(value, derivative float64) (step float64, e EvaluationType, err error)
Iterate(value, derivative float64) (step float64, r RequestType, err error)
}

// NextDirectioner implements a strategy for computing a new line search
Expand Down Expand Up @@ -84,5 +84,5 @@ type Statuser interface {
// the progress to StdOut or to a log file. A Recorder must not modify any data.
type Recorder interface {
Init() error
Record(*Location, EvaluationType, IterationType, *Stats) error
Record(*Location, RequestType, *Stats) error
}
8 changes: 4 additions & 4 deletions lbfgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type LBFGS struct {
rhoHist []float64 // last Store iterations of rho
}

func (l *LBFGS) Init(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
func (l *LBFGS) Init(loc *Location) (RequestType, error) {
if l.Linesearcher == nil {
l.Linesearcher = &Bisection{}
}
Expand All @@ -47,11 +47,11 @@ func (l *LBFGS) Init(loc *Location, xNext []float64) (EvaluationType, IterationT
}
l.ls.Linesearcher = l.Linesearcher
l.ls.NextDirectioner = l
return l.ls.Init(loc, xNext)
return l.ls.Init(loc)
}

func (l *LBFGS) Iterate(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
return l.ls.Iterate(loc, xNext)
func (l *LBFGS) Iterate(loc *Location) (RequestType, error) {
return l.ls.Iterate(loc)
}

func (l *LBFGS) InitDirection(loc *Location, dir []float64) (stepSize float64) {
Expand Down
Loading

0 comments on commit 37b6118

Please sign in to comment.