PuLP Interface

PuLP is an open source modeling tool based on Python, it is mainly used for modeling integer programming problems. This chapter introduces how to use the Cardinal Optimizer (COPT) in PuLP.

Installation guide

Before calling COPT in PuLP to solve problem, users need to setup PuLP and COPT correctly. PuLP currently supports Python 2.7 and later versions of Python. Users can download Python from Anaconda distribution or Python official distribution . We recommend users install the Anaconda distribution, because it is more user-friendly and convenient for Python novices.

Install via conda

We recommend that users who have installed the Anaconda distribution of Python use its own conda tool to install PuLP. Execute the following commands in Windows command prompt or terminal on Linux and MacOS:

conda install -c conda-forge pulp

Install via pip

Users can also install PuLP through the standard pip tool, execute the following command in Windows command prompt or Linux and MacOS terminal:

pip install pulp

Setup PuLP interface

For PuLP V2.8.0 and above, COPT can be applied directly. After installing and configuring the COPT, users can proceed with the following steps:

from pulp import *

In the solving function solve, specify the solver to COPT to solve:

solver = COPT()
result = prob.solve(solver)

Notes:

  1. Calling COPT in PuLP via solver = COPT() relies on the Python interface of COPT so coptpy needs to be installed first.

  2. By specifying: solver = COPT_DLL() , solver = COPT_CMD() , you can also call COPT, depending on COPT installation package.

Introduction of features

The PuLP interface of COPT provides two methods: command line and dynamic library, which are introduced as follows:

Command line

The command-line method actually calls the interactive tool copt_cmd of COPT to solve problems. In this way, PuLP generates the MPS format file corresponding to the model, and combines the parameter settings passed by the user to generate the solving commands. Upon finish of solving, COPT writes and reads the result file, and assigns values to the corresponding variables and return them to PuLP.

Functions of the command line method are encapsulated as class COPT_CMD. Users can set parameters when creating the object of the class and provides the following parameters:

  • keepFiles

    This option controls whether to keep the generated temporary files. The default value is 0, which means no temporary files are kept.

  • mip

    This option controls whether to support solving integer programming models. The default value is True, which means support solving integer programming models.

  • msg

    This option controls whether to print log information on the screen. The default value is True, that is, print log information.

  • mip_start

    This option controls whether to use initial solution information for integer programming models. The default value is False, that is, the initial solution information will not be used.

  • logfile

    This option specifies the solver log. The default value is None, which means no solver log will be generated.

  • params

    This option sets optimization parameters in the form of key=value. Please refer to the chapter Parameters for currently supported parameters.

Dynamic library

The dynamic library method directly calls COPT C APIs to solve problems. In this way, PuLP generates problem data and call COPT APIs to load the problem and parameters set by the user. When optimization finishes, the solution is obtained by calling COPT APIs, and then assigned to the corresponding variables and constraints, and passed back to PuLP.

Functions of the dynamic library method are encapsulated as class COPT_DLL. Users can set parameters when creating the object of the class and provides the following parameters:

  • mip

    This option controls whether to support solving integer programming models. The default value is True, which means support solving integer programming models.

  • msg

    This option controls whether to print log information on the screen. The default value is True, that is, print log information.

  • mip_start

    This option controls whether to use initial solution information for integer programming models. The default value is False, that is, the initial solution information will not used.

  • logfile

    This option specifies the solver log. The default value is None, which means no solver log will be generated.

  • params

    This option sets optimization parameters in the form of key=value. Please refer to the chapter Parameters for currently supported parameters.

In addition, the following methods are provided:

  • setParam(self, name, val)

    Set optimization solution parameters.

  • getParam(self, name)

    Obtain optimized solution parameters.

  • getAttr(self, name)

    Get the attribute information of the model.

  • write(self, filename)

    Output MPS/LP format model file, COPT binary format model file, result file, basic solution file, initial solution file and parameter setting file.