如何在 python 中使用 Gurobi 的 MIPGap 和 TimeLimit?
How to use MIPGap and TimeLimit from Gurobi in python?
我正在处理大规模 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)
我已经有几年没有 运行 这个代码了...我没有 Gurobi 许可证了。但是,这样的事情应该有效。你没有提到你是如何编码你的模型的。以下来自 pyomo
脚本。我认为类似的东西会起作用,但是你可以处理求解器实例。
solver = SolverFactory("gurobi")
solver.options['timeLimit'] = 1200 # seconds
solver.options['mipgap'] = 0.01
调用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)
我正在处理大规模 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)
我已经有几年没有 运行 这个代码了...我没有 Gurobi 许可证了。但是,这样的事情应该有效。你没有提到你是如何编码你的模型的。以下来自 pyomo
脚本。我认为类似的东西会起作用,但是你可以处理求解器实例。
solver = SolverFactory("gurobi")
solver.options['timeLimit'] = 1200 # seconds
solver.options['mipgap'] = 0.01
调用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)