# Can I print the linear programming sensitivity analysis report using google or-tools?

One can solve the linear programs using google or-tools with GLOP solver. I wonder if there is a way to print the sensitivity report (like shadow prices etc.).

• Could you provide some context - are you using google spreadsheets or a programming language? Jan 12, 2022 at 8:32
• There's an example in their repo: github.com/google/or-tools/blob/stable/examples/python/… Jan 12, 2022 at 18:14
• @CMichael: Thank you. I am using or-tools as a framework with GLOP solver in python. Jan 15, 2022 at 7:00
• @DavidTorres: Thank you for the reference. This is what I was looking for. The last part of the code gives me the reduced cost for a Linear Program. Jan 15, 2022 at 7:03

Here is a straightforward conversion of Python PuLP code from https://machinelearninggeek.com/sensitivity-analysis-in-python/ into Google OR Tools:

import pandas as pd
from ortools.linear_solver import pywraplp
from ortools.init import pywrapinit

solver = pywraplp.Solver.CreateSolver('GLOP')
A = solver.NumVar(0.0, solver.infinity(), "A")
B = solver.NumVar(0.0, solver.infinity(), "B")
solver.Add(4 * A + 10 * B <= 100, "c0")
solver.Add(2 * A + 1 * B <= 22, "c1")
solver.Add(3 * A + 3 * B <= 39, "c2")

solver.Maximize(60 * A + 50 * B )

status = solver.Solve()
if status == pywraplp.Solver.OPTIMAL:
print('Solution:')
print('Objective value =', solver.Objective().Value())
print('A =', A.solution_value())
print('B =', B.solution_value())
else:
print('The problem does not have an optimal solution.')

activities = solver.ComputeConstraintActivities()
o = [{'Name':c.name(), 'shadow price':c.dual_value(), 'slack': c.ub() - activities[i]} for i, c in enumerate(solver.constraints())]
print(pd.DataFrame(o))


Output is

Solution:
Objective value = 740.0
A = 9.000000000000002
B = 3.9999999999999973