User Tools

Site Tools


How do I model a band matrix?

A band matrix can be modeled efficiently in GAMS using circular leads (++) and circular lags (--). See chapter 13.4 of the GAMS User's Guide for more information. Using circular leads and lags, the first and last members of the set are assumed to be adjacent, so as to form a circular sequence of members. These operators only work with ordered sets.

Find below an example, where the usage of circular leads (equation eq) was ~60 times faster than the original formulation using sum - statements and lots of comparisons (equation eq1 and eq2). Both models are identical.

set k / k1*k300/
    t / t1*t700/;
alias (t,tt,ll, t2);

parameter l(k); l(k) = uniformInt(1,6);

variable y(k,t), z(k,t), obj;
equation defobj;
defobj.. obj =e= 0;
set ktl(k,t),
kl(k,ll) = ord(ll) <= l(k);

equation eq;
eq(k,t).. sum(kl(k,ll), y(k,t++(ord(ll)-1))) =l= z(k,t++(l(k)-1));

model m1 /eq, defobj/;
solve m1 min obj using lp;

*original formulation
equation eq1, eq2;

              sum(t2$((ord(t2)>=ord(t)) and(ord(t2)<ord(t)+l(k))),y(k,t2))
eq2(k,t)$(ord(t)> card(t)-l(k)+1) ..
      sum(t2$(ord(t2)>=ord(t)),Y(k,t2)) +

model m2 /defobj, eq1, eq2/;
solve m2 min obj using lp;
gams/model_a_band_matrix.txt · Last modified: 2008/09/25 12:56 by support