aimsalgo  5.0.5
Neuroimaging image processing
deterministic.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
35 #ifndef AIMS_OPTIMIZATION_DETERMINISTIC_H
36 #define AIMS_OPTIMIZATION_DETERMINISTIC_H
37 
38 #include <cstdlib>
39 #include <aims/math/mathelem.h>
40 #include <aims/math/random.h>
41 #include <aims/vector/vector.h>
42 #include <aims/data/data.h>
43 #include <aims/def/assert.h>
46 
47 
48 //
49 // class DetermOptimizer
50 //
51 template <class T, int D>
52 class DetermOptimizer : public Optimizer<T, D>
53 {
54  public:
55  DetermOptimizer( const ObjectiveFunc<T,D>& func, T error,
56  int maxIter = 100000, int stability = 1,
57  bool verbose = false )
58  : Optimizer< T, D >( func, error ),
59  _maxIter( maxIter ), _stability( stability ),
60  _verbose( verbose )
61  { }
62  virtual ~DetermOptimizer() { }
63 
64  AimsVector<T,D> doit( const AimsVector<T,D> & pinit,
65  const AimsVector<T,D> & deltaP );
66 
67  private:
68  int _maxIter;
69  int _stability;
70  bool _verbose;
71 };
72 
73 
74 template <class T,int D> inline
77  const AimsVector<T,D> & deltaP )
78 {
79  AimsVector<T,D> p( pinit ), new_p( pinit ), dP( deltaP );
80  T eval, new_eval, old_eval, err;
81 
82  old_eval = eval = new_eval = this->_func.eval( p );
83  int iter=0, cntStab = 0, k;
84  do
85  {
86  for ( k = 0; k < D; k++ )
87  new_p[ k ] = p[k] + (T)UniformRandom(-1.0,+1.0) * dP[ k ];
88  new_eval = this->_func.eval( new_p );
89  if ( new_eval < eval )
90  {
91  old_eval = eval;
92  p = new_p;
93  eval = new_eval;
94  }
95  if ( ( err = fabs( eval - old_eval ) ) < this->_error )
96  {
97  cntStab++;
98  }
99  else if ( cntStab )
100  {
101  cntStab = 0;
102  }
103  if ( _verbose )
104  std::cout
105  << "it=" << iter
106  << " param=" << p
107  << " stab=" << cntStab
108  << " objective=" << eval
109  << " error=" << err
110  << std::endl;
111  iter++;
112  dP *= (T)0.995f;
113  ASSERT( iter != _maxIter );
114  }
115  while ( cntStab != _stability );
116 
117  return p;
118 }
119 
120 #endif
DetermOptimizer(const ObjectiveFunc< T, D > &func, T error, int maxIter=100000, int stability=1, bool verbose=false)
Definition: deterministic.h:55
double UniformRandom()
Uniform distribution between 0.0 and 1.0.
int verbose
AimsVector< T, D > doit(const AimsVector< T, D > &pinit, const AimsVector< T, D > &deltaP)
Definition: deterministic.h:76
const ObjectiveFunc< T, D > & _func
Definition: optimizer.h:88
#define ASSERT(EX)
virtual ~DetermOptimizer()
Definition: deterministic.h:62