Main Content

optimineq

Create empty optimization inequality array

Description

Use optimineq to initialize a set of inequality expressions.

Tip

For the full workflow, see Problem-Based Optimization Workflow.

constr = optimineq(N) creates an N-by-1 array of empty optimization inequalities. Use constr to initialize a loop that creates inequality expressions.

example

constr = optimineq(cstr) creates an array of empty optimization constraints that are indexed by cstr, a cell array of character vectors or string vectors.

If cstr is 1-by-ncstr, where ncstr is the number of elements of cstr, then constr is also 1-by-ncstr. Otherwise, constr is ncstr-by-1.

example

constr = optimineq(cstr1,N2,...,cstrk) or constr = optimineq({cstr1,cstr2,...,cstrk}) or constr = optimineq([N1,N2,...,Nk]), for any combination of cstr and N arguments, creates an ncstr1-by-N2-by-...-by-ncstrk array of empty optimization inequalities, where ncstr is the number of elements in cstr.

Examples

collapse all

Create the constraint that a two-element variable x must lie in the intersections of a number of disks whose centers and radii are in the arrays centers and radii.

x = optimvar('x',1,2);
centers = [1 -2;3 -4;-2 3];
radii = [6 7 8];
constr = optimineq(length(radii));
for i = 1:length(constr)
    constr(i) = sum((x - centers(i,:)).^2) <= radii(i)^2;
end

View the inequality expressions.

show(constr)
  arg_LHS <= arg_RHS

  where:

        arg1 = zeros(3, 1);
        arg1(1) = sum((x - extraParams{1}).^2);
        arg1(2) = sum((x - extraParams{2}).^2);
        arg1(3) = sum((x - extraParams{3}).^2);
        arg_LHS = arg1(:);
        arg1 = zeros(3, 1);
        arg1(1) = 36;
        arg1(2) = 49;
        arg1(3) = 64;
        arg_RHS = arg1(:);

    extraParams{1}:

     1    -2


  extraParams{2}:

     3    -4


  extraParams{3}:

    -2     3

Instead of using a loop, you can create the same constraints by using matrix operations on the variables.

constr2 = sum(([x;x;x] - centers).^2,2) <= radii'.^2;

Creating inequalities in a loop can be more time consuming than creating inequalities by using matrix operations.

Create indexed inequalities and variables to represent the calories consumed in a diet. Each meal has a different calorie limit. Create arrays representing the meals, foods, and calories for each food.

meals = ["breakfast","lunch","dinner"];
foods = ["cereal","oatmeal","yogurt","peanut butter sandwich","pizza","hamburger",...
    "salad","steak","casserole","ice cream"];
calories = [200,175,150,450,350,800,150,650,350,300]';

Create optimization variables representing the foods for each meal, indexed by food names and meal names.

diet = optimvar('diet',foods,meals,'LowerBound',0);

Set the inequality constraints that each meal has an upper bound on the calories in the meal.

constr = optimineq(meals);
for i = 1:3
    constr(i) = diet(:,i)'*calories <= 250*i;
end

View the inequalities for dinner.

show(constr("dinner"))
  200*diet('cereal', 'dinner') + 175*diet('oatmeal', 'dinner') + 150*diet('yogurt', 'dinner') + 450*diet('peanut butter sandwich', 'dinner') + 350*diet('pizza', 'dinner') + 800*diet('hamburger', 'dinner') + 150*diet('salad', 'dinner') + 650*diet('steak', 'dinner') + 350*diet('casserole', 'dinner') + 300*diet('ice cream', 'dinner') <= 750

Instead of using a loop, you can create the same inequalities by using matrix operations on the variables.

constr2 = diet'*calories <= 250*(1:3)';

Include the appropriate index names for the inequalities by setting the IndexNames property.

constr2.IndexNames = {meals, {}};

Display the new inequalities for dinner. Note that they are the same as the previous inequalities.

show(constr2("dinner"))
  200*diet('cereal', 'dinner') + 175*diet('oatmeal', 'dinner') + 150*diet('yogurt', 'dinner') + 450*diet('peanut butter sandwich', 'dinner') + 350*diet('pizza', 'dinner') + 800*diet('hamburger', 'dinner') + 150*diet('salad', 'dinner') + 650*diet('steak', 'dinner') + 350*diet('casserole', 'dinner') + 300*diet('ice cream', 'dinner') <= 750

Creating inequalities in a loop can be more time consuming than creating inequalities by using matrix operations.

Input Arguments

collapse all

Size of the constraint dimension, specified as a positive integer.

  • The size of constr = optimineq(N) is N-by-1.

  • The size of constr = optimineq(N1,N2) is N1-by-N2.

  • The size of constr = optimineq(N1,N2,...,Nk) is N1-by-N2-by-...-by-Nk.

Example: 5

Data Types: double

Names for indexing, specified as a cell array of character vectors or a string vector.

Note

cstr cannot be a string scalar such as "Tp", but must be a vector such as ["Tp" "ul"]. To specify a single name, use {'Tp'} or the equivalent cellstr("Tp").

Example: {'red','orange','green','blue'}

Example: ["red";"orange";"green";"blue"]

Data Types: string | cell

Output Arguments

collapse all

Constraints, returned as an empty OptimizationInequality array. Use constr to initialize a loop that creates constraint expressions.

For example,

x = optimvar('x',8);
constr = optimineq(4);
for k = 1:4
    constr(k) = 5*k*(x(2*k) - x(2*k-1)) <= 10 - 2*k;
end

Tips

  • It is generally more efficient to create constraints by vectorized expressions rather than loops. See Create Efficient Optimization Problems.

  • You can use optimconstr instead of optimineq to create inequality constraints for optimization problems.

Version History

Introduced in R2019b