Reputation: 575
Consider the following data:
set i / 1 * 4 /
tid / 2019, 2020 /
iagg / 1 * 2 /;
parameter t(i,tid),tagg(iagg,tid);
t(i,tid) = uniform(0,1);
set itoiagg(i,iagg) /
1.1
2.1
3.2
4.2
/;
I want to do the following calculation the aggregated set (iagg):
tagg("1","2020") = (t("1","2020")/t("1","2019")*(t("1","2019")/(t("1","2019")+t("2","2019")))) +
(t("2","2020")/t("2","2019")*(t("2","2019")/(t("1","2019")+t("2","2019"))));
In this simple case, I could also do it for the second iagg:
tagg("2","2020") = (t("3","2020")/t("3","2019")*(t("3","2019")/(t("3","2019")+t("4","2019")))) +
(t("4","2020")/t("4","2019")*(t("4","2019")/(t("3","2019")+t("4","2019"))));
But I have a big dataset, so is there an easy way where I can do this calculation using a loop or something like that?
Upvotes: 0
Views: 65
Reputation: 2292
What about this:
set i / 1 * 4 /
tid / 2019, 2020 /
iagg / 1 * 2 /;
parameter t(i,tid),tagg(iagg,tid);
t(i,tid) = uniform(0,1);
set itoiagg(iagg,i) /
1.1
1.2
2.3
2.4
/;
tagg(iagg,tid)$(ord(tid)>1) = sum(itoiagg(iagg,i), (t(i,tid)/t(i,tid-1)*(t(i,tid-1)/(t(i,tid-1)+t(i,tid-1)))));
Sidenote: I turned around the indices of itoiagg
in the assignment, since it is generally avisable to reference indices in the order they are controlled in GAMS. That could make a performance difference, if the data gets large.
Upvotes: 1