GAMS PYTHON API - INTERFACE JUPYTER

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
patriciars
User
User
Posts: 8
Joined: 3 months ago

GAMS PYTHON API - INTERFACE JUPYTER

Post by patriciars » 3 months ago

Hello.

I wrote a file in gams and would like to run in python through the Jupyter interface.
But it gives the following error:

gamsException: Invalid GAMS system directory: D:\JWP\WinPython-32bit-2.7\notebooks\-f

It finds another directory for GAMS and not the one that should be used. (THIS SHOULD BE USED = D:\JWP\GAMS\win32\25.1).


If I use spyder, it works. But I need to use the Jupyter notebook.

Any suggestions on what I can do?

User avatar
Clemens
Posts: 38
Joined: 2 years ago

Re: GAMS PYTHON API - INTERFACE JUPYTER

Post by Clemens » 3 months ago

Hi,

When calling the GamsWorkspace constructor, the API tries to find a valid GAMS system directory. The exact mechanism for this is depending on the operating system. If you want to specify a system directory explicitly you can do this by providing the named argument system_directory: GamsWorkspace(system_directory="my/sys/dir"). See also the documentation: https://www.gams.com/latest/docs/apis/p ... space.html

Best,
Clemens

patriciars
User
User
Posts: 8
Joined: 3 months ago

Re: GAMS PYTHON API - INTERFACE JUPYTER

Post by patriciars » 3 months ago

Hello.

Now the problem is code 2 0 compilation.

I'm testing gams api pyhton with the TRNSPORT.gms example and ran it right on the spyder.
But when I test for another file I created myself (and this file runs correctly on gams.exe), in spyder it gives: gamslib return code not 0 (2)

How can I solve this?

Thank you very much.

User avatar
Clemens
Posts: 38
Joined: 2 years ago

Re: GAMS PYTHON API - INTERFACE JUPYTER

Post by Clemens » 3 months ago

You can not use ws.gamslib() for you custom models. You need to use ws.add_job_from_file in order to load a model from a specific file that you have created.

Best,
Clemens

patriciars
User
User
Posts: 8
Joined: 3 months ago

Re: GAMS PYTHON API - INTERFACE JUPYTER

Post by patriciars » 3 months ago

Hello Clemens

Now, it's work.
Thank you.

But I have just onde more doubt.
When accessing the variable cost of my gams file, the message that appears is: index too large.

Below is the gms file I'm using and the code I used to run the spyder.
If you can help me I thank you very much, because I am not aware of this type of program.

Code: Select all

