gonum.org/v1/gonum@v0.14.0/optimize/interfaces.go (about) 1 // Copyright ©2014 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package optimize 6 7 // A localMethod 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 Operation 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 // 18 // An evaluation operation is one or more of the Evaluation operations 19 // (FuncEvaluation, GradEvaluation, etc.) which can be combined with 20 // the bitwise or operator. In an evaluation operation, the requested fields of 21 // Problem will be evaluated at the point specified in Location.X. 22 // The corresponding fields of Location will be filled with the results that 23 // can be retrieved upon the next call to Iterate. The Method interface 24 // requires that entries of Location are not modified aside from the commanded 25 // evaluations. Thus, the type implementing Method may use multiple Operations 26 // to set the Location fields at a particular x value. 27 // 28 // Instead of an Evaluation, a Method may declare MajorIteration. In 29 // a MajorIteration, the values in the fields of Location are treated as 30 // a potential optimizer. The convergence of the optimization routine 31 // (GradientThreshold, etc.) is checked at this new best point. In 32 // a MajorIteration, the fields of Location must be valid and consistent. 33 // 34 // A Method must not return InitIteration and PostIteration operations. These are 35 // reserved for the clients to be passed to Recorders. A Method must also not 36 // combine the Evaluation operations with the Iteration operations. 37 type localMethod interface { 38 // Init initializes the method based on the initial data in loc, updates it 39 // and returns the first operation to be carried out by the caller. 40 // The initial location must be valid as specified by Needs. 41 initLocal(loc *Location) (Operation, error) 42 43 // Iterate retrieves data from loc, performs one iteration of the method, 44 // updates loc and returns the next operation. 45 iterateLocal(loc *Location) (Operation, error) 46 47 needser 48 } 49 50 type needser interface { 51 // needs specifies information about the objective function needed by the 52 // optimizer beyond just the function value. The information is used 53 // internally for initialization and must match evaluation types returned 54 // by Init and Iterate during the optimization process. 55 needs() struct { 56 Gradient bool 57 Hessian bool 58 } 59 } 60 61 // Statuser can report the status and any error. It is intended for methods as 62 // an additional error reporting mechanism apart from the errors returned from 63 // Init and Iterate. 64 type Statuser interface { 65 Status() (Status, error) 66 } 67 68 // Linesearcher is a type that can perform a line search. It tries to find an 69 // (approximate) minimum of the objective function along the search direction 70 // dir_k starting at the most recent location x_k, i.e., it tries to minimize 71 // the function 72 // 73 // φ(step) := f(x_k + step * dir_k) where step > 0. 74 // 75 // Typically, a Linesearcher will be used in conjunction with LinesearchMethod 76 // for performing gradient-based optimization through sequential line searches. 77 type Linesearcher interface { 78 // Init initializes the Linesearcher and a new line search. Value and 79 // derivative contain φ(0) and φ'(0), respectively, and step contains the 80 // first trial step length. It returns an Operation that must be one of 81 // FuncEvaluation, GradEvaluation, FuncEvaluation|GradEvaluation. The 82 // caller must evaluate φ(step), φ'(step), or both, respectively, and pass 83 // the result to Linesearcher in value and derivative arguments to Iterate. 84 Init(value, derivative float64, step float64) Operation 85 86 // Iterate takes in the values of φ and φ' evaluated at the previous step 87 // and returns the next operation. 88 // 89 // If op is one of FuncEvaluation, GradEvaluation, 90 // FuncEvaluation|GradEvaluation, the caller must evaluate φ(step), 91 // φ'(step), or both, respectively, and pass the result to Linesearcher in 92 // value and derivative arguments on the next call to Iterate. 93 // 94 // If op is MajorIteration, a sufficiently accurate minimum of φ has been 95 // found at the previous step and the line search has concluded. Init must 96 // be called again to initialize a new line search. 97 // 98 // If err is nil, op must not specify another operation. If err is not nil, 99 // the values of op and step are undefined. 100 Iterate(value, derivative float64) (op Operation, step float64, err error) 101 } 102 103 // NextDirectioner implements a strategy for computing a new line search 104 // direction at each major iteration. Typically, a NextDirectioner will be 105 // used in conjunction with LinesearchMethod for performing gradient-based 106 // optimization through sequential line searches. 107 type NextDirectioner interface { 108 // InitDirection initializes the NextDirectioner at the given starting location, 109 // putting the initial direction in place into dir, and returning the initial 110 // step size. InitDirection must not modify Location. 111 InitDirection(loc *Location, dir []float64) (step float64) 112 113 // NextDirection updates the search direction and step size. Location is 114 // the location seen at the conclusion of the most recent linesearch. The 115 // next search direction is put in place into dir, and the next step size 116 // is returned. NextDirection must not modify Location. 117 NextDirection(loc *Location, dir []float64) (step float64) 118 } 119 120 // StepSizer can set the next step size of the optimization given the last Location. 121 // Returned step size must be positive. 122 type StepSizer interface { 123 Init(loc *Location, dir []float64) float64 124 StepSize(loc *Location, dir []float64) float64 125 } 126 127 // A Recorder can record the progress of the optimization, for example to print 128 // the progress to StdOut or to a log file. A Recorder must not modify any data. 129 type Recorder interface { 130 Init() error 131 Record(*Location, Operation, *Stats) error 132 }