numpy.bool_'对象没有属性'parameters'
numpy.bool_' object has no attribute 'parameters'
我遇到了一个奇怪的错误,报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]))
问题出在行(obj_constraints += [-agg[0]<=SoC, SoC<= agg[0]])。但是,如果我将 agg[0] 替换为 10,-agg[0] 将替换为 -10。然后它通过并给了我一个结果。所以我检查了 agg[0],
的类型和值
IPdb [2]: agg[0]
10.0
IPdb [4]: type(agg[0])
<class 'numpy.float64'>
所以这是怎么回事?
谢谢!
完整堆栈:
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'
更新:现在可以将代码复制并粘贴 运行 到您的机器上。它是独立的。
在 offline_opt
内循环的第一次迭代中,time
被设置为零。内部循环 (for i in range(time)
) 不会发生(因为 range(0)
),所以 SoC
仍然为零。
第一次迭代后的约束是:
[-agg[0]<=0, 0<= agg[0]]
,它们被评估为布尔值(因为没有涉及变量的表达式)。如果在第一次迭代后打印 obj_constraints
的值,您将看到它是 [True, True]
。 CVXPY 不知道如何处理布尔值,因为它期望约束包含表达式。
“快速但愚蠢”的修复方法是在将 SoC
添加为约束之前检查它是否属于 cvxpy.Expression
类型:
if isinstance(SoC, cp.Expression):
obj_constraints += [-agg[0]<=SoC, SoC<= agg[0]]
P.S - 而不是 [-agg[0]<=SoC, SoC<= agg[0]]
你可以简单地写 [SoC <= cp.abs(agg[0])]
我遇到了一个奇怪的错误,报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]))
问题出在行(obj_constraints += [-agg[0]<=SoC, SoC<= agg[0]])。但是,如果我将 agg[0] 替换为 10,-agg[0] 将替换为 -10。然后它通过并给了我一个结果。所以我检查了 agg[0],
的类型和值IPdb [2]: agg[0]
10.0
IPdb [4]: type(agg[0])
<class 'numpy.float64'>
所以这是怎么回事?
谢谢!
完整堆栈:
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'
更新:现在可以将代码复制并粘贴 运行 到您的机器上。它是独立的。
在 offline_opt
内循环的第一次迭代中,time
被设置为零。内部循环 (for i in range(time)
) 不会发生(因为 range(0)
),所以 SoC
仍然为零。
第一次迭代后的约束是:
[-agg[0]<=0, 0<= agg[0]]
,它们被评估为布尔值(因为没有涉及变量的表达式)。如果在第一次迭代后打印 obj_constraints
的值,您将看到它是 [True, True]
。 CVXPY 不知道如何处理布尔值,因为它期望约束包含表达式。
“快速但愚蠢”的修复方法是在将 SoC
添加为约束之前检查它是否属于 cvxpy.Expression
类型:
if isinstance(SoC, cp.Expression):
obj_constraints += [-agg[0]<=SoC, SoC<= agg[0]]
P.S - 而不是 [-agg[0]<=SoC, SoC<= agg[0]]
你可以简单地写 [SoC <= cp.abs(agg[0])]