【问题标题】:How to use MIPGap and TimeLimit from Gurobi in python?如何在 python 中使用 Gurobi 的 MIPGap 和 TimeLimit?
【发布时间】:2021-01-22 14:53:34
【问题描述】:

我正在研究大型 MILP。所以我必须将时间限制设置为一个合理的值,或者我必须将 MIPGap 设置为一个合理的水平。我已经知道 gurobi 的文档了。

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

时间限制: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,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)
  

【问题讨论】:

    标签: python optimization mathematical-optimization gurobi approximation


    【解决方案1】:

    我已经有几年没有运行此代码了...我不再拥有 Gurobi 许可证了。但是,这样的事情应该有效。你没有提到你是如何编码你的模型的。以下来自pyomo 脚本。我认为类似的东西会起作用,但是您可以处理求解器实例。

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

    【讨论】:

    • 我添加了一些我的代码也许有帮助 谢谢你我会试试的
    • OK...所以你在 gurobipy 中对其进行编码。尽管您的变量 mipgap 似乎没有定义,但您的陈述看起来很正确。此外,您需要在调用优化之前设置这些参数 以产生任何效果。您还可以尝试设置参数,然后使用一次性打印语句检查它们。看起来您正在按照此处的指南进行操作。 gurobi.com/documentation/9.0/refman/…
    • 原始问题涉及 Gurobi Python API,而不是 Pyomo。
    • 只有在它被编辑为包含 Gurobi API 代码之后......看看 OP 的编辑。但感谢您重复我的评论作为答案。
    【解决方案2】:

    您需要在调用 Model.optimize() 之前设置参数。此外,MIPGap 和 TimeLimit 的单位分别是分数和秒。所以你的代码应该是:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多