[color=#0000BF]Sets
i source /REF/
j demand /NHT1, NHT2, NHT3,LNIS, GHT/
k purifiers /PSA1/;

Parameters
*source
FH2Smin(i) MIN FLOW SOURCE [Nm3h-1]
/REF 1/
FH2Smax(i) MAX FLOW SOURCE  [Nm3h-1]
/REF 400000/
YH2S(i) PURITY SOURCE
/REF 0.67/
PH2S(i) PRESSURE SOURCE  [MPa]
/REF 2.4/
CH2S(i) COST SOURCE [pounds per Nm3h-1]
/REF 0.11183/

*demand
FH2C(j) FLOW DEMAND INLET [Nm3h-1]
/NHT1 10.600
NHT2 9.500
NHT3 2.500
LNIS 48.500
GHT 62.200/
YH2C(j) PURITY DEMAND INLET
/NHT1 0.67
NHT2 0.999
NHT3 0.67
LNIS 0.67
GHT 0.805/
PH2C(j) PRESSURE INLET [MPa]
/NHT1 2.4
NHT2 2.4
NHT3 2.4
LNIS 3.5
GHT 5.0/
FPpoc(j) FLOW OUTLET PURGE [Nm3h-1]
/NHT1 0.700
NHT2 0
NHT3 0
LNIS 51.900
GHT 5.700/
YPpoc(j) PURITY OUTLET PURGE
/NHT1 0.461
NHT2 0.45
NHT3 0.46
LNIS 0.456
GHT 0.454/
PP(j) PRESSURE OUTLET [MPa]
/NHT1 2.0
NHT2 2.0
NHT3 2.0
LNIS 3.0
GHT 3.9/
PPCMU(j) Power rate of make-up compressors of hydrogen consumers [kW Nm3-1]
/NHT1 0.1333
NHT2 0.1333
NHT3 0.1333
LNIS 0.1333
GHT 0.1333/
*Purifiers
Pmax(k) MAX FLOW PURIFICATION INLET
/PSA1 115.000/
PPO(k) PRESSURE PURIFICATION OUTLET MPa
/PSA1 2.4/
REC(k) RECOVERY OF PURIFICATION
/PSA1 0.55/
YH2P(k) PURITY OF PURIFIED STREAM
/PSA1 0.999/
YLOZP(k) PURITY OF WASTE STREAM
/PSA1 0.402/

CH2P(k) COST OF PURIFICATION [pound per Nm3h-1]
/PSA1 0.12183/

fator percntual perdido; fator=0.0822;
;
Scalar YIKL "H2 PURITY WASTE STREAN FROM SOURCE" /0.336/ ;
Scalar HLPV "H2 PRICE IN FUEL GAS" /0.05/;
Scalar MLPV "METANE PRICE IN FUEL GAS" /0.16/ ;
Scalar PWR "power rate (compressor)" /0.01/ ;

Table z(i,j) "COMPRESSOR I-J"
    NHT1 NHT2 NHT3 LNIS GHT
REF  0    0    0    1    1;

Table z2(k,j) "COMPRESSOR K-J"
     NHT1 NHT2 NHT3 LNIS GHT
PSA1  0    0    0    1    1
;

Table z3(j,k) "COMPRESSOR J-K"
     PSA1
NHT1  1
NHT2  1
NHT3  1
LNIS  0
GHT   0     ;

Variables
COST
;

Positive variables
FI(i) Total current flow rich in hydrogen produced at source [Nm3.h-1]
FIKL(i,k) Waste gas from source to purifier
FC(j) Inlet flow of hydrogen reactor
YC(j) Purity of the inlet flow of hydrogen
FP(j) exit flow hydrogen reactor
YP(j) Purity of the exit flow of hydrogen
PHL(j) Balance of hydrogen at the consumer's entrance
DPHLJ(j) Hydrogen consumption in the reactor - from pure hydrogen
PHLJO(j) the pure hydrogen load on the sink outlet
FLOZI(i) unused  waste gas from the source
FLOZ(j) Unused Waste Gas from Consumers - The Unit Limiting Capacity of the Unit purification
YLOZ(j) Waste gas purity
FLOZP(k) Waste gas from the condensing unit additionally due to the satisfaction of the output balance hydrogen
FLOZPR(k) Waste gas from the purification unit is solely due to recovery factor [residue]
FIJ(i,j) Flow from hydrogen sources i to consumers j [Nm3.h-1]
FIK(i,k) Flow from the source i of hydrogen to the purification unit k[Nm3.h-1]
FJK(j,k) Flow from hydrogen consumers j to the purification unit k [Nm3.h-1]
FKJ(k,j) Flow rate of the purifier k to the hydrogen consuming j [Nm3.h-1]
FH2I(i) Production of hydrogen from the source
CH2I The cost of producing hydrogen from the source
FH2K(k) Production of hydrogen by purifiers
CH2K(k) Price of hydrogen production by purifier (operating costs)
CH2KP(k) The price of hydrogen production by purifier
CH2KU Total price of hydrogen production by purifier
TKIJ(j) Compression Price Source-Consumer
TKIU Total cost of compression source-consumer
TKKJ(j) The price of compression-consuming compression
TKKU Total cost of compression-consuming compression
TKJK(j) The cost of compression-consumption
TKJKU Total cost of compression-consumption
TKU Total compression costs
LOZIVIPLIN Total gas flow rate
VLPHM The value of the fuel gas (hydrogen and methane)
;



Equations
MaxFI(i) Limit - maximum current source flow i
MinFI(i) Limit - minimum flow of source current i
CIYmin(j) Limit - the minimum required hydrogen concentration at the input to the consumer j
CIYmax(j) Limit - the maximum required hydrogen concentration at the input to the consumer j
Production(i) Production of hydrogen at source
LOZizvora(i) Production of hydrogen at the source waste stream
PHLJ(j) The balance of hydrogen at the consumer input
HMBJ(j) Consumption hydrogen balance
MBJ(j) Balance of flows at the consumers input
HMBJO(j) The balance of hydrogen at the exit to the consumer
SID(j) Defining inputs into a reactor j consumer
C(j) Consumption in the reactor
SC(j) Output of the reactor
COY(j) Limit - hydrogen concentration at exit from the consumer
CYLOZ(j) Limit - hydrogen concentration at exit from consumer - waste stream
SOD(j) Define the output from the reactor
PIC(k) Limit - Input Flow into purification Unit
MBP(k) Balance of flows around the purifier unit
HMBP(k) Hydrogen balance in the purifier unit
LOZREC(k) Waste gas extraction from the Recovery Factor due to Recovery Factor
LOZukupno total leaking gas
PH2I(i) Production of hydrogen from sources
H2I Cost of hydrogen production from sources
PH2K(k) Production of hydrogen purifier
H2K(k) Cost of hydrogen production by individual purifier for each unit
H2KU Cost of hydrogen production by total purifier
TKI(j) Costs of consumer compression by consumers
TKIsuma Costs of compression from sources to consumers in total
TKK(j) Compression Costs from Procurers to Consumers
TKKsuma Costs of compression from consumables to consumers in total
TKJ(j) Costs of compression from consumers to procurers
TKJsuma Costs of compression from consumers to total costs
TK Total compression costs
VLP The value of the leaking gas
OBJ Objective function min. costs [FUNTE_ Nm3h-1]
;

*FI.up(i)= FH2Smax(i);


*SOURCES
*MaxFI(i) Limit - maximum current source flow i
MaxFI(i).. FI(i) =l= FH2Smax(i);
*MinFI(i) Limit - minimum flow of source current i
MinFI(i).. FI(i) =g= FH2Smin(i);
*CIYmin(j) Limit - the minimum required hydrogen concentration at the input to the consumer j
 CIYmin(j).. YC(j) =g= YH2C(j);
*CIYmax(j) Limit - the maximum required hydrogen concentration at the input to the consumer j
CIYmax(j).. YC(j) =l= 1;

*Production(i) Production of hydrogen at source
Production(i).. FI(i) =e= sum(j,FIJ(i,j));

*Production(i).. FI(i) =e= sum(j,FIJ(i,j))+ sum(k,FIK(i,k));

*LOZizvora(i) Production of hydrogen at the source waste stream



LOZizvora(i).. sum(k, FIKL(i,k))+ FLOZI(i)=e= fator*FH2I(i);

*CONSUMERS
*HMBJ(j) Consumption hydrogen balance
HMBJ(j).. sum(i,(FIJ(i,j)*YH2S(i)))+ sum(k,(FKJ(k,j)*YH2P(k))) =e= PHL(j);
*PHLJ(j) The balance of hydrogen at the consumer input
PHLJ(j).. PHL(j) =e= FH2C(j)*YH2C(j);
*MBJ(j) Balance of flows at the consumers input
MBJ(j).. sum(i, FIJ(i,j))+ sum(k, FKJ(k,j))=e= FC(j);
*SID(j) Defining inputs into a reactor j consumer
SID(j).. PHL(j) =e= FC(j)*YC(j);
*C(j) Consumption in the reactor
C(j).. DPHLJ(j)=e= (FH2C(j)*YH2C(j))- (FPpoc(j)*YPpoc(j));
*SC(j) Output of the reactor
SC(j).. PHLJO(j)=e= PHL(j)-DPHLJ(j);
*COY(j) Limit - hydrogen concentration at exit from the consumer
COY(j).. YP(j) =e= YPpoc(j);
*CYLOZ(j) Limit - hydrogen concentration at exit from consumer - waste stream
CYLOZ(j).. YLOZ(j) =e= YPpoc(j);
*HMBJO(j) The balance of hydrogen at the exit to the consumer
HMBJO(j).. PHLJO(j) =e= FP(j)*YP(j);
*SOD(j) Define the output from the reactor
SOD(j).. FP(j)=e= sum(k, FJK(j,k))+ FLOZ(j);
*PIC(k) Limit - Input Flow into purification Unit

*PURIFIER
PIC(k).. sum(i, FIK(i,k)) + sum(i, FIKL(i,k)) + sum(j, FJK(j,k))=l= Pmax(k);
*MBP(k) Balance of flows around the purifier unit
MBP(k).. sum(i, FIK(i,k)) + sum(i, FIKL(i,k)) + sum(j, FJK(j,k)) =e= sum(j,FKJ(k,j))+
FLOZP(k)+FLOZPR(k);
*HMBP(k) Hydrogen balance in the purifier unit
HMBP(k).. ((sum(i, FIK(i,k)*YH2S(i))+ sum(i, FIKL(i,k)*YIKL)+ sum(j, FJK(j,k)*YP(j)))) =e= (sum(j,
FKJ(k,j)*YH2P(k)))+ FLOZP(k)*YLOZP(k)+ FLOZPR(k)*YLOZP(k);
*LOZREC(k) Waste gas extraction from the Recovery Factor due to Recovery Factor
LOZREC(k).. FLOZPR(k)*YLOZP(k)=e=(1-REC(k))*((sum(i, FIK(i,k)*YH2S(i))+ sum(i, FIKL(i,k)*YIKL)+
sum(j, FJK(j,k)*YP(j))));
*LOZukupno total leaking gas
LOZukupno.. LOZIVIPLIN =e= sum(i, FLOZI(i))+ sum(j, FLOZ(j))+ sum(k, FLOZP(k))+sum(k,
FLOZPR(k));

*COSTS
*PH2I(i) Production of hydrogen from sources
PH2I(i).. FH2I(i) =e= sum(j,FIJ(i,j))+ sum(k, FIK(i,k));
*H2I Cost of hydrogen production from sources
H2I.. CH2I=e= sum(i,FH2I(i)*CH2S(i));
*PH2K(k) Production of hydrogen purifier
PH2K(k).. FH2K(k) =e= sum(j, FKJ(k,j));
*H2K(k) Cost of hydrogen production by individual purifier for each unit
H2K(k).. CH2K(k) =e= FH2K(k)*CH2P(k);
*H2KU Cost of hydrogen production by total purifier
H2KU.. CH2KU=e= sum(k, CH2K(k));
*TKI(j) Costs of consumer compression by consumers
TKI(j).. TKIJ(j) =e= 0.6*PWR*sum(i,FIJ(i,j)*z(i,j));
*TKIsuma Costs of compression from sources to consumers in total
TKIsuma.. TKIU =e= sum(j, TKIJ(j));
*TKK(j) Compression Costs from Procurers to Consumers
TKK(j).. TKKJ(j) =e= 0.6*PWR*sum(k,FKJ(k,j)*z2(k,j));
*TKKsuma Costs of compression from consumables to consumers in total
TKKsuma.. TKKU =e= sum(j,TKKJ(j));
*TKJ(j) Costs of compression from consumers to procurers
TKJ(j).. TKJK(j) =e= 0.6*PWR*sum(k,FJK(j,k)*z3(j,k));
*TKJsuma Costs of compression from consumers to total costs
TKJsuma.. TKJKU =e= sum(j,TKJK(j));
*TK Total compression costs
TK.. TKU =e= TKIU+ TKKU+TKJKU;
*VLP The value of the leaking gas
VLP.. VLPHM=e=(sum(i, FLOZI(i)*YIKL)+sum(j, FLOZ(j)*YLOZ(j))+ sum(k,
FLOZP(k)*YLOZP(k))+sum(k, FLOZPR(k)*YLOZP(k)))*HLPV +(sum(i, FLOZI(i)*(1-YIKL))+sum(j, FLOZ(j)*(1-
YLOZ(j)))+ sum(k, FLOZP(k)*(1-YLOZP(k)))+sum(k, FLOZPR(k)*(1-YLOZP(k))))*MLPV ;
*OBJ Objective function min. costs [FUNTE_ Nm3h-1]
OBJ.. COST =e= CH2I + CH2KU + TKU - VLPHM;


model M /all/;


solve M using NLP minimizing COST;




display COST.L, FH2I.l, CH2I.l, FH2K.l, CH2K.l, CH2KU.l, TKU.L, TKIJ.l, TKIU.l, TKKJ.l, TKKU.l, TKJK.L,
TKJKU.L, LOZIVIPLIN.L, VLPHM.l, FI.l, FIK.L, FIKL.l, FLOZI.l, FC.l, YC.l, FIJ.L, FKJ.l, FP.l, YP.l, FJK.L, FLOZ.l,
FLOZP.l, FLOZPR.l, YLOZP;[/color]

Code: Select all

[color=#FF0040]from gams import *
ws = GamsWorkspace("F:\\JWP\\GAMS\\win32\\25.1\\mccarl")
#ws.gamslib("trnsport1")
    
t1 = ws.add_job_from_file("Artigo_LJ1.gms")
 #ws = GamsWorkspace(debug=DebugLevel.KeepFiles)
t1.run()
print("Ran with Default:")

for rec in t1.out_db["cost"]:
  print("cost(" + rec.key(0) + "," + rec.key(1) + "): level=" + str(rec.level) + " marginal=" + str(rec.marginal))[/color]
 

User avatar
Clemens
Posts: 38
Joined: 2 years ago

Re: GAMS PYTHON API - INTERFACE JUPYTER

Post by Clemens » 3 months ago

From your code it seems that the variable COST is zero dimensional, but you try to access key(0) and key(1) which would only be possible for GAMS symbols with at least two dimensions. Instead of doing this:

Code: Select all

for rec in t1.out_db["cost"]:
  print("cost(" + rec.key(0) + "," + rec.key(1) + "): level=" + str(rec.level) + " marginal=" + str(rec.marginal))
try something like this:

Code: Select all

rec = t1.out_db["cost"].first_record()
print("level=" + str(rec.level) + " marginal=" + str(rec.marginal))

patriciars
User
User
Posts: 8
Joined: 3 months ago

Re: GAMS PYTHON API - INTERFACE JUPYTER

Post by patriciars » 3 months ago

OK. Thank you.
Your idea worked with cases ate leats two dimension(i,j).

for rec in t1.out_db["cost"]:
print("cost(" + rec.key(0) + "," + rec.key(1) + "): level=" + str(rec.level) + " marginal=" + str(rec.marginal))


And zero dimensional too.

rec = t1.out_db["cost"].first_record()
print("level=" + str(rec.level) + " marginal=" + str(rec.marginal))

But how can I acess the parameter (j) , with just one dimension.

Thank you.

User avatar
Clemens
Posts: 38
Joined: 2 years ago

Re: GAMS PYTHON API - INTERFACE JUPYTER

Post by Clemens » 2 months ago

There is no fundamental difference when accessing records of GAMS Symbols with a dimension greater than zero. You can iterate through the records as you already did and for each record you can access the available information. This is .key(x) where x needs to be between 0 and dimension-1. You get the label in the x-th index position. Furthermore depending on the actual type of the GAMS symbol you can access different attributes which are as follows:
  • GAMS Sets: .text
  • GAMS Parameters: .value
  • GAMS Variables/Equations: .level, .marginal, .lower, .upper, .scale
Hope that helps,
Clemens

Post Reply