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.
Define your objective function as a file or anonymous function. For details, see Compute Objective Functions. If your solver is
lsqcurvefitorlsqnonlin, ensure the objective function returns a vector, not scalar.If relevant, create your constraints, such as bounds and nonlinear constraint functions. For details, see Write Constraints.
Create a start point. For example, to create a three-dimensional random start point
xstart:xstart = randn(3,1);
(Optional) Create options using
optimoptions. For example,options = optimoptions(@fmincon,Algorithm="interior-point");Enter
problem = createOptimProblem(solver,
where
solveris the name of your local solver:For
GlobalSearch:"fmincon"For
MultiStartthe choices are:"fmincon""fminunc""lsqcurvefit""lsqnonlin"
For help choosing, see Optimization Decision Table.
Set an initial point using the
x0parameter. If your initial point isxstart, and your solver isfmincon, your entry is nowproblem = createOptimProblem("fmincon",x0=xstart,Include the function handle for your objective function in
objective:problem = createOptimProblem("fmincon",x0=xstart, ... objective=@objfun,
Set bounds and other constraints as applicable.
Constraint Name lower bounds "lb"upper bounds "ub"matrix Aineqfor linear inequalitiesAineq x≤bineq"Aineq"vector bineqfor linear inequalitiesAineq x≤bineq"bineq"matrix Aeqfor linear equalitiesAeq x=beq"Aeq"vector beqfor linear equalitiesAeq x=beq"beq"nonlinear constraint function "nonlcon"If using the
lsqcurvefitlocal solver, include vectors of input data and response data, named"xdata"and"ydata"respectively.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].
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);Write the linear constraint matrices. Change the constraint to “less than” form:
A = [-1,-2]; b = -4;
Create the local options to use the
interior-pointalgorithm:opts = optimoptions(@fmincon,Algorithm="interior-point");Create the problem structure with
createOptimProblem:problem = createOptimProblem("fmincon", ... x0=[2;3],objective=sixmin, ... Aineq=A,bineq=b,options=opts)
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]Best practice: validate the problem structure by running your solver on the structure:
[x,fval,exitflag,output] = fmincon(problem);