sum function problem Topic is solved

Problems with modeling
Post Reply
abdullahturk
User
User
Posts: 4
Joined: 1 year ago

sum function problem

Post by abdullahturk »

Hi everybody,
I want to solve inequality in the GAMS environment but I can't. The inequality that I want to solve is as follow:
constraint (k4).jpg
constraint (k4).jpg (4.23 KiB) Viewed 1529 times
And, I coded the exact form of the constraint in GAMS as follows (Here, x(i,t) is binary variable, R and r(i) are parameters.) (Also, x(i,t) is written as x10, x20,... and r(i) is written as r1,r2,...):

kaynak1.. r1*x(10+r2*x20+r3*x30+r4*x40 =l= R;
kaynak2.. r1*(x10+x11)+r2*(x20+x21)+r3*(x30+x31)+r4*(x40+x41) =l=R;
kaynak3.. r1*(x10+x11+x12)+r2*(x20+x21+x22)+r3*(x30+x31+x32)+r4*(x40+x41+x42) =l=R;
kaynak4.. r1*(x10+x11+x12+x13)+r2*(x21+x22+x23)+r3*(x30+x31+x32+x33)+r4*(x40+x41+x42+x43) =l=R;
kaynak5.. r1*(x11+x12+x13+x14)+r2*(x22+x23+x24)+r3*(x30+x31+x32+x33+x34)+r4*(x40+x41+x42+x43+x44) =l=R;
kaynak6.. r1*(x12+x13+x14+x15)+r2*(x23+x24+x25)+r3*(x31+x32+x33+x34+x35)+r4*(x40+x41+x42+x43+x44+x45) =l=R;
kaynak7.. r1*(x13+x14+x15+x16)+r2*(x24+x25+x26)+r3*(x32+x33+x34+x35+x36)+r4*(x40+x41+x42+x43+x44+x45+x46) =l=R;
kaynak8.. r1*(x14+x15+x16+x17)+r2*(x25+x26+x27)+r3*(x33+x34+x35+x36+x37)+r4*(x40+x41+x42+x43+x44+x45+x46+x47) =l=R;
kaynak9.. r1*(x15+x16+x17+x18)+r2*(x26+x27+x28)+r3*(x34+x35+x36+x37+x38)+r4*(x41+x42+x43+x44+x45+x46+x47+x48) =l=R;
kaynak10.. r1*(x16+x17+x18+x19)+r2*(x27+x28+x29)+r3*(x35+x36+x37+x38+x39)+r4*(x42+x43+x44+x45+x46+x47+x48+x49) =l=R;
kaynak11.. r1*(x17+x18+x19+x110)+r2*(x28+x29+x210)+r3*(x36+x37+x38+x39+x310)+r4*(x43+x44+x45+x46+x47+x48+x49+x410) =l=R;
kaynak12.. r1*(x18+x19+x110+x111)+r2*(x29+x210+x211)+r3*(x37+x38+x39+x310+x311)+r4*(x44+x45+x46+x47+x48+x49+x410+x411) =l=R;
kaynak13.. r1*(x19+x110+x111+x112)+r2*(x210+x211+x212)+r3*(x38+x39+x310+x311+x312)+r4*(x45+x46+x47+x48+x49+x410+x411+x412) =l=R;
kaynak14.. r1*(x110+x111+x112+x113)+r2*(x211+x212+x213)+r3*(x39+x310+x311+x312+x313)+r4*(x46+x47+x48+x49+x410+x411+x412+x413) =l=R;
kaynak15.. r1*(x111+x112+x113+x114)+r2*(x212+x213+x214)+r3*(x310+x311+x312+x313+x314)+r4 (x47+x48+x49+x410+x411+x412+x413+x414) =l=R;
kaynak16.. r1*(x112+x113+x114+x115)+r2*(x213+x214+x215)+r3*(x311+x312+x313+x314+x315)+r4 (x48+x49+x410+x411+x412+x413+x414+x415) =l=R;

As above, I obtain the desired result in GAMs with its exact form. I coded it as follows but I can obtain the true value:

Code: Select all

k3(t).. sum((i,q)$((ord(q)=max(0, ord(t)-p(i)+1)), r(i)*x(i,q)) =l= R;
How can I code this inequality in GAMS environment? Thanks.
Attachments
image.png
image.png (7.99 KiB) Viewed 1529 times
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: sum function problem

Post by bussieck »

You want to sum over multiple q not just one, so you need to provide the interval for q:

Code: Select all

k3(t).. sum((i,q)$((ord(q)>=max(0, ord(t)-p(i)+1) and ord(q)<=ord(t)), r(i)*x(i,q)) =l= R;
Please be aware of that ord() returns the ordinal position of a set element in an order set and starts with 1. If your q and t start with "0" that you need to adjust for this. If t and q set elements represent numbers, you can access this number using t.val and adjust the $() condition accordingly.

-Michael
abdullahturk
User
User
Posts: 4
Joined: 1 year ago

Re: sum function problem

Post by abdullahturk »

Thank you so much, Micheal. It works :)
I haven't been able to solve this problem for a while.
Thank you again :)
Post Reply