英文:
Pyomo neglect a constraint
问题
上午好。
我正在尝试在TEMOA中集成一个新的约束条件,这是一个基于Python和Pyomo的能源系统优化模型。该存储库可在以下链接的GitHub上找到:https://github.com/TemoaProject/temoa
在这个存储库中,我在模型定义文件中添加了以下元素:
在temoa_model中:
M.land_types = Set()
M.LandArea = Param(M.regions, M.land_types, M.time_optimize)
M.LandUseIntensity = Param(M.regions, M.land_types, M.tech_all)
M.LandUseConstraint = Constraint(
M.regions, M.time_optimize, M.land_types,
rule=LandUse_Constraint
)
在temoa_rules中:
def LandUse_Constraint(M, r, p, l):
return (0,
sum(
M.V_Capacity[r, S_t, v] * value(M.LandUseIntensity[r, l, S_t]) * 1e10 + 1e10
for S_t in M.LandUseIntensity.sparse_iterkeys()
if (r, p, S_t) in M.processVintages.keys()
for v in M.processVintages[r, p, S_t]
), value(M.LandArea[r, l, p]))
我已经插入了1e10的总和和乘法,以强制约束条件为假。然而,模型完全忽略了这个约束条件。这是语法问题吗?我应该在哪里查找核心问题?
英文:
Goodmorning.
I am trying to integrate a new constraint inside TEMOA, a python / pyomo based energy system optimization model. The repository is available on GitHub at the following link: https://github.com/TemoaProject/temoa
At this repository, I have added in the model definition file the following elements:
In temoa_model:
M.land_types = Set()
M.LandArea = Param(M.regions, M.land_types, M.time_optimize)
M.LandUseIntensity = Param(M.regions, M.land_types, M.tech_all)
M.LandUseConstraint = Constraint(
M.regions, M.time_optimize, M.land_types,
rule=LandUse_Constraint
)
In temoa_rules:
def LandUse_Constraint(M,r,p,l):
return (0,
sum(
M.V_Capacity[r, S_t, v] * value(M.LandUseIntensity[r, l, S_t]) * 1e10 + 1e10
for S_t in M.LandUseIntensity.sparse_iterkeys()
if (r, p, S_t) in M.processVintages.keys()
for v in M.processVintages[r, p, S_t]
), value(M.LandArea[r, l, p]))
I have inserted 1e10 sum and multiplier, to force the constraint to be false. Neverthless, the model totally neglect the constraint. Is a problem of syntax? Where i have to look to find the core problem?
答案1
得分: 0
为了测试目的,请尝试插入一行代码以打印约束并查看数学是否正确。
在实例化模型并使用数据后,您希望这样做。因此,在temoa_run.py
文件中,您可以在创建实例后的第354行处执行此操作。尝试插入此行代码,它将在终端上打印有问题的约束:
self.instance.LandUse_Constraint.pprint()
如果看起来“可信”,您应该检查在求解后插入值后它的样子。您可以在求解命令后的第384行左右执行“display” pyomo命令,该命令会插入值。当然,在此之前,您应该始终首先检查求解器的状态,然后再查看这个。如果它不说“最优”,不要麻烦,这是无意义的。
self.instance.LandUse_Constraint.display()
编辑:刚刚又想了一下... 实际上,在哪里执行.pprint()
并不重要,因为它不受solve的影响,所以您可以将它们都放在后面的位置,但我仍然建议您稍早放置它们,这样您就不必等待解决或者如果它在那里卡住了可以进行检查。
英文:
For t/s purposes, try inserting a line to print the constraint and see if the math looks right.
You want to do this after the model is instantiated with data. So, in the temoa_run.py
file, you can do that at line #354 after the instance is created. Try inserting this line, which will pprint the offending constraint to the terminal:
self.instance.LandUse_Constraint.pprint()
If that looks "believable" you should check what it looks like with the values plugged in after the solve. You can do that around line #384 after the solve command with the "display" pyomo command which plugs in values. Of course, backing up a bit, you should always check the solver status FIRST before looking at this. If it doesn't say "optimal", don't bother, it's gibberish.
self.instance.LandUse_Constraint.display()
Edit: just thought on this again.... It actually doesn't matter where you do the .pprint()
as it is not affected by solve, so you could put them both in the latter spot, but I'd still pop it in earlier so you don't have to wait for solve or if it gets hung up there to inspect.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论