Main Content

varindex

Map problem variables to solver-based variable index

Description

example

idx = varindex(prob) returns the linear indices of problem variables as a structure or an integer vector. If you convert prob to a problem structure by using prob2struct, idx gives the variable indices in the resulting problem structure that correspond to the variables in prob.

example

idx = varindex(prob,varname) returns the linear indices of elements of varname.

Examples

collapse all

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 prob variables.

idx = varindex(prob);
disp(idx.x)
     1     2     3
disp(idx.y)
     4     5     6     7     8     9    10    11    12

Obtain the y indices only.

idxy = varindex(prob,'y')
idxy = 1×9

     4     5     6     7     8     9    10    11    12

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 true.

solverprob = prob2struct(prob,x0);
solverprob.options = optimoptions(solverprob.options,"SpecifyObjectiveGradient",true);

Solve the problem using fmincon.

[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 varindex.

idx = varindex(prob);
sol.x = sols(idx.x);
sol.y = sols(idx.y);

The y index that varindex uses is a linear index. Reshape the variable sol.y to have the size of x0.y.

sol.y = reshape(sol.y,size(x0.y));

Check that the two solution structures are identical.

isequal(sol,solp)
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 solve cannot.

Input Arguments

collapse all

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 eqnproblem.

Warning

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.

Example: prob = optimproblem; prob.Objective = obj; prob.Constraints.cons1 = cons1;

Example: prob = eqnproblem; prob.Equations = eqs;

Variable name, specified as a character vector or string.

Example: 'x'

Data Types: char | string

Output Arguments

collapse all

Linear indices of problem variables, returned as a structure or an integer vector. If you convert prob to a problem structure by using prob2struct, idx gives the variable indices in the resulting problem structure that correspond to the variables in prob.

  • When you call idx = varindex(prob), the returned idx is 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 idx is the vector of linear indices to which the variable varname maps in the associated solver-based problem variable.

See Obtain Problem Indices.

Introduced in R2019a