4.4. Call MindOpt with Python

This section uses a simple example to show how to use the Python to call MindOpt to read and solve the optimization model.

4.4.1. Install mindoptpy Library

Python libraries for MindOpt can be installed in two ways. Before formal installation, please pay attention to the following precautions.

Note

  • It is recommended to create a virtual environment through conda, and install the MindOpt package in the virtual environment (conda installation please refer to: conda documentation).

  • For macOS users who use the conda environment, it is necessary to keep the architecture information of conda and macOS consistent, which can be judged by judging whether the __archspec field output by conda info is consistent with the output of uname -m.

conda info
uname -m

Verify that the __archspec field in the conda info output matches the uname -m output.

4.4.1.2. Method 2: Install via MindOpt Stand-alone Version

See Installation Instructions for installation of MindOpt stand-alone version.

First execute the following command to install the dependency package:

pip install numpy scipy

Finally, enter the directory where the Python SDK is located, and execute the following command to install the mindoptpy library. Take the Linux x86 platform as an example:

cd <MDOHOME>/linux64-x86/lib/python
python setup.py install

4.4.2. Edit .py File

Below we will show how to call MindOpt Python API in the .py file to read the optimization problem model file and solve it.

First import the Python module:

1from mindoptpy import *

Then create the environment and start the environment:

17    env = Env()
18    env.start()

Use read() to read optimization problems in MPS/LP format:

18    model = read(filename, env)

Finally, use optimize() to solve the problem, and view the objective function value of the optimal solution through the model attribute ObjVal.

21        model.optimize()
22        print(model.objval)

Below is the complete source code file read_mps.py.

 1from mindoptpy import *
 2import argparse
 3
 4
 5if __name__ == "__main__":
 6
 7    # Register arguments.
 8    parser = argparse.ArgumentParser(description='Run MindOpt.')
 9    parser.add_argument('--filename', type=str, default='../data/afiro.mps', help='Input LP/MPS filename.')
10    args = parser.parse_args()
11    filename = args.filename
12
13    print("Started MindOpt.")
14    print(" - Filename  : {0}".format(filename))
15
16    env = Env()
17    env.start()
18    model = read(filename, env)
19
20    try:
21        model.optimize()
22        print(model.objval)
23
24    except MindoptError as e:
25        print("Received MindOpt exception.")
26        print(" - Code          : {}".format(e.errno))
27        print(" - Reason        : {}".format(e.message))
28    except Exception as e:
29        print("Received exception.")
30        print(" - Reason        : {}".format(e))
31    finally:
32        model.dispose()

You can find more Python language-related example files in the installation path <MDOHOME>/<VERSION>/examples/python.

4.4.3. Execution on Windows Platform

We provide example files under the installation path <MDOHOME>\<VERSION>\examples\python. Execute the following instructions to run the sample code to complete the optimization solution:

cd <MDOHOME>\examples\python
python read_mps.py --filename=..\data\afiro.mps

4.4.4. Execution on Linux and macOS

We provide example files under the installation path <MDOHOME>/<VERSION>/examples/python. Execute the following instructions to run the sample code to complete the optimization solution:

cd <MDOHOME>/examples/python
python read_mps.py --filename=../data/afiro.mps

Note

  • In order for the application to correctly locate the dynamic library, the user needs to specify the path of the dynamic library in the environment variable. If the environment variable is not specified, the user needs to place the dynamic library file in an appropriate location according to the logic of the operating system to find the dynamic library. For environment variable settings, see Installation Instructions.

  • See Supported Platform for supported Python language compiler versions.