Python之CVXOPT模块

??Python中支持Convex Optimization(凸规划)的模块为CVXOPT,其安装方式为:

  1. 卸载原Pyhon中的Numpy
  2. 安装CVXOPT的whl文件,链接为:https://www.lfd.uci.edu/~gohlke/pythonlibs/
  3. 安装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

原文地址:https://www.cnblogs.com/jclian91/p/8446646.html

时间: 2024-08-30 16:21:54

Python之CVXOPT模块的相关文章

Python中subprocess 模块 创建并运行一个进程

python的subprocess模块,看到官方声明里说要尽力避免使用shell=True这个参数,于是测试了一下: from subprocess import call import shlex cmd = "cat test.txt; rm test.txt" call(cmd, shell=True) 运行之后: 1:打开并浏览了test.txt文件 2:删除了test.txt文件 from subprocess import call import shlex cmd = &

python时间处理模块 datetime time模块 deltetime模块

1 首先介绍time模块,因为简单 python 自带模块 本人使用time模块,只使用两个函数 time函数和sleep函数 import time a.     time.time()   函数 返回unix时间  常用作两个时间差的计算 b.     time.sleep()  休眠多久,精度为子秒(subsecond) In [90]: t1 = time.time() In [91]: t1 Out[91]: 1461400225.877932 In [92]: time.sleep(

python安装mysqldb模块

今天在阿里云一台新的服务器部署程序后台,发现上面的python缺少MySQLDB 模块,记录安装过程. 首先django程序,运行 python manage.py sycdb 报错: ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb MySQLdb模块的包名字叫mysql-python,于是pip安装之,(关于pip,可以参考这篇文章) 运行: pip install mysql-python

Python 利用pytesser模块识别图像文字

使用的是python的pytesser模块,原先想做的是图片中文识别,搞了一段时间了,在中文的识别上还是有很多问题,这里做记录分享. pytesser,OCR in Python using the Tesseract engine from Google.是谷歌OCR开源项目的一个模块,可将图片中的文字转换成文本(主要是英文). 1.pytesser安装 使用设备:win8 64位 PyTesser使用Tesseract OCR引擎,将图像转换到可接受的格式,然后执行tesseract提取出文

python学习--创建模块

昨天做了python客户端和服务器端通信,并把接收到的信息写到数据库,因为对数据库进行操作是个经常调用的行为,所以我想把调用数据库的操作写成一个module来给其它python程序调用,所以将昨天的服务器端程序拆分为两个文件: 1.主程序python.py #!/usr/bin/env python import socket import json import connmysql s = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) h

Python中time模块详解

在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主要调用C库,所以各个平台可能有所不同. UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间.在中国为UTC+8.DST

[ python编程 ] subprocess模块学习总结

转载:http://www.jb51.net/article/48086.htm 从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn*.os.popen*.popen2.*.commands.*不但可以调用外部的命令作为子进程,而且可以连接到子进程的input/output/error管道,获取相关的返回信息. 一.subprocess以及常用的封装函数    运行python的时候,我们都是在创建并

定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容。提示(可以了解python的urllib模块)

1 定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容.提示(可以了解python的urllib模块) 2 import urllib.request 3 4 def get_page(url): 5 response = urllib.request.urlopen(url) 6 html = response.read() 7 return html 8 9 print(get_page(url='https://www.baidu,com'))

python之OS模块(对文件or目录操作)

OS模块 os,语义为操作系统,包含普遍的操作系统功能,与具体的平台无关.python编程时,处理文件和目录这些操作,就比如说:显示当前目录下所有文件/删除某个文件/获取文件大小-- os模块不受平台限制,也就是说:当我们要在linux中显示当前命令时就要用到pwd命令,而Windows中cmd命令行下就要用到这个,例如:这时候我们使用python中os模块的os.path.abspath(name)功能,甭管是linux或者Windows都可以获取当前的绝对路径. 常见函数列表 os.name