353 Error GAMS Python API

Archive of Gamsworld Google Group
Post Reply
Archiver
User
User
Posts: 7876
Joined: 7 years ago

353 Error GAMS Python API

Post by Archiver »


Hi Everyone,

I'm trying to run a GAMS profit max model using the Python API. The .gms file from command line works, but I'm trying to load and run the model all within Python using the get_model_text() function and setting up and using a database.

The main problem I'm noticing is that I get the #353 error when including a data table item as D(FM, "COL_NAME") within my Equations, where D is a data table (2-d Parameter as in Python API), FM is a set, and "COL_NAME" is a column name in my data table. This also occurs with my Variable when I have XV("COL_NAME").
ERROR 353 The domain for this index position is unknown and the element
cannot be checked at this point. Missing data statement.

The only major difference between my Python implementation and the straight GAMS command line implementation is that I do not include a $INCLUDE dat file with the data table in the Python implementation.
So, my question is, Can you tell me why I am not able to call D(FM, "COL_NAME") or XV("COL_NAME") in the Python API representation? Any pointers on setting the Python API correctly?

Thanks in advance!
Brad



My .lst file is attached showing the error. Also, my Python code specifically related to populating the database is as follows (and attached; I populated a lot of these variables in the .py code using the ref_dat.csv):

#Initialize Workspace
ws = GamsWorkspace(debug=2) #1,2,3

#Set up database
db = ws.add_database()


##Add in appropriate Sets and Parameters
FM = db.add_set("FM", 1, "farms")
for m in range(0,87):
FM.add_record(FMdata[m])

ONEDATA = db.add_set_dc("ONEDATA", [FM], "farms")
for m in range(0,87):
ONEDATA.add_record(FMdata[m])

R = db.add_set("R", 1, "R list of names")
for m in Rdata:
R.add_record(m)

FIXED_INPUTS = db.add_set_dc("FIXED_INPUTS", [R], "R list of names")
for m in FIXED_INPUTSdata:
FIXED_INPUTS.add_record(m)


OUTPUTS = db.add_set_dc("OUTPUTS", [R], "R list of names")
for m in OUTPUTSdata:
OUTPUTS.add_record(m)


INPUTS = db.add_set_dc("INPUTS", [R], "R list of names")
for m in INPUTSdata:
INPUTS.add_record(m)

FSS = db.add_set_dc("FSS", [ONEDATA], "R list of names")
#for m in range(0,87):
# FSS.add_record(FMdata[m])

##Add table, which can be treted as a parameter with dim=2.
D = GamsParameter(db, "D", 2, "distance in thousands of miles")
for k, v in Ddata.iteritems():
D.add_record(k).value = v
print k, v
#SNIPPET OF D PARAMETER TABLE TAKEN FROM A PYTHON DICTIONARY I CREATED.
#('FM20', 'TOTALFUEL') 286.337127463
#('FM67', 'TOTALFUEL') 57968.0865303
#('FM47', 'FERTILIZER') 60703.2489191
#('FM66', 'SEED') 78969.5099126
#('FM79', 'MAINTENANCE') 97355
#('FM18', 'CROP_SALES') 3269.50205695
('FM44', 'TAX') 75691.4106682
#('FM72', 'ZIPC') 284527.501893
#('FM13', 'TAX') 50624.2413051
#('FM8', 'CROP_SALES') 40005.6856242
#('FM10', 'UTILITIES') 143682.975168
#('FM7', 'TAX') 1704.99069405
#('FM25', 'FERTILIZER') 17407.1955689

t2 = ws.add_job_from_string(get_model_text())

#Run Model
t2.run(databases=db)




--
Attachments

[The extension py has been deactivated and can no longer be displayed.]

ref_dat.csv
(16.44 KiB) Downloaded 259 times

[The extension lst has been deactivated and can no longer be displayed.]

Archiver
User
User
Posts: 7876
Joined: 7 years ago

Re: 353 Error GAMS Python API

Post by Archiver »


Bradley,
a GamsDatabase is exported to a GDX file when you run a GamsJob and pass the database as argument.
In order to load the data, it is required to use $GDXIN in the GAMS file. I changed your Python script as follows:

line 25: load data with $GDXIN %data%
line 381: create a commandline parameter called "data" that holds the name of the GDX file (GamsDatabase)
line 392: pass the GamsOptions instance "opt" to the run method

