python 版本:3.7.5
一、Windows 平台安装:
1、下载cvxopt-1.2.5-cp37-cp37m-win_amd64.whl 文件
https://www.lfd.uci.edu/~gohlke/pythonlibs/#cvxopt
2、下载 numpy-1.19.1+mkl-cp37-cp37m-win_amd64.whl
https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
进入cmd 命令,进入下载路径,执行如下命令:
pip uninstall numpy
pip install numpy-1.19.1+mkl-cp37-cp37m-win_amd64.whl
pip install cvxopt-1.2.5-cp37-cp37m-win_amd64.whl
import time
import numpy as np
np.set_printoptions(suppress=True)
import pprint
from cvxopt import matrix, solvers
P = matrix([[4.0 ,1.0] ,[1.0 ,2.0]])
q = 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])
time1=time.time()
result = solvers.qp(P ,q ,G ,h ,A ,b)
time2=time.time()
print('x\n' ,result['x'])
print('总共耗时:' + str(time2 - time1) + 's')
运行结果:
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 2e-16 6e-02
2: 1.8750e+00 1.8739e+00 1e-03 2e-16 5e-04
3: 1.8750e+00 1.8750e+00 1e-05 1e-16 5e-06
4: 1.8750e+00 1.8750e+00 1e-07 1e-16 5e-08
Optimal solution found.
x
[ 2.50e-01]
[ 7.50e-01]
总共耗时:0.0039904117584228516s
二、Linux 平台安装:
Linux 平台安装直接用 conda install cvxopt 即可以。
conda activate model
conda install cvxopt