feasopt.py
#!/usr/bin/python
# Copyright 2009, Gurobi Optimization, Inc.
# This example reads a MIP model from a file, adds artificial
# variables to each constraint, and then minimizes the sum of the
# artificial variables. A solution with objective zero corresponds
# to a feasible solution to the input model.
import sys
from gurobipy import *
if len(sys.argv) < 2:
print 'Usage: feasopt.py filename'
quit()
feasmodel = gurobi.read(sys.argv[1])
# clear objective
for v in feasmodel.getVars():
v.Obj = 0.0
# add slack variables
for c in feasmodel.getConstrs():
sense = c.Sense
if sense != '>':
feasmodel.addVar(obj=1.0, name="ArtN_" + c.ConstrName,
column=Column([-1], [c]))
if sense != '<':
feasmodel.addVar(obj=1.0, name="ArtP_" + c.ConstrName,
column=Column([1], [c]))
feasmodel.update()
# optimize modified model
feasmodel.write('feasopt.lp')
feasmodel.optimize()
