Skip to main content

Python linprog interface for GLPK

Project description

sckit-glpk

Proof of concept Python wrappers for GLPK.

Installation

Should be an easy pip installation:

pip install scikit-glpk

Usage

There are a few things in this package:

  • glpk() : the wrappers over the solvers (basically acts like Pythonized glpsol)

  • mpsread() : convert an MPS file to some matrices

from glpk import glpk, GLPK

res = glpk(
    c, A_ub, b_ub, A_eq, b_eq, bounds, solver, sense, maxit, timeout,
    basis_fac, message_level, disp, simplex_options, ip_options,
    mip_options, libpath)

c, A_ub, b_ub, A_eq, b_eq, bounds = mpsread(
    filename, fmt=GLPK.GLP_MPS_FILE, ret_glp_prob=False, libpath=None)

There’s lots of information in the docstrings for these functions, please check there for a complete listing and explanation.

Notice that glpk is the wrapper and GLPK acts as a namespace that holds constants.

bounds is also behaves a little differently for both of these:

  • as an input to glpk, bounds is a list of triplets in the style of GLPK (probably should be converted to linprog conventions)

  • as an output of mpsread, bounds is a list of tuples in the style of linprog

GLPK stuffs

GLPK must be installed in order to use the wrappers. Download the latest version from here and follow the instructions for installation. If you use Linux/Mac, you should be able to run the following to compile from source (see docs for different configuration options):

./configure
make -j
make install

For Windows you will need at least Visual Studio Build Tools. Go to the correct subdirectory (w32 for 32-bit or w64 for 64-bit) and the run the batch script:

Build_GLPK_with_VC14_DLL.bat

To use the GLPK installation, either provide the location of the shared library to the function, i.e. glpk(…, libpath=’path/to/libglpk.so’) or set the environment variable GLPK_LIB_PATH=path/to/libglpk.so. The wrappers have nothing to wrap if they don’t know where to find the library.

If you already have Octave installed, note that GLPK is bundled with the installation, so you can find libglpk.[so|dll] in the bin directory and do not have to install it from source as above.

Background

The GNU Linear Programming Kit (GLPK) has simplex, interior-point, and MIP solvers all callable from a C library. We would like to be able to use these from within Python and be potentially included as a backend for scipy’s linprog function.

Note that there are several projects that aim for something like this, but which don’t match up for what I’m looking for:

  • python-glpk : no longer maintained (dead as of ~2013)

  • PyGLPK : GPL licensed

  • PyMathProg : GPL licensed, uses different conventions than that of linprog

  • Pyomo : Big, uses different conventions than that of linprog

  • CVXOPT : Big, GPL licensed

  • Sage : Big, GPL licensed

  • pulp : Calls glpsol from command line (writes problems, solutions to file instead of shared memory model – this is actually easy to do)

  • yaposib : seems dead? OSI-centric

  • ecyglpki : GPL licensed, dead?

  • swiglpk : GPL licensed, low level

  • optlang : sympy-like, cool project otherwise

Most existing projects lean to GPL licenses. Not a bad thing, but would hinder adoption into scipy.

Why do we want this?

GLPK has a lot of options that the current scipy solvers lack as well as robust MIP support (only basic in HiGHS). It is also a standard, well known solver in the optimization community. The only thing that I want that it lacks on an API level is robust support for column generation. Easy access to GLPK as a backend to linprog would be very welcome (to me at least).

Approach

Since the underlying API is quite simple and written in C and only C, ctypes is a good fit for this.

GLPK will not be packaged with scipy due to licensing issues, so the strategy will be to specify where the installation is on a user’s computer (i.e., path to the shared library). linprog could then presumably route the problem to the GLPK backend instead of HiGHS or the existing native python solvers.

The ctypes wrapper is required for integrating GLPK into the Python runtime. Instead of using MPS files to communicate problems and reading solutions from files, scipy.sparse.coo_matrix and numpy arrays can be passed directly to the library. More information can be extracted from GLPK this way as well (For example, there is no way to get iteration count except by reading directly from the underlying structs. It is only ever printed to stdout, no other way to get it).

TODO

  • Several GLPK solver options (notably tolerances) not wrapped yet

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scikit-glpk-0.0.5.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scikit_glpk-0.0.5-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file scikit-glpk-0.0.5.tar.gz.

File metadata

  • Download URL: scikit-glpk-0.0.5.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9

File hashes

Hashes for scikit-glpk-0.0.5.tar.gz
Algorithm Hash digest
SHA256 cbf1fff2c4c914e54aba9c62f895cc6c41bdd9f26ec669c9af294f7defafd776
MD5 e641caa2044de57707bb2f1bda43166f
BLAKE2b-256 98007730979f0853595f1e77ac8b0b661bec8b7881b3aa6b89e5c2e2514f6166

See more details on using hashes here.

File details

Details for the file scikit_glpk-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: scikit_glpk-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9

File hashes

Hashes for scikit_glpk-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7d84e17b3df0c2c07c994fe13f40cd97da5bb9273789f881fa4aebb91f2c7921
MD5 285baaaad04d5575944c457bf2e20ac1
BLAKE2b-256 bfc1b7dbdc0e5a09606e62da6f244c13ae243eded56dd2af18b3b643d96c4c81

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page