PyGLPK
PyGLPK (Python GLPK) is a Python module that provides a comprehensive interface to the GNU Linear Programming Kit (GLPK). It allows Python users to model and solve linear programming (LP), mixed integer programming (MIP), and other related optimization problems. The current version is 0.4.8. It has a moderate release cadence, largely tied to maintenance and updates related to the underlying GLPK C library.
Common errors
-
ModuleNotFoundError: No module named 'glpk'
cause The PyGLPK Python package has not been installed or is not accessible in the current Python environment.fixRun `pip install glpk` to install the PyGLPK package. -
ImportError: libglpk.so.40: cannot open shared object file: No such file or directory (or similar for .dylib on macOS, .dll on Windows)
cause The underlying GLPK C library is not found by PyGLPK, likely because it is not installed or not in the system's dynamic link library search path.fixInstall the GLPK C library for your operating system (e.g., `sudo apt-get install libglpk-dev` on Debian/Ubuntu, `brew install glpk` on macOS). Ensure it's correctly linked or added to your system's PATH/LD_LIBRARY_PATH environment variable. -
glpk.GLPKError: problem has no solution
cause The optimization problem is infeasible (no solution satisfies all constraints) or unbounded (the objective function can be improved indefinitely without violating constraints).fixReview your constraints and objective function for logical errors or contradictions. Use `lp.write_lp("problem.lp")` to export the problem to an LP file and inspect it with a dedicated LP viewer or GLPK's own `glpsol` tool for detailed analysis.
Warnings
- gotcha PyGLPK requires the GLPK C library to be installed on your system BEFORE installing the Python package. The Python package is a wrapper; it does not install the underlying C library.
- gotcha Be mindful of variable types (continuous, integer, binary) and bounds. Incorrectly defined variables can lead to infeasible or unbounded solutions, or incorrect solver behavior.
- gotcha The GLPK solver's status codes (e.g., `lp.status`) are integers. For clear and robust interpretation, always compare them against constants from the `glpk.GLPK` module (e.g., `GLPK.LP_OPT`, `GLPK.LP_FEAS`).
Install
-
pip install glpk
Imports
- GLPK
from glpk import GLPK
- LP
from glpk import LP
- Var
from glpk import Var
- Term
from glpk import Term
- N
from glpk import N
- C
from glpk import C
- R
from glpk import R
Quickstart
from glpk import LP, Var, N, C, GLPK
# Create a new problem
lp = LP()
# Define variables
x1 = lp.add_var(name='x1', lb=0.0, ub=N, kind=C) # lb=0, ub=infinity, continuous
x2 = lp.add_var(name='x2', lb=0.0, ub=N, kind=C)
# Set objective function (maximize 10*x1 + 6*x2)
lp.obj.dir = LP.MAX
lp.obj += 10 * x1 + 6 * x2
# Add constraints
# Constraint 1: x1 + x2 <= 100
c1 = lp.add_row(name='c1')
c1.add_term(x1, 1.0)
c1.add_term(x2, 1.0)
c1.upper_bound = 100.0
# Constraint 2: 7*x1 + 3*x2 <= 350
c2 = lp.add_row(name='c2')
c2.add_term(x1, 7.0)
c2.add_term(x2, 3.0)
c2.upper_bound = 350.0
# Constraint 3: 3*x1 + 5*x2 <= 150
c3 = lp.add_row(name='c3')
c3.add_term(x1, 3.0)
c3.add_term(x2, 5.0)
c3.upper_bound = 150.0
# Solve the problem
lp.simplex()
# Print results
if lp.status == GLPK.LP_OPT:
print(f"Status: Optimal")
print(f"Objective value: {lp.obj.value:.2f}")
print(f"x1 = {x1.value:.2f}")
print(f"x2 = {x2.value:.2f}")
elif lp.status == GLPK.LP_FEAS:
print(f"Status: Feasible, but not necessarily optimal")
print(f"Objective value: {lp.obj.value:.2f}")
print(f"x1 = {x1.value:.2f}")
print(f"x2 = {x2.value:.2f}")
else:
print(f"Status: Solver terminated with status code: {lp.status}")
# Clean up
lp.delete()