'Pyomo Error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I am building an optimization model in pyomo and keep facing this error which I cannot solve. Here is the part where the error occurs:

model.ct2demand = ConstraintList()
for n in model.N: 
    for s in model.S:
        for t in model.T: 
            for p in model.P:
                lhs = model.f[p,t,s,n]*1000 
                rhs = model.y[p,t,s,n] + model.sales[p,t,s,n] + model.error[p,t,s,n] 
                model.ct2demand.add (lhs == rhs) 

the variable f and error are multidimensional parameters (non-negative reals), which I entered as numpy arrays and think are causing this issue based on my research, but I haven't really figured out exactly why. y and sales are decision variables. And the following is the error message:

ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_21076/2031668078.py in <module>
    422             for p in model.P:
    423                 lhs = model.f[p,t,s,n]
--> 424                 rhs = model.y[p,t,s,n] + model.sales[p,t,s,n] + model.error[p,t,s,n]
    425                 model.ct2demand.add (lhs == rhs)
    426 

pyomo\core\expr\numvalue.pyx in pyomo.core.expr.numvalue.NumericValue.__add__()

pyomo\core\expr\numeric_expr.pyx in pyomo.core.expr.numeric_expr._generate_sum_expression()

pyomo\core\expr\numeric_expr.pyx in pyomo.core.expr.numeric_expr.SumExpression.add()

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I did some research but still didn't really get what is causing the issue here. Any guidance would be highly appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source