mmin_fix.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, 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  \comment
66  We cannot really do a version of mmin_de() for this class
67  because there's no good way to rewrite the gradient
68  without the contribution from the parameters which are
69  fixed.
70  \endcomment
71  */
72  template<class func_t=multi_funct11,
74  class mmin_fix_params : public mmin_base<func_t,func_t,vec_t> {
75 
76  public:
77 
79 
80  /// The generic minimizer type
83 
84  /// The default minimizer type
86 
87  /** \brief Specify the member function pointer
88  */
90  mmp=&def_mmin;
91  }
92 
93  virtual ~mmin_fix_params() {}
94 
95  /** \brief Calculate the minimum \c min of \c func w.r.t. the
96  array \c x of size \c nvar.
97  */
98  virtual int mmin(size_t nvar, vec_t &x, double &fmin,
99  func_t &func) {
100 
101  if (fixp.size()<nvar) {
102  fixp.resize(nvar);
103  for(size_t i=0;i<nvar;i++) fixp[i]=false;
104  }
105 
106  // Copy arguments for later use
107  xp=&x;
108  funcp=&func;
109  unv=nvar;
110 
111  nv_new=0;
112  for(size_t i=0;i<nvar;i++) {
113  if (fixp[i]==false) nv_new++;
114  }
115  if (nv_new==0) return 0;
116 
117  // Copy initial guess to new format
118  size_t j=0;
119  ubvector xnew(nv_new);
120  for(size_t i=0;i<nvar;i++) {
121  if (fixp[i]==false) {
122  xnew[j]=x[i];
123  j++;
124  }
125  }
126 
127  // Perform minimization
128  mmp->verbose=this->verbose;
129  mmp->ntrial=this->ntrial;
130  mmp->tol_rel=this->tol_rel;
131  mmp->tol_abs=this->tol_abs;
132 
133  int ret=mmp->mmin(nv_new,xnew,fmin,*this);
134  if (ret!=0) {
135  O2SCL_ERR("Minimizer failed in mmin_fix_params::mmin_fix().",ret);
136  }
137 
138  // Copy final result back to original format
139  j=0;
140  for(size_t i=0;i<nvar;i++) {
141  if (fixp[i]==false) {
142  x[i]=xnew[j];
143  j++;
144  }
145  }
146 
147  this->last_ntrial=mmp->last_ntrial;
148 
149  return ret;
150  }
151 
152  template<class bool_vec_t> void set_fix(size_t n, bool_vec_t &fix) {
153  fixp.resize(n);
154  for(size_t i=0;i<n;i++) fixp[i]=fix[i];
155  return;
156  }
157 
158  /** \brief Calculate the minimum of \c func while fixing
159  some parameters as specified in \c fix.
160 
161  If all of entries <tt>fix[0], fix[1], ... fix[nvar-1]</tt>
162  are true, then this function assumes all of the parameters
163  are fixed and that there is no minimization to be performed.
164  In this case, it will return 0 for success without calling
165  the error handler.
166  */
167  template<class bool_vec_t>
168  int mmin_fix(size_t nvar, ubvector &x, double &fmin,
169  bool_vec_t &fix, multi_funct11 &func) {
170 
171  fixp.resize(nvar);
172  for(size_t i=0;i<nvar;i++) fixp[i]=fix[i];
173 
174  return mmin(nvar,x,fmin,func);
175  }
176 
177  /// Change the base minimizer
178  int set_mmin(base_mmin_t &min) {
179 
180  mmp=&min;
181  return 0;
182  }
183 
184  /// The default base minimizer
185  def_mmin_t def_mmin;
186 
187  /** \brief The new function to send to the minimizer
188  */
189  virtual double operator()(size_t nv, const vec_t &x) {
190 
191  ubvector tmp(unv);
192  size_t j=0;
193  for(size_t i=0;i<unv;i++) {
194  if (nv_new<unv && fixp[i]==true) {
195  tmp[i]=(*xp)[i];
196  } else {
197  tmp[i]=x[j];
198  j++;
199  }
200  }
201  return (*funcp)(unv,tmp);
202  }
203 
204 #ifndef DOXYGEN_INTERNAL
205 
206  protected:
207 
208  /// The minimizer
209  base_mmin_t *mmp;
210 
211  /// The user-specified function
212  func_t *funcp;
213 
214  /// The user-specified number of variables
215  size_t unv;
216 
217  /// The new number of variables
218  size_t nv_new;
219 
220  /// Specify which parameters to fix
221  std::vector<bool> fixp;
222 
223  /// The user-specified initial vector
224  vec_t *xp;
225 
226  private:
227 
229  mmin_fix_params& operator=(const mmin_fix_params&);
230 
231 #endif
232 
233  };
234 
235 
236 #ifndef DOXYGEN_NO_O2NS
237 }
238 #endif
239 
240 #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:168
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:98
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:82
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
vec_t * xp
The user-specified initial vector.
Definition: mmin_fix.h:224
size_t nv_new
The new number of variables.
Definition: mmin_fix.h:218
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:189
func_t * funcp
The user-specified function.
Definition: mmin_fix.h:212
int set_mmin(base_mmin_t &min)
Change the base minimizer.
Definition: mmin_fix.h:178
Multidimensional minimization by the Simplex method (v2) (GSL)
Definition: mmin_simp2.h:123
int verbose
Output control.
Definition: mmin.h:194
mmin_fix_params()
Specify the member function pointer.
Definition: mmin_fix.h:89
Multidimensional minimization [abstract base].
Definition: mmin.h:164
def_mmin_t def_mmin
The default base minimizer.
Definition: mmin_fix.h:185
#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:209
std::vector< bool > fixp
Specify which parameters to fix.
Definition: mmin_fix.h:221
int last_ntrial
The number of iterations for in the most recent minimization.
Definition: mmin.h:206
size_t unv
The user-specified number of variables.
Definition: mmin_fix.h:215
double tol_abs
The independent variable tolerance.
Definition: mmin.h:203
double tol_rel
Function value tolerance.
Definition: mmin.h:200
Multidimensional minimizer fixing some parameters and varying others.
Definition: mmin_fix.h:74
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:85
int ntrial
Maximum number of iterations.
Definition: mmin.h:197

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).