侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python CVXOPT模块安装及使用解析

Python  /  管理员 发布于 7年前   404

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 npfrom cvxopt import matrix, solversA = 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-08Optimal 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 npfrom cvxopt import matrix, solversA = 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-09Optimal 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-07Optimal solution found.min=2.00000000952,max=5.99999904803

二次型规划问题

其中P,q,G,h,A,b为输入矩阵,该问题求解采用QP算法。
例1:

Python程序代码:

from cvxopt import matrix, solversQ = 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-08Optimal solution found.[ 2.50e-01][ 7.50e-01]

例2:

Python程序代码:

from cvxopt import matrix, solversP = 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-16Optimal solution found.[ 7.13e-07][ 5.00e+00]20.00000617311241

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    python代码 FTP备份交换机配置脚本实例解析
    下一条:
    Python Selenium 之数据驱动测试的实现
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客