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

Commit f760a2e

Browse files
committedSep 18, 2015
Rename RequestType to Operation
1 parent 7725c51 commit f760a2e

13 files changed

+110
-103
lines changed
 

‎backtracking.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Backtracking struct {
3333
initG float64
3434
}
3535

36-
func (b *Backtracking) Init(f, g float64, step float64) RequestType {
36+
func (b *Backtracking) Init(f, g float64, step float64) Operation {
3737
if step <= 0 {
3838
panic("backtracking: bad step size")
3939
}
@@ -64,10 +64,10 @@ func (b *Backtracking) Finished(f, _ float64) bool {
6464
return ArmijoConditionMet(f, b.initF, b.initG, b.stepSize, b.FuncConst)
6565
}
6666

67-
func (b *Backtracking) Iterate(_, _ float64) (float64, RequestType, error) {
67+
func (b *Backtracking) Iterate(_, _ float64) (float64, Operation, error) {
6868
b.stepSize *= b.Decrease
6969
if b.stepSize < minimumBacktrackingStepSize {
70-
return 0, NoRequest, ErrLinesearchFailure
70+
return 0, NoOperation, ErrLinesearchFailure
7171
}
7272
return b.stepSize, FuncEvaluation, nil
7373
}

‎bfgs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type BFGS struct {
3939
first bool // Is it the first iteration (used to set the scale of the initial hessian)
4040
}
4141

42-
func (b *BFGS) Init(loc *Location) (RequestType, error) {
42+
func (b *BFGS) Init(loc *Location) (Operation, error) {
4343
if b.Linesearcher == nil {
4444
b.Linesearcher = &Bisection{}
4545
}
@@ -52,7 +52,7 @@ func (b *BFGS) Init(loc *Location) (RequestType, error) {
5252
return b.ls.Init(loc)
5353
}
5454

55-
func (b *BFGS) Iterate(loc *Location) (RequestType, error) {
55+
func (b *BFGS) Iterate(loc *Location) (Operation, error) {
5656
return b.ls.Iterate(loc)
5757
}
5858

‎bisection.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type Bisection struct {
2626
maxGrad float64
2727
}
2828

29-
func (b *Bisection) Init(f, g float64, step float64) RequestType {
29+
func (b *Bisection) Init(f, g float64, step float64) Operation {
3030
if step <= 0 {
3131
panic("bisection: bad step size")
3232
}
@@ -73,7 +73,7 @@ func (b *Bisection) Finished(f, g float64) bool {
7373
return false
7474
}
7575

76-
func (b *Bisection) Iterate(f, g float64) (float64, RequestType, error) {
76+
func (b *Bisection) Iterate(f, g float64) (float64, Operation, error) {
7777
// Deciding on the next step size
7878
if math.IsInf(b.maxStep, 1) {
7979
// Have not yet bounded the minimum
@@ -135,10 +135,10 @@ func (b *Bisection) Iterate(f, g float64) (float64, RequestType, error) {
135135
// both of which indicate the minimization must stop. If the steps are different,
136136
// it sets the new step size and returns the step and evaluation type. If the steps
137137
// are the same, it returns an error.
138-
func (b *Bisection) checkStepEqual(newStep float64, r RequestType) (float64, RequestType, error) {
138+
func (b *Bisection) checkStepEqual(newStep float64, op Operation) (float64, Operation, error) {
139139
if b.currStep == newStep {
140-
return b.currStep, NoRequest, ErrLinesearchFailure
140+
return b.currStep, NoOperation, ErrLinesearchFailure
141141
}
142142
b.currStep = newStep
143-
return newStep, r, nil
143+
return newStep, op, nil
144144
}

‎cg.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ type CG struct {
9494
gradPrevNorm float64
9595
}
9696

97-
func (cg *CG) Init(loc *Location) (RequestType, error) {
97+
func (cg *CG) Init(loc *Location) (Operation, error) {
9898
if cg.IterationRestartFactor < 0 {
9999
panic("cg: IterationRestartFactor is negative")
100100
}
@@ -128,7 +128,7 @@ func (cg *CG) Init(loc *Location) (RequestType, error) {
128128
return cg.ls.Init(loc)
129129
}
130130

131-
func (cg *CG) Iterate(loc *Location) (RequestType, error) {
131+
func (cg *CG) Iterate(loc *Location) (Operation, error) {
132132
return cg.ls.Iterate(loc)
133133
}
134134

‎gradientdescent.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type GradientDescent struct {
1818
ls *LinesearchMethod
1919
}
2020

21-
func (g *GradientDescent) Init(loc *Location) (RequestType, error) {
21+
func (g *GradientDescent) Init(loc *Location) (Operation, error) {
2222
if g.StepSizer == nil {
2323
g.StepSizer = &QuadraticStepSize{}
2424
}
@@ -34,7 +34,7 @@ func (g *GradientDescent) Init(loc *Location) (RequestType, error) {
3434
return g.ls.Init(loc)
3535
}
3636

37-
func (g *GradientDescent) Iterate(loc *Location) (RequestType, error) {
37+
func (g *GradientDescent) Iterate(loc *Location) (Operation, error) {
3838
return g.ls.Iterate(loc)
3939
}
4040

‎interfaces.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ package optimize
3030
// combine the Evaluation operations with the Iteration operations.
3131
type Method interface {
3232
// Init initializes the method based on the initial data in loc, updates it
33-
// and returns the first request.
33+
// and returns the first operation to be carried out by the caller.
3434
// The initial location must be valid as specified by Needs().
35-
Init(loc *Location) (RequestType, error)
35+
Init(loc *Location) (Operation, error)
3636

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

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

7474
// Finished takes in the values of φ and φ' evaluated at the previous step,
7575
// and returns whether a sufficiently accurate minimum of φ has been found.
@@ -78,7 +78,7 @@ type Linesearcher interface {
7878
// Iterate takes in the values of φ and φ' evaluated at the previous step
7979
// and returns the next step size and the type of evaluation to be
8080
// performed at x_k + step * dir_k.
81-
Iterate(value, derivative float64) (step float64, r RequestType, err error)
81+
Iterate(value, derivative float64) (step float64, op Operation, err error)
8282
}
8383

8484
// NextDirectioner implements a strategy for computing a new line search
@@ -109,5 +109,5 @@ type StepSizer interface {
109109
// the progress to StdOut or to a log file. A Recorder must not modify any data.
110110
type Recorder interface {
111111
Init() error
112-
Record(*Location, RequestType, *Stats) error
112+
Record(*Location, Operation, *Stats) error
113113
}

‎lbfgs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type LBFGS struct {
3838
rhoHist []float64 // last Store iterations of rho
3939
}
4040

41-
func (l *LBFGS) Init(loc *Location) (RequestType, error) {
41+
func (l *LBFGS) Init(loc *Location) (Operation, error) {
4242
if l.Linesearcher == nil {
4343
l.Linesearcher = &Bisection{}
4444
}
@@ -50,7 +50,7 @@ func (l *LBFGS) Init(loc *Location) (RequestType, error) {
5050
return l.ls.Init(loc)
5151
}
5252

53-
func (l *LBFGS) Iterate(loc *Location) (RequestType, error) {
53+
func (l *LBFGS) Iterate(loc *Location) (Operation, error) {
5454
return l.ls.Iterate(loc)
5555
}
5656

‎linesearch.go

+35-35
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ type LinesearchMethod struct {
2424
first bool // Indicator of the first iteration.
2525
nextMajor bool // Indicates that MajorIteration must be requested at the next call to Iterate().
2626

27-
loc Location // Storage for intermediate locations.
28-
eval RequestType // Indicator of valid fields in loc.
27+
loc Location // Storage for intermediate locations.
28+
eval Operation // Indicator of valid fields in loc.
2929

30-
lastStep float64 // Step taken from x in the previous call to Iterate().
31-
lastRequest RequestType // Request returned from the previous call to Iterate().
30+
lastStep float64 // Step taken from x in the previous call to Iterate().
31+
lastOp Operation // Operation returned from the previous call to Iterate().
3232
}
3333

34-
func (ls *LinesearchMethod) Init(loc *Location) (RequestType, error) {
34+
func (ls *LinesearchMethod) Init(loc *Location) (Operation, error) {
3535
if loc.Gradient == nil {
3636
panic("linesearch: gradient is nil")
3737
}
@@ -51,14 +51,14 @@ func (ls *LinesearchMethod) Init(loc *Location) (RequestType, error) {
5151
}
5252

5353
ls.lastStep = math.NaN()
54-
ls.lastRequest = NoRequest
54+
ls.lastOp = NoOperation
5555

5656
return ls.initNextLinesearch(loc.X)
5757
}
5858

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

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

7171
default:
72-
if ls.lastRequest&EvaluationRequest == 0 {
73-
panic("linesearch: unexpected request")
72+
if !ls.lastOp.IsEvaluation() {
73+
panic("linesearch: unexpected operation")
7474
}
7575

7676
// Store the result of the previously requested evaluation into ls.loc.
77-
if ls.lastRequest&FuncEvaluation != 0 {
77+
if ls.lastOp&FuncEvaluation != 0 {
7878
ls.loc.F = loc.F
7979
}
80-
if ls.lastRequest&GradEvaluation != 0 {
80+
if ls.lastOp&GradEvaluation != 0 {
8181
copy(ls.loc.Gradient, loc.Gradient)
8282
}
83-
if ls.lastRequest&HessEvaluation != 0 {
83+
if ls.lastOp&HessEvaluation != 0 {
8484
ls.loc.Hessian.CopySym(loc.Hessian)
8585
}
8686
// Update the indicator of valid fields of ls.loc.
87-
ls.eval |= ls.lastRequest
87+
ls.eval |= ls.lastOp
8888

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

9696
copyLocation(loc, &ls.loc)
97-
ls.lastRequest = MajorIteration
98-
return ls.lastRequest, nil
97+
ls.lastOp = MajorIteration
98+
return ls.lastOp, nil
9999
}
100100
}
101101

102102
projGrad := floats.Dot(ls.loc.Gradient, ls.dir)
103103
if ls.Linesearcher.Finished(ls.loc.F, projGrad) {
104-
// Form a request that evaluates invalid fields of ls.loc.
105-
ls.lastRequest = complementEval(&ls.loc, ls.eval)
106-
if ls.lastRequest == NoRequest {
104+
// Form an operation that evaluates invalid fields of ls.loc.
105+
ls.lastOp = complementEval(&ls.loc, ls.eval)
106+
if ls.lastOp == NoOperation {
107107
// ls.loc is complete and MajorIteration can be announced directly.
108108
copyLocation(loc, &ls.loc)
109-
ls.lastRequest = MajorIteration
109+
ls.lastOp = MajorIteration
110110
} else {
111111
ls.nextMajor = true
112112
}
113-
return ls.lastRequest, nil
113+
return ls.lastOp, nil
114114
}
115115

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

133133
ls.lastStep = step
134134
copy(ls.loc.X, loc.X) // Move ls.loc to the next evaluation point
135-
ls.eval = NoRequest // and invalidate all its fields.
135+
ls.eval = NoOperation // and invalidate all its fields.
136136
} else {
137137
// Linesearcher is requesting another evaluation at the same point
138138
// which is stored in ls.loc.X.
139139
copy(loc.X, ls.loc.X)
140140
}
141141

142-
ls.lastRequest = request
143-
return ls.lastRequest, nil
142+
ls.lastOp = op
143+
return ls.lastOp, nil
144144
}
145145

146-
func (ls *LinesearchMethod) error(err error) (RequestType, error) {
147-
ls.lastRequest = NoRequest
148-
return ls.lastRequest, err
146+
func (ls *LinesearchMethod) error(err error) (Operation, error) {
147+
ls.lastOp = NoOperation
148+
return ls.lastOp, err
149149
}
150150

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

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

170-
ls.lastRequest = ls.Linesearcher.Init(ls.loc.F, projGrad, step)
170+
ls.lastOp = ls.Linesearcher.Init(ls.loc.F, projGrad, step)
171171

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

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

184-
return ls.lastRequest, nil
184+
return ls.lastOp, nil
185185
}
186186

187187
// ArmijoConditionMet returns true if the Armijo condition (aka sufficient

0 commit comments

Comments
 (0)
This repository has been archived.