optimconstr - Create empty optimization constraint array - MATLAB (original) (raw)

Create empty optimization constraint array

Syntax

Description

Use optimconstr to initialize a set of constraint expressions.

[constr](#d126e141364) = optimconstr([N](#d126e141279)) creates an N-by-1 array of empty optimization constraints. Useconstr to initialize a loop that creates constraint expressions.

example

[constr](#d126e141364) = optimconstr([cstr](#d126e141328)) creates an array of empty optimization constraints that are indexed bycstr, a cell array of character vectors or string vectors.

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

example

[constr](#d126e141364) = optimconstr([cstr](#d126e141328)1,[N](#d126e141279)2,...,[cstr](#d126e141328)k) or`constr` = optimconstr({`cstr`1,`cstr`2,...,`cstr`k}) or`constr` = optimconstr([`N`1,`N`2,...,`N`k]), for any combination of cstr and N arguments, creates an_n_cstr1-by-N2-by-...-by-ncstrk array of empty optimization constraints, where_n_cstr is the number of elements incstr.

Examples

collapse all

Create constraints for an inventory model. The stock of goods at the start of each period is equal to the stock at the end of the previous period. During each period, the stock increases by buy and decreases by sell. The variable stock is the stock at the end of the period.

N = 12; stock = optimvar('stock',N,1,'Type','integer','LowerBound',0); buy = optimvar('buy',N,1,'Type','integer','LowerBound',0); sell = optimvar('sell',N,1,'Type','integer','LowerBound',0); initialstock = 100;

stockbalance = optimconstr(N,1);

for t = 1:N if t == 1 enterstock = initialstock; else enterstock = stock(t-1); end stockbalance(t) = stock(t) == enterstock + buy(t) - sell(t); end

show(stockbalance)

(1, 1)

-buy(1) + sell(1) + stock(1) == 100

(2, 1)

-buy(2) + sell(2) - stock(1) + stock(2) == 0

(3, 1)

-buy(3) + sell(3) - stock(2) + stock(3) == 0

(4, 1)

-buy(4) + sell(4) - stock(3) + stock(4) == 0

(5, 1)

-buy(5) + sell(5) - stock(4) + stock(5) == 0

(6, 1)

-buy(6) + sell(6) - stock(5) + stock(6) == 0

(7, 1)

-buy(7) + sell(7) - stock(6) + stock(7) == 0

(8, 1)

-buy(8) + sell(8) - stock(7) + stock(8) == 0

(9, 1)

-buy(9) + sell(9) - stock(8) + stock(9) == 0

(10, 1)

-buy(10) + sell(10) - stock(9) + stock(10) == 0

(11, 1)

-buy(11) + sell(11) - stock(10) + stock(11) == 0

(12, 1)

-buy(12) + sell(12) - stock(11) + stock(12) == 0

Include the constraints in a problem.

prob = optimproblem; prob.Constraints.stockbalance = stockbalance;

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

tt = ones(N-1,1); d = diag(tt,-1); % shift index by -1 stockbalance2 = stock == d*stock + buy - sell; stockbalance2(1) = stock(1) == initialstock + buy(1) - sell(1);

Show the new constraints to verify that they are the same as the constraints in stockbalance.

(1, 1)

-buy(1) + sell(1) + stock(1) == 100

(2, 1)

-buy(2) + sell(2) - stock(1) + stock(2) == 0

(3, 1)

-buy(3) + sell(3) - stock(2) + stock(3) == 0

(4, 1)

-buy(4) + sell(4) - stock(3) + stock(4) == 0

(5, 1)

-buy(5) + sell(5) - stock(4) + stock(5) == 0

(6, 1)

-buy(6) + sell(6) - stock(5) + stock(6) == 0

(7, 1)

-buy(7) + sell(7) - stock(6) + stock(7) == 0

(8, 1)

-buy(8) + sell(8) - stock(7) + stock(8) == 0

(9, 1)

-buy(9) + sell(9) - stock(8) + stock(9) == 0

(10, 1)

-buy(10) + sell(10) - stock(9) + stock(10) == 0

(11, 1)

-buy(11) + sell(11) - stock(10) + stock(11) == 0

(12, 1)

-buy(12) + sell(12) - stock(11) + stock(12) == 0

Creating constraints in a loop can be more time-consuming than creating constraints by matrix operations. However, you are less likely to create an erroneous constraint by using loops.

Create indexed constraints and variables to represent the calories consumed in a diet. Each meal has a different calorie limit.

meals = ["breakfast","lunch","dinner"]; constr = optimconstr(meals); foods = ["cereal","oatmeal","yogurt","peanut butter sandwich","pizza","hamburger",... "salad","steak","casserole","ice cream"]; diet = optimvar('diet',foods,meals,'LowerBound',0); calories = [200,175,150,450,350,800,150,650,350,300]'; for i = 1:3 constr(i) = diet(:,i)'calories <= 250i; end

Check the constraint for dinner.

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

Input Arguments

collapse all

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

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 equivalentcellstr("Tp").

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

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

Data Types: string | cell

Output Arguments

collapse all

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

For example:

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

Limitations

Tips

Version History

Introduced in R2017b