{"id":46565,"library":"swiglpk","title":"swiglpk","description":"swiglpk provides simple SWIG-generated Python bindings for the GNU Linear Programming Kit (GLPK), a library for solving linear programming (LP) and mixed integer programming (MIP) problems. Current version is 5.0.13 with an irregular release cadence.","status":"active","version":"5.0.13","language":"python","source_language":"en","source_url":"https://github.com/biosustain/swiglpk","tags":["glpk","linear programming","optimization","swig"],"install":[{"cmd":"pip install swiglpk","lang":"bash","label":"PyPI"},{"cmd":"conda install -c conda-forge swiglpk","lang":"bash","label":"Conda"}],"dependencies":[{"reason":"Requires GLPK shared library (libglpk) installed on system or via conda","package":"glpk","optional":false}],"imports":[{"note":"The module is named swiglpk, not glpk.","wrong":"import glpk","symbol":"glpk","correct":"import swiglpk"}],"quickstart":{"code":"import swiglpk\n\n# Create problem object\nlp = swiglpk.glp_create_prob()\nswiglpk.glp_set_prob_name(lp, 'sample')\n\n# Add columns (variables)\nswiglpk.glp_add_cols(lp, 2)\nswiglpk.glp_set_col_name(lp, 1, 'x1')\nswiglpk.glp_set_col_bnds(lp, 1, swiglpk.GLP_LO, 0.0, 0.0)\nswiglpk.glp_set_col_name(lp, 2, 'x2')\nswiglpk.glp_set_col_bnds(lp, 2, swiglpk.GLP_LO, 0.0, 0.0)\n\n# Add rows (constraints)\nswiglpk.glp_add_rows(lp, 1)\nswiglpk.glp_set_row_name(lp, 1, 'c1')\nswiglpk.glp_set_row_bnds(lp, 1, swiglpk.GLP_UP, 0.0, 100.0)\n\n# Set objective coefficients\nswiglpk.glp_set_obj_coef(lp, 1, 10.0)\nswiglpk.glp_set_obj_coef(lp, 2, 6.0)\n\n# Set coefficients for constraint rows\n# (indexing: 1-based for rows, columns, and matrix)\nrows = [1, 1]\ncols = [1, 2]\nvalues = [1.0, 1.0]\nswiglpk.glp_set_mat_row(lp, 1, 2, rows, cols, values)  # Actually: indices, values? Check API\n\n# Solve\nswiglpk.glp_simplex(lp, None)\n\n# Get solution\nprint('Objective value:', swiglpk.glp_get_obj_val(lp))\nprint('x1 =', swiglpk.glp_get_col_prim(lp, 1))\nprint('x2 =', swiglpk.glp_get_col_prim(lp, 2))\n\n# Clean up\nswiglpk.glp_delete_prob(lp)","lang":"python","description":"Quickstart solving a simple LP with two variables and one constraint using swiglpk."},"warnings":[{"fix":"Start row/column indices from 1.","message":"All indices in GLPK (rows, columns, matrix entries) are 1-based, not 0-based. Common mistake to use 0-based indexing.","severity":"gotcha","affected_versions":"all"},{"fix":"Provide ind as list of column indices starting at 1, and val as list of corresponding coefficients.","message":"The function glp_set_mat_row() expects arguments: (lp, row, len, ind, val) where ind and val are lists of indices and values. The indices must be 1-based column indices. Refer to GLPK documentation for correct usage.","severity":"gotcha","affected_versions":"all"},{"fix":"Call glp_delete_prob(lp) after use.","message":"Memory management: created problems (glp_create_prob) must be explicitly deleted with glp_delete_prob to avoid leaks.","severity":"gotcha","affected_versions":"all"},{"fix":"Accept the procedural API; consider using higher-level wrappers like pulp or cvxopt for simpler tasks.","message":"The library does not have Python-friendly wrappers; you must use the low-level C-style API. Objects like glp_prob are opaque pointers.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"search_vec":"'5.0.13':33 'bind':9 'cadenc':38 'current':30 'generat':7 'glpk':16,39 'gnu':12 'integ':26 'irregular':36 'kit':15 'librari':18 'linear':13,21,40 'lp':23 'mip':28 'mix':25 'optim':42 'problem':29 'program':14,22,27,41 'provid':3 'python':8 'releas':37 'simpl':4 'solv':20 'swig':6,43 'swig-gener':5 'swiglpk':1,2 'version':31","created_at":"2026-06-07T13:00:33.365179+00:00","updated_at":"2026-06-07T13:00:33.365179+00:00","problems":[{"fix":"Run 'pip install swiglpk' in the correct Python environment.","cause":"swiglpk not installed or installed in wrong environment.","error":"ImportError: No module named swiglpk"},{"fix":"Install GLPK via system package manager (e.g., apt-get install glpk-utils libglpk-dev on Debian/Ubuntu) or via conda: conda install -c conda-forge glpk.","cause":"The GLPK shared library is missing from the system.","error":"OSError: libglpk.so: cannot open shared object file: No such file or directory"},{"fix":"Change ind list to start from 1. For example, for columns 1 and 2, use ind = [1, 2].","cause":"Indices in ind list are 0-based, but GLPK expects 1-based indices.","error":"glp_set_mat_row: len = 2; ind[1] = 0; column index out of range"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/biosustain/swiglpk","github":"https://github.com/biosustain/swiglpk","docs":null,"changelog":null,"pypi":null,"npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-29","last_verified":"2026-06-29","next_check":"2026-07-29","install_tag":null}}