Search code examples
pythonoptimizationmathematical-optimizationgurobiapproximation

How to use MIPGap and TimeLimit from Gurobi in python?


I'm working on a large scale MILP. So I have to set the time limit to a reasonable value or I have to set the MIPGap to a reasonable level. I already know the documentation from gurobi.

MIPGap: https://www.gurobi.com/documentation/6.5/refman/mipgap.html

TimeLimit: https://www.gurobi.com/documentation/8.0/refman/timelimit.html#parameter:TimeLimit

MIPGap Gurobi will stop when it finds a solution within a percentage of optimal

TimeLimit Gurobi will stop after a certain amount of time.

But can you send me an example with setting for example the time limit to 5 minutes or setting the MIPGap to 5 % ?

I don't know how to exactly implement those character?

Please help me I am quite new to python

I tried this but this doesn't work

    model.Params.TimeLimit = 5
    model.setParam("MIPGap", mipgap)

Here is a short version of my model


from gurobipy import *  
import csv
import geopandas as gpd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from pandas.core.common import flatten
import math

################################# SOLVE function START ###################################################################
def solve(
      
       vpmaint, wpunit, wuunit, vumaint,
       kfuel, koil, kbio,
       hb, ht,
       cj, ci,
       zinvestp, zinvestu,
       DEMAND, DEMANDM,
       LOCATION, SOURCE, BTYPE, SOURCEM,
       osi, oij, ojm
       ):
   model = Model("Biomass to liquid supply chain network design")
################################# SOLVE function END ###################################################################

####################################################### variable section START ####################################################################################################
#binary variables #############################   Binary 1-2     ####################################################

#binary 1: Pyrolyse i with capacity p open?
           
   fpopen = {}
   for i in LOCATION:
       for p in R:
           fpopen[i,p] = model.addVar(vtype = GRB.BINARY,name = "fpopen_%s_%s" % (i,p))

#binary 2: Upgrading j with capacity r and technology t open?
           
   fuopen = {}    
   for j in LOCATION:
       for r in R:
           for t in TECHNOLOGY:
               fuopen[j,r,t] = model.addVar(vtype = GRB.BINARY,name = "fuopen_%s_%s_%s" % (j,r,t))
  
################################################ continous variables Integer 1-9      #############################################################
#integer 1: Mass of Biomass type b from Source s to Pyrolyse i         
   xsi = {}    
   for s in SOURCE:
       for i in LOCATION:
           for b in BTYPE:
               xsi[s,i,b] = model.addVar(vtype = GRB.INTEGER,name = "xsi_%s_%s_%s" % (s,i,b))
               
#integer 2:Mass of Biomass type b from Source s to Pyrolyse i
   xjm =  {}
   for j in LOCATION:
       for m in DEMAND:
           xjm[j,m] = model.addVar(vtype = GRB.INTEGER,name = "xjm_%s_%s" % (j,m))
    

   model.update()
   model.modelSense = GRB.MAXIMIZE          
#######################################################   Objective Function START 
      
   model.setObjective(       
                       #quicksum(DEMANDM[m] * l for m in DEMANDM  )   
                       quicksum(xjm[j,m] * l for j in LOCATION for m in DEMAND)
                      - quicksum(ainvest[i] + aoperation[i] + aprod[i] for i in LOCATION)
                      - quicksum(einvest[j] + eoperation[j] + eprod[j] for j in LOCATION)
                     
## ......
   
#######################################################   Constraints  

############################## Satisfy Demand Constraint 1-3 
# Constraint 1: Always Satisfy Demand at marketplace m

   for m in DEMAND:
       model.addConstr(quicksum(xjm[j,m] for j in LOCATION) <= int(DEMANDM[m]))

   # for m in DEMAND:
   #     model.addConstr(quicksum(x[j,m] for j in LOCATION) >= DEMANDM[m])  
                     
# Constraint 2: The amount of bio-oil sent from pyrolyse station i to Upgrading 

###...Here are more constraints


   model.optimize()
   model.getVars()
   model.MIPGap = 5
   model.Params.TimeLimit = 1.0
   model.setParam("MIPGap", mipgap)
  

Solution

  • Alternatively, you could call the setParam() method of the model:

    model.setParam('MIPGap', 0.05)
    model.setParam('Timelimit', 300)