Map problem variables to solver-based variable index
Obtain Problem Indices
Create an optimization problem.
x = optimvar('x',3); y = optimvar('y',3,3); prob = optimproblem('Objective',x'*y*x);
Convert the problem to a structure.
problem = prob2struct(prob);
Obtain the linear indices in
problem of all
idx = varindex(prob); disp(idx.x)
1 2 3
4 5 6 7 8 9 10 11 12
y indices only.
idxy = varindex(prob,'y')
idxy = 1×9 4 5 6 7 8 9 10 11 12
Solve Problem Using Both Approaches
This example shows how to obtain most of the same information using either the problem-based approach or the solver-based approach. First create a problem and solve it using the problem based approach.
x = optimvar('x',3,1,'LowerBound',1,'UpperBound',1); y = optimvar('y',3,3,'LowerBound',-1,'UpperBound',1); prob = optimproblem('Objective',x'*y*x + [2 3 4]*x); rng default x0.x = rand(3, 1); x0.y = rand(3, 3); [solp,fvalp,exitflagp,outputp] = solve(prob,x0);
Solving problem using fmincon. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
Next, convert the problem to solver-based form using
prob2struct. To have the
fmincon solver use the automatic gradients in the problem, set the
SpecifyObjectiveGradient option to
solverprob = prob2struct(prob,x0); solverprob.options = optimoptions(solverprob.options,"SpecifyObjectiveGradient",true);
Solve the problem using
[sols,fvals,exitflags,outputs] = fmincon(solverprob);
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
To convert the
fmincon solution to the structure form returned by
solve, create appropriate structures using
idx = varindex(prob); sol.x = sols(idx.x); sol.y = sols(idx.y);
y index that
varindex uses is a linear index. Reshape the variable
sol.y to have the size of
sol.y = reshape(sol.y,size(x0.y));
Check that the two solution structures are identical.
ans = logical 1
The reason that the two approaches are not completely equivalent is that
fmincon can return more arguments such as Lagrange multipliers, whereas
prob — Optimization problem or equation problem
OptimizationProblem object |
Optimization problem or equation problem, specified as an
OptimizationProblem object or an
EquationProblem object. Create an optimization problem by using
optimproblem; create an equation problem by using
The problem-based approach does not support complex values in an objective function, nonlinear equalities, or nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result can be incorrect.
prob = optimproblem; prob.Objective = obj; prob.Constraints.cons1 =
prob = eqnproblem; prob.Equations = eqs;
varname — Variable name
character vector | string
Variable name, specified as a character vector or string.
idx — Linear indices of problem variables
structure | integer vector
Linear indices of problem variables, returned as a structure or an integer vector.
If you convert
prob to a problem structure by using
idx gives the variable indices in the resulting problem structure
that correspond to the variables in
When you call
idx = varindex(prob), the returned
idxis a structure. The field names of the structure are the variable names in
prob. The value for each field is the integer vector of linear indices to which the variables map in the associated solver-based problem variable.
When you call
idx = varindex(prob,varname), the returned
idxis the vector of linear indices to which the variable
varnamemaps in the associated solver-based problem variable.