Pyomo:将结果保存到 CSV 文件 [英] Pyomo: Save results to CSV files

查看:85
本文介绍了Pyomo:将结果保存到 CSV 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找保存 Pyomo 结果的方法花了我 2 小时 :(

Seeking for ways to save my Pyomo results took my 2 hours :(

我需要在成功解决问题后保存我的最佳变量值和双变量值.让我们从最优变量开始.

I need to save my optimal variable values and dual variable values after successfully solving a problem. Let's start from the optimal variables.

我的模型中有三个变量,120 个带有 3 个索引的P"变量、120 个带有 3 个索引的I"变量和 576 个带有 3 个索引的F"变量.它们的指标不同.以下是我的解决语句:

I have three variables in my model, 120 'P' variables with 3 indices, 120 'I' variables with 3 indices and 576 'F' variables with 3 indices. Their indices are different. The following is my solve statements:

solver = pyomo.opt.SolverFactory('gurobi')
results = solver.solve(m, tee=True, keepfiles=True)
m.display()

results.write()
m.solutions.load_from(results)

这里似乎一切正常,我在 0xa23c7bcf8> 处获得了 m.solutions 作为

Everything seems to be fine here, and I have obtained a m.solutions as type of <pyomo.core.base.PyomoModel.ModelSolutions at 0xa23c7bcf8>. Then I tried the next thing:

list_of_vars = [v for v in m.component_objects(ctype=pe.Var, active=True, descend_into=True)]
print(list_of_vars)

之后,我得到了另一个 list_of_vars 对象作为列表类型,其中 3 个元素为 '',这也是合理的.

Afterwards, I got another list_of_vars object as type of a list with 3 elements as '', which is also reasonable.

那我下一步需要做什么?如何将 m.solutionslist_of_vars 转换为数据框或列表或 csv 文件?非常感谢!

Then what do I need to do in the next step? How can I transform the m.solutions or list_of_vars to a dataframe or list or csv files? Thanks a lot!

加布里埃尔

推荐答案

好的让我来回答我自己的问题 :)

OK let me answer my own question :)

我找到了两种有效解决这个问题的方法.

I found two ways to efficiently tackle this problem.

1).手动写入 CSV 文件

1). Manually write into CSV files

将变量记为P,模型名记为m,然后在解后,即执行如下命令,

Denote the variable as P, the model name as m, then after the solution, i.e. executing the following command,

results = solver.solve(m, tee=True)

我们将获得元组中的最优变量值,例如m.P.只需使用它们的索引就可以检索元组的索引值.以我的三索引案例为例,将其保存到一个csv文件中:

we will obtain the optimal variable values in tuple, e.g. m.P. The indexed value of the tuple can be retrieved by just using their indices. Take my three-index case as example, to save it to a csv file:

with open('P.csv', 'w') as f:
    f.write('index1, index2, index3, value\n')
    for (n1,n2,n3) in index_set:
        f.write('%s, %s, %s, %s\n' % (n1, n2, n3, m.P[(n1,n2,n3)].value))

2).元组到数组的转换

2). Tuple-to-array Transformation

这种方式在某种程度上更加棘手,当我们需要首先通过使用面向对象编程理论中的广播将索引和值与最佳结果相除时.Pyomo 开发人员可能会忘记编写一种迭代方式来直接从他们构造的格式中导入它们以保存最佳解决方案,因此我们必须使用它:

This way is somehow more tricky, when we need to first divide the indices and values from the optimal results, by using broadcasting in the OOP theory. Pyomo developers might forget to write an iterative way to directly import them from their constructed format for saving the optimal solutions, so we have to use it:

ind = list(m.P)
val = list(m.P[:,:,:].value)

然后使用 zip 链接它们:

Then use a zip to link them:

result_P = [ i + tuple([j]) for i,j in zip(ind, val)]

现在我们获得了包含我们想要的所有索引和值的元组 result_P.之后,将此元组转换为数组,然后转换为 CSV 是微不足道的:

Now we obtain the tuple result_P with all the indices and values that we want. Afterwards it is trivial to convert this tuple to array, then to CSV:

result_P = np.asarray(result_P)
np.savetxt('result_P.csv', result_P, delimiter=',')

加布里埃尔

这篇关于Pyomo:将结果保存到 CSV 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