minimize
subject to
For this example, we will assume that that analytic first derivatives are available (but not second derivatives). We will also assume that the subroutines that initialize, evaluate the function, and evaluate the nonlinear constraint are in the same file. We step through the specifics below.
First, include the necessary header files. In this case, we need iostream so we can print error messages and the OPT++ header file, NLP.h, for some definitions. Also, because we are going to great a dynamically loadable library, we need to surround all of our code by an extern "C" statement.
#include <iostream> #include "NLP.h" extern "C" { |
The subroutine that initializes the problem should perform any one-time tasks that are needed for the problem. One part of that is checking for error conditions in the setup. In this case, the dimension, ndim, can only take on a value of 2. Using "exit" is not the ideal way to deal with error conditions, but it serves well as an example. In addition to checking the error condition, we also do some manipulation of the initial values.
void init_hs14(int ndim, ColumnVector& x) { if (ndim != 2) { cerr << "Number of variables for Hock Problem 14 should be 2." << " The number of variables given is " << ndim << endl; exit (1); } //end if x(1) = 2+(x(1) -1)* 1.1771243447; x(2) = 2+(x(2) -1)* 1.0885621722; } //end init_hs14 |
The second required subroutine will evaluate the function. In this problem, we are trying to find the minimum value of Hock-Schittkowski Problem 14, so it is necessary to write the code that computes the value of that function given some set of optimization parameters. In addition, since we are assuming that first derivatives are available, we must also provide the code to compute the gradient. Mathematically, the function is given by:
The following code will compute the value of f(x).
First, some manipulation of the optimization parameters, x, is done.
void hs14(int mode, int n, const ColumnVector& x, double& fx, ColumnVector& g, int& result) { double f1, f2, x1, x2; x1 = x(1); x2 = x(2); f1 = x1 - 2.0; f2 = x2 - 1.0; |
Then the function or gradient is computed. Notice how the mode and result variables are used to determine/report the type of evaluation done.
if (mode & NLPFunction) { fx = f1*f1 + f2*f2; result = NLPFunction; } //end f(x) if (mode & NLPGradient) { g(1) = 2*(x1 - 2.0); g(2) = 2*(x2 - 1.0); result = NLPGradient; } //end g(x) } //end hs14 |
In addition to the function, we must also provide the code to evaluate the nonlinear constraint. We will also include code for the first derivative of the constraint, and we will put this code in the same library as the function evaluation. The code looks very similar to that for the function, except now the expression is the following:
Also, notice the differences in the argument list. This is due to the fact that there could be multiple constraints computed in this function. In the current GUI/XML set-up, however, we are restricted to only one constraint per function. This will change in future releases.
First, some manipulation of the optimization parameters, x, is done.
void ineq_hs14(int mode, int n, const ColumnVector& x, ColumnVector& fx, Matrix& g, int& result) { double f1, f2, x1, x2; x1 = x(1); x2 = x(2); f1 = x1*x1; f2 = x2*x2; |
Then the function or gradient is computed. Notice how the mode and result variables are used to determine/report the type of evaluation done.
if (mode & NLPFunction) { fx = -.25*f1 - f2 + 1.0; result = NLPFunction; } //end f(x) if (mode & NLPGradient) { g(1,1) = -0.5*x1; g(2,1) = -2.0*x2; result = NLPGradient; } //end g(x) } //end ineq_hs14 } //end extern "C" |
Now that we have all of the code necessary to initialize and evaluate Hock-Schittkowski Problem 14, give it a try!
SOURCES = testexample.C
for csh or tcsh, <br> setenv LD_LIBRARY_PATH /home/pdhough/TooMuchFun
for bash, <br> set LD_LIBRARY_PATH=/home/pdhough/TooMuchFun export LD_LIBRARY_PATH
./testexample
If you like, you can compare your output to our results. There may be slight differences, but if you used the same input that we did, the results should look pretty much the same.
Previous Example: Subroutine Example 1: Rosenbrock's Function | Back to Using the (beta) GUI/XML Interface
Last revised July 25, 2006