Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
optimizer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_NUMERICS_OPTIMIZER
41 #define GEOGRAM_NUMERICS_OPTIMIZER
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/numeric.h>
46 #include <geogram/basic/counted.h>
47 #include <geogram/basic/assert.h>
48 #include <geogram/basic/factory.h>
49 
50 class HESSIAN_MATRIX;
51 
58 namespace GEO {
59 
77  class GEOGRAM_API Optimizer : public Counted {
78  public:
84  typedef void (* funcgrad_callback)(
85  index_t N, double* x, double& f, double* g
86  );
87 
92  typedef void (* newiteration_callback)(
93  index_t N, const double* x, double f, const double* g, double gnorm
94  );
95 
101  typedef void (* evalhessian_callback)(
102  index_t N, double* x, double& f, double* g, HESSIAN_MATRIX& hessian
103  );
104 
121  static Optimizer* create(const std::string& name = "default");
122 
127  virtual void optimize(double* x) = 0;
128 
132  void set_N(index_t N) {
133  n_ = N;
134  }
135 
139  index_t get_N() const {
140  return n_;
141  }
142 
147  void set_M(index_t M) {
148  m_ = M;
149  }
150 
154  index_t get_M() const {
155  return m_;
156  }
157 
161  void set_max_iter(index_t maxiter) {
162  max_iter_ = maxiter;
163  }
164 
169  return max_iter_;
170  }
171 
176  void set_funcgrad_callback(funcgrad_callback fp) {
177  funcgrad_callback_ = fp;
178  }
179 
184  void set_newiteration_callback(newiteration_callback fp) {
185  newiteration_callback_ = fp;
186  }
187 
195  void set_evalhessian_callback(evalhessian_callback fp) {
196  evalhessian_callback_ = fp;
197  }
198 
203  void set_epsg(double eg) {
204  epsg_ = eg;
205  }
206 
211  void set_epsf(double ef) {
212  epsf_ = ef;
213  }
214 
219  void set_epsx(double ex) {
220  epsx_ = ex;
221  }
222 
226  void set_verbose(bool verb) {
227  verbose_ = verb;
228  }
229 
230  protected:
236 
240  ~Optimizer() override;
241 
242  protected:
252 
253  funcgrad_callback funcgrad_callback_;
254  newiteration_callback newiteration_callback_;
255  evalhessian_callback evalhessian_callback_;
256 
258  double epsg_, epsf_, epsx_;
259 
260  bool verbose_;
261  };
262 
268 
278 
284 #define geo_register_Optimizer_creator(type, name) \
285  geo_register_creator(GEO::OptimizerFactory, type, name)
286 }
287 
288 #endif
Assertion checking mechanism.
Base class for reference-counted objects.
Definition: counted.h:71
Factory for types without constructor arguments.
Definition: factory.h:292
Optimizer minimizes a multivariate function.
Definition: optimizer.h:77
void set_max_iter(index_t maxiter)
Defines the maximum number of iterations.
Definition: optimizer.h:161
~Optimizer() override
Optimizer destructor.
index_t m_
Definition: optimizer.h:249
void set_M(index_t M)
Defines the inner number of iterations.
Definition: optimizer.h:147
void set_epsg(double eg)
Defines the stopping criterion in terms of gradient magnitude.
Definition: optimizer.h:203
index_t n_
Definition: optimizer.h:244
index_t get_N() const
Returns the number of variables.
Definition: optimizer.h:139
static Optimizer * create(const std::string &name="default")
Creates an Optimizer.
double epsg_
Definition: optimizer.h:258
Optimizer()
Optimizer constructor.
void set_evalhessian_callback(evalhessian_callback fp)
Defines the callback that evaluates the Hessian of function to be minimized (second order derivatives...
Definition: optimizer.h:195
index_t get_max_iter() const
Returns the maximum number of iterations.
Definition: optimizer.h:168
Factory0< Optimizer > OptimizerFactory
Optimizer Factory.
Definition: optimizer.h:277
void set_epsx(double ex)
Defines the stopping criterion in terms of variation of x.
Definition: optimizer.h:219
SmartPointer< Optimizer > Optimizer_var
Smart pointer that contains an Optimizer object.
Definition: optimizer.h:267
void set_verbose(bool verb)
Enables or disables verbose logs.
Definition: optimizer.h:226
index_t get_M() const
Returns the inner number of iterations.
Definition: optimizer.h:154
index_t max_iter_
Definition: optimizer.h:251
void set_funcgrad_callback(funcgrad_callback fp)
Defines the callback that evaluates the function to be minimized and its gradient.
Definition: optimizer.h:176
virtual void optimize(double *x)=0
Minimizes a function, starting from initial value x.
void set_epsf(double ef)
Defines the stopping criterion in terms of function value.
Definition: optimizer.h:211
void set_N(index_t N)
Defines the number of variables.
Definition: optimizer.h:132
void set_newiteration_callback(newiteration_callback fp)
Defines a callback that will be called at each iteration.
Definition: optimizer.h:184
A smart pointer with reference-counted copy semantics.
Definition: smart_pointer.h:76
Base class of reference-counted objects, to be used with smart pointers.
Generic factory mechanism.
Common include file, providing basic definitions. Should be included before anything else by all head...
Global Vorpaline namespace.
Definition: basic.h:55
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
Types and functions for numbers manipulation.
Pointers with automatic reference counting.