Search code examples
pythonconstraintspyomo

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


I have the following piece of code (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 is a scalar, SUSC is a dictionary with keys (n,m) where n and m are indexed over N=['N1'...'N6'], model.Tht is a variable, where t belongs to T=[1,2,...7] and Fmax is a dictionary.

I am getting the following error when defining constraint 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) 

Any help will be appreciated.


Solution

  • You are getting that error because when you pass in the values cited in the error (or anytime n==m), the variable is cancelling out, leaving you with no variables, and just a boolean expression.

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

    when n==m this simplifies to:

    0 >= -Fmax[n,m]
    

    You will need to make a subset of bus-bus that excludes the "diagonal", which is easy to do with a list comprehension and use that subset as the input to your constraint.

    ***** edit...

    Note you will have similar problem anywhere SUSC[b, b'] == 0

    Here is a fix that I think solves all:

    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)