Google OR-Tools
Google OR-Tools is an open-source, fast, and portable software suite for solving combinatorial optimization problems. It provides a comprehensive collection of solvers for linear programming, mixed-integer programming, constraint programming (CP-SAT), vehicle routing, and network flow problems. Written primarily in C++, it offers official wrappers for Python, C#, and Java. As of version 9.15.6755, the library is actively developed with frequent releases delivering new features, performance enhancements, and bug fixes across its various solvers.
Warnings
- breaking OR-Tools 9.x and later require Python 3.9 or higher. Support for Python 3.8 was dropped.
- deprecated The `MPSolver` for linear and mixed-integer programming is deprecated. Users should migrate to `ModelBuilder` or the newer `MathOpt` API for future compatibility and improved features. `MathOpt` is being actively developed and will eventually replace `ModelBuilder`.
- gotcha After OR-Tools 9.8, the CP-SAT Python API methods were refactored to use PEP8-compliant snake_case naming conventions (e.g., `solver.parameters.set_max_time_in_seconds`). While older CamelCase methods are currently still supported, it is best practice to update your code to use the snake_case equivalents to avoid potential future breakage or deprecation.
- gotcha Using callbacks (e.g., `on_solution_callback` in CP-SAT) can significantly degrade search performance, especially if not implemented efficiently. Additionally, unhandled exceptions within these callbacks can lead to a Fatal Python error and program termination without a stack trace.
- gotcha It's a common mistake to confuse Mixed-Integer Programming (MIP) solvers with Linear Programming (LP) solvers. Ensure you select the correct solver backend (e.g., `pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING` for MIP or `pywraplp.Solver.GLOP_LINEAR_PROGRAMMING` for LP) appropriate for your problem type, otherwise results may be incorrect (e.g., always returning 0 for integer variables if an LP solver is used for an MIP problem).
- breaking OR-Tools v10.0 is slated to migrate Python wrappers for routing and constraint_solver from SWIG to pybind11, which may introduce breaking changes in API structure or behavior. Additionally, Python 3.14 support has been identified as broken in some pre-release testing.
Install
-
pip install ortools
Imports
- pywraplp
from ortools.linear_solver import pywraplp
- cp_model
from ortools.sat.python import cp_model
- routing_enums_pb2, pywrapcp
from ortools.constraint_solver import routing_enums_pb2, pywrapcp
- init
from ortools.init.python import init
Quickstart
from ortools.linear_solver import pywraplp
def main():
# Create the linear solver with the GLOP backend.
solver = pywraplp.Solver.CreateSolver('GLOP')
if not solver:
return
# Create the variables x and y.
x = solver.NumVar(0, 1, 'x')
y = solver.NumVar(0, 2, 'y')
print('Number of variables =', solver.NumVariables())
# Define the constraints: x + y <= 2
solver.Add(x + y <= 2.0)
print('Number of constraints =', solver.NumConstraints())
# Define the objective function: Maximize 3 * x + y.
solver.Maximize(3 * x + y)
# Invoke the solver and display the results.
status = solver.Solve()
if status == pywraplp.Solver.OPTIMAL:
print('Solution:')
print('Objective value =', solver.Objective().Value())
print('x =', x.solution_value())
print('y =', y.solution_value())
else:
print('The problem does not have an optimal solution.')
print('\nAdvanced statistics:')
print('Problem solved in %f milliseconds' % solver.wall_time())
print('Problem solved in %d iterations' % solver.iterations())
if __name__ == '__main__':
main()