谢谢你的帮助~
程序抛出ArithmeticError和ValueError的错误,最后一行显示错误信息:
ValueError: Rank(A) < p or Rank([P; A; G]) < n
原题目为:
Python的代码为:
from cvxopt import matrix,solvers
import numpy
n=5
P=matrix(numpy.diag((-2.,-2,-6,-8,-4)))
q=matrix([8.,2,3,1,2])
G1=matrix([[1.,1,1,1,1],[1.,2,2,1,6],[2.,1,6,0,0],[0.,0,1,1,5]])
G2=matrix(numpy.diag([1.]*5))
G3=matrix(numpy.dot(G2,-1.))
G=matrix(numpy.c_[G1,G2,G3].T)
h=matrix([400.,800,200,200,99,99,99,99,99,0,0,0,0,0])
print('P:','\n',P,'q:','\n',q,'G:','\n',G,'h:','\n',h)
res=solvers.qp(P,q,G,h)
print(res['x'],res['primal objective'])
其中P,q,G,h分别是:
P:
[-2.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00]
[ 0.00e+00 -2.00e+00 0.00e+00 0.00e+00 0.00e+00]
[ 0.00e+00 0.00e+00 -6.00e+00 0.00e+00 0.00e+00]
[ 0.00e+00 0.00e+00 0.00e+00 -8.00e+00 0.00e+00]
[ 0.00e+00 0.00e+00 0.00e+00 0.00e+00 -4.00e+00]
q:
[ 8.00e+00]
[ 2.00e+00]
[ 3.00e+00]
[ 1.00e+00]
[ 2.00e+00]
G:
[ 1.00e+00 1.00e+00 1.00e+00 1.00e+00 1.00e+00]
[ 1.00e+00 2.00e+00 2.00e+00 1.00e+00 6.00e+00]
[ 2.00e+00 1.00e+00 6.00e+00 0.00e+00 0.00e+00]
[ 0.00e+00 0.00e+00 1.00e+00 1.00e+00 5.00e+00]
[ 1.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00]
[ 0.00e+00 1.00e+00 0.00e+00 0.00e+00 0.00e+00]
[ 0.00e+00 0.00e+00 1.00e+00 0.00e+00 0.00e+00]
[ 0.00e+00 0.00e+00 0.00e+00 1.00e+00 0.00e+00]
[ 0.00e+00 0.00e+00 0.00e+00 0.00e+00 1.00e+00]
[-1.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00]
[ 0.00e+00 -1.00e+00 0.00e+00 0.00e+00 0.00e+00]
[ 0.00e+00 0.00e+00 -1.00e+00 0.00e+00 0.00e+00]
[ 0.00e+00 0.00e+00 0.00e+00 -1.00e+00 0.00e+00]
[ 0.00e+00 0.00e+00 0.00e+00 0.00e+00 -1.00e+00]
h:
[ 4.00e+02]
[ 8.00e+02]
[ 2.00e+02]
[ 2.00e+02]
[ 9.90e+01]
[ 9.90e+01]
[ 9.90e+01]
[ 9.90e+01]
[ 9.90e+01]
[ 0.00e+00]
[ 0.00e+00]
[ 0.00e+00]
[ 0.00e+00]
[ 0.00e+00]
错误信息为:
---------------------------------------------------------------------------
ArithmeticError Traceback (most recent call last)
D:\Anaconda3\lib\site-packages\cvxopt\misc.py in factor(W, H, Df)
1428 if type(F['S']) is matrix:
-> 1429 lapack.potrf(F['S'])
1430 else:
ArithmeticError: 3
During handling of the above exception, another exception occurred:
ArithmeticError Traceback (most recent call last)
D:\Anaconda3\lib\site-packages\cvxopt\coneprog.py in coneqp(P, q, G, h, dims, A, b, initvals, kktsolver, xnewcopy, xdot, xaxpy, xscal, ynewcopy, ydot, yaxpy, yscal, **kwargs)
2064 for rti in W['rti']: rti[::rti.size[0]+1 ] = 1.0
-> 2065 try: f = kktsolver(W)
2066 except ArithmeticError:
D:\Anaconda3\lib\site-packages\cvxopt\coneprog.py in kktsolver(W)
1980 def kktsolver(W):
-> 1981 return factor(W, P)
1982
D:\Anaconda3\lib\site-packages\cvxopt\misc.py in factor(W, H, Df)
1443 if type(F['S']) is matrix:
-> 1444 lapack.potrf(F['S'])
1445 else:
ArithmeticError: 3
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-43-f8dc8d1c2d9d> in <module>()
11 h=matrix([400.,800,200,200,99,99,99,99,99,0,0,0,0,0])
12 print('P:','\n',P,'q:','\n',q,'G:','\n',G,'h:','\n',h)
---> 13 res=solvers.qp(P,q,G,h)
14 print(res['x'],res['primal objective'])
D:\Anaconda3\lib\site-packages\cvxopt\coneprog.py in qp(P, q, G, h, A, b, solver, kktsolver, initvals, **kwargs)
4483 'residual as dual infeasibility certificate': dinfres}
4484
-> 4485 return coneqp(P, q, G, h, None, A, b, initvals, kktsolver = kktsolver, options = options)
D:\Anaconda3\lib\site-packages\cvxopt\coneprog.py in coneqp(P, q, G, h, dims, A, b, initvals, kktsolver, xnewcopy, xdot, xaxpy, xscal, ynewcopy, ydot, yaxpy, yscal, **kwargs)
2065 try: f = kktsolver(W)
2066 except ArithmeticError:
-> 2067 raise ValueError("Rank(A) < p or Rank([P; A; G]) < n")
2068
2069
ValueError: Rank(A) < p or Rank([P; A; G]) < n