Python's cvxopt module

Source: Internet
Author: User
Tags svm

?? The modules in Python that support convex optimization (convex planning) are cvxopt and are installed in the following ways:

    1. Unloading the NumPy in the original Pyhon
    2. Install cvxopt WHL file, link to: https://www.lfd.uci.edu/~gohlke/pythonlibs/
    3. Install numpy+mkl WHL file, link to: https://www.lfd.uci.edu/~gohlke/pythonlibs/

This installation is chosen because of the incompatibility of Python's WHL and Pip Direct install.

?? Cvxopt Official documentation URL: http://cvxopt.org/index.html, now the latest version is 1.1.9, co-developed by Martin Andersen, Joachim Dahl and Lieven Vandenberghe , it can solve the problem of linear programming and two sub-type programming, and its application scenarios such as hard Margin SVM in SVM.

?? Examples of cvxopt use are:

Linear programming problems

Example 1:



Python Program code:

ImportNumPy asNp fromCvxoptImportMatrix, 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 '])

Output Result:

     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+0 0 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+0 0 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]] 

Example 2



Python program code

ImportNumPy asNp fromCvxoptImportMatrix, 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]))

Output Result:

     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

Two sub-type planning problems




Where p,q,g,h,a,b is the input matrix, the problem is solved by using QP algorithm.

Example 1:



Python Program code:

 fromCvxoptImportMatrix, 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=Matrix1.0) Sol=SOLVERS.QP (Q, p, G, H, A, B)Print(sol[' x '])Print(sol[' Primal objective '])

Output Result:

     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]

Example 2:



Python Program code:

 fromCvxoptImportMatrix, 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 '])

Output results

     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's cvxopt module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.