Open this Example

Create Constraints in Loop

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, 1) + sell(1, 1) + stock(1, 1) == 100

(2, 1)

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

(3, 1)

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

(4, 1)

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

(5, 1)

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

(6, 1)

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

(7, 1)

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

(8, 1)

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

(9, 1)

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

(10, 1)

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

(11, 1)

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

(12, 1)

  -buy(12, 1) + sell(12, 1) - stock(11, 1) + stock(12, 1) == 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, 1) + sell(1, 1) + stock(1, 1) == 100

(2, 1)

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

(3, 1)

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

(4, 1)

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

(5, 1)

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

(6, 1)

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

(7, 1)

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

(8, 1)

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

(9, 1)

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

(10, 1)

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

(11, 1)

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

(12, 1)

  -buy(12, 1) + sell(12, 1) - stock(11, 1) + stock(12, 1) == 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.