Try our new documentation site.


sensitivity.py


#!/usr/bin/python

# Copyright 2013, Gurobi Optimization, Inc.

# Simple MIP sensitivity analysis example.
# For each integer variable, fix it to its lower and upper bound
# and check the impact on the objective.

import sys
from gurobipy import *

if len(sys.argv) < 2:
    print('Usage: sensitivity.py filename')
    quit()


a = gurobi.read(sys.argv[1])
a.optimize()
a.params.outputFlag = 0

# Extract variables from model

avars = a.getVars()

# Iterate through binary variables in model

for i in range(len(avars)):
    v = avars[i]
    if v.vType == GRB.BINARY:

# Create clone and fix variable

        b = a.copy()
        bv = b.getVars()[i]
        if v.x - v.lb < 0.5:
            bv.lb = bv.ub
        else:
            bv.ub = bv.lb

        b.optimize()

        if b.status == GRB.status.OPTIMAL:
            objchg = b.objVal - a.objVal
            if objchg < 0:
                objchg = 0
            print('Objective sensitivity for variable %s is %g' % \
                  (v.varName, objchg))
        else:
            print('Objective sensitivity for variable %s is infinite' % \
                  v.varName)

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.

Search