在cvxpy中的约束

时间:2020-05-11 23:15:31

标签: python cvxpy

我正在编写用于解决最佳功率流的代码。代码是:

import random 
import numpy as np
import cvxpy as cv
import math
n=5
m=4
U= cv.Variable (n)
Sg= cv.Variable (shape=(n,1), complex=True)
Se= cv.Variable (shape=(m,1), complex=True)
Sr= cv.Variable (shape=(m,1), complex=True)
W= cv.Variable (shape=(m,1), complex=True)
Pl=cv.Variable (1)
Qd= np.array([0, 1, 0, 4, 0])
Pd= np.array([0, 3, 0, 8.5, 0])
Sd= np.array ([(Pd**2+ Qd**2)**(1/2)]).reshape((5,1))
Smax= np.array([10, 0, 5, 0, 2])
Apos= np.array([[1, 0, 0, 0],[0, 1, 0, 0],[0, 0, 1, 0],[0, 0, 0, 1],[0, 0, 0, 0]])
Aneg= np.array([[0, 0, 0, 0],[-1, 0, 0, 0],[0, -1, 0, 0],[0, 0, -1, 0],[0, 0, 0, -1]])
Ybus=np.array([[166.605-96.2606j, -166.605+96.2606j, 0, 0, 0],[-166.605+96.2606j, 305.267-246.08j, -138.662+146.819j, 0, 0],[0, -138.662+146.819j, 356.948-394.397j, -215.285+247.578j, 0],[0, 0, -215.285+247.578j, 773.225-590.926j, -557.94+343.348j],[0, 0, 0, -557.94+343.348j, 557.94-343.348j]])
objective= cv.Minimize (Pl)
res=[]
res+=[Pl>= cv.sum(cv.real(Se+Sr))]
res+= [U[1]==1]
for j in range (m):
  res+= [Se[j]==Ybus[j][j+1].conjugate()*U[j]-W[j]*Ybus[j][j+1].conjugate()]
  res+= [Sr[j]==Ybus[j][j+1].conjugate()*U[j+1]-W[j]*Ybus[j][j+1].conjugate()]
res+= [Sg-Sd== Apos@Se + Aneg@Sr]
for i in range (m):
  res+= [cv.norm(Sg[i])<=Smax[i]]
  res+= [cv.norm([[2*W[i]],[U[i]-U[i+1]]])<=U[i]+U[i+1]]
res+=[cv.real(Se+Sr)>=0]
sos = cv.Problem(objective,res)
optval=sos.solve (verbose=True)
print (U.value)

最后的错误是: TypeError跟踪(最近一次通话) TypeError:float()参数必须是字符串或数字,而不是“乘”

上述异常是以下异常的直接原因:

然后 ValueError:设置具有序列的数组元素。

0 个答案:

没有答案