All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mmin_fix.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2014, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_MMIN_FIX_H
24 #define O2SCL_MMIN_FIX_H
25 
26 /** \file mmin_fix.h
27  \brief File defining \ref o2scl::mmin_fix_params
28 */
29 
30 #include <vector>
31 
32 #include <o2scl/mmin.h>
33 #include <o2scl/mmin_simp2.h>
34 
35 #ifndef DOXYGEN_NO_O2NS
36 namespace o2scl {
37 #endif
38 
39  /** \brief Multidimensional minimizer fixing some parameters and
40  varying others
41 
42  This class allows one to min a function after having fixed
43  some of the parameters. The parameters which should be fixed are
44  specified through a <tt>bool</tt> vector. This class performs the
45  extra bookkeeping associated with reordering the parameters and
46  performs the minimization with a separate minimizer object. This
47  class is most useful for minimization problems which do not use
48  information about the gradient.
49 
50  The number of trials used in the minimizer can be specified in
51  the data member of the parent class \ref mmin_base::ntrial
52  associated with the \ref o2scl::mmin_fix_params object.
53  Similarly for the verbosity parameter in \ref
54  mmin_base::verbose, the absolute tolerance in \ref
55  mmin_base::tol_abs, and the relative tolerance in \ref
56  mmin_base::tol_abs. These values are copied to the minimizer
57  used by \ref mmin_fix_params::mmin() during each call. After the
58  minimizer is called, the value of \ref mmin_base::ntrial
59  associated with the \ref mmin_fix_params object is filled with
60  the last number of trials required for the last minimization.
61 
62  See an example for the usage of this class in \ref
63  ex_mmin_fix_sect .
64 
65  \todo This hasn't been converted yet with the new C++11
66  function types.
67 
68  \comment
69  We cannot really do a version of mmin_de() for this class
70  because there's no good way to rewrite the gradient
71  without the contribution from the parameters which are
72  fixed.
73  \endcomment
74  */
75  template<class func_t=multi_funct11,
77  class mmin_fix_params : public mmin_base<func_t,func_t,vec_t> {
78 
79  public:
80 
82 
83  /// The generic minimizer type
86 
87  /// The default minimizer type
89 
90  /** \brief Specify the member function pointer
91  */
93  mmp=&def_mmin;
94  }
95 
96  virtual ~mmin_fix_params() {}
97 
98  /** \brief Calculate the minimum \c min of \c func w.r.t. the
99  array \c x of size \c nvar.
100  */
101  virtual int mmin(size_t nvar, vec_t &x, double &fmin,
102  func_t &func) {
103 
104  if (fixp.size()<nvar) {
105  fixp.resize(nvar);
106  for(size_t i=0;i<nvar;i++) fixp[i]=false;
107  }
108 
109  // Copy arguments for later use
110  xp=&x;
111  funcp=&func;
112  unv=nvar;
113 
114  nv_new=0;
115  for(size_t i=0;i<nvar;i++) {
116  if (fixp[i]==false) nv_new++;
117  }
118  if (nv_new==0) return 0;
119 
120  // Copy initial guess to new format
121  size_t j=0;
122  ubvector xnew(nv_new);
123  for(size_t i=0;i<nvar;i++) {
124  if (fixp[i]==false) {
125  xnew[j]=x[i];
126  j++;
127  }
128  }
129 
130  // Perform minimization
131  mmp->verbose=this->verbose;
132  mmp->ntrial=this->ntrial;
133  mmp->tol_rel=this->tol_rel;
134  mmp->tol_abs=this->tol_abs;
135 
136  int ret=mmp->mmin(nv_new,xnew,fmin,*this);
137  if (ret!=0) {
138  O2SCL_ERR("Minimizer failed in mmin_fix_params::mmin_fix().",ret);
139  }
140 
141  // Copy final result back to original format
142  j=0;
143  for(size_t i=0;i<nvar;i++) {
144  if (fixp[i]==false) {
145  x[i]=xnew[j];
146  j++;
147  }
148  }
149 
150  this->last_ntrial=mmp->last_ntrial;
151 
152  return ret;
153  }
154 
155  template<class bool_vec_t> void set_fix(size_t n, bool_vec_t &fix) {
156  fixp.resize(n);
157  for(size_t i=0;i<n;i++) fixp[i]=fix[i];
158  return;
159  }
160 
161  /** \brief Calculate the minimum of \c func while fixing
162  some parameters as specified in \c fix.
163 
164  If all of entries <tt>fix[0], fix[1], ... fix[nvar-1]</tt>
165  are true, then this function assumes all of the parameters
166  are fixed and that there is no minimization to be performed.
167  In this case, it will return 0 for success without calling
168  the error handler.
169  */
170  template<class bool_vec_t>
171  int mmin_fix(size_t nvar, ubvector &x, double &fmin,
172  bool_vec_t &fix, multi_funct11 &func) {
173 
174  fixp.resize(nvar);
175  for(size_t i=0;i<nvar;i++) fixp[i]=fix[i];
176 
177  return mmin(nvar,x,fmin,func);
178  }
179 
180  /// Change the base minimizer
181  int set_mmin(base_mmin_t &min) {
182 
183  mmp=&min;
184  return 0;
185  }
186 
187  /// The default base minimizer
189 
190  /** \brief The new function to send to the minimizer
191  */
192  virtual double operator()(size_t nv, const vec_t &x) {
193 
194  ubvector tmp(unv);
195  size_t j=0;
196  for(size_t i=0;i<unv;i++) {
197  if (nv_new<unv && fixp[i]==true) {
198  tmp[i]=(*xp)[i];
199  } else {
200  tmp[i]=x[j];
201  j++;
202  }
203  }
204  return (*funcp)(unv,tmp);
205  }
206 
207 #ifndef DOXYGEN_INTERNAL
208 
209  protected:
210 
211  /// The minimizer
213 
214  /// The user-specified function
215  func_t *funcp;
216 
217  /// The user-specified number of variables
218  size_t unv;
219 
220  /// The new number of variables
221  size_t nv_new;
222 
223  /// Specify which parameters to fix
224  std::vector<bool> fixp;
225 
226  /// The user-specified initial vector
227  vec_t *xp;
228 
229  private:
230 
232  mmin_fix_params& operator=(const mmin_fix_params&);
233 
234 #endif
235 
236  };
237 
238 
239 #ifndef DOXYGEN_NO_O2NS
240 }
241 #endif
242 
243 #endif
int mmin_fix(size_t nvar, ubvector &x, double &fmin, bool_vec_t &fix, multi_funct11 &func)
Calculate the minimum of func while fixing some parameters as specified in fix.
Definition: mmin_fix.h:171
virtual int mmin(size_t nvar, vec_t &x, double &fmin, func_t &func)
Calculate the minimum min of func w.r.t. the array x of size nvar.
Definition: mmin_fix.h:101
mmin_base< mmin_fix_params< func_t, vec_t >, mmin_fix_params< func_t, vec_t >, vec_t > base_mmin_t
The generic minimizer type.
Definition: mmin_fix.h:85
vec_t * xp
The user-specified initial vector.
Definition: mmin_fix.h:227
size_t nv_new
The new number of variables.
Definition: mmin_fix.h:221
virtual int mmin(size_t nvar, vec_t &x, double &fmin, func_t &func)=0
Calculate the minimum min of func w.r.t. the array x of size nvar.
virtual double operator()(size_t nv, const vec_t &x)
The new function to send to the minimizer.
Definition: mmin_fix.h:192
func_t * funcp
The user-specified function.
Definition: mmin_fix.h:215
int set_mmin(base_mmin_t &min)
Change the base minimizer.
Definition: mmin_fix.h:181
Multidimensional minimization by the Simplex method (v2) (GSL)
Definition: mmin_simp2.h:119
int verbose
Output control.
Definition: mmin.h:193
mmin_fix_params()
Specify the member function pointer.
Definition: mmin_fix.h:92
Multidimensional minimization [abstract base].
Definition: mmin.h:163
def_mmin_t def_mmin
The default base minimizer.
Definition: mmin_fix.h:188
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
Definition: err_hnd.h:273
base_mmin_t * mmp
The minimizer.
Definition: mmin_fix.h:212
std::vector< bool > fixp
Specify which parameters to fix.
Definition: mmin_fix.h:224
int last_ntrial
The number of iterations for in the most recent minimization.
Definition: mmin.h:205
size_t unv
The user-specified number of variables.
Definition: mmin_fix.h:218
double tol_abs
The independent variable tolerance.
Definition: mmin.h:202
double tol_rel
Function value tolerance.
Definition: mmin.h:199
Multidimensional minimizer fixing some parameters and varying others.
Definition: mmin_fix.h:77
std::function< double(size_t, const boost::numeric::ublas::vector< double > &)> multi_funct11
Multi-dimensional function typedef.
Definition: multi_funct.h:45
mmin_simp2< mmin_fix_params< func_t, vec_t >, vec_t > def_mmin_t
The default minimizer type.
Definition: mmin_fix.h:88
int ntrial
Maximum number of iterations.
Definition: mmin.h:196

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..