Please tell me how can I solve my problem with the below variables.
Which solver is better?
Objective function = x^T Q x + C^T x
Q
is diagonal matrix withQ(i,i)>=0
x.shape=(n,1)
Equal constraint : Aeq.x =beq
Aeq.shape=(n,n)
andbeq.shape=(n,1)
Lower bound and upper bound : lb <= x <= ub
import random
import numpy as np
lb.shape=(n,1) and ub.shape=(n,1)
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=30.0
max_pdch=30.0
ppp=random.randint(150,200, size=(24,))
Q=np.asarray(np.bmat([[a*I,Z,Z,Z],[Z,a*I,Z,Z],[Z,Z,0.00001*I,Z],[Z,Z,Z,0.00001*I] ]))
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],[Z,Z,Z,Z],[Z,Z,Z,Z]]))
beq=np.asarray(np.bmat([[M],[np.zeros((72,1))]]))
lb=np.asarray(np.bmat([[0*np.ones(24),0*np.ones(24),[0.1],0.1*np.ones(22),[0.9],0.0*np.ones(24)]]))
ub=np.asarray(np.bmat([[max_pch*np.ones(24),max_pdch*np.ones(24),[0.9],0.9*np.ones(22),[0.9],500*np.ones(24)]]))
x = solve_qp(P=Q, q=C.T.reshape((96,)),
G=None , h=None,
A=Aeq , b=beq.reshape(96,),
lb=lb.T.reshape((96,)) , ub=ub.T.reshape((96,)))
print("QP solution: x = {}".format(x))
What is the problem?
- QP solution:
x = None
The same code in Matlab (with fmincon
) gives the correct result. However, in Python, I can't get that result.