# Copyright 2016, Gurobi Optimization, Inc.

# Use parameters that are associated with a model.
# A MIP is solved for 5 seconds with different sets of parameters.
# The one with the smallest MIP gap is selected, and the optimization
# is resumed until the optimal solution is found.

import sys
from gurobipy import *

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

# Read model and verify that it is a MIP
m = read(sys.argv[1])
if m.isMIP == 0:
    print('The model is not an integer program')

# Set a 5 second time limit
m.Params.timeLimit = 5

# Now solve the model with different values of MIPFocus
bestModel = m.copy()
for i in range(1, 4):
    m.Params.MIPFocus = i
    if bestModel.MIPGap > m.MIPGap:
        bestModel, m = m, bestModel # swap models

# Finally, delete the extra model, reset the time limit and
# continue to solve the best model to optimality
del m
bestModel.Params.timeLimit = "default"
print('Solved with MIPFocus: %d' % bestModel.Params.MIPFocus)