4.4. Using Python

4.4.1. Brief description

// Method 1: A new method for creating model is introduced from version 0.19.0.
env = mindoptpy.MdoEnv()
model = mindoptpy.MdoModel(env)
model.read_prob(filename)
model.solve_prob()
model.display_results()

// Method 2: The old method for creating model is still supported. It will be removed in future version.
# model = mindoptpy.MdoModel()
# model.read_prob(filename)
# model.solve_prob()
# model.display_results()

The following describes how to call the DLL of MindOpt by using Python and provides an example. The example describes how to read an optimization model from the MPS file and solve an optimization problem.

Note

The DLL is connected only when the application runs. Therefore, you must specify the path of the DLL in an environment variable so that the application can correctly locate the DLL. If the corresponding environment variable is not specified, you must place the DLL beside the application. For more information about how to set environment variables, see Installation instructions. For more information about the support for Python compiler versions, see Supported platforms.

From version V0.24.1, Python users can install MindOpt via pip install. The following describes two ways to install MindOpt Python package.

4.4.2. Install MindOpt Python from the complete installation package

Prerequisite: The complete standalone installation package has been installed. Please refer to Installation instructions .

  1. (Recommended) Create and activate a Python virtual environment.

conda create -n myenv python=3.9 --yes
conda activate myenv

Note

In this step, use Conda to create a virtual environment and install the MindOpt package in this virtual environment. For more information about how to install Conda, see Conda. If you do not use Conda, you can install the MindOpt Python software package in other Python environments. It is proved that MindOpt Python package runs more stable in Conda.

  1. Install the MindOpt Python dependency library.

python <MDOHOME>/<VERSION>/<PLATFORM>/lib/python/setup.py install

You can execute the following command to check whether the MindOpt Python package has been correctly installed:

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

Warning

If the default Python 3.8+ is used, the following call issue may occur when the system calls the DLL of MindOpt:

from mindoptpy import *
ImportError: DLL load failed while importing mindoptpy

To address this issue, use os.add_dll_directory to specify the position of the MindOpt DLL.

import os
os.add_dll_directory("<MDOHOME>/<VERSION>/<PLATFORM>/lib/")

4.4.3. pip install MindOpt Python package

Prerequisite: no need to install the standalone package, just need pip.

pip install mindoptpy

From version V0.24.1, A specific license is include in the pip install package. It is valid from 2023/01/01 to 2024/12/31.

This installation method only supports the “mindoptpy” library and does not support other features such as Commandline interface . If you need other features, please do a complete installation by referring to Installation instructions .

4.4.4. Python compilation example: mdo_mps

The following part describes how to call the Python API of MindOpt to read an optimization problem file in the MPS/LP format and solve the problem.

Import the Python module:

4import mindoptpy

Create an optimization model:

28        model.read_prob(filename)

Call mindoptpy.MdoModel.read_prob() to read an optimization problem file in the MPS/LP format:

29        model.solve_prob()

Call mindoptpy.MdoModel.solve_prob() to solve the optimization problem, and call mindoptpy.MdoModel.display_results() to view the optimization result:

30        model.display_results()
31

mdo_mps.py is the source code file.

 1"""
 2Test
 3"""
 4import mindoptpy
 5from mindoptpy import MdoError
 6import logging
 7import argparse
 8
 9
10def setup_logging(level):
11    logging.basicConfig(format='[%(asctime)s] %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p', level=level)
12
13
14if __name__ == "__main__":
15
16    # Register arguments.
17    parser = argparse.ArgumentParser(description='Run MindOpt.')
18    parser.add_argument('--filename', type=str, default='../data/afiro.mps', help='Input LP/MPS filename.')
19    args = parser.parse_args()
20    filename = args.filename
21   
22    logging.info("Started MindOpt.")
23    logging.info(" - Filename  : {0}".format(filename))
24
25    model = mindoptpy.MdoModel()
26
27    try:
28        model.read_prob(filename)
29        model.solve_prob()
30        model.display_results()
31
32    except MdoError as e:
33        logging.error("Received MindOpt exception.")
34        logging.error(" - Code          : {}".format(e.code))
35        logging.error(" - Reason        : {}".format(e.message))
36    except Exception as e:
37        logging.error("Received exception.")
38        logging.error(" - Reason        : {}".format(e))
39    finally:
40        model.free_mdl()
41
42

You can find more Python example files under <MDOHOME>/<VERSION>/examples/python of the installation package.