/** \page example2 Example 2: Nonlinear Interior-Point Method With General Constraints This example is intended to demonstrate how to set up and solve a problem with general constraints and analytic derivative information. In particular, this example is Hock and Schittkowski problem number 65, i.e. minimize \f[ (x_1 - x_2)^2 + (1/9)(x_1 + x_2 - 10)^2 + (x_3 - 5)^2 \f] subject to \f[ x_1^2 + x_2^2 + x_3^2 \le 48, \f] \f[-4.5 \le x_1 \le 4.5, \f] \f[-4.5 \le x_2 \le 4.5, \f] \f[ -5.0 \le x_3 \le 5.0 \f] Hock and Schittkowski problem number 65 has bound constraints and nonlinear constraints. In addition, analytic gradient and Hessian information is available for both the objective function and the nonlinear constraints. We opted to use a nonlinear interior-point method to solve this problem. Recall that it is necessary to write C++ code for the main routine that sets up the problem and the algorithm and for the subroutines that initialize and evaluate the function and the constraints. We step through the specifics below.
\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 |
\code
void 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 |
\code int 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 |
\code NLP* chs65 = new NLP(new NLF2(ndim, 1, ineq_hs65, init_hs65)); Constraint nleqn = new NonLinearInequality(chs65); \endcode |
\code CompoundConstraint* constraints = new CompoundConstraint(nleqn, c1); \endcode |
\code NLF2 nips(ndim, hs65, init_hs65, constraints); \endcode |
\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 |
\code objfcn.optimize(); \endcode |
\code objfcn.printStatus("Solution from nips"); objfcn.cleanup(); } \endcode |
\code #include "NLP.h" using NEWMAT::ColumnVector; using NEWMAT::Matrix; using NEWMAT::SymmetricMatrix; \endcode |
\code void init_hs65(int ndim, ColumnVector& x) { if (ndim != 3) exit (1); double factor = 0.0; \endcode |
\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 |
\code void 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 |
\code if (mode & NLPFunction) { fx = f1*f1+ (f2*f2)/9.0 +f3*f3; result = NLPFunction; } \endcode |
\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 |
\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 |
\code
void ineq_hs65(int mode, int ndim, const ColumnVector& x, ColumnVector& cx, Matrix& cgx, OptppArray |
Previous Example: \ref example1 | Back to \ref SetUp
Last revised April 27, 2007 . */