找到 'True' 需要一个元组或等式。约束没有正确的值

Found 'True' Expecting a tuple or equation. Constraint does not have a proper value

我有如下一段代码(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'], model.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]

您将需要制作一个 bus-bus 的子集来排除“对角线”,这很容易通过列表理解来实现,并将该子集用作您的约束的输入。

***** 编辑...

注意你在任何地方都会遇到类似的问题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 中似乎不是问题,但您的观察是正确的,因为需要创建一个子集以避免在这些无用值处评估约束。 再次感谢您阐明这一点。最良好的祝愿,