奥伊米亚慷 2020-11-04 11:38 采纳率: 0%
浏览 1091

【Python】小白求助:关于使用CVXOPT解二次规划报错的问题

谢谢你的帮助~

程序抛出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
  • 写回答

2条回答 默认 最新

  • ncisiad 2022-11-29 18:50
    关注

    sol = cvxopt.solvers.qp(Q, c, G, h, kktsolver='ldl', options={'kktreg':1e-9}) 加两个参数

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?