Best
Clemens


On 2/26/2016 11:52 PM, Bradley Barnhart wrote:
> Hi Everyone,
>
> I'm trying to run a GAMS profit max model using the Python API. The .gms file from command line works, but I'm trying to load and run the model all within Python using the get_model_text() function and setting up and using a database.
>
> The main problem I'm noticing is that I get the #353 error when including a data table item as D(FM, "COL_NAME") within my Equations, where D is a data table (2-d Parameter as in Python API), FM is a set, and "COL_NAME" is a column name in my data table. This also occurs with my Variable when I have XV("COL_NAME").
> ERROR 353 The domain for this index position is unknown and the element
> cannot be checked at this point. Missing data statement.
>
> The only major difference between my Python implementation and the straight GAMS command line implementation is that I do not include a $INCLUDE dat file with the data table in the Python implementation.
> So, my question is, Can you tell me why I am not able to call D(FM, "COL_NAME") or XV("COL_NAME") in the Python API representation? Any pointers on setting the Python API correctly?
>
> Thanks in advance!
> Brad
>
>
>
> My .lst file is attached showing the error. Also, my Python code specifically related to populating the database is as follows (and attached; I populated a lot of these variables in the .py code using the ref_dat.csv):
>
> #Initialize Workspace
> ws = GamsWorkspace(debug=2) #1,2,3
>
> #Set up database
> db = ws.add_database()
>
>
> ##Add in appropriate Sets and Parameters
> FM = db.add_set("FM", 1, "farms")
> for m in range(0,87):
> FM.add_record(FMdata[m])
>
> ONEDATA = db.add_set_dc("ONEDATA", [FM], "farms")
> for m in range(0,87):
> ONEDATA.add_record(FMdata[m])
>
> R = db.add_set("R", 1, "R list of names")
> for m in Rdata:
> R.add_record(m)
>
> FIXED_INPUTS = db.add_set_dc("FIXED_INPUTS", [R], "R list of names")
> for m in FIXED_INPUTSdata:
> FIXED_INPUTS.add_record(m)
>
>
> OUTPUTS = db.add_set_dc("OUTPUTS", [R], "R list of names")
> for m in OUTPUTSdata:
> OUTPUTS.add_record(m)
>
>
> INPUTS = db.add_set_dc("INPUTS", [R], "R list of names")
> for m in INPUTSdata:
> INPUTS.add_record(m)
>
> FSS = db.add_set_dc("FSS", [ONEDATA], "R list of names")
> #for m in range(0,87):
> # FSS.add_record(FMdata[m])
>
> ##Add table, which can be treted as a parameter with dim=2.
> D = GamsParameter(db, "D", 2, "distance in thousands of miles")
> for k, v in Ddata.iteritems():
> D.add_record(k).value = v
> print k, v
> #SNIPPET OF D PARAMETER TABLE TAKEN FROM A PYTHON DICTIONARY I CREATED.
> #('FM20', 'TOTALFUEL') 286.337127463
> #('FM67', 'TOTALFUEL') 57968.0865303
> #('FM47', 'FERTILIZER') 60703.2489191
> #('FM66', 'SEED') 78969.5099126
> #('FM79', 'MAINTENANCE') 97355
> #('FM18', 'CROP_SALES') 3269.50205695
> ('FM44', 'TAX') 75691.4106682
> #('FM72', 'ZIPC') 284527.501893
> #('FM13', 'TAX') 50624.2413051
> #('FM8', 'CROP_SALES') 40005.6856242
> #('FM10', 'UTILITIES') 143682.975168
> #('FM7', 'TAX') 1704.99069405
> #('FM25', 'FERTILIZER') 17407.1955689
>
> t2 = ws.add_job_from_string(get_model_text())
>
> #Run Model
> t2.run(databases=db)
>
>
>
>
> --
Attachments

[The extension py has been deactivated and can no longer be displayed.]

Archiver
User
User
Posts: 7876
Joined: 7 years ago

Re: 353 Error GAMS Python API

Post by Archiver »


Thank you very much!
Brad

On Monday, February 29, 2016 at 1:58:56 AM UTC-8, C. Westphal wrote:

Bradley,
a GamsDatabase is exported to a GDX file when you run a GamsJob and pass the database as argument.
In order to load the data, it is required to use $GDXIN in the GAMS file. I changed your Python script as follows:

