#!/usr/bin/env python3# Copyright 2025, Gurobi Optimization, LLC# Use parameters that are associated with a model.## A MIP is solved for a few 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.importsysimportgurobipyasgpiflen(sys.argv)<2:print("Usage: params.py filename")sys.exit(0)# Read model and verify that it is a MIPm=gp.read(sys.argv[1])ifm.IsMIP==0:print("The model is not an integer program")sys.exit(1)# Set a 2 second time limitm.Params.TimeLimit=2# Now solve the model with different values of MIPFocusbestModel=m.copy()bestModel.optimize()foriinrange(1,4):m.reset()m.Params.MIPFocus=im.optimize()ifbestModel.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 optimalitydelmbestModel.Params.TimeLimit=float("inf")bestModel.optimize()print(f"Solved with MIPFocus:{bestModel.Params.MIPFocus}")
Help and Feedback