Constrained Minimization

O2scl reimplements the Open Optimization Library (OOL) available at http://ool.sourceforge.net. The associated classes allow constrained minimization when the constraint can be expressed as a hyper-cubic constraint on all of the independent variables. The routines have been rewritten and reformatted for C++ in order to facilitate the use of member functions and user-defined vector types as arguments. The base class is mmin_constr and there are two different constrained minimzation algorithms implemented in o2scl::mmin_constr_pgrad, o2scl::mmin_constr_spg. (The o2scl::mmin_constr_gencan minimizer is not yet finished). The O2scl implementation should be essentially identical to the most recently released version of OOL.

The constrained minimization classes operate in a similar way to the other multi-dimensional minimization classes (which are derived from o2scl::mmin_base). The constraints are specified with the function

mmin_constr::set_constraints(size_t nc, vec_t &lower,
vec_t &upper);

and the minimization can be performed by calling either o2scl::mmin_base::mmin() or o2scl::mmin_base::mmin_de() (if the gradient is provided by the user). The method in o2scl::mmin_constr_gencan requires a Hessian vector product and the user can specify this product for the minimization by using o2scl::mmin_constr::mmin_hess(). The Hessian product function can be specified as an object of type o2scl::ool_hfunct in a similar way to the other function objects in O2scl .

There are five error codes defined in o2scl::mmin_constr which are specific to the classes derived from OOL.

The class o2scl::anneal_gsl 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 o2scl::lower_bound(). There are two analogous functions, o2scl::cont_constraint() and o2scl::cont_lower_bound(), which continuous and differentiable versions. Where possible, it is better to use the constrained minimization routines described above.

Constrained minimization example

This example minimizes the function

\[ f(x,y) = \left[x^2 \log(x)+1\right]\left[\sqrt{y}(y-1)+1\right)] \]

which is undefined for $ x<0 $ and $ y<0 $. The function is also minimized by o2scl::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 <boost/numeric/ublas/vector.hpp>
#include <gsl/gsl_math.h>
#include <gsl/gsl_blas.h>
#include <o2scl/test_mgr.h>
#include <o2scl/mmin_constr_spg.h>
#include <o2scl/mmin_simp2.h>
using namespace std;
using namespace o2scl;
double func(size_t nv, const ubvector &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, ubvector &x, ubvector &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) {
cout.setf(ios::scientific);
static const size_t nv=2;
// Specify the function to minimize and its gradient
multi_funct mff11=func;
grad_funct gff=dfunc;
// The unconstrained minimizer
// The constrained minimizer
// The constraints and the location of the minimum
ubvector 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,mff11);
cout << endl;
cout << "Constrained minimizer: " << endl;
// Initial guess
for(size_t i=0;i<nv;i++) {
x[i]=10.0;
}
// Set constraints
for(size_t i=0;i<nv;i++) {
c1[i]=1.0e-9;
c2[i]=100.0;
}
omp.set_constraints(nv,c1,c2);
// Minimize
omp.mmin_de(nv,x,fmin,mff11,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
o2scl::mmin_simp2
Multidimensional minimization by the Simplex method (v2) (GSL)
Definition: mmin_simp2.h:123
boost::numeric::ublas::vector< double >
o2scl::mmin_simp2::mmin
virtual int mmin(size_t nn, vec_t &xx, double &fmin, func_t &ufunc)
Calculate the minimum min of func w.r.t the array x of size nvar.
Definition: mmin_simp2.h:410
o2scl
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
Definition: anneal.h:42
o2scl::test_mgr::report
bool report() const
Provide a report of all tests so far.
o2scl::mmin_constr< multi_funct, grad_funct, ool_hfunct, boost::numeric::ublas::vector< double > >::mmin_de
virtual int mmin_de(size_t nvar, boost::numeric::ublas::vector< double > &xx, double &fmin, multi_funct &ff, grad_funct &df)
Calculate the minimum min of func w.r.t. the array x of size nvar with gradient dfunc.
Definition: mmin_constr.h:385
o2scl::multi_funct
std::function< double(size_t, const boost::numeric::ublas::vector< double > &)> multi_funct
Multi-dimensional function typedef in src/base/multi_funct.h.
Definition: multi_funct.h:45
o2scl::test_mgr::test_rel
bool test_rel(data_t result, data_t expected, data_t rel_error, std::string description)
Test for .
Definition: test_mgr.h:168
o2scl::mmin_constr< multi_funct, grad_funct, ool_hfunct, boost::numeric::ublas::vector< double > >::set_constraints
virtual int set_constraints(size_t nc, boost::numeric::ublas::vector< double > &lower, boost::numeric::ublas::vector< double > &upper)
Set the constraints.
Definition: mmin_constr.h:327
o2scl::test_mgr
A class to manage testing and record success and failure.
Definition: test_mgr.h:50
o2scl::grad_funct
std::function< int(size_t, boost::numeric::ublas::vector< double > &, boost::numeric::ublas::vector< double > &)> grad_funct
Array of multi-dimensional functions typedef in src/min/mmin.h.
Definition: mmin.h:42
o2scl::test_mgr::set_output_level
void set_output_level(int l)
Set the output level.
Definition: test_mgr.h:119
o2scl::mmin_constr_spg
Constrained minimization by the spectral projected gradient method (OOL)
Definition: mmin_constr_spg.h:84

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