找到'True' 期待元组或方程。约束没有适当的值



我有以下代码(mwe(:

import pandas as pd
import numpy as np   
from pyomo.environ import *
from os import system, name 

T     = [1, 2, 3, 4, 5, 6, 7] 
BUS   = ['N1', 'N2', 'N3', 'N4', 'N5', 'N6']
SBASE = 100
b = np.array( 
[[    0,    10,     10,      0,      0,        0],
[   10,     0,     10,     10,      0,        0],
[   10,    10,      0,      0,      0,       10],
[    0,    10,      0,      0,     10,       10],
[    0,     0,      0,     10,      0,       10],
[    0,     0,     10,     10,     10,        0]])   
SUSC = { (BUS[i], BUS[j]): float(b[i,j]) 
for i in range(b.shape[0])
for j in range(b.shape[1])}
fmax = np.array([
[0,       5000,   5000,      0  ,      0 ,   0  ],  
[5000,       0,   5000,     150 ,      0 ,   0  ],
[5000,    5000,    0  ,      0  ,      0 ,  150 ],
[ 0,       150,    0  ,      0  ,    5000,  5000], 
[ 0,        0 ,    0  ,     5000,      0 ,  5000],
[ 0,        0 ,    150,     5000,    5000,     0]])

Fmax = {(BUS[i],BUS[j]): fmax[i,j]
for i in range(fmax.shape[0])
for j in range(fmax.shape[1])}

# Optimization problem:
model = ConcreteModel()
model.dual = Suffix(direction=Suffix.IMPORT) 
model.Tht  = Var(BUS,T,within = Reals, bounds = (None,None)) 

def r91(model,n,m,t):
return SBASE*SUSC[n,m]*(model.Tht[n,t]-model.Tht[m,t]) >= -Fmax[n,m]
model.R91 = Constraint(BUS,BUS,T, rule = r91)

SBASE是一个标量,SUSC是一个带有键(n,m(的字典,其中n和m在n=[‘N1’…‘N6’],模型上进行索引。Tht是一个变量,其中t属于t=[1,2,…7],Fmax是一个字典。

我在定义约束R91时得到以下错误:

ERROR: Rule failed when generating expression for constraint R91 with index
('N1', 'N1', 1): ValueError: Constraint 'R91[N1,N1,1]' does not have a
proper value. Found 'True' Expecting a tuple or equation. Examples:
sum(model.costs) == model.income (0, model.price[item], 50)
ERROR: Constructing component 'R91' from data=None failed: ValueError:
Constraint 'R91[N1,N1,1]' does not have a proper value. Found 'True'
Expecting a tuple or equation. Examples:
sum(model.costs) == model.income (0, model.price[item], 50) 

任何帮助都将不胜感激。

您之所以会得到这个错误,是因为当您传入错误中引用的值时(或在n=m的任何时候(,变量会被取消,因此您没有变量,只有一个布尔表达式。

SBASE*SUSC[n,m]*(model.Tht[n,t]-model.Tht[m,t]) >= -Fmax[n,m]

当n==m时,这简化为:

0 >= -Fmax[n,m]

您将需要制作总线总线的子集;"对角线";,这很容易通过列表理解来完成,并将该子集用作约束的输入。

*****编辑。。。

请注意,在SUSC[b, b']==0 的任何位置,您都会遇到类似的问题

这里有一个我认为可以解决所有问题的解决方案:

bus_combos = [(b, b_prime) for b in BUS for b_prime in BUS 
if SUSC[b, b_prime] > 0]
def r91(model,n,m,t):
return SBASE*SUSC[n,m]*(model.Tht[n,t]-model.Tht[m,t]) >= -Fmax[n,m]
model.R91 = Constraint(bus_combos,T, rule = r91)

非常感谢您的评论和提出的解决方案。有趣的是,您对约束的分析确实导致了:

0 >= 0

这从数学观点(约束(来看不是问题,但是Pyomo不能评估该约束。尽管这段代码在AMPL中似乎不是问题,但您的观察是正确的,因为需要创建一个子集来避免在这种无用的值下评估约束。再次感谢您阐明这一点。最美好的祝愿,

最新更新