Pyomo:保存来自Gurobi的亚二元解(SolCount,X,Xn,SolutionNumber)

时间:2019-05-06 17:48:09

标签: python gurobi pyomo

我需要在指定的期限内访问并保存Gurobi找到的最佳次优解决方案。

大家好,

我一直在使用Pyomo和求解器gurobi来求解特定模型。达到最佳状态的时间非常长,因此我通过访问TimeLimit选项为求解器指定了时间限制:

optsolver = SolverFactory('gurobi',solver_io='python')
optsolver.options['TimeLimit'] = 30
results = optsolver.solve(instance1, tee=True)

此后,我建立了一个条件,因此,如果解决方案以最佳状态结束,则该参数将作为参数传递给某个pyomo_postprocess函数,该函数以一种方便的方式为我的应用程序组织变量的值。

现在,我要使用gurobi SolCount,X,Xn,SolutionNumber 的属性来保存在时间限制之前达到的最佳可行解决方案。

这是我的第一次尝试:

if (results.solver.status == SolverStatus.ok) and (
        results.solver.termination_condition == TerminationCondition.optimal):

    registro = open('registro'+data_name+'.txt', 'w+')
    registro.write('optimal and feasible\n')
    registro.close()
    pyomo_postprocess(instance1, results,data_name)

elif (results.solver.status == SolverStatus.aborted):
    registro = open('registro' + data_name + '.txt', 'w+')
    registro.write('TimeLimit\n')
    registro.write(str(results.solver.status))
    registro.close()
    pyomo_postprocess(instance1, results, data_name)

但是:

警告:正在加载状态为“异常终止”的SolverResults对象,但是     包含解决方案。 ValueError:无法加载状态错误的SolverResults对象:已终止

您能告诉我如何对这些次优的Gurobi解决方案进行后期处理吗? 非常感谢哥伦比亚的问候!

0 个答案:

没有答案