Linear Programming with OR-Tools in Python
This guide will help you understand how to solve a simple linear programming problem using the OR-Tools library in Python. Linear programming helps us find the best outcome (such as maximum profit or lowest cost) under a given set of constraints.
Step-by-Step Explanation
Install OR-Tools Library First, you need to install the OR-Tools library. This library contains tools for optimization, including linear programming.
pip install ortools
Import the OR-Tools Linear Solver We import the necessary module from OR-Tools:
from ortools.linear_solver import pywraplp
Create a Solver We create a solver object using the
pywraplp.Solver.CreateSolver
method. Here, we use “GLOP”, which stands for Google’s Linear Optimization Package.solver = pywraplp.Solver.CreateSolver("GLOP")
Define Variables We define the variables we want to solve for. In this case, we have two variables,
x
andy
. We also set their lower and upper bounds (0 to 100).x = solver.NumVar(0, 100, 'x') y = solver.NumVar(0, 100, 'y')
Add Constraints We add the constraints to our problem. Constraints are conditions that our solution must satisfy.
solver.Add(2*x + 3*y >= 15) # First constraint solver.Add(3*x + 5*y <= 60) # Second constraint solver.Add(x + y == 18) # Third constraint
Define the Objective Function We define our objective function, which is what we want to maximize (or minimize). Here, we aim to maximize the expression
5*x + 8*y
.solver.Maximize(5*x + 8*y)
Solve the Problem We call the
Solve
method to find the optimal solution.results = solver.Solve()
Print the Results We print the values of
x
andy
that give the optimal solution.print(f"x: {x.solution_value()}") print(f"y: {y.solution_value()}")
9. Check for Optimal Solution
We check if the solver found an optimal solution and print a confirmation message.
if results == pywraplp.Solver.OPTIMAL:
print("Optimal Found")
Complete Code
Here is the complete code with all the steps explained:
# First: pip install ortools
from ortools.linear_solver import pywraplp
# Create the solver
solver = pywraplp.Solver.CreateSolver("GLOP")
# Define the variables with their bounds
x = solver.NumVar(0, 100, 'x')
y = solver.NumVar(0, 100, 'y')
# Add the constraints
solver.Add(2 * x + 3 * y >= 15) # First constraint
solver.Add(3 * x + 5 * y <= 60) # Second constraint
solver.Add(x + y == 18) # Third constraint
# Define the objective function to maximize
solver.Maximize(5 * x + 8 * y)
# Solve the problem
results = solver.Solve()
# Print the results
print(f"x: {x.solution_value()}")
print(f"y: {y.solution_value()}")
# Check if an optimal solution was found
if results == pywraplp.Solver.OPTIMAL:
print("Optimal Found")