Loop and summation upper limit

Problems with syntax of GAMS
Post Reply
i_sam
User
User
Posts: 5
Joined: 2 weeks ago

Loop and summation upper limit

Post by i_sam » 2 weeks ago

Hello everyone!

I have an MILP model, which I have coded on GAMS. However, there is an equation, which I was not able to code, and would appreciate any help and insight from you guys. I attached the equation I am struggling with as well as the extract related to this equation from the GAMS code.

I am unable to set the upper limit for the summation, and I think I need to add a loop for GAMS to find the optimal value of t' (which is called tp in the code). t and tp both have the same domain. t is the planning time horizon however tp is the time at which a decision is made within the model. Therefore, at any point in time, t is greater than tp.

Thanks a lot in advance!
WSC Fixing.gms
(5.3 KiB) Downloaded 15 times
Attachments
image.png
image.png (7.6 KiB) Viewed 503 times

User avatar
bussieck
Moderator
Moderator
Posts: 832
Joined: 6 years ago

Re: Loop and summation upper limit

Post by bussieck » 2 weeks ago

Why do you have the "$ (ord(t)-ord(tp) >= 0)" attached to C3. The math clearly says that you should do the equation for all t = 1..T. The only time tp plays a role is in the second sum. You are supposed to sum from tp=1 to t, so limit the summation of tp by this: sum(tp$(ord(tp)<=ord(t)), ...). Moreover, ord(t-tp) does not work you need to do ord(t)-ord(tp). So the code looks as follows:

Code: Select all

    Eqn3(p,JR(j),k,t).. C3(p,j,k,t)  =e= sum(u, Z(u,p,j,k,t) * C(u,p,j,k) * rho(k)**ord(t) $ Unit(p,j,k)  + sum(tp$(ord(tp)<=ord(t)), rho(k)**(ord(t)-ord(tp))) * CP(k) * RU(p,j,k,t)) ;
Since rho is exogenous, you could alternatively calculate a parameter rhosum before the model and use that:

Code: Select all

    parameter rhosum(k,t); rhosum(k,t) = sum(tp$(ord(tp)<=ord(t)), rho(k)**(ord(t)-ord(tp)));
    Eqn3(p,JR(j),k,t).. C3(p,j,k,t)  =e= sum(u, Z(u,p,j,k,t) * C(u,p,j,k) * rho(k)**ord(t) $ Unit(p,j,k)  + rhosum(k,t) * CP(k) * RU(p,j,k,t)) ;
-Michael

i_sam
User
User
Posts: 5
Joined: 2 weeks ago

Re: Loop and summation upper limit

Post by i_sam » 2 weeks ago

Thank you so much Michael! Finally got that much needed help! Thanks a lot for the quick response too.

i_sam
User
User
Posts: 5
Joined: 2 weeks ago

Re: Loop and summation upper limit

Post by i_sam » 1 week ago

Hello guys,

I have another question regarding the model attached in the above post. I am trying to type this equation (see image), and I am getting a bunch of errors.

Code: Select all

 Eqn11(PL(j),tp,t).. sum((tp$(ord(tp)<=ord(t)),k),NU(j,k,tp)) =l= Umax2(j) ; 
Upon running this code, I get the following errors: 125, 8, 148, 653, 37, and 409. I appreciate your help fixing this guys.

In this model, a variable is defined a certain way (i.e NU(j,k,t)) and used in equations as NU(j,k,tp). Is this valid? Considering that t and tp are two distinct sets, is there a way I can let GAMS run both notations without having to create two separate variables?

Thanks a lot!
Attachments
Capture.JPG
Capture.JPG (11.01 KiB) Viewed 347 times

User avatar
bussieck
Moderator
Moderator
Posts: 832
Joined: 6 years ago

Re: Loop and summation upper limit

Post by bussieck » 1 week ago

Send the entire model because compilation errors often relate to other code pieces.

-Michael

i_sam
User
User
Posts: 5
Joined: 2 weeks ago

Re: Loop and summation upper limit

Post by i_sam » 1 week ago

Hello Michael,

Here is the requested model.

Thank you
WSC Model.docx
(33.07 KiB) Downloaded 12 times
WSC1.gms
(22.41 KiB) Downloaded 13 times

User avatar
bussieck
Moderator
Moderator
Posts: 832
Joined: 6 years ago

Re: Loop and summation upper limit

Post by bussieck » 1 week ago

If you look at the constraint, it needs to be indexed by t and j. Your GAMS code indexes by t, j, and tp. I see no reason for tp. Moreover, when you sum over a tuple (tp,k) your syntax was just wrong. Here is the (IMHO) correct version of Eqn11:

Code: Select all

Eqn11(PL(j),t).. sum((tp,k)$(ord(tp)<=ord(t)),NU(j,k,tp)) =l= Umax2(j) ;
Next problem, its your declaration of tp. You have it independent of t. That difficult if you want to index something with tp that is mean to be indexed over t. I suggest you learn the fundamentals of GAMS domains. For now, as tp is same as t, I made tp an alias of t. I understand that this might not be the solution, but it works for this instance.

Next problem, you have not understood how maps work in GAMS. Read more about how to effectively work with GAMS sets:

Code: Select all

Eqn37(JRPL(j),t-1).. Wvs(j,t-1) + sum(p, Wo2(p,j,t)) + Won2(j,t) + sum(jp, Wl2(Ajj(j,jp),JRPL(j),t)$offdiag2(j,jp)) - sum(JRPL(j), Wl(JRPL(j),jp,t)$offdiag2(j,jp)) - sum(d, Wl3(JRPL(j),d,t)) - Wvs(JRPL(j),t) =e= WD(JRPL(j),t) ;
    
The construct "sum(jp, Wl2(Ajj(j,jp),JRPL(j),t)$offdiag2(j,jp))" does not work. You probably mean "sum(Ajj(j,jp), Wl2(jp,JRPL(j),t)$offdiag2(j,jp))".

There are many many more errors. Many are of the same type (e.g. you sum over an index that is already controlled or you use JRPL(j) in the equation definition, then you either need to use j or just JRPL to index symbols on the left, but not JRPL(j), ...).

You (as everybody) will make lots of mistakes translating a complex model from Word/LaTeX to GAMS. Even if the model in Word/LaTeX is "correct", have you though about how to debug the GAMS model. This is a major task.

I have attached the model with my edits. It still does not compile but I have corrected some errors and the types of the remaining ones (I believe) I already corrected before.

Good luck.

-Michael
WSC1.gms
(22.39 KiB) Downloaded 12 times

i_sam
User
User
Posts: 5
Joined: 2 weeks ago

Re: Loop and summation upper limit

Post by i_sam » 5 days ago

Dear Michael,

Thanks a lot for your thorough response. I was able to eliminate all the errors in the code. However, I am getting errors 66 and 256 next to the "solve" command upon running the code. Some online sources suggested that some data might be missing, but I already made sure it is not the case. Yet, I am still getting the errors. Could you please take a look at it.

Thank you.
WSC1 Edited.gms
(22.2 KiB) Downloaded 2 times

Post Reply