Main Content

Create Problem Structure

About Problem Structures

To use the `GlobalSearch` or `MultiStart` solvers, you must first create a problem structure. The recommended way to create a problem structure is using the `createOptimProblem` function. You can create a structure manually, but doing so is error-prone.

Use the `createOptimProblem` Function

Follow these steps to create a problem structure using the `createOptimProblem` function.

1. Define your objective function as a file or anonymous function. For details, see Compute Objective Functions. If your solver is `lsqcurvefit` or `lsqnonlin`, ensure the objective function returns a vector, not scalar.

2. If relevant, create your constraints, such as bounds and nonlinear constraint functions. For details, see Write Constraints.

3. Create a start point. For example, to create a three-dimensional random start point `xstart`:

`xstart = randn(3,1);`
4. (Optional) Create options using `optimoptions`. For example,

`options = optimoptions(@fmincon,'Algorithm','interior-point');`
5. Enter

`problem = createOptimProblem(solver,`

where `solver` is the name of your local solver:

• For `GlobalSearch`: `'fmincon'`

• For `MultiStart` the choices are:

• `'fmincon'`

• `'fminunc'`

• `'lsqcurvefit'`

• `'lsqnonlin'`

For help choosing, see Optimization Decision Table.

6. Set an initial point using the `'x0'` parameter. If your initial point is `xstart`, and your solver is `fmincon`, your entry is now

`problem = createOptimProblem('fmincon','x0',xstart,`
7. Include the function handle for your objective function in `objective`:

```problem = createOptimProblem('fmincon','x0',xstart, ... 'objective',@objfun,```
8. Set bounds and other constraints as applicable.

ConstraintName
lower bounds`'lb'`
upper bounds`'ub'`
matrix `Aineq` for linear inequalities `Aineq x` ≤ `bineq``'Aineq'`
vector `bineq` for linear inequalities `Aineq x` ≤ `bineq``'bineq'`
matrix `Aeq` for linear equalities `Aeq x` = `beq``'Aeq'`
vector `beq` for linear equalities `Aeq x` = `beq``'beq'`
nonlinear constraint function`'nonlcon'`
9. If using the `lsqcurvefit` local solver, include vectors of input data and response data, named `'xdata'` and `'ydata'` respectively.

10. Best practice: validate the problem structure by running your solver on the structure. For example, if your local solver is `fmincon`:

`[x,fval,exitflag,output] = fmincon(problem);`

Example: Create a Problem Structure with `createOptimProblem`

This example minimizes the function from Run the Solver, subject to the constraint x1 + 2x2 ≥ 4. The objective is

 sixmin = 4x2 – 2.1x4 + x6/3 + xy – 4y2 + 4y4. (1)

Use the `interior-point` algorithm of `fmincon`, and set the start point to `[2;3]`.

1. Write a function handle for the objective function.

```sixmin = @(x)(4*x(1)^2 - 2.1*x(1)^4 + x(1)^6/3 ... + x(1)*x(2) - 4*x(2)^2 + 4*x(2)^4);```
2. Write the linear constraint matrices. Change the constraint to “less than” form:

```A = [-1,-2]; b = -4;```
3. Create the local options to use the `interior-point` algorithm:

`opts = optimoptions(@fmincon,'Algorithm','interior-point');`
4. Create the problem structure with `createOptimProblem`:

```problem = createOptimProblem('fmincon', ... 'x0',[2;3],'objective',sixmin, ... 'Aineq',A,'bineq',b,'options',opts)```
5. The resulting structure:

```problem = struct with fields: objective: @(x)(4*x(1)^2-2.1*x(1)^4+x(1)^6/3+x(1)*x(2)-4*x(2)^2+4*x(2)^4) x0: [2x1 double] Aineq: [-1 -2] bineq: -4 Aeq: [] beq: [] lb: [] ub: [] nonlcon: [] solver: 'fmincon' options: [1x1 optim.options.Fmincon]```
6. Best practice: validate the problem structure by running your solver on the structure:

`[x,fval,exitflag,output] = fmincon(problem);`