line 25: load data with $GDXIN %data%
line 381: create a commandline parameter called "data" that holds the name of the GDX file (GamsDatabase)
line 392: pass the GamsOptions instance "opt" to the run method

Best
Clemens


On 2/26/2016 11:52 PM, Bradley Barnhart wrote:
> Hi Everyone,
>
> I'm trying to run a GAMS profit max model using the Python API. The .gms file from command line works, but I'm trying to load and run the model all within Python using the get_model_text() function and setting up and using a database.
>
> The main problem I'm noticing is that I get the #353 error when including a data table item as D(FM, "COL_NAME") within my Equations, where D is a data table (2-d Parameter as in Python API), FM is a set, and "COL_NAME" is a column name in my data table. This also occurs with my Variable when I have XV("COL_NAME").
> ERROR 353 The domain for this index position is unknown and the element
> cannot be checked at this point. Missing data statement.
>
> The only major difference between my Python implementation and the straight GAMS command line implementation is that I do not include a $INCLUDE dat file with the data table in the Python implementation.
> So, my question is, Can you tell me why I am not able to call D(FM, "COL_NAME") or XV("COL_NAME") in the Python API representation? Any pointers on setting the Python API correctly?
>
> Thanks in advance!
> Brad
>
>
>
> My .lst file is attached showing the error. Also, my Python code specifically related to populating the database is as follows (and attached; I populated a lot of these variables in the .py code using the ref_dat.csv):
>
> #Initialize Workspace
> ws = GamsWorkspace(debug=2) #1,2,3
>
> #Set up database
> db = ws.add_database()
>
>
> ##Add in appropriate Sets and Parameters
> FM = db.add_set("FM", 1, "farms")
> for m in range(0,87):
> FM.add_record(FMdata[m])
>
> ONEDATA = db.add_set_dc("ONEDATA", [FM], "farms")
> for m in range(0,87):
> ONEDATA.add_record(FMdata[m])
>
> R = db.add_set("R", 1, "R list of names")
> for m in Rdata:
> R.add_record(m)
>
> FIXED_INPUTS = db.add_set_dc("FIXED_INPUTS", [R], "R list of names")
> for m in FIXED_INPUTSdata:
> FIXED_INPUTS.add_record(m)
>
>
> OUTPUTS = db.add_set_dc("OUTPUTS", [R], "R list of names")
> for m in OUTPUTSdata:
> OUTPUTS.add_record(m)
>
>
> INPUTS = db.add_set_dc("INPUTS", [R], "R list of names")
> for m in INPUTSdata:
> INPUTS.add_record(m)
>
> FSS = db.add_set_dc("FSS", [ONEDATA], "R list of names")
> #for m in range(0,87):
> # FSS.add_record(FMdata[m])
>
> ##Add table, which can be treted as a parameter with dim=2.
> D = GamsParameter(db, "D", 2, "distance in thousands of miles")
> for k, v in Ddata.iteritems():
> D.add_record(k).value = v
> print k, v
> #SNIPPET OF D PARAMETER TABLE TAKEN FROM A PYTHON DICTIONARY I CREATED.
> #('FM20', 'TOTALFUEL') 286.337127463
> #('FM67', 'TOTALFUEL') 57968.0865303
> #('FM47', 'FERTILIZER') 60703.2489191
> #('FM66', 'SEED') 78969.5099126
> #('FM79', 'MAINTENANCE') 97355
> #('FM18', 'CROP_SALES') 3269.50205695
> ('FM44', 'TAX') 75691.4106682
> #('FM72', 'ZIPC') 284527.501893
> #('FM13', 'TAX') 50624.2413051
> #('FM8', 'CROP_SALES') 40005.6856242
> #('FM10', 'UTILITIES') 143682.975168
> #('FM7', 'TAX') 1704.99069405
> #('FM25', 'FERTILIZER') 17407.1955689
>
> t2 = ws.add_job_from_string(get_model_text())
>
> #Run Model
> t2.run(databases=db)
>
>
>
>
> --
> To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+...@googlegroups.com.
> To post to this group, send email to gams...@googlegroups.com.
> Visit this group at https://groups.google.com/group/gamsworld.
> For more options, visit https://groups.google.com/d/optout.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.
Post Reply