CVXOPT模块怎么在Python中安装与使用
CVXOPT模块怎么在Python中安装与使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
CVXOPT的官方说明文档网址为:http://cvxopt.org/index.html, 现最新版本为1.1.9,由Martin Andersen, Joachim Dahl 和Lieven Vandenberghe共同开发完成,能够解决线性规划和二次型规划问题,其应用场景如SVM中的Hard Margin SVM.
CVXOPT使用举例如下:
线性规划问题
例1:
Python程序代码:
importnumpyasnp fromcvxoptimportmatrix,solvers A=matrix([[-1.0,-1.0,0.0,1.0],[1.0,-1.0,-1.0,-2.0]]) b=matrix([1.0,-2.0,0.0,4.0]) c=matrix([2.0,1.0]) sol=solvers.lp(c,A,b) print(sol['x']) print(np.dot(sol['x'].T,c)) print(sol['primalobjective'])
输出结果:
pcostdcostgappresdresk/t 0:2.6471e+00-7.0588e-012e+018e-012e+001e+00 1:3.0726e+002.8437e+001e+001e-012e-013e-01 2:2.4891e+002.4808e+001e-011e-022e-025e-02 3:2.4999e+002.4998e+001e-031e-042e-045e-04 4:2.5000e+002.5000e+001e-051e-062e-065e-06 5:2.5000e+002.5000e+001e-071e-082e-085e-08 Optimalsolutionfound. {'primalobjective':2.4999999895543072,'s':<4x1matrix,tc='d'>,'dualinfeasibility':2.257878974569382e-08,'primalslack':2.0388399547464153e-08,'dualobjective':2.4999999817312535,'residualasdualinfeasibilitycertificate':None,'dualslack':3.529915972607509e-09,'x':<2x1matrix,tc='d'>,'iterations':5,'gap':1.3974945737723005e-07,'residualasprimalinfeasibilitycertificate':None,'z':<4x1matrix,tc='d'>,'y':<0x1matrix,tc='d'>,'status':'optimal','primalinfeasibility':1.1368786228004961e-08,'relativegap':5.5899783359379607e-08} [5.00e-01] [1.50e+00] [[2.49999999]]
例2
Python程序代码
importnumpyasnp fromcvxoptimportmatrix,solvers A=matrix([[1.0,0.0,-1.0],[0.0,1.0,-1.0]]) b=matrix([2.0,2.0,-2.0]) c=matrix([1.0,2.0]) d=matrix([-1.0,-2.0]) sol1=solvers.lp(c,A,b) min=np.dot(sol1['x'].T,c) sol2=solvers.lp(d,A,b) max=-np.dot(sol2['x'].T,d) print('min=%s,max=%s'%(min[0][0],max[0][0]))
输出结果:
pcostdcostgappresdresk/t 0:4.0000e+00-0.0000e+004e+000e+000e+001e+00 1:2.7942e+001.9800e+008e-019e-177e-162e-01 2:2.0095e+001.9875e+002e-024e-162e-167e-03 3:2.0001e+001.9999e+002e-042e-166e-167e-05 4:2.0000e+002.0000e+002e-066e-175e-167e-07 5:2.0000e+002.0000e+002e-083e-167e-167e-09 Optimalsolutionfound. pcostdcostgappresdresk/t 0:-4.0000e+00-8.0000e+004e+000e+001e-161e+00 1:-5.2058e+00-6.0200e+008e-011e-167e-162e-01 2:-5.9905e+00-6.0125e+002e-021e-160e+007e-03 3:-5.9999e+00-6.0001e+002e-041e-162e-167e-05 4:-6.0000e+00-6.0000e+002e-061e-162e-167e-07 Optimalsolutionfound. min=2.00000000952,max=5.99999904803
二次型规划问题
其中P,q,G,h,A,b为输入矩阵,该问题求解采用QP算法。例1:
Python程序代码:
fromcvxoptimportmatrix,solvers Q=2*matrix([[2,.5],[.5,1]]) p=matrix([1.0,1.0]) G=matrix([[-1.0,0.0],[0.0,-1.0]]) h=matrix([0.0,0.0]) A=matrix([1.0,1.0],(1,2)) b=matrix(1.0) sol=solvers.qp(Q,p,G,h,A,b) print(sol['x']) print(sol['primalobjective'])
输出结果:
pcostdcostgappresdres 0:1.8889e+007.7778e-011e+002e-162e+00 1:1.8769e+001.8320e+004e-020e+006e-02 2:1.8750e+001.8739e+001e-031e-165e-04 3:1.8750e+001.8750e+001e-056e-175e-06 4:1.8750e+001.8750e+001e-072e-165e-08 Optimalsolutionfound. [2.50e-01] [7.50e-01]
例2:
Python程序代码:
fromcvxoptimportmatrix,solvers P=matrix([[1.0,0.0],[0.0,0.0]]) q=matrix([3.0,4.0]) G=matrix([[-1.0,0.0,-1.0,2.0,3.0],[0.0,-1.0,-3.0,5.0,4.0]]) h=matrix([0.0,0.0,-15.0,100.0,80.0]) sol=solvers.qp(P,q,G,h) print(sol['x']) print(sol['primalobjective'])
输出结果
pcostdcostgappresdres 0:1.0780e+02-7.6366e+029e+020e+004e+01 1:9.3245e+019.7637e+008e+016e-173e+00 2:6.7311e+013.2553e+013e+016e-171e+00 3:2.6071e+011.5068e+011e+012e-177e-01 4:3.7092e+012.3152e+011e+015e-184e-01 5:2.5352e+011.8652e+017e+007e-173e-16 6:2.0062e+011.9974e+019e-022e-163e-16 7:2.0001e+012.0000e+019e-048e-175e-16 8:2.0000e+012.0000e+019e-061e-162e-16 Optimalsolutionfound. [7.13e-07] [5.00e+00] 20.00000617311241
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注恰卡编程网行业资讯频道,感谢您对恰卡编程网的支持。
推荐阅读
-
Python中怎么动态声明变量赋值
这篇文章将为大家详细讲解有关Python中怎么动态声明变量赋值,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文...
-
python中变量的存储原理是什么
-
Python中怎么引用传递变量赋值
这篇文章将为大家详细讲解有关Python中怎么引用传递变量赋值,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文...
-
python中怎么获取程序执行文件路径
python中怎么获取程序执行文件路径,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的...
-
Python中如何获取文件系统的使用率
Python中如何获取文件系统的使用率,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴...
-
Python中怎么获取文件的创建和修改时间
这篇文章将为大家详细讲解有关Python中怎么获取文件的创建和修改时间,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读...
-
python中怎么获取依赖包
今天就跟大家聊聊有关python中怎么获取依赖包,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据...
-
python怎么实现批量文件加密功能
-
python中怎么实现threading线程同步
小编给大家分享一下python中怎么实现threading线程同步,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!...
-
python下thread模块创建线程的方法
本篇内容介绍了“python下thread模块创建线程的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来...