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

Commit

Permalink
Rename RequestType to Operation
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-ch committed Sep 17, 2015
1 parent bdd4285 commit 20c5a76
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 105 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) RequestType {
func (b *Backtracking) Init(f, g float64, step float64) Operation {
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, RequestType, error) {
func (b *Backtracking) Iterate(_, _ float64) (float64, Operation, error) {
b.stepSize *= b.Decrease
if b.stepSize < minimumBacktrackingStepSize {
return 0, NoRequest, ErrLinesearchFailure
return 0, NoOperation, ErrLinesearchFailure
}
return b.stepSize, FuncEvaluation, nil
}
4 changes: 2 additions & 2 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) (RequestType, error) {
func (b *BFGS) Init(loc *Location) (Operation, error) {
if b.Linesearcher == nil {
b.Linesearcher = &Bisection{}
}
Expand All @@ -52,7 +52,7 @@ func (b *BFGS) Init(loc *Location) (RequestType, error) {
return b.ls.Init(loc)
}

func (b *BFGS) Iterate(loc *Location) (RequestType, error) {
func (b *BFGS) Iterate(loc *Location) (Operation, error) {
return b.ls.Iterate(loc)
}

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) RequestType {
func (b *Bisection) Init(f, g float64, step float64) Operation {
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, RequestType, error) {
func (b *Bisection) Iterate(f, g float64) (float64, Operation, 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, RequestType, 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, r RequestType) (float64, RequestType, error) {
func (b *Bisection) checkStepEqual(newStep float64, op Operation) (float64, Operation, error) {
if b.currStep == newStep {
return b.currStep, NoRequest, ErrLinesearchFailure
return b.currStep, NoOperation, ErrLinesearchFailure
}
b.currStep = newStep
return newStep, r, nil
return newStep, op, nil
}
4 changes: 2 additions & 2 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) (RequestType, error) {
func (cg *CG) Init(loc *Location) (Operation, error) {
if cg.IterationRestartFactor < 0 {
panic("cg: IterationRestartFactor is negative")
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (cg *CG) Init(loc *Location) (RequestType, error) {
return cg.ls.Init(loc)
}

func (cg *CG) Iterate(loc *Location) (RequestType, error) {
func (cg *CG) Iterate(loc *Location) (Operation, error) {
return cg.ls.Iterate(loc)
}

Expand Down
4 changes: 2 additions & 2 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) (RequestType, error) {
func (g *GradientDescent) Init(loc *Location) (Operation, error) {
if g.StepSizer == nil {
g.StepSizer = &QuadraticStepSize{}
}
Expand All @@ -34,7 +34,7 @@ func (g *GradientDescent) Init(loc *Location) (RequestType, error) {
return g.ls.Init(loc)
}

func (g *GradientDescent) Iterate(loc *Location) (RequestType, error) {
func (g *GradientDescent) Iterate(loc *Location) (Operation, error) {
return g.ls.Iterate(loc)
}

Expand Down
22 changes: 11 additions & 11 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ package optimize
//
// When Method needs the client to perform an operation like function
// evaluation or check for convergence, it fills the Location with data and
// returns a RequestType that correspond to the operation. The result then can
// be retrieved from Location upon the next call to Iterate().
// returns a corresponding Operation. The result then can be retrieved from
// Location upon the next call to Iterate().
//
// Methods must not return InitIteration and PostIteration requests. These are
// reserved for the clients to be passed to Recorders.
// Methods must not return InitIteration and PostIteration operations. These
// are reserved for the clients to be passed to Recorders.
type Method interface {
// Init initializes the method based on the initial data in loc, updates it
// and returns the first request.
// and returns the first operation to be carried out by the caller.
// The initial location must be valid as specified by Needs().
Init(loc *Location) (RequestType, error)
Init(loc *Location) (Operation, error)

// Iterate retrieves data from loc, performs one iteration of the method,
// updates loc and returns the next request.
// updates loc and returns the next operation.
// TODO(vladimir-ch): When decided, say something whether the contents of
// Location is preserved between calls to Iterate().
Iterate(loc *Location) (RequestType, error)
Iterate(loc *Location) (Operation, error)

// Needs specifies information about the objective function needed by the
// optimizer beyond just the function value. The information is used
Expand Down Expand Up @@ -60,7 +60,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) RequestType
Init(value, derivative float64, step float64) Operation

// 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 @@ -69,7 +69,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, r RequestType, err error)
Iterate(value, derivative float64) (step float64, op Operation, err error)
}

// NextDirectioner implements a strategy for computing a new line search
Expand Down Expand Up @@ -100,5 +100,5 @@ type StepSizer interface {
// the progress to StdOut or to a log file. A Recorder must not modify any data.
type Recorder interface {
Init() error
Record(*Location, RequestType, *Stats) error
Record(*Location, Operation, *Stats) error
}
4 changes: 2 additions & 2 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) (RequestType, error) {
func (l *LBFGS) Init(loc *Location) (Operation, error) {
if l.Linesearcher == nil {
l.Linesearcher = &Bisection{}
}
Expand All @@ -50,7 +50,7 @@ func (l *LBFGS) Init(loc *Location) (RequestType, error) {
return l.ls.Init(loc)
}

func (l *LBFGS) Iterate(loc *Location) (RequestType, error) {
func (l *LBFGS) Iterate(loc *Location) (Operation, error) {
return l.ls.Iterate(loc)
}

Expand Down
70 changes: 35 additions & 35 deletions linesearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ type LinesearchMethod struct {
first bool // Indicator of the first iteration.
nextMajor bool // Indicates that MajorIteration must be requested at the next call to Iterate().

loc Location // Storage for intermediate locations.
eval RequestType // Indicator of valid fields in loc.
loc Location // Storage for intermediate locations.
eval Operation // Indicator of valid fields in loc.

lastStep float64 // Step taken from x in the previous call to Iterate().
lastRequest RequestType // Request returned from the previous call to Iterate().
lastStep float64 // Step taken from x in the previous call to Iterate().
lastOp Operation // Operation returned from the previous call to Iterate().
}

func (ls *LinesearchMethod) Init(loc *Location) (RequestType, error) {
func (ls *LinesearchMethod) Init(loc *Location) (Operation, error) {
if loc.Gradient == nil {
panic("linesearch: gradient is nil")
}
Expand All @@ -51,14 +51,14 @@ func (ls *LinesearchMethod) Init(loc *Location) (RequestType, error) {
}

ls.lastStep = math.NaN()
ls.lastRequest = NoRequest
ls.lastOp = NoOperation

return ls.initNextLinesearch(loc.X)
}

func (ls *LinesearchMethod) Iterate(loc *Location) (RequestType, error) {
switch ls.lastRequest {
case NoRequest:
func (ls *LinesearchMethod) Iterate(loc *Location) (Operation, error) {
switch ls.lastOp {
case NoOperation:
// TODO(vladimir-ch): We have previously returned with an error and
// Init() was not called. What to do? What about ls's internal state?

Expand All @@ -69,22 +69,22 @@ func (ls *LinesearchMethod) Iterate(loc *Location) (RequestType, error) {
return ls.initNextLinesearch(loc.X)

default:
if ls.lastRequest&EvaluationRequest == 0 {
panic("linesearch: unexpected request")
if !ls.lastOp.IsEvaluation() {
panic("linesearch: unexpected operation")
}

// Store the result of the previously requested evaluation into ls.loc.
if ls.lastRequest&FuncEvaluation != 0 {
if ls.lastOp&FuncEvaluation != 0 {
ls.loc.F = loc.F
}
if ls.lastRequest&GradEvaluation != 0 {
if ls.lastOp&GradEvaluation != 0 {
copy(ls.loc.Gradient, loc.Gradient)
}
if ls.lastRequest&HessEvaluation != 0 {
if ls.lastOp&HessEvaluation != 0 {
ls.loc.Hessian.CopySym(loc.Hessian)
}
// Update the indicator of valid fields of ls.loc.
ls.eval |= ls.lastRequest
ls.eval |= ls.lastOp

if ls.nextMajor {
ls.nextMajor = false
Expand All @@ -94,26 +94,26 @@ func (ls *LinesearchMethod) Iterate(loc *Location) (RequestType, error) {
// can announce MajorIteration.

copyLocation(loc, &ls.loc)
ls.lastRequest = MajorIteration
return ls.lastRequest, nil
ls.lastOp = MajorIteration
return ls.lastOp, nil
}
}

projGrad := floats.Dot(ls.loc.Gradient, ls.dir)
if ls.Linesearcher.Finished(ls.loc.F, projGrad) {
// Form a request that evaluates invalid fields of ls.loc.
ls.lastRequest = complementEval(&ls.loc, ls.eval)
if ls.lastRequest == NoRequest {
// Form an operation that evaluates invalid fields of ls.loc.
ls.lastOp = complementEval(&ls.loc, ls.eval)
if ls.lastOp == NoOperation {
// ls.loc is complete and MajorIteration can be announced directly.
copyLocation(loc, &ls.loc)
ls.lastRequest = MajorIteration
ls.lastOp = MajorIteration
} else {
ls.nextMajor = true
}
return ls.lastRequest, nil
return ls.lastOp, nil
}

step, request, err := ls.Linesearcher.Iterate(ls.loc.F, projGrad)
step, op, err := ls.Linesearcher.Iterate(ls.loc.F, projGrad)
if err != nil {
return ls.error(err)
}
Expand All @@ -132,26 +132,26 @@ func (ls *LinesearchMethod) Iterate(loc *Location) (RequestType, error) {

ls.lastStep = step
copy(ls.loc.X, loc.X) // Move ls.loc to the next evaluation point
ls.eval = NoRequest // and invalidate all its fields.
ls.eval = NoOperation // and invalidate all its fields.
} else {
// Linesearcher is requesting another evaluation at the same point
// which is stored in ls.loc.X.
copy(loc.X, ls.loc.X)
}

ls.lastRequest = request
return ls.lastRequest, nil
ls.lastOp = op
return ls.lastOp, nil
}

func (ls *LinesearchMethod) error(err error) (RequestType, error) {
ls.lastRequest = NoRequest
return ls.lastRequest, err
func (ls *LinesearchMethod) error(err error) (Operation, error) {
ls.lastOp = NoOperation
return ls.lastOp, err
}

// initNextLinesearch initializes the next linesearch using the previous
// complete location stored in ls.loc. It fills xNext and returns an
// evaluation request to be performed at xNext.
func (ls *LinesearchMethod) initNextLinesearch(xNext []float64) (RequestType, error) {
// complete location stored in ls.loc. It fills xNext and returns an evaluation
// to be performed at xNext.
func (ls *LinesearchMethod) initNextLinesearch(xNext []float64) (Operation, error) {
copy(ls.x, ls.loc.X)

var step float64
Expand All @@ -167,7 +167,7 @@ func (ls *LinesearchMethod) initNextLinesearch(xNext []float64) (RequestType, er
return ls.error(ErrNonNegativeStepDirection)
}

ls.lastRequest = ls.Linesearcher.Init(ls.loc.F, projGrad, step)
ls.lastOp = ls.Linesearcher.Init(ls.loc.F, projGrad, step)

floats.AddScaledTo(xNext, ls.x, step, ls.dir)
if floats.Equal(ls.x, xNext) {
Expand All @@ -179,9 +179,9 @@ func (ls *LinesearchMethod) initNextLinesearch(xNext []float64) (RequestType, er

ls.lastStep = step
copy(ls.loc.X, xNext) // Move ls.loc to the next evaluation point
ls.eval = NoRequest // and invalidate all its fields.
ls.eval = NoOperation // and invalidate all its fields.

return ls.lastRequest, nil
return ls.lastOp, nil
}

// ArmijoConditionMet returns true if the Armijo condition (aka sufficient
Expand Down
Loading

0 comments on commit 20c5a76

Please sign in to comment.