Python之CVXOPT模块
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程序代码:
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程序代码
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
二次型规划问题

其中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程序代码:
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
Python之CVXOPT模块的更多相关文章
- python的库有多少个?python有多少个模块?
这里列举了大概500个左右的库: ! Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主 ...
- python之platform模块
python之platform模块 ^_^第三个模块从天而降喽!! 函数列表 platform.system() 获取操作系统类型,windows.linux等 platform.platform() ...
- python之OS模块详解
python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
- 学习PYTHON之路, DAY 6 - PYTHON 基础 6 (模块)
一 安装,导入模块 安装: pip3 install 模块名称 导入: import module from module.xx.xx import xx from module.xx.xx impo ...
- linux下python调用c模块
在C调用Python模块时需要初始化Python解释器,导入模块等,但Python调用C模块却比较简单,下面还是以helloWorld.c 和 main.py 做一说明: (1)编写C代码,hel ...
- Python学习之模块进程函数详解
今天在看<Beginning Linux Programming>中的进程相关部分,讲到Linux几个进程相关的系统函数: system , exec , fork ,wait . Pyt ...
- python基础——第三方模块
python基础——第三方模块 在Python中,安装第三方模块,是通过包管理工具pip完成的. 如果你正在使用Mac或Linux,安装pip本身这个步骤就可以跳过了. 如果你正在使用Window ...
- python基础——使用模块
python基础——使用模块 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env ...
随机推荐
- [国嵌笔记][030][U-Boot工作流程分析]
uboot工作流程分析 程序入口 1.打开顶层目录的Makefile,找到目标smdk2440_config的命令中的第三项(smdk2440) 2.进入目录board/samsung/smdk244 ...
- Ajax及异步操作
之前我们使用的是jQuery的Ajax,这是一种极为便捷的Ajax操作方式,但是我们还需要对Ajax技术进行进一步的了解. <input type="text" id=&qu ...
- POJ 3461 Oulipo(——KMP算法)
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
- How to bypass Win10 logon password?
Usually we will use LiveView or VFC to "boot up" the evidence files acquired from suspect' ...
- phpstudy最新版中php5.6版报错
- 什么是WEBserver? 经常使用的WEBserver有哪些?
地址:http://www.mamicode.com/ 什么是WEBserver? 经常使用的WEBserver有哪些? 一.什么是WEBserver Webserver能够解析HTTP协议.当Web ...
- 关于在vue-cli中使用微信自动登录和分享
(以下所有接口由后台提供) 一.微信自动登录 //定义事件 methods:{ //判断是否微信登陆 是不是微信浏览器 isWeiXin() { let ua = window.navigator.u ...
- JS_全
<script src="jquery-1.9.1.js" type="text/javascript"></script> <s ...
- dedecms环境优化
路径:dedecms/dede/templates/index_body.htm <script type="text/javascript">function sho ...
- Flexible Box布局基础知识详解
1.基本概念,借用阮一峰老师的一张图: 容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis).主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫 ...