【发布时间】:2021-06-07 04:34:10
【问题描述】:
我正在使用 pyomo 及其“gurobi_persistent”接口来解决 MIP。我试图实现一个回调,它检查一个足够小的最优性差距,如果达到它就会终止。当然,这必须以某种方式发生,即当前的解决方案将被读回 pyomo 模型。我不知道该怎么做。当满足停止条件时,我得到
AttributeError: 'GurobiPersistent' 对象没有属性 'terminate'
通常,在 gurobipy 中,语句 model.terminate() 应该可以工作。 另请参阅下面的回调定义。
solver = pe.SolverFactory('gurobi_persistent')
solver.set_instance(m)
def my_callback(cb_m, cb_opt, cb_where):
if cb_where == GRB.Callback.MIP:
# General MIP callback
objbst = cb_opt.cbGet(GRB.Callback.MIP_OBJBST)
objbnd = cb_opt.cbGet(GRB.Callback.MIP_OBJBND)
if abs(objbst - objbnd) < percentGap * (1.0 + abs(objbst)):
print('Stop early - {} % gap achieved'.format(percentGap*100))
# statement that does not exist
cb_opt.terminate()
solver.set_callback(my_callback)
solver.solve(tee=True)
我将不胜感激有关如何实现所需行为的一些提示。 祝你有美好的一天!
【问题讨论】: