gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:30:25 51 4
gpt4 key购买 nike

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

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com