微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在python中使用Gurobi的MIPGap和TimeLimit?

如何解决如何在python中使用Gurobi的MIPGap和TimeLimit?

我正在大规模进行MILP。因此,我必须将时间限制设置为合理的值,或者将MIPGap设置为合理的水平。我已经知道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将停止

TimeLimit Gurobi将在一定时间后停止。

但是您能给我一个例子,将时间限制设置为5分钟吗? 或将MIPGap设置为5%?

我不知道该如何准确地实现那些角色?

请帮助我,我对python还是很陌生

我尝试了这个,但这不起作用

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

这是我模型的简短版本


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,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,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)
  

解决方法

在调用Model.optimize()之前,需要设置参数。同样,MIPGap和TimeLimit的单位分别是小数和秒。因此您的代码应为:

model.Params.MIPGap = 0.05    # 5%
model.Params.TimeLimit = 300  # 5 minutes
model.optimize()
,

或者,您可以调用模型的setParam() method

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

我已经有两年没有运行这段代码了……我再也没有Gurobi许可证了。但是,这样的事情应该起作用。您没有提到如何编码模型。以下来自pyomo脚本。我认为类似的方法也可以,但是您可以在求解器实例上找到一个句柄。

solver = SolverFactory("gurobi")
solver.options['timeLimit'] = 1200  # seconds
solver.options['mipgap'] = 0.01

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。