Loop and summation upper limit

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 KB)
image.png

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:

    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:

    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

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

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.

 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!
Capture.JPG

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

-Michael

Hello Michael,

Here is the requested model.

Thank you
WSC Model.docx (33.1 KB)
WSC1.gms (22.4 KB)

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:

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:

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.4 KB)

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 KB)

Hello all,

My model is running error free now. However, it is converging in less than a second, which is not supposed be the case. Can you please help fix this issue?

i cannot thank you enough for the provided help!

Hello,

Attaching my latest model file. The model is compiling without errors however it is not solving to optimality.

I tried reading a lot but I am still unable to fix the issue. Can you please help me figure out what I can do to fix the issue?

Best regards,
Sami
WSC1 Edited.gms (28 KB)

I don’t understand. The model solved to global optimality in less than a second:

Proven optimal solution
MIP Solution:           43.692101    (89 iterations, 0 nodes)
Final Solve:            43.692101    (14 iterations)

Best possible:          43.692101
Absolute gap:            0.000000
Relative gap:            0.000000

--- Reading solution for model Water
*** Status: Normal completion
--- Job WSC1 Edited.gms Stop 11/08/22 08:56:29 elapsed 0:00:00.637

-Michael

Dear Michael,

Yes, the fact that the model is solved to optimality in less than a second is strange. I realised that I can read the results on excel to validate the model. After using the command: execute_unload “results.gdx” ; I got the results file which I can open using GAMS. The next step was to right click the data then click write then write all symbols to excel. However, I was not able to find these options on my results file. I tried searching further without success. How can I generate the results on excel for all variables?

Unfortunately, you describe the process not very precise so I have to do some guessing. I assume that the variables in the GDX file look fine. A quick inspection show that many variables have value 0, but some variable, e.g. C3 have a level not equal to 0. When I use the IDE and the GDX Viewer within the IDE and select Write->Write ALL Symbols to Excel then I get an Excel file which looks fine to me. Studio also has has an Excel exporter in the latest version (GAMS 41) and also that looks fine. Both xlsx files are attached. I don’t know if the solution makes sense or not but the process is fine. You need to be more precise in your description and perhaps all send files what you got and what you expect to get.

-Michael
studio.xlsx (438 KB)
ide.xlsx (552 KB)