Source code for the experiment on a thin feasible region


from gurobipy import *
import random
import sys

# Test the effect of small perturbations on the optimal solutions
# for a problem with a thin feasible region
rhs = 1e3
m   = Model('Thin line Optimization')
x   = m.addVar(obj=1)
y   = m.addVar(obj=0, lb=-GRB.INFINITY, ub=GRB.INFINITY)
c1  = m.addConstr(  1e-5 * y + 1e-0 * x <= rhs)
c2  = m.addConstr(- 1e-5 * y + 1e-0 * x <= rhs)
m.Params.OutputFlag = 0
m.Params.Presolve   = 0
m.optimize()
xval    = x.X
yval    = y.X
maxdiff = 0
for i in range(1024*1024):
  c1.Rhs = rhs + 2e-6 * random.random()
  c2.Rhs = rhs + 2e-6 * random.random()
  x.Obj  = 1   + 2e-6 * random.random()
  y.Obj  = 0   + 2e-6 * random.random()
  m.optimize()
  x2val = x.X
  y2val = y.X
  error = (xval-x2val)*(xval-x2val) + (yval-y2val)*(yval-y2val)
  if error > 1e-5 + maxdiff:
    print('New maxdiff %g Iter %d Kappa %g Violations: %g %g %g' %
          (error, i, m.KappaExact, m.BoundVio, m.ConstrVio,
           m.DualVio))
    sys.stdout.flush()
    maxdiff = error