GAMS API Python

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
Romario7
User
User
Posts: 2
Joined: 5 months ago

GAMS API Python

Post by Romario7 » 5 months ago

Hi all,

I am a beginner with Python but I would like to run my "model.gms" file through it.
My GAMS version is 25.1.1 32 bit while I run the 3.6 32 bit version of Python from RT-Lab.
From the transport1.py file I run only the very first rows to check if the GAMS library is imported:
from __future__ import print_function
from gams import *
import os
import sys

but I get the following error:
Traceback (most recent call last):
File "D:\03_RTLAB_Workspace\AutoPython\PythonScript.py", line 2, in <module>
from gams import *
File "D:\03_RTLAB_Workspace\AutoPython\gams\__init__.py", line 8, in <module>
from .workspace import *
ImportError: DLL load failed: %1 is not a valid Win32 application.

Do you have any suggestion to solve this issue?

I take the opportunity also to ask the following:
- Do the API files have to be in the same folder of my model's folder?
- Do I have to install the API also in GAMS? If so, I tried to run the code suggested in the Getting started section (cd api && python setup.py install && cd ..) but I get the following: "Error 140: unknown symbol, which is linked to the above-mentioned suggested command".

Thank you for your support!

Clemens
Posts: 29
Joined: 2 years ago

Re: GAMS API Python

Post by Clemens » 5 months ago

Hi,

The error is most likely caused by a bitness mismatch of your GAMS version including the Python API and your version of Python itself. Please double check your Python version if it is really 32 bit by just launching the live interpreter on the command line and looking at the output.

Regarding the other questions: Python needs to somehow find the files of the GAMS Object-oriented Python API (<Path/To/GAMS>/apifiles/Python/api_36). This can be done by setting the PYTHONPATH or by installing the API into the site-packages directory of your Python installation. (see https://www.gams.com/latest/docs/API_PY_TUTORIAL.html). It is in general not required (and not recommended) to copy the API files next to your model.

Please let me know if this helps.

Best,
Clemens

Romario7
User
User
Posts: 2
Joined: 5 months ago

Re: GAMS API Python

Post by Romario7 » 5 months ago

Clemens wrote:
5 months ago
Hi,

The error is most likely caused by a bitness mismatch of your GAMS version including the Python API and your version of Python itself. Please double check your Python version if it is really 32 bit by just launching the live interpreter on the command line and looking at the output.

Regarding the other questions: Python needs to somehow find the files of the GAMS Object-oriented Python API (<Path/To/GAMS>/apifiles/Python/api_36). This can be done by setting the PYTHONPATH or by installing the API into the site-packages directory of your Python installation. (see https://www.gams.com/latest/docs/API_PY_TUTORIAL.html). It is in general not required (and not recommended) to copy the API files next to your model.

Please let me know if this helps.

Best,
Clemens

Hi Clemens,

Thank you very much for your quick reply!
I have just checked the version (import sys print (sys.version)): I have installed Python 2.7.12 (not 3.6) but still 32 bit, in accordance with the 32 bit of GAMS.

Roberto


I have just updated Python to the new version 3.6 64 bit, also in accordance with my computer bitness. In fact, RT-Lab by default installs a 32 bit version of Python. Now I can import the GAMS API, I have to discover the way it works now!

Thank you again

Clemens
Posts: 29
Joined: 2 years ago

Re: GAMS API Python

Post by Clemens » 5 months ago

Then it was perhaps a mismatch in the version of the Python API and the Python interpreter. Both the bitness as well as the version (2.7, 3.6 etc) need to match. Good to hear that you got it working.
You have probably seen the tutorial already, but in case you did not, here it is: https://www.gams.com/latest/docs/API_PY ... L_EXAMPLES
This is a good resource to get started with the API since it explains the most important aspects of the API and how to use them.

Best,
Clemens

patriciars
User
User
Posts: 2
Joined: 2 days ago

Re: GAMS API Python

Post by patriciars » 2 days ago

I am having a problem with gams python api.
I have a gams file that runs correctly.
When I import to use the interface with python through spyder, it gives compilation error: GamsException: gamslib return code not 0 (2)


The code in python to run gams:
if __name__ == "__main__":
if len(sys.argv) > 1:
ws = GamsWorkspace(system_directory = sys.argv[1])
else:
ws = GamsWorkspace()

ws.gamslib("Artigo_LJ1")

t1 = ws.add_job_from_file("Artigo_LJ1.gms")
t1.run()
print("Ran with Default:")

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

opt = ws.add_options()
opt.all_model_types = "xpress"
t1.run(opt)

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

file = open(os.path.join(ws.working_directory, "xpress.opt"), "w")
file.write("algorithm=barrier")
file.close()

opt.optfile = 1
t1.run(opt)

print("Ran with XPRESS with non-default option:")
for rec in t1.out_db["x"]:
print("x(" + rec.key(0) + "," + rec.key(1) + "): level=" + str(rec.level) + " marginal=" + str(rec.marginal))





The gams file:
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;



parameter FJC(j) Total quantity at the entrance to the unit;
FJC(j)= sum(i,FIJ.l(i,j)) + sum(k,FKJ.l(k,j));



display FJC;

parameter YJC(j) the purity at the entrance to the unit;
YJC(j)= (sum(i,FIJ.l(i,j)*YH2S(i)) + sum(k,FKJ.l(k,j)*YH2P(k)))/(sum(i,FIJ.l(i,j)) + sum(k,FKJ.l(k,j)));
display YJC;



parameter KFulaz(k) The total flow rate on the purifier;
KFulaz(k) = sum(i, FIK.l(i,k)) +sum(j, FJK.l(j,k))+ sum(i, FIKL.L(i,k));
display KFulaz;
parameter KYulaz(k) The purity of the current on the insertion in the purifier;
KYulaz(k)= (sum(i, FIK.l(i,k)*YH2S(i))+ sum(j, FJK.l(j,k)*YP.l(j))+ sum(i, FIKL.L(i,k)*YIKL))/KFulaz(k);
display KYulaz;
parameter KFprocisceno Total flow of pure hydrogen after processing;
KFprocisceno= sum(k,sum(j,FKJ.l(k,j)));
display KFprocisceno;
parameter KFresidue(k) Residue flowrate;
KFresidue(k)=FLOZP.l(k)+FLOZPR.l(k);
display KFresidue;
parameter YLOZ_izlaz The purity of the total fuel gas;
YLOZ_izlaz= (sum(i, FLOZI.l(i)*YIKL)+sum(j, FLOZ.l(j)*YLOZ.l(j))+ sum(k, FLOZP.l(k)*YLOZP(k))+sum(k,
FLOZPR.l(k)*YLOZP(k)))/ LOZIVIPLIN.L;
display YLOZ_izlaz;


Clemens
Posts: 29
Joined: 2 years ago

Re: GAMS API Python

Post by Clemens » 1 day ago

Hello,

The ws.gamslib call tries to find the model Artigo_LJ1 in the GAMS Model Library and can not find it. If you want to create a GamsJob from a specific file you need to use ws.add_job_from_file (https://www.gams.com/latest/docs/apis/p ... d6035c7526).

Hope that helps,
Clemens

Post Reply