Python API Exception Handling

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
reo
User
User
Posts: 3
Joined: 6 days ago

Python API Exception Handling

Post by reo » 6 days ago

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 !!

User avatar
Clemens
Posts: 46
Joined: 2 years ago

Re: Python API Exception Handling

Post by Clemens » 5 days ago

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

reo
User
User
Posts: 3
Joined: 6 days ago

Re: Python API Exception Handling

Post by reo » 5 days ago

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?

User avatar
Clemens
Posts: 46
Joined: 2 years ago

Re: Python API Exception Handling

Post by Clemens » 5 days ago

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

reo
User
User
Posts: 3
Joined: 6 days ago

Re: Python API Exception Handling

Post by reo » 4 days ago

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?

User avatar
Clemens
Posts: 46
Joined: 2 years ago

Re: Python API Exception Handling

Post by Clemens » 4 days ago

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

Post Reply