Search code examples
pythoncvxpy

numpy.bool_' object has no attribute 'parameters'


I have encountered a weird error, reporting AttributeError: 'numpy.bool_' object has no attribute 'parameters'

import numpy as np
import math
import cvxpy as cp

def max_peak(power_signal,overall_ld):

    max_value = overall_ld[0] + power_signal[0]
    for i in range(1,time_slot):
        max_value= cp.maximum(max_value,\
                              overall_ld[i]+power_signal[i])
    return max_value

def offline_opt(overall_load):

    power_signal = cp.Variable(time_slot)
    
    obj_constraints = []

    for time in range(time_slot):
        SoC = 0
        for i in range(time):
            SoC += ((1-agg[3])**(i))*power_signal[time-i] 
        print("agg[0]=",agg[0])
        obj_constraints += [-agg[0]<=SoC, SoC<= agg[0]]
    
    prob = cp.Problem(cp.Minimize(max_peak(power_signal, overall_load)), \
                      constraints=obj_constraints)

    prob.solve(solver=cp.SCS)

    return prob.value

if __name__ == "__main__":

    time_slot = 3

    agg = np.array([10.0,2.0,2.0,0])
    
    offline_opt(np.array([10,5,3]))

The problem lies in the line (obj_constraints += [-agg[0]<=SoC, SoC<= agg[0]]). But if I replace agg[0] by 10, -agg[0] by -10. Then it goes through and gives me a result. So I checked the type and value of agg[0],

IPdb [2]: agg[0]
10.0

IPdb [4]: type(agg[0])
<class 'numpy.float64'>

So what is going on?

Thanks!

Full stack here:

  File "", line 1187, in <module>
    off_peak = offline_opt(overall_ld)

  File "", line 261, in offline_opt
    prob.solve(solver=cp.MOSEK)

  File "C:\Users\anaconda3\lib\site-packages\cvxpy\problems\problem.py", line 462, in solve
    return solve_func(self, *args, **kwargs)

  File "C:\Users\anaconda3\lib\site-packages\cvxpy\problems\problem.py", line 878, in _solve
    for parameter in self.parameters():

  File "C:\Users\anaconda3\lib\site-packages\cvxpy\utilities\performance_utils.py", line 70, in _compute_once
    result = func(self, *args, **kwargs)


  File "C:\Users\anaconda3\lib\site-packages\cvxpy\problems\problem.py", line 336, in parameters
    params += constr.parameters()

AttributeError: 'numpy.bool_' object has no attribute 'parameters'

UPDATED: The code could be copy and paste run on your machine now. It's self-contained.


Solution

  • In the first iteration of the loops inside offline_opt, time is set to zero. The inner loop (for i in range(time)) doesn't happen (because of range(0)), so SoC is still zero.

    The constraints after the first iterations are: [-agg[0]<=0, 0<= agg[0]], which are evaluated to boolean (as no expression of variables involved). If you print the value of obj_constraints after the first iteration you will see it's [True, True]. CVXPY doesn't know what to do with boolean values, as it expected the constraints to include expressions.

    A "quick but stupid" fix is to check if SoC is of type cvxpy.Expression before adding it as a constraint:

    if isinstance(SoC, cp.Expression):
        obj_constraints += [-agg[0]<=SoC, SoC<= agg[0]]
    

    P.S - instead of [-agg[0]<=SoC, SoC<= agg[0]] you can simply write [SoC <= cp.abs(agg[0])]