aimsalgo  5.1.2
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/def/assert.h>
45 
46 
47 //
48 // class DetermOptimizer
49 //
50 template <class T, int D>
51 class DetermOptimizer : public Optimizer<T, D>
52 {
53  public:
54  DetermOptimizer( const ObjectiveFunc<T,D>& func, T error,
55  int maxIter = 100000, int stability = 1,
56  bool verbose = false )
57  : Optimizer< T, D >( func, error ),
58  _maxIter( maxIter ), _stability( stability ),
59  _verbose( verbose )
60  { }
61  virtual ~DetermOptimizer() { }
62 
63  AimsVector<T,D> doit( const AimsVector<T,D> & pinit,
64  const AimsVector<T,D> & deltaP );
65 
66  private:
67  int _maxIter;
68  int _stability;
69  bool _verbose;
70 };
71 
72 
73 template <class T,int D> inline
76  const AimsVector<T,D> & deltaP )
77 {
78  AimsVector<T,D> p( pinit ), new_p( pinit ), dP( deltaP );
79  T eval, new_eval, old_eval, err;
80 
81  old_eval = eval = new_eval = this->_func.eval( p );
82  int iter=0, cntStab = 0, k;
83  do
84  {
85  for ( k = 0; k < D; k++ )
86  new_p[ k ] = p[k] + (T)UniformRandom(-1.0,+1.0) * dP[ k ];
87  new_eval = this->_func.eval( new_p );
88  if ( new_eval < eval )
89  {
90  old_eval = eval;
91  p = new_p;
92  eval = new_eval;
93  }
94  if ( ( err = fabs( eval - old_eval ) ) < this->_error )
95  {
96  cntStab++;
97  }
98  else if ( cntStab )
99  {
100  cntStab = 0;
101  }
102  if ( _verbose )
103  std::cout
104  << "it=" << iter
105  << " param=" << p
106  << " stab=" << cntStab
107  << " objective=" << eval
108  << " error=" << err
109  << std::endl;
110  iter++;
111  dP *= (T)0.995f;
112  ASSERT( iter != _maxIter );
113  }
114  while ( cntStab != _stability );
115 
116  return p;
117 }
118 
119 #endif
#define ASSERT(EX)
virtual ~DetermOptimizer()
Definition: deterministic.h:61
AimsVector< T, D > doit(const AimsVector< T, D > &pinit, const AimsVector< T, D > &deltaP)
Definition: deterministic.h:75
DetermOptimizer(const ObjectiveFunc< T, D > &func, T error, int maxIter=100000, int stability=1, bool verbose=false)
Definition: deterministic.h:54
int verbose
double UniformRandom()
Uniform distribution between 0.0 and 1.0.