# How to create constrained objects

a constraint has finite lower and upper bounds, OPT++ treats it as two separate constraints. In the computation of the residuals and gradients constraints with finite lower bounds appear first followed by those with finte upper bounds. Consequently, in the optimization summary, you will see the constraint count is double the original number of constraints.

Now, we are ready to build an constrained nonlinear program.

Let's consider the two-dimensional Rosenbrock problem with bound constraints.

minimize

subject to

Step 1: Build your compound constraint.

```   int ndim =  2;
ColumnVector lower(ndim), upper(ndim);
lower    = -2.0;
upper    =  2.0;

Constraint bc = new BoundConstraint(ndim, lower, upper);
CompoundConstraint* rosen_constraints = new CompoundConstraint(bc);
```

Step 2: Create a nonlinear function with analytic derivatives.

• First, write a function which initializes x0.
```   void init_rosen (int ndim, ColumnVector& x)
{
if (ndim != 2)
{
exit (1);
}
x(1) = -1.2;
x(2) =  1.0;
}
```
• Next, write a function that evaluates the Rosenbrock function and gradient.
```   void rosen(int mode, int n, const ColumnVector& x, double& fx,
ColumnVector& g, int& result)
{ // Rosenbrock's function
double f1, f2, x1, x2;

if (n != 2) return;

x1 = x(1);
x2 = x(2);
f1 = (x2 - x1 * x1);
f2 = 1. - x1;

if (mode & NLPFunction) {
fx  = 100.* f1*f1 + f2*f2;
}
if (mode & NLPGradient) {
g(1) = -400.*f1*x1 - 2.*f2;
g(2) = 200.*f1;
}
result = NLPFunction & NLPGradient;
}
```

Step 3: Create a constrained nonlinear problem with analytic derivatives

```   NLF1 nips(n,rosen,init_rosen,rosen_constraints);
```

Voila! You're done.

Bug Reports    OPT++ Developers    Copyright Information    GNU Lesser General Public License
Documentation, generated by , last revised August 30, 2006.