0

首先,我以适当的形状定义了一些矩阵和向量。

初始化

I=np.eye(24)
Z=np.zeros((24,24))
a=0.012
b=1.1
gamma1=0.9/80
gamma2=1.1/80
MM=np.eye(24)
for i in range (22):
    MM[i+1,i]=-1
    MM[0,23]=-1
M=random.randint(200,300, size=(24,1))
max_pch=5
max_pdch=5
ppp=random.randint(150,200, size=(24,))

定义目标函数矩阵

Q 和 C 分别是目标函数 1/2 x^TQ x +C^T x 的矩阵和向量。

Q=np.asarray(np.bmat([[a*I,Z,Z,Z],[Z,a*I,Z,Z],[Z,Z,Z,Z],[Z,Z,Z,Z]  ]))
C=np.asarray(np.bmat([[b*np.ones(24),b*np.ones(24),0*np.ones(24),ppp]]))

##创建平等主题

在我的问题中,我只有相等的约束以及定义自爆的上限和下限。

Aeq=np.asarray(np.bmat([[-I,I,Z,I], [-gamma1*I, gamma2*I,MM,Z],[np.zeros((48,96))]]))
beq=np.asarray(np.bmat([[M],[np.zeros((72,1))]]))

##创建形状的上限和下限(1,96)

lb=np.asarray(np.bmat([[0*np.ones(24),0*np.ones(24),[0.1],0.1*np.ones(22), 
    [0.1],100*np.ones(24)]]))

ub=np.asarray(np.bmat([[max_pch*np.ones(24),max_pdch*np.ones(24),[0.1],0.9*np.ones(22), 
    [0.9],500*np.ones(24)]]))

x = solve_qp(P=matrix(Q), q=C.T,
         G=None,h=None, A=matrix(Aeq), b=beq, lb=lb.T, ub=ub.T,solver='quadprog')

##错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-48-111d9695d5a8> in <module>
     25 
     26 x = solve_qp(P=matrix(Q), q=C.T,
---> 27              G=None,h=None, A=matrix(Aeq), b=beq, lb=lb.T, ub=ub.T,solver='quadprog')
     28 
     29 

~\Anaconda3\lib\site-packages\qpsolvers\__init__.py in solve_qp(P, q, G, h, A, b, lb, ub, 
solver, initvals, sym_proj, verbose, **kwargs)
    271     kwargs["verbose"] = verbose
    272     try:
--> 273         return __solve_function__[solver](*args, **kwargs)
    274     except KeyError:
    275         raise SolverNotFound(f"solver '{solver}' is not available")

~\Anaconda3\lib\site-packages\qpsolvers\quadprog_.py in quadprog_solve_qp(P, q, G, h, A, b, 
initvals, verbose)
     85         else:
     86             qp_C = -vstack([A, G]).T
---> 87             qp_b = -hstack([b, h])
     88         meq = A.shape[0]
     89     else:  # no equality constraint

~\Anaconda3\lib\site-packages\numpy\core\shape_base.py in hstack(tup)
    338         return _nx.concatenate(arrs, 0)
    339     else:
--> 340         return _nx.concatenate(arrs, 1)
    341 
    342 

ValueError: all the input array dimensions except for the concatenation axis must match 
exactly

如果有人可以帮助我,我很高兴。

4

0 回答 0