![]() |
Object-oriented Scientific Computing Library: Version 0.910
|
The constrained minimization classes operate in a similar way to the other multi-dimensional minimization classes (which are derived from multi_min). The constraints are specified with the function
ool_constr_mmin::set_constraints(size_t nc, vec_t &lower, vec_t &upper);
and the minimization can be performed by calling either multi_min::mmin() or multi_min::mmin_de() (if the gradient is provided by the user). The method in ool_mmin_gencan requires a Hessian vector product and the user can specify this product for the minimization by using ool_constr_mmin::mmin_hess(). The Hessian product function can be specified as an object of type ool_hfunct in a similar way to the other function objects in O2scl .
There are five error codes defined in ool_constr_mmin which are specific to the classes derived from OOL.
The class gsl_anneal can handle some kinds of constraints by ignoring proposed steps which cause the user-specified function to return a non-zero value.
Also, a simple way of implementing constraints is to add a function to the original which increases the value outside of the allowed region. This can be done with the functions constraint() and lower_bound(). There are two analogous functions, cont_constraint() and cont_lower_bound(), which continuous and differentiable versions. Where possible, it is better to use the constrained minimization routines described above.
This example minimizes the function
which is undefined for and
. The function is also minimized by gsl_mmin_simp2, which goes outside the allowed region where the function is undefined.
/* Example: ex_conmin.cpp ------------------------------------------------------------------- This gives an example of the use of a constrained minimizer. This code finds the global minimum of a two-dimensional function which is not well-defined outside the region of interest. */ #include <gsl/gsl_math.h> #include <gsl/gsl_blas.h> #include <o2scl/test_mgr.h> #include <o2scl/ool_mmin_spg.h> #include <o2scl/gsl_mmin_simp2.h> using namespace std; using namespace o2scl; double func(size_t nv, const ovector_base &x) { if (x[0]<0.0 || x[1]<0.0 || x[0]>100.0 || x[1]>100.0) { cout << "Outside constraint region." << endl; } double ret=(log(x[0])*x[0]*x[0]+1.0)*(sqrt(x[1])*(x[1]-1.0)+1.0); return ret; } int dfunc(size_t nv, ovector_base &x, ovector_base &g) { g[0]=(x[0]+2.0*x[0]*log(x[0]))*(sqrt(x[1])*(x[1]-1.0)+1.0); g[1]=(log(x[0])*x[0]*x[0]+1.0)*(sqrt(x[1])+(x[1]-1.0)/2.0/sqrt(x[1])); return 0; } int main(void) { test_mgr t; t.set_output_level(1); cout.setf(ios::scientific); static const size_t nv=2; // Specify the function to minimize and its gradient multi_funct_fptr<> mff(func); grad_funct_fptr<> gff(dfunc); // The unconstrained minimizer gsl_mmin_simp2<> gm1; // The constrained minimizer ool_mmin_spg<> omp; // The constraints and the location of the minimum ovector c1(nv), c2(nv), x(nv); double fmin; cout << "Simple minimizer: " << endl; // Initial guess for(size_t i=0;i<nv;i++) { x[i]=10.0; } // Minimize gm1.mmin(nv,x,fmin,mff); cout << endl; cout << "Constrained minimizer: " << endl; // Initial guess for(size_t i=0;i<nv;i++) { x[i]=10.0; } // Set constraints c1.set_all(1.0e-9); c2.set_all(100.0); omp.set_constraints(nv,c1,c2); // Minimize omp.mmin_de(nv,x,fmin,mff,gff); // Output results cout << x[0] << " " << x[1] << " " << fmin << endl; // Test the constrained minimizer results t.test_rel(x[0],0.60655,1.0e-4,"x0"); t.test_rel(x[1],1.0/3.0,1.0e-4,"x1"); t.report(); return 0; } // End of example
Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).