Page 1 of 1

Create an shifting index gap

Posted: Fri Jun 11, 2021 7:54 am
by gams_learner58
Hi everyone,

I'm fairly new to gams and I have a problem that I'd appreciate your input on: I have a set I which includes hours in a year so 1 to 8760. I have a variable g(h) which is index by hour h. I want to write an equation to satisfy these constraints:
g(1) + g(2) + g(3) + g(4) = 0
g(3) + g(4) + g(5) + g(6) = 0
g(5) + g(6) + g(7) + g(8) = 0
...

So starting at hour 1, the sum of the first 4 consecutive variables is zero, after that there is a shifting window of 2 indices then the sum of the next 4 indices are zero, and so on and so on until the end of the year. I can't think of how to do this. Any help is greatly appreciated! Thanks.

Re: Create an shifting index gap

Posted: Sat Jun 12, 2021 8:57 am
by bussieck
Read about ordered set including lag and leeds at https://www.gams.com/latest/docs/UG_OrderedSets.html.

Code: Select all

set h / 1*8760 /, gh(h,h);
scalar i;
for (i=0 to 3,
  gh(h,h+i)$mod(ord(h),2)= yes;
)

alias (h,hh);
variable g(h);
equation e(h);
e(h)$mod(ord(h),2).. sum(gh(h,hh), g(hh)) =e= 0;
-Michael

Re: Create an shifting index gap

Posted: Mon Jun 14, 2021 7:27 am
by bussieck
My colleague Alex Meeraus had an even better suggestion:

Code: Select all

set h / 1*8760 /, hh(h) / 1*4 /;
variable g(h);
equation e(h);
e(h)$mod(ord(h),2).. sum(hh, g(h+hh.off)) =e= 0;
He also pointed out that one as a modeler always has to think about the boundaries of the set. So what should happen for the last elements of h? Should the sequence be just shorter or do you want circular behavior (then use h++hh.off).

-Michael