Documentation

# optimconstr

Create empty optimization constraint array

## Syntax

``constr = optimconstr(N)``
``constr = optimconstr(cstr)``
``constr = optimconstr(cstr1,N2,...,cstrk)``
``constr = optimconstr({cstr1,cstr2,...,cstrk})``
``constr = optimconstr([N1,N2,...,Nk])``

## Description

example

````constr = optimconstr(N)` creates an `N`-by-1 array of empty optimization constraints. Use `constr` to initialize a loop that creates constraint expressions.```

example

````constr = optimconstr(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-n`cstr`, where n`cstr` is the number of elements of `cstr`, then `constr` is also 1-by-n`cstr`. Otherwise, `constr` is n`cstr`-by-1.```
````constr = optimconstr(cstr1,N2,...,cstrk)` or `constr = optimconstr({cstr1,cstr2,...,cstrk})` or `constr = optimconstr([N1,N2,...,Nk])`, for any combination of `cstr` and `N` arguments, creates an n`cstr`1-by-`N`2-by-...-by-n`cstr`k array of empty optimization constraints, where n`cstr` is the number of elements in `cstr`.```

## 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 showconstr(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);```

See that the new constraints are the same as the constraints in `stockbalance`:

`showconstr(stockbalance2)`
```(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 <= 250*i; end```

Check the constraint for `dinner`.

`showconstr(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 ```

## Input Arguments

collapse all

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

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

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

• The size of `constr = optimconstr(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.

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) = 5*k*(x(2*k) - x(2*k-1)) <= 10 - 2*k; end```

## Limitations

• Each constraint expression in a problem must use the same comparison. For example, the following code leads to an error, because `cons1` uses the `<=` comparison, `cons2` uses the `>=` comparison, and `cons1` and `cons2` are in the same expression.

```prob = optimproblem; x = optimvar('x',2,'LowerBound',0); cons1 = x(1) + x(2) <= 10; cons2 = 3*x(1) + 4*x(2) >= 2; prob.Constraints = [cons1;cons2]; % This line throws an error```

You can avoid this error by using separate expressions for the constraints.

```prob.Constraints.cons1 = cons1; prob.Constraints.cons2 = cons2;```