user496181
user496181

Reputation: 119

create equation summing over variables with shifting index gaps in GAMS

I want to add a constraint in gams that sums over a variable that takes indices with a shifting gap between them. For example, I have a set of hour (h) in the year. My variable is g(h). My desired equations are:

g(1) + g(2) + g(3) = 0
g(3) + g(4) + g(5) = 0
g(5) + g(6) + g(7) = 0

So summing over every three consecutive g(h) but shifting by 2 indices each time. Is there a way to do this? Thank you

Upvotes: 0

Views: 161

Answers (1)

Lutz
Lutz

Reputation: 2292

Here you go:

Set i /i1*i10/;
Alias (i,ii);

Variable z, g(i);

Equation dummy, e(i);

dummy.. z =e= 1;
e(i)$(mod(ord(i),2)=1).. sum(ii$((ord(ii)>=ord(i) and (ord(ii)<=ord(i)+2))), g(ii)) =e= 0;

option LimRow = 10;
model m /all/;
solve m min z use lp;

This will result in the following solution listing:

---- e  =E=  

e(i1)..  g(i1) + g(i2) + g(i3) =E= 0 ; (LHS = 0)
     
e(i3)..  g(i3) + g(i4) + g(i5) =E= 0 ; (LHS = 0)
     
e(i5)..  g(i5) + g(i6) + g(i7) =E= 0 ; (LHS = 0)
     
e(i7)..  g(i7) + g(i8) + g(i9) =E= 0 ; (LHS = 0)
     
e(i9)..  g(i9) + g(i10) =E= 0 ; (LHS = 0)

Upvotes: 1

Related Questions