github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/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  //  φ(step) := f(x_k + step * dir_k) where step > 0.
    73  // Typically, a Linesearcher will be used in conjunction with LinesearchMethod
    74  // for performing gradient-based optimization through sequential line searches.
    75  type Linesearcher interface {
    76  	// Init initializes the Linesearcher and a new line search. Value and
    77  	// derivative contain φ(0) and φ'(0), respectively, and step contains the
    78  	// first trial step length. It returns an Operation that must be one of
    79  	// FuncEvaluation, GradEvaluation, FuncEvaluation|GradEvaluation. The
    80  	// caller must evaluate φ(step), φ'(step), or both, respectively, and pass
    81  	// the result to Linesearcher in value and derivative arguments to Iterate.
    82  	Init(value, derivative float64, step float64) Operation
    83  
    84  	// Iterate takes in the values of φ and φ' evaluated at the previous step
    85  	// and returns the next operation.
    86  	//
    87  	// If op is one of FuncEvaluation, GradEvaluation,
    88  	// FuncEvaluation|GradEvaluation, the caller must evaluate φ(step),
    89  	// φ'(step), or both, respectively, and pass the result to Linesearcher in
    90  	// value and derivative arguments on the next call to Iterate.
    91  	//
    92  	// If op is MajorIteration, a sufficiently accurate minimum of φ has been
    93  	// found at the previous step and the line search has concluded. Init must
    94  	// be called again to initialize a new line search.
    95  	//
    96  	// If err is nil, op must not specify another operation. If err is not nil,
    97  	// the values of op and step are undefined.
    98  	Iterate(value, derivative float64) (op Operation, step float64, err error)
    99  }
   100  
   101  // NextDirectioner implements a strategy for computing a new line search
   102  // direction at each major iteration. Typically, a NextDirectioner will be
   103  // used in conjunction with LinesearchMethod for performing gradient-based
   104  // optimization through sequential line searches.
   105  type NextDirectioner interface {
   106  	// InitDirection initializes the NextDirectioner at the given starting location,
   107  	// putting the initial direction in place into dir, and returning the initial
   108  	// step size. InitDirection must not modify Location.
   109  	InitDirection(loc *Location, dir []float64) (step float64)
   110  
   111  	// NextDirection updates the search direction and step size. Location is
   112  	// the location seen at the conclusion of the most recent linesearch. The
   113  	// next search direction is put in place into dir, and the next step size
   114  	// is returned. NextDirection must not modify Location.
   115  	NextDirection(loc *Location, dir []float64) (step float64)
   116  }
   117  
   118  // StepSizer can set the next step size of the optimization given the last Location.
   119  // Returned step size must be positive.
   120  type StepSizer interface {
   121  	Init(loc *Location, dir []float64) float64
   122  	StepSize(loc *Location, dir []float64) float64
   123  }
   124  
   125  // A Recorder can record the progress of the optimization, for example to print
   126  // the progress to StdOut or to a log file. A Recorder must not modify any data.
   127  type Recorder interface {
   128  	Init() error
   129  	Record(*Location, Operation, *Stats) error
   130  }