In other posts I have demonstrated how one can solve e.g. linear optimization problems using modules such as SciPy and PuLP in Python. In R I have also demonstrated e.g. the lpSolve package.

In this post I want to demonstrate how one can use Google´s Glop solver from the ortools module in Python.

The problem to solve is stated below:

In order for me to be able to solve the problem with Google’s Glop solver I first need to instsall the ortools module in Python. ortools is Google’s OR-Tool module in Python.

After installing ortools I import the pywraplp sub-module from ortools.linear_solver in Python:

from ortools.linear_solver import pywraplp

Next, I create a solver instance (using GLOP solver) and store its reference to a reference handler:

solver = pywraplp.Solver.CreateSolver('linear_programming_examples', 'GLOP')

I now have to declare relevant optimization variables. In this example the optimization variables are x, y and z:

# declare variable x, with lower bound 0 and no upper bound x = solver.NumVar(0, solver.infinity(), "x") # declare variable y, with lower bound 0 and no upper bound y = solver.NumVar(0, solver.infinity(), "y") # declare variable z, with lower bound 0 and no upper bound z = solver.NumVar(0, solver.infinity(), "z")

As explained in the comments above one must specify the lower and upper bound when declaring the optimization variable. Thereby the non-negativity constraint is already considered by the solver after having declared the optimization variables.

Having declared the optimization variables I can now declare the objective function:

# add an objective to the solver objective = solver.Objective() # add terms to objective such that objective function results from it objective.SetCoefficient(x, 1) objective.SetCoefficient(y, 2) objective.SetCoefficient(z, 3) # declare problem as an maximization problem objective.SetMaximization()

After having defined the objective function I now have to add relevant constraints to the solver:

# add constraint: 2x + y + z <= 20 constraint = solver.Constraint(-solver.infinity(), 20) constraint.SetCoefficient(x, 2) constraint.SetCoefficient(y, 1) constraint.SetCoefficient(z, 1) # add constraint: x + y + z <= 15 constraint = solver.Constraint(-solver.infinity(),15) constraint.SetCoefficient(x, 1) constraint.SetCoefficient(y, 1) constraint.SetCoefficient(z, 1) # add constraint: x - y - z >= 0 constraint = solver.Constraint(0,solver.infinity()) constraint.SetCoefficient(x, 1) constraint.SetCoefficient(y, -1) constraint.SetCoefficient(z, -1)

The model is now complete.

We can proceed with solving the problem. This is done in the line of code below:

solver.Solve()

0

The optimal solution for x is outputed below:

print("x_opt: ", x.solution_value())

x_opt: 6.666666666666667

The optimal solution for y is outputed below:

print("y_opt: ", y.solution_value())

y_opt: 0.0

The optimal solution for z is outputed below:

print("z_opt: ", z.solution_value())

z_opt: 6.666666666666667

The optimal objective function value is outputed below:

print("optimal value: " + str(x.solution_value()+2*y.solution_value()+3*z.solution_value()))

optimal value: 26.666666666666668

Data scientist focusing on simulation, optimization and modeling in R, SQL, VBA and Python

## 2 thoughts on “Simple linear programming with Google ortools in Python”