Example: reading a problem from a file lpex2.py¶
The sample lpex2.py illustrates reading a problem from a file.
The sample lpex2.py shows how to read an optimization problem from a file, and solve the problem with a specified optimizer option. If solution information or a simplex basis are available, the sample application prints them. Finally the sample prints the maximum infeasibility of any variable of the solution.
The user passes the name of the file to read and the choice of optimizers as arguments on the command line. For example, this command:
$ python lpex2.py example.mps d
reads the file example.mps and solves the problem with the dual simplex optimizer.
Alternatively, you may execute the statement:
import lpex2
within the Python interpreter and specify the problem file and optimizer interactively.
In summary, this example shows how to:
read a model from a formatted file;
read a model from a formatted file;
select the optimizer;
select the optimizer;
access basis information;
access basis information;
query quality measures of the solution.
query quality measures of the solution.
The general structure of this sample is similar to the sample lpex1.py. It starts by creating an instance of the class Cplex. A try/except statement encloses the code that follows to handle errors gracefully. You can see the complete application online in the standard distribution of the product at yourCPLEXHome/examples/src/python/lpex2.py.