Sums and loops
- Summations
-
Sum up an array of variables in a constraint:
MaxCap := sum(p in 1..10) buy(p) <= 100 MaxCap := sum(p in 1..10) (buy(p) + sum(r in 1..5) make(p,r)) <= 100 MaxCap := sum(p in 1..NP, t in 1..NT) CAP(p)*buy(p,t) <= MAXCAP MaxCap := sum(p in 1..NP) (2*CAP(p)*buy(p)/10 + SCAP(p)*sell(p)) <= MAXCAP
- Loops
-
Use a loop to assign an array of constraints:
forall(t in 2..NT) Inven(t) := bal(t) = bal(t-1) + buy(t) - sell(t)
Use do/ end-do to group several statements into one loopforall(t in 1..NT) do MaxRef(t) := sum(i in 1..NI) use(i,t) <= MAXREF(t) Inven(t) := store(t) = store(t-1) + buy(t) - use(t) end-do
Can nest forall statements:forall(t in 1..NT) do MaxRef(t) := sum(i in 1..NI) use(i,t) <= MAXREF(t) forall(i in 1..NI) Inven(i,t) := store(i,t) = store(i,t-1) + buy(i,t) - use(i,t) end-do
Similarly for specification of bounds (a bound is just a simple unnamed constraint):forall(i in 1..NI) do forall(t in 1..NT) store(i,t) <= MAXSTORE(t) store(i,0) = STORE0 end-do
May include conditions in sums or loops:forall(c in 1..10 | CAP(c)>=100.0) MaxCap(c) := sum(i in 1..10, j in 1..10 | i<>j) TECH(i,j,c)*x(i,j,c) <= MAXTECH(c)