How to: Use Constrained Non-Linear Programming Using the Nelder-Mead Solver

# How to: Use Constrained Non-Linear Programming Using the Nelder-Mead Solver

Solver Foundation 3.0

You can use the Nelder-Mead solver (NelderMeadSolver) for some types of constrained non-linear programming. For example, the Nelder-Mead solver can find the local optimum of a general variable constrained objective function.

One example of this technique is to use the Nelder-Mead (NM) solver to find the global minimum of the box-constrained two-dimensional Rosenbrock function created by mathematician Howard Rosenbrock in 1960. The following example uses a variant of the Nelder-Mead method to search for a local optimum by iteratively changing variable values over a range of possible solutions. The solver stops computation when the difference in objective values is less than ToleranceDifference.

### To solve a 2-D Rosenbrock function using the Nelder-Mead solver

1. Create a Console Application named Rosenbrock.

2. Add a reference to Microsoft Solver Foundation on the .NET tab.

3. Add the following Imports or using statements to the top of the Program code file.

```using Microsoft.SolverFoundation.Common;
using Microsoft.SolverFoundation.Solvers;
```
4. In the Main method, define initial values, lower bounds and upper bounds for the output variables by typing the following code:

```double[] xInitial = new double[] { 1, 1 };
double[] xLower = new double[] { -1, -1 };
double[] xUpper = new double[] { 1, 1 };
```
5. Call the NelderMeadSolver and supply the Rosenbrock function, initial values, and bounds by typing the following code:

```var solution = NelderMeadSolver.Solve(
x => (100 * Math.Pow(x[1] - Math.Pow(x[0], 2), 2)) + Math.Pow(1 - x[0], 2), xInitial, xLower, xUpper);
```
6. Print the results by typing the following code.

```Console.WriteLine(solution.Result);
Console.WriteLine("solution = {0}", solution.GetSolutionValue(0));
Console.WriteLine("x = {0}", solution.GetValue(1));
Console.WriteLine("y = {0}", solution.GetValue(2));
```
7. Press F5 to build and run the code.

The command window shows the following results.

LocalOptimal

solution = 0

x = 1

y = 1

Show: