欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Python CVXOPT模塊安裝及使用解析

系統 2090 0

Python中支持Convex Optimization(凸規劃)的模塊為CVXOPT,其安裝方式為:

卸載原Pyhon中的Numpy

安裝CVXOPT的whl文件,鏈接為:https://www.lfd.uci.edu/~gohlke/pythonlibs/

安裝Numpy+mkl的whl文件,鏈接為:https://www.lfd.uci.edu/~gohlke/pythonlibs/

之所以選擇這種安裝方式,是因為Python的whl和pip直接install的不兼容性。

CVXOPT的官方說明文檔網址為:http://cvxopt.org/index.html, 現最新版本為1.1.9,由Martin Andersen, Joachim Dahl 和Lieven Vandenberghe共同開發完成,能夠解決線性規劃和二次型規劃問題,其應用場景如SVM中的Hard Margin SVM.

CVXOPT使用舉例如下:

線性規劃問題

例1:

Python CVXOPT模塊安裝及使用解析_第1張圖片

Python程序代碼:

            
import numpy as np
from cvxopt import matrix, 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['primal objective'])
          

輸出結果:

            
   pcost    dcost    gap  pres  dres  k/t
 0: 2.6471e+00 -7.0588e-01 2e+01 8e-01 2e+00 1e+00
 1: 3.0726e+00 2.8437e+00 1e+00 1e-01 2e-01 3e-01
 2: 2.4891e+00 2.4808e+00 1e-01 1e-02 2e-02 5e-02
 3: 2.4999e+00 2.4998e+00 1e-03 1e-04 2e-04 5e-04
 4: 2.5000e+00 2.5000e+00 1e-05 1e-06 2e-06 5e-06
 5: 2.5000e+00 2.5000e+00 1e-07 1e-08 2e-08 5e-08
Optimal solution found.
{'primal objective': 2.4999999895543072, 's': <4x1 matrix, tc='d'>, 'dual infeasibility': 2.257878974569382e-08, 'primal slack': 2.0388399547464153e-08, 'dual objective': 2.4999999817312535, 'residual as dual infeasibility certificate': None, 'dual slack': 3.529915972607509e-09, 'x': <2x1 matrix, tc='d'>, 'iterations': 5, 'gap': 1.3974945737723005e-07, 'residual as primal infeasibility certificate': None, 'z': <4x1 matrix, tc='d'>, 'y': <0x1 matrix, tc='d'>, 'status': 'optimal', 'primal infeasibility': 1.1368786228004961e-08, 'relative gap': 5.5899783359379607e-08}
[ 5.00e-01]
[ 1.50e+00]

[[ 2.49999999]]
          

例2

Python CVXOPT模塊安裝及使用解析_第2張圖片

Python程序代碼

            
import numpy as np
from cvxopt import matrix, 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]))
          

輸出結果:

            
   pcost    dcost    gap  pres  dres  k/t
 0: 4.0000e+00 -0.0000e+00 4e+00 0e+00 0e+00 1e+00
 1: 2.7942e+00 1.9800e+00 8e-01 9e-17 7e-16 2e-01
 2: 2.0095e+00 1.9875e+00 2e-02 4e-16 2e-16 7e-03
 3: 2.0001e+00 1.9999e+00 2e-04 2e-16 6e-16 7e-05
 4: 2.0000e+00 2.0000e+00 2e-06 6e-17 5e-16 7e-07
 5: 2.0000e+00 2.0000e+00 2e-08 3e-16 7e-16 7e-09
Optimal solution found.
   pcost    dcost    gap  pres  dres  k/t
 0: -4.0000e+00 -8.0000e+00 4e+00 0e+00 1e-16 1e+00
 1: -5.2058e+00 -6.0200e+00 8e-01 1e-16 7e-16 2e-01
 2: -5.9905e+00 -6.0125e+00 2e-02 1e-16 0e+00 7e-03
 3: -5.9999e+00 -6.0001e+00 2e-04 1e-16 2e-16 7e-05
 4: -6.0000e+00 -6.0000e+00 2e-06 1e-16 2e-16 7e-07
Optimal solution found.
min=2.00000000952,max=5.99999904803
          

二次型規劃問題

Python CVXOPT模塊安裝及使用解析_第3張圖片

其中P,q,G,h,A,b為輸入矩陣,該問題求解采用QP算法。
例1:

Python程序代碼:

            
from cvxopt import matrix, 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['primal objective'])
          

輸出結果:

            
   pcost    dcost    gap  pres  dres
 0: 1.8889e+00 7.7778e-01 1e+00 2e-16 2e+00
 1: 1.8769e+00 1.8320e+00 4e-02 0e+00 6e-02
 2: 1.8750e+00 1.8739e+00 1e-03 1e-16 5e-04
 3: 1.8750e+00 1.8750e+00 1e-05 6e-17 5e-06
 4: 1.8750e+00 1.8750e+00 1e-07 2e-16 5e-08
Optimal solution found.
[ 2.50e-01]
[ 7.50e-01]
          

例2:

Python CVXOPT模塊安裝及使用解析_第4張圖片

Python程序代碼:

            
from cvxopt import matrix, 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['primal objective'])
          

輸出結果

            
   pcost    dcost    gap  pres  dres
 0: 1.0780e+02 -7.6366e+02 9e+02 0e+00 4e+01
 1: 9.3245e+01 9.7637e+00 8e+01 6e-17 3e+00
 2: 6.7311e+01 3.2553e+01 3e+01 6e-17 1e+00
 3: 2.6071e+01 1.5068e+01 1e+01 2e-17 7e-01
 4: 3.7092e+01 2.3152e+01 1e+01 5e-18 4e-01
 5: 2.5352e+01 1.8652e+01 7e+00 7e-17 3e-16
 6: 2.0062e+01 1.9974e+01 9e-02 2e-16 3e-16
 7: 2.0001e+01 2.0000e+01 9e-04 8e-17 5e-16
 8: 2.0000e+01 2.0000e+01 9e-06 1e-16 2e-16
Optimal solution found.
[ 7.13e-07]
[ 5.00e+00]

20.00000617311241
          

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 奇米影视 首页 | 无码日本精品久久久久久 | 伊人伊人网 | 国产91精品黄网在线观看 | 国产成人精品一区二区仙踪林 | 日本a v在线播放 | 国产福利在线观看永久免费 | 日韩午夜影院 | 99久久亚洲精品日本无码 | 国产激爽大片高清在线观看 | 成人午夜视频在线观看 | 日本黄在线观看免费播放 | 久久香蕉国产线看观看网站 | 搡女人视频免费 | 好骚综合在线 | 色网在线| 久久精品99| 人人香蕉| ppp42.com| 欧美大片欧美大片 | 日本高清一区二区三区不卡免费 | 91精选国产91在线观看 | 六月激情婷婷 | 欧美日剧在线免费 | 亚洲一区二区av | 亚洲精品1区 | 久久精品免费一区二区三区 | 日本看片一区二区三区高清 | 亚洲精品福利你懂 | 99久久精品国产一区二区三区 | 一区二区三区国产好 | 欧美视频在线第一页 | 午夜视频在线免费观看 | 国产一区二区黑人欧美xxxx | 亚洲 日本 欧美 中文幕 | 婷婷色九月综合激情丁香 | 黄色婷婷| a级成人毛片久久 | 国产亚洲综合一区在线 | 搜一级毛片 | 国内精品伊人久久久久7777人 |