Page 1 of 1

Python API Exception Handling

Posted: Tue Aug 13, 2019 7:42 am
by reo
Hello,

We are using GAMS Python API to solve a MILP Problem and have an application around it, we have noticed from the documentation that currently there is no support for execution errors.

Is it possible to get more information from the GAMSException, for instance, which constraint is causing the error.

If not is there any other way to get this information.

Any help/leads appreciated. Thanks in advance !!

Re: Python API Exception Handling

Posted: Tue Aug 13, 2019 1:20 pm
by Clemens
Hello,

The GamsException itself as well as the GamsExceptionExecution do not give you detailed information about the cause of an error. You can use the GamsException.get_rc() call in order to get the return code of GAMS which tells you what kind of problem occurred. See the documentation for possible values: https://www.gams.com/latest/docs/apis/p ... tCode.html

The best way to get detailed information is to specify the debug flag in the GamsWorksapce constructor like this:

Code: Select all

ws = GamsWorkspace(debug=DebugLevel.KeepFiles)
As soon as you get an GamsException the exception message will point you to the corresponding listing file (.lst). This file has detailed information about the cause of the error

Let me know if this helps,
Clemens

Re: Python API Exception Handling

Posted: Tue Aug 13, 2019 3:21 pm
by reo
Hello Clemens,

Yes, we have gone through the documentation and for now, we are using the GAMSException.get_rc(), but just the return code is insufficient,
our model sometimes gives errors like the following:

Code: Select all

Bound infeasibility column 'cost('i1'.5.'k2')'. 
we are able to get this from the .lst file, but we need to find the column that is causing the error.

Is there any other way to get this information instead of looking for the errors in .lst or log files?

Is it possible to get just the errors into the log file the API is creating?

Re: Python API Exception Handling

Posted: Tue Aug 13, 2019 4:26 pm
by Clemens
The log output can be captured by specifying the output argument in GamsJob.run(). The following code snipped shows how to get the log output redirected to stdout:

Code: Select all

from gams import *
import sys

if __name__ == "__main__":
    if len(sys.argv) > 1:
        ws = GamsWorkspace(system_directory = sys.argv[1])
    else:
        ws = GamsWorkspace()

    ws.gamslib("trnsport")
    
    t1 = ws.add_job_from_file("trnsport.gms")
    t1.run(output=sys.stdout)
Best,
Clemens

Re: Python API Exception Handling

Posted: Wed Aug 14, 2019 10:30 am
by reo
Thank you, for the reply Clemens.
The log output can be captured by specifying the output argument in GamsJob.run().
We figured this from the documentation

we are trying to find out if it is possible to get the Exception information through the API.
So that I can handle it programmatically in my application without having to go through the log files.
Is there any other way to get this information instead of looking for the errors in .lst or log files?
Is it possible to get just the errors into the log file the API is creating?

Re: Python API Exception Handling

Posted: Wed Aug 14, 2019 11:42 am
by Clemens
This is not possible right now. In order to get detailed information about an error it is required to have a look into the listing file or to examine the log output.

Best,
Clemens