⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example2.html.svn-base

📁 OPT++
💻 SVN-BASE
字号:
/** \page example2 Example 2: Nonlinear Interior-Point Method With General ConstraintsThis example is intended to demonstrate how to set up and solve aproblem with general constraints and analytic derivative information.In particular, this example is Hock and Schittkowski problem number65, i.e.<em> minimize </em> \f[ (x_1 - x_2)^2 + (1/9)(x_1 + x_2 - 10)^2 + (x_3 - 5)^2 \f]<em> subject to </em> \f[ x_1^2 + x_2^2 + x_3^2 \le 48, \f]<em> </em>  \f[-4.5 \le x_1 \le 4.5, \f]<em> </em>  \f[-4.5 \le x_2 \le 4.5, \f]<em> </em>  \f[ -5.0 \le x_3 \le 5.0 \f]Hock and Schittkowski problem number 65 has bound constraints and nonlinear constraints. In addition, analyticgradient and Hessian information is available for both the objectivefunction and the nonlinear constraints.  We opted to use a nonlinearinterior-point method to solve this problem. Recall that it is necessary to write C++ code for the main routinethat sets up the problem and the algorithm and for the subroutinesthat initialize and evaluate the function and the constraints.  Westep through the specifics below.<ul>  <li> \ref main2 <br>  <li> \ref function2 <br>  <li> \ref run2 <br></ul>\section main2 Main RoutineFirst include the necessary header files.  Start with any C++/C headerfiles that are needed.  In this case, none are necessary.  The twoheader files are OPT++ header files.  NLF contains objects, data, andmethods required for setting up the function/problem.  The next threeheader files contain objects, data, and methods required for theconstraints.  OptNIPS contains the objects, data, and methods requiredfor using the nonlinear interior-point optimization method.  The last four statements correspond to the use of namespaces. The<tt> using NEWMAT::ColumnVector</tt> statement imports the data member \a ColumnVector from the matrix library namespace NEWMAT.  The use of namespaces prevents potential conflicts with third party libraries that may also have a datamembers named \a ColumnVector, \a Matrix, and \a SymmetricMatrix. The last statement allows you to access methods and data members in the OPTPP namespace.<table><tr><td>\code#include "NLF.h"#include "BoundConstraint.h"#include "NonLinearInequality.h"#include "CompoundConstraint.h"#include "OptNIPS.h"using NEWMAT::ColumnVector;using NEWMAT::Matrix;using NEWMAT::SymmetricMatrix;using namespace OPTPP;\endcode</table>The first two lines serve as the declarations of the pointers to thesubroutines that initialize the problem and evaluate the objectivefunction, respectively.  The third line is the pointer to thesubroutine that evaluates the nonlinear constraints.<table><tr><td>\codevoid init_hs65(int ndim, ColumnVector& x);void hs65(int mode, int ndim, const ColumnVector& x, double& fx,           ColumnVector& gx, SymmetricMatrix& Hx, int& result);void ineq_hs65(int mode, int ndim, const ColumnVector& x,	       ColumnVector& cx, Matrix& cgx,               OptppArray<SymmetricMatrix>& cHx, int& result);\endcode</table>The first thing to do is set up the constraint object.  This is brokendown into several phases.  First, set the dimension of the problem andallocate the space for the upper and lower bounds.  Assign the valuesof the bounds, and create a Constraint object to hold all of the boundconstraints.<table><tr><td>\codeint main (){  int ndim = 3;  ColumnVector lower(ndim), upper(ndim); // Here is one way to assign values to a ColumnVector.  lower << -4.5 << -4.5 << -5.0;  upper <<  4.5 <<  4.5 <<  5.0 ;  Constraint c1 = new BoundConstraint(ndim, lower, upper);\endcode</table>Nonlinear constraints are similar in nature to the objective function.As such they are created in a similar manner.  Since analytic firstand second derivatives for the nonlinear constraints are available, anNLF2 is constructed.  The calling sequence includes the dimension ofthe problem, the number of nonlinear constraints, the pointer to thesubroutine that performs the constraint evaluation, and the pointer tothe subroutine that initializes the problem.  Then a Constraint objectis created to hold all of the nonlinear constraints.<table><tr><td>\code  NLP* chs65 = new NLP(new NLF2(ndim, 1, ineq_hs65, init_hs65));  Constraint nleqn = new NonLinearInequality(chs65);\endcode</table>Once the constraint sets for each particular type of constraint havebeen created, it is time to roll them all into one object.  That iseasily accomplished by the following line.<table><tr><td>\code  CompoundConstraint* constraints = new CompoundConstraint(nleqn, c1);\endcode</table>The next few lines complete the setup of the problem.  Create thenonlinear function object using the dimension of the problem, thepointers to the subroutines declared above, and theCompoundConstraint object.  The NLF2 object is used since analyticgradient and Hessian are available.  <table><tr><td>\code  NLF2 nips(ndim, hs65, init_hs65, constraints);\endcode</table>  Build a nonlinear interior-point algorithm object using the nonlinearproblem that has just been created.  In addition, set any of thealgorithmic parameters to desired values.  All parameters have defaultvalues, so it is not necessary to set them unless you have specificvalues you wish to use.  In this example, we set the name of theoutput file, the function tolerance (used as a stopping criterion),the maximum number iterations allowed, and the merit function to beused.<table><tr><td>\code  OptNIPS objfcn(&nips);// The "0" in the second argument says to create a new file.  A "1"// would signify appending to an existing file.  objfcn.setOutputFile("example2.out", 0);  objfcn.setFcnTol(1.0e-06);  objfcn.setMaxIter(150);  objfcn.setMeritFcn(ArgaezTapia);\endcode</table>Now call the algorithm's optimize method to solve the problem.<table><tr><td>\code  objfcn.optimize();\endcode</table>Print out some summary information and clean up before exiting.  Thesummary information is handy, but not necessary.  The cleanup flushesthe I/O buffers.<table><tr><td>\code  objfcn.printStatus("Solution from nips");  objfcn.cleanup();}\endcode</table>Now that the main routine is in place, we step through the coderequired for the initialization and evaluation of the function.\section function2 User-Defined FunctionsThis section contains examples of the user-defined functions that arerequired.  The first performs the initialization of the problem.  Thesecond performs the evaluation of the function.  The last performs theevaluation of the nonlinear constraints.First, include the necessary header files.  In this case, we need theOPT++ header file, NLP, for some definitions. We also need to list whichitems we are using from the NEWMAT namespace.  We recommend this approach as opposed <tt>using namespace NEWMAT </tt> to reduce the potential fornaming conflicts.<table><tr><td>\code#include "NLP.h"using NEWMAT::ColumnVector;using NEWMAT::Matrix;using NEWMAT::SymmetricMatrix;\endcode</table>The subroutine that initializes the problem should perform anyone-time tasks that are needed for the problem.  One part of that ischecking for error conditions in the setup.  In this case, thedimension, \a ndim, can only take on a value of 3.  Using "exit"is not the ideal way to deal with error conditions, but it serves wellas an example.<table><tr><td>\codevoid init_hs65(int ndim, ColumnVector& x){  if (ndim != 3)    exit (1);  double factor = 0.0;\endcode</table>The initialization is also an ideal place to set the initial values ofthe optimization parameters, \a x.  This can be hard coded, asdone here, or it can be done in some other manner (e.g., reading themin from a file, the code for which should appear here).<table><tr><td>\code// ColumnVectors are indexed from 1, and they use parentheses around// the index.  x(1) = -5.0  - (factor - 1)*8.6505;  x(2) =  5.0  + (factor - 1)*1.3495;  x(3) =  0.0  - (factor - 1)*4.6204;}\endcode</table>The next piece of code is a subroutine that will evaluate thefunction.  In this problem, we are trying to find the minimum value ofHock and Schittkowski problem 65, so it is necessary to write the codethat computes the value of that function given some set of optimizationparameters.  Mathematically, that function is:\f[f(x) = (x_1 - x_2)^2 + (1/9)(x_1 + x_2 - 10)^2 + (x_3 - 5)^2 \f]The following code will compute the value of \a f(x).First, some error checking and manipulation of the optimizationparameters, \a x, are done.<table><tr><td>\codevoid hs65(int mode, int ndim, const ColumnVector& x, double& fx, ColumnVector& gx, SymmetricMatrix& Hx, int& result){  double f1, f2, f3, x1, x2, x3;  if (ndim != 3)     exit(1);  x1 = x(1);  x2 = x(2);  x3 = x(3);  f1 = x1 - x2;  f2 = x1 + x2 - 10.0;  f3 = x3 - 5.0;\endcode</table>If a function evaluation is requested, then the function value,\a fx, is computed, and the \a result variable is set toindicate that a function evaluation has been done.  <table><tr><td>\code  if (mode & NLPFunction) {    fx  = f1*f1+ (f2*f2)/9.0 +f3*f3;    result = NLPFunction;  }\endcode</table>If a gradient evaluation is requested, then the gradient, \a gx,is computed, and the \a result variable is set to indicate thata gradient evaluation has been done.<table><tr><td>\code  if (mode & NLPGradient) {    gx(1) =  2*f1 + (2.0/9.0)*f2;    gx(2) = -2*f1 + (2.0/9.0)*f2;    gx(3) =  2*f3;    result = NLPGradient;  }\endcode</table>If a Hessian evaluation is requested, then the Hessian, \a Hx,is computed, and the \a result variable is set to indicate thata Hessian evaluation has been done.<table><tr><td>\code// The various Matrix objects have two indices, are indexed from 1,// and they use parentheses around // the index.  if (mode & NLPHessian) {    Hx(1,1) =  2 + (2.0/9.0);    Hx(2,1) = -2 + (2.0/9.0);    Hx(2,2) =  2 + (2.0/9.0);    Hx(3,1) = 0.0;    Hx(3,2) =  0.0;    Hx(3,3) =  2.0;    result = NLPHessian;  }}\endcode</table>In a similar manner, the function, gradient, and Hessian values forthe nonlinear constraints must be computed.  For this problem, thenonlinear constraint is the following:\f[c(x) = 48 - x_1^2 - x_2^2 - x_3^2 \f]The code for computing the function, gradient, and Hessian for thisconstraint appears below.  The step-by-step breakdown is almostexactly the same as it is for the function evaluation, so we do not gothrough it here.<table><tr><td>\codevoid ineq_hs65(int mode, int ndim, const ColumnVector& x, ColumnVector& cx, Matrix& cgx, OptppArray<SymmetricMatrix>& cHx, int& result){ // Hock and Schittkowski's Problem 65   double f1, f2, f3, x1, x2, x3;  SymmetricMatrix Htmp(ndim);  if (ndim != 3)     exit(1);  x1 = x(1);  x2 = x(2);  x3 = x(3);  f1 = x1;  f2 = x2;  f3 = x3;  if (mode & NLPFunction) {    cx(1)  = 48 - f1*f1 - f2*f2 - f3*f3;    result = NLPFunction;  }  if (mode & NLPGradient) {    cgx(1,1) = -2*x1;    cgx(2,1) = -2*x2;    cgx(3,1) = -2*x3;    result = NLPGradient;  }  if (mode & NLPHessian) {    Htmp(1,1) = -2;    Htmp(1,2) = 0.0;    Htmp(1,3) = 0.0;    Htmp(2,1) = 0.0;    Htmp(2,2) = -2;    Htmp(2,3) = 0.0;    Htmp(3,1) = 0.0;    Htmp(3,2) = 0.0;    Htmp(3,3) = -2;    cHx[0] = Htmp;    result = NLPHessian;  }}\endcode</table>On a more general note, these subroutines could serve as wrappers to Cor Fortran subroutines.  Similarly, it could make a system call to acompletely independent executable.  As long as the values of<em>fx</em> and <em>result</em> are set when all is said and done, itdoes not matter how the function value is computed.Now that we have all of the code necessary to set up and solve thisHock and Schittkowski function, give it a try!\section run2 Building and Running the ExampleIf you want to try running this example, the following steps should dothe trick.<ol>      <li> Determine which defines you need.  If the C++ compiler you	   are using supports the ANSI standard style of C header	   files, you will need           \verbatim		-DHAVE_STD           \endverbatim	   If the C++ compiler you are using supports namespaces, you	   will need           \verbatim		-DHAVE_NAMESPACES           \endverbatim	   If you are using the parallel version of OPT++, you will	   need           \verbatim		-DWITH_MPI           \endverbatim      <li> Determine the location of the header files.  If you did a	   "make install", they will be located in the "include"	   subdirectory of the directory in which OPT++ is installed.	   If that directory is not one your compiler normally checks,	   you will need           \verbatim		-IOPT++_install_directory/include           \endverbatim	   If you did not do a "make install", the header files will	   almost certainly be in a directory not checked by your	   compiler.  Thus, you will need           \verbatim		-IOPT++_top_directory/include -IOPT++_top_directory/newmat11           \endverbatim	<li> Determine the location of the libraries.  If you did a	   "make install", they will be located in the "lib"	   subdirectory of the directory in which OPT++ is installed.	   If that directory is not one your compiler normally checks,	   you will need           \verbatim		-LOPT++_install_directory/lib           \endverbatim	   If you did not do a "make install", the libraries will	   almost certainly be in a directory not checked by your	   compiler.  Thus, you will need           \verbatim		-LOPT++_top_directory/lib/.libs           \endverbatim	<li> If you configured OPT++ for the default behavior of using	   the BLAS and/or you configure OPT++ to use NPSOL, you will	   need the appropriate Fortran libraries for linking.  The	   easiest way to get these is to look in the Makefile for the	   value of FLIBS.	<li> If all is right in the world, the following format for your	   compilation command should work:           \verbatim		$CXX <defines> <includes> example2.C tstfcn.C <lib \		directory> -lopt -lnewmat -l$BLAS_LIB $FLIBS            \endverbatim	   $CXX is the C++ compiler you are using.  <defines> and	   <includes> are the flags determined in steps 1-2.  example2.C	   is your main routine, and tstfcn.C contains your function	   evaluations.  (Note: If you have put them both in one file,	   you need only list that single file here.)  <lib_directory	   was determined in step 3.  -lopt and -lnewmat are the two	   OPT++ libraries.  $BLAS_LIB is the BLAS library you are	   using, and $FLIBS is the list of Fortran libraries	   determined in step 4.</ol>You should now be able to run the executable (type "./example2").  Youcan compare the results, found in example2.out, to <ahref="example2_out.html">our results</a>.  There may be slightdifferences due to operating system, compiler, etc., but the resultsshould very nearly match.<p> Previous Example:  \ref example1 | Back to \ref SetUp </p>Last revised <em> April 27, 2007 </em>.*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -