我可以将字符串转换为 pyomo 变量吗? [英] Can I transform string to pyomo variable?

查看:87
本文介绍了我可以将字符串转换为 pyomo 变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .txt 文件,其中包含我的模型的变量.如果我将文件的内容复制并粘贴到我的程序中作为

I have a .txt file which contains a variable for my model. If I copy and paste the contents of the file in my program as

def Wind_phi_definition(model, i):
         return m.Wind_phi[i] ==-19.995904426195736*Sinc(0.04188790204786391*(0. + m.lammda[i]*180/np.pi))*Sinc(0.08975979010256552*(-65. + m.phi[i]*180/np.pi))
m.Wind_phi_const = Constraint(m.N, rule = Wind_phi_definition)

代码执行没有问题.我想通过让程序直接从 .txt 文件中读取来加快速度.

The code is executed without issue. I want to speed this by making the program read directly from the .txt file.

我试图将变量读作

f = open("savedWindXPython.txt", "r")
a=f.readline()
f.close

def Wind_lammda_definition(model, i):
    return m.Wind_phi[i] == a
m.Wind_phi_const = Constraint(m.N, rule = Wind_lammda_definition)

但是返回错误AttributeError: 'str' 对象没有属性 'is_relational'

However, an error is returned AttributeError: 'str' object has no attribute 'is_relational'

我知道发生这种情况是因为 python 将其作为字符串而不是 pyomo 变量读取.我试图在 m.Wind_phi 的定义中使用 exec(a) 而不是 a 来解决这个问题.但是,我仍然得到一个错误,这次它说

I understand that this happens because python is reading this as a string and not as a pyomo variable. I have attempted to go arround this problem using exec(a) instead of just a in the definition of m.Wind_phi. However, I still obtain an error, this time it says

'NoneType' 对象没有属性 'is_relational'

'NoneType' object has no attribute 'is_relational'

有没有办法做我想做的事情并通过读取 .txt 文件而不是手动复制它的内容来定义变量?

Is there a way to do what I want to do and define the variable by reading the .txt file isntead of having to copy it's contents by hand?

推荐答案

您要实现的目标称为 解组(或反序列化)

What you are trying to achieve is called unmarshalling (or deserialization)

如果您的变量是一个简单的数字,反序列化它就像 int(my_string_var) 一样简单.如果它看起来像您链接的那个(您需要在其中调用函数的地方),则还有更多工作要做.您使用的库还可以提供一些序列化/反序列化功能.不幸的是,您似乎 pyomo 目前不提供此功能.

If your variable is a simple number, deserializing it is as easy as int(my_string_var). If it looks like the one you linked (where you need function calls in it), there is more work to be done. The lib you're using can also provide some serialization/deserialization feature. Unfortunately for you it seems like pyomo doesn't provide this feature at the moment.

所以,现在,如果您首先可以访问谁或什么正在编写您的 .txt 文件,请考虑将其序列化为 json(或任何数据-您最喜欢的存储格式;如果您不喜欢任何格式,请尝试 json).然后,您现在将了解如何在您的代码中反序列化它.

So, now, if you have access to who or what is writing your .txt files in the first place, consider serializing it into json (or whatever data-storage format you're the most comfortable with; if you're not comfortable with any, try json). Then you will now how to deserialize it in your code.

否则,您将不得不编写一个字符串解析器来从您的字符串中提取数据并将其序列化为 Python 对象.如果您只是试图解析这个特定变量,这可能是微不足道的,但如果您试图反序列化许多这些变量,这最终也会变得相当具有挑战性.

Otherwise, you will have to write a string parser to extract the data from your string and serialize it into python objects. This could be trivial if you're only trying to parse this specific variable, but could also end up being rather challenging if you are trying to deserialize a lot of these variables.

这篇关于我可以将字符串转换为 pyomo 变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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