{"id":9016,"library":"glpk","title":"PyGLPK","description":"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.","status":"active","version":"0.4.8","language":"en","source_language":"en","source_url":"https://github.com/bradfordboyle/pyglpk","tags":["optimization","linear programming","MIP","GLPK","solver","OR"],"install":[{"cmd":"pip install glpk","lang":"bash","label":"Install PyGLPK"}],"dependencies":[{"reason":"PyGLPK is a wrapper around the GLPK C library and requires it to be pre-installed on the system. It is not automatically installed by pip.","package":"GNU Linear Programming Kit (GLPK) C library","optional":false}],"imports":[{"symbol":"GLPK","correct":"from glpk import GLPK"},{"symbol":"LP","correct":"from glpk import LP"},{"symbol":"Var","correct":"from glpk import Var"},{"symbol":"Term","correct":"from glpk import Term"},{"note":"Represents infinity for variable bounds.","symbol":"N","correct":"from glpk import N"},{"note":"Represents a continuous variable kind.","symbol":"C","correct":"from glpk import C"},{"note":"Represents row/constraint.","symbol":"R","correct":"from glpk import R"}],"quickstart":{"code":"from glpk import LP, Var, N, C, GLPK\n\n# Create a new problem\nlp = LP()\n\n# Define variables\nx1 = lp.add_var(name='x1', lb=0.0, ub=N, kind=C) # lb=0, ub=infinity, continuous\nx2 = lp.add_var(name='x2', lb=0.0, ub=N, kind=C)\n\n# Set objective function (maximize 10*x1 + 6*x2)\nlp.obj.dir = LP.MAX\nlp.obj += 10 * x1 + 6 * x2\n\n# Add constraints\n# Constraint 1: x1 + x2 <= 100\nc1 = lp.add_row(name='c1')\nc1.add_term(x1, 1.0)\nc1.add_term(x2, 1.0)\nc1.upper_bound = 100.0\n\n# Constraint 2: 7*x1 + 3*x2 <= 350\nc2 = lp.add_row(name='c2')\nc2.add_term(x1, 7.0)\nc2.add_term(x2, 3.0)\nc2.upper_bound = 350.0\n\n# Constraint 3: 3*x1 + 5*x2 <= 150\nc3 = lp.add_row(name='c3')\nc3.add_term(x1, 3.0)\nc3.add_term(x2, 5.0)\nc3.upper_bound = 150.0\n\n# Solve the problem\nlp.simplex()\n\n# Print results\nif lp.status == GLPK.LP_OPT:\n    print(f\"Status: Optimal\")\n    print(f\"Objective value: {lp.obj.value:.2f}\")\n    print(f\"x1 = {x1.value:.2f}\")\n    print(f\"x2 = {x2.value:.2f}\")\nelif lp.status == GLPK.LP_FEAS:\n    print(f\"Status: Feasible, but not necessarily optimal\")\n    print(f\"Objective value: {lp.obj.value:.2f}\")\n    print(f\"x1 = {x1.value:.2f}\")\n    print(f\"x2 = {x2.value:.2f}\")\nelse:\n    print(f\"Status: Solver terminated with status code: {lp.status}\")\n\n# Clean up\nlp.delete()","lang":"python","description":"This quickstart defines and solves a simple linear programming problem with two continuous variables and three constraints. It demonstrates variable definition, objective function setup, constraint creation, solving the problem, and interpreting the results."},"warnings":[{"fix":"Install the GLPK C library for your operating system. For Debian/Ubuntu: `sudo apt-get install libglpk-dev`. For macOS (Homebrew): `brew install glpk`. For Windows, you typically need to build from source or use a pre-compiled binary and ensure it's in your system's PATH.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always explicitly set `kind` (C, I, B for continuous, integer, binary respectively) and `lb` (lower bound) / `ub` (upper bound) for variables. Use `N` from `glpk` for infinity.","message":"Be mindful of variable types (continuous, integer, binary) and bounds. Incorrectly defined variables can lead to infeasible or unbounded solutions, or incorrect solver behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `if lp.status == GLPK.LP_OPT:` instead of `if lp.status == 5:` (or other magic numbers). Refer to GLPK documentation for all status constants.","message":"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`).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run `pip install glpk` to install the PyGLPK package.","cause":"The PyGLPK Python package has not been installed or is not accessible in the current Python environment.","error":"ModuleNotFoundError: No module named 'glpk'"},{"fix":"Install 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.","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.","error":"ImportError: libglpk.so.40: cannot open shared object file: No such file or directory (or similar for .dylib on macOS, .dll on Windows)"},{"fix":"Review 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.","cause":"The optimization problem is infeasible (no solution satisfies all constraints) or unbounded (the objective function can be improved indefinitely without violating constraints).","error":"glpk.GLPKError: problem has no solution"}]}