{"id":7408,"library":"mip","title":"Python MIP","description":"Python-MIP (Mixed-Integer Programming) is a Python library for modeling and solving Mixed-Integer Linear Programs (MIPs). It provides a high-level, intuitive API inspired by Pulp, offering access to advanced solver features like cut generation, lazy constraints, and solution pools. The library integrates with popular open-source solvers like COIN-OR CBC and HiGHS, as well as the commercial Gurobi solver. It is actively maintained, with version 1.17.6 being the latest, and aims for high performance and extensibility, supporting Python 3.10+ and PyPy for faster model generation.","status":"active","version":"1.17.6","language":"en","source_language":"en","source_url":"https://github.com/coin-or/python-mip","tags":["optimization","linear programming","mixed-integer programming","MIP","solver","operations research","mathematical modeling"],"install":[{"cmd":"pip install mip","lang":"bash","label":"Install with default (CBC and HiGHS) solvers"},{"cmd":"pip install mip gurobipy","lang":"bash","label":"Install with Gurobi support (requires Gurobi license)"}],"dependencies":[{"reason":"Provides binaries for the HiGHS open-source solver, automatically installed with `pip install mip` since v1.17.","package":"highsbox","optional":true},{"reason":"Required to use the Gurobi commercial solver backend with Python-MIP. Requires a separate Gurobi installation and license.","package":"gurobipy","optional":true}],"imports":[{"symbol":"Model","correct":"from mip import Model"},{"note":"Used for compact summation expressions in objective functions and constraints.","symbol":"xsum","correct":"from mip import xsum"},{"note":"Sets the objective function sense to maximization.","symbol":"maximize","correct":"from mip import maximize"},{"note":"Can be used to explicitly set the objective function sense to minimization (default).","symbol":"MINIMIZE","correct":"from mip import MINIMIZE"},{"note":"Defines a binary variable type (0 or 1). Other types include CONTINUOUS (default) and INTEGER.","symbol":"BINARY","correct":"from mip import BINARY"}],"quickstart":{"code":"from mip import Model, xsum, maximize, BINARY, OptimizationStatus\n\n# 0/1 Knapsack Problem example\np = [10, 13, 18, 31, 7, 15] # profits\nw = [11, 15, 20, 35, 10, 33] # weights\nc, I = 47, range(len(w)) # capacity, items index\n\nm = Model(\"knapsack\")\n\nx = [m.add_var(var_type=BINARY, name=f'x_{i}') for i in I]\n\nm.objective = maximize(xsum(p[i] * x[i] for i in I))\n\nm += xsum(w[i] * x[i] for i in I) <= c\n\nstatus = m.optimize()\n\nif status == OptimizationStatus.OPTIMAL:\n    selected_items = [i for i in I if x[i].x >= 0.99]\n    print(f\"Selected items: {selected_items}\")\n    print(f\"Total profit: {m.objective_value}\")\n    print(f\"Total weight: {sum(w[i] for i in selected_items)}\")\nelif status == OptimizationStatus.FEASIBLE:\n    print(\"Solution found, but not necessarily optimal.\")\nelif status == OptimizationStatus.INFEASIBLE:\n    print(\"Model is infeasible.\")\nelif status == OptimizationStatus.UNBOUNDED:\n    print(\"Model is unbounded.\")\nelse:\n    print(f\"Optimization status: {status}\")","lang":"python","description":"This quickstart solves a 0/1 Knapsack Problem, demonstrating model creation, variable definition, objective function, adding constraints, optimization, and result querying."},"warnings":[{"fix":"Consider optimizing your model construction logic, especially for very large, dense problems. For extremely performance-critical setups, exploring alternative data structures for linear expressions might be necessary, or consider if another library is more suitable for problem generation speed.","message":"For large-scale models, problem setup (defining variables and constraints) in Python-MIP can be noticeably slower compared to other modeling libraries like CVXPY due to internal object instance checks.","severity":"gotcha","affected_versions":"1.x"},{"fix":"Check for solver-specific logs if available. Ensure your model is well-formed, data types are correct, and variable bounds are consistent. Sometimes simplifying the problem or updating the `mip` package and its underlying solver binaries can resolve such issues. Contacting developers with a minimal reproducible example is also an option.","message":"When `model.optimize()` returns `OptimizationStatus.ERROR`, it indicates a problem at a deeper system level (e.g., within the solver's C library) which often doesn't raise a Python exception. This makes debugging difficult.","severity":"gotcha","affected_versions":"1.x"},{"fix":"Upgrade to the latest version of `python-mip`. Version 1.13.0 and later include updated CBC binaries that addressed some longstanding bugs on MacOS and likely improved stability on Windows as well.","message":"Older versions of `python-mip` (pre-1.13.0) on Windows could experience silent crashes (e.g., 'Faulting module name: msvcrt.dll') when solving complex models, often related to older CBC binaries.","severity":"gotcha","affected_versions":"<1.13.0"},{"fix":"Ensure your Python environment is 3.10 or newer. Python 3.11 support was explicitly added in version 1.15.0.","message":"Python-MIP officially supports Python 3.10 and newer. While older versions might work with Python 3.7-3.9, compatibility may not be guaranteed, and new features (like HiGHS solver support) might require newer Python versions.","severity":"deprecated","affected_versions":"<1.15.0"},{"fix":"Review the HiGHS integration documentation for 1.17.x to leverage the new, official support. `highsbox` is now automatically installed as an optional dependency.","message":"Version 1.17.x introduced full, first-class support for the HiGHS solver, replacing any prior experimental or pre-release integrations. While a major improvement, users who were relying on specific (potentially undocumented) HiGHS configurations in older pre-release versions might need to adjust their code.","severity":"breaking","affected_versions":"<1.17.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Inspect the model for inconsistencies (e.g., conflicting constraints, incorrect bounds). Try a simpler version of the model to isolate the issue. Ensure `mip` and its solver dependencies are up to date. Check for solver-specific log files if enabled.","cause":"The solver encountered an unrecoverable error at a low level, often without a specific Python exception. This could be due to memory issues, inconsistent model data, or solver bugs.","error":"OptimizationStatus.ERROR"},{"fix":"Refactor model construction to use iterative loops instead of recursion where possible. For expressions, leverage `mip.xsum()` for summations. If necessary, `sys.setrecursionlimit()` can be increased, but this is generally not recommended as a primary fix.","cause":"This typically occurs when constructing very large, deeply nested expressions or lists of variables/constraints in a recursive manner, exceeding Python's default recursion limit.","error":"RuntimeError: maximum recursion depth exceeded while calling a Python object"},{"fix":"Profile your code to pinpoint exact bottlenecks. Ensure you're using efficient methods like `m.add_var_tensor()` for blocks of variables and `xsum()` for summations. Consider generating model data using NumPy/Pandas and then efficiently transferring it to the MIP model. For extremely large models, sometimes other modeling tools or direct solver APIs might offer faster model construction if that is the primary bottleneck.","cause":"Python-MIP's overhead for creating variables and expressions, particularly due to internal object instance checks and Python's dynamic nature, can be a bottleneck for extremely large models.","error":"Model construction is very slow for large problems."},{"fix":"Update `python-mip` to the latest version (`pip install --upgrade mip`). Version 1.13.0 and newer include updated CBC binaries which improved stability. If the problem persists, ensure your C++ redistributables are up to date.","cause":"Older versions of `python-mip` sometimes shipped with outdated or incompatible CBC solver binaries, leading to instability on certain Windows systems.","error":"Crashes on Windows with 'Faulting module name: msvcrt.dll' or similar DLL errors."}]}