5
5
package optimize
6
6
7
7
// A Method can optimize an objective function.
8
+ //
9
+ // It uses a reverse-communication interface between the optimization method
10
+ // and the caller. Method acts as a client that asks the caller to perform
11
+ // needed operations via RequestType returned from Init and Iterate methods.
12
+ // This provides independence of the optimization algorithm on user-supplied
13
+ // data and their representation, and enables automation of common operations
14
+ // like checking for (various types of) convergence and maintaining statistics.
15
+ //
16
+ // A Method can command an Evaluation, a MajorIteration or NoOperation operations.
17
+ // An evaluation operation is one or more of the Evaluation operations
18
+ // (FuncEvaluation, GradEvaluation, etc.) which can be combined with
19
+ // the bitwise or operator. In an evaluation operation, the requested routines
20
+ // will be evaluated at the point specified in Location.X. The corresponding
21
+ // fields of Location will be filled with the results from the routine and can
22
+ // be retrieved upon the next call to Iterate. Alternatively, a Method can
23
+ // declare a MajorIteration. In a MajorIteration, all values in Location must
24
+ // be valid and consistent, and are interpreted as a new minimum. Convergence
25
+ // of the optimization (GradientThreshold, etc.) will be checked using this new
26
+ // minimum.
27
+ //
28
+ // A Method must not return InitIteration and PostIteration operations. These are
29
+ // reserved for the clients to be passed to Recorders. A Method must also not
30
+ // combine the Evaluation operations with the Iteration operations.
8
31
type Method interface {
9
- // Init initializes the method and stores the first location to evaluate
10
- // in xNext.
11
- Init (loc * Location , xNext []float64 ) (EvaluationType , IterationType , error )
32
+ // Init initializes the method based on the initial data in loc, updates it
33
+ // and returns the first operation to be carried out by the caller.
34
+ // The initial location must be valid as specified by Needs().
35
+ Init (loc * Location ) (Operation , error )
12
36
13
- // Iterate performs one iteration of the method and stores the next
14
- // location to evaluate in xNext.
15
- Iterate (loc * Location , xNext []float64 ) (EvaluationType , IterationType , error )
37
+ // Iterate retrieves data from loc, performs one iteration of the method,
38
+ // updates loc and returns the next operation.
39
+ // TODO(vladimir-ch): When decided, say something whether the contents of
40
+ // Location is preserved between calls to Iterate().
41
+ Iterate (loc * Location ) (Operation , error )
16
42
17
43
// Needs specifies information about the objective function needed by the
18
44
// optimizer beyond just the function value. The information is used
@@ -24,6 +50,13 @@ type Method interface {
24
50
}
25
51
}
26
52
53
+ // Statuser can report the status and any error. It is intended for methods as
54
+ // an additional error reporting mechanism apart from the errors returned from
55
+ // Init() and Iterate().
56
+ type Statuser interface {
57
+ Status () (Status , error )
58
+ }
59
+
27
60
// Linesearcher is a type that can perform a line search. It tries to find an
28
61
// (approximate) minimum of the objective function along the search direction
29
62
// dir_k starting at the most recent location x_k, i.e., it tries to minimize
@@ -36,7 +69,7 @@ type Linesearcher interface {
36
69
// φ(0) and φ'(0), respectively, and step contains the first trial step
37
70
// length. It returns the type of evaluation to be performed at
38
71
// x_0 + step * dir_0.
39
- Init (value , derivative float64 , step float64 ) EvaluationType
72
+ Init (value , derivative float64 , step float64 ) Operation
40
73
41
74
// Finished takes in the values of φ and φ' evaluated at the previous step,
42
75
// and returns whether a sufficiently accurate minimum of φ has been found.
@@ -45,7 +78,7 @@ type Linesearcher interface {
45
78
// Iterate takes in the values of φ and φ' evaluated at the previous step
46
79
// and returns the next step size and the type of evaluation to be
47
80
// performed at x_k + step * dir_k.
48
- Iterate (value , derivative float64 ) (step float64 , e EvaluationType , err error )
81
+ Iterate (value , derivative float64 ) (step float64 , op Operation , err error )
49
82
}
50
83
51
84
// NextDirectioner implements a strategy for computing a new line search
@@ -72,17 +105,9 @@ type StepSizer interface {
72
105
StepSize (loc * Location , dir []float64 ) float64
73
106
}
74
107
75
- // Statuser returns the status of the Function being optimized. This can be used
76
- // by the Function to terminate early, for example with an error. The user can
77
- // use one of the pre-provided Status constants, or may call NewStatus to create
78
- // a custom Status value.
79
- type Statuser interface {
80
- Status () (Status , error )
81
- }
82
-
83
108
// A Recorder can record the progress of the optimization, for example to print
84
109
// the progress to StdOut or to a log file. A Recorder must not modify any data.
85
110
type Recorder interface {
86
111
Init () error
87
- Record (* Location , EvaluationType , IterationType , * Stats ) error
112
+ Record (* Location , Operation , * Stats ) error
88
113
}
0 commit comments