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

标签 python optimization mathematical-optimization gurobi approximation

我正在研究大规模 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)
  

最佳答案

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

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

关于python - 如何在 python 中使用 Gurobi 的 MIPGap 和 TimeLimit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64247609/

相关文章:

python - simplejson - 编码 regexp\d+

python - 需要有关 python 中生产者和消费者线程的帮助

java - 如何在java中优化素数生成器?

optimization - 此操作的最佳数据结构

java - 优化 : Oj algorithms (java) versus SCIP (python)

algorithm - 优化参数的类强盗算法?

python - 如何设置 docker ,但在准备好新容器之前使容器保持事件状态

python - xml 文件每次迭代的新列表

node.js - 如何优化我的 heroku web 应用程序

python - 如何使用 Python Gekko 修复 IPOPT 中的局部不可行性?