windows 7 安装最优化计算包cvxopt

目标:在windows 7 (64bit) ,python2.7 环境下面安装cvxopt,以便进行线性规划、二次规划、半正定规划等的计算。

解决过程简略记录:参考了cvxopt的官方说明 来安装cvxopt,但卡在了LAPACK的编译结果liblapack.a不能正确输出上面,LAPACK啊LAPACK。。。

于是又参考了 《如何在Windows下使用LAPACK和ARPACK》 的工作,正确输出了liblapack.a,之后再按照cvxopt的官方说明进行就没有问题了。


整体的过程如下:

1,windows7上安装矩阵计算BLAS/LAPACK

1.1编译BLAS

下载BLAS的最新版源文件 并解压为BLAS文件夹。

cd BLAS
sed 's/_LINUX/_WIN/' make.inc -i
make && cp blas_WIN.a ../libblas.a

这样就得到了 libblas.a,在cvxopt的编译过程中需要。

1.2编译LAPACK

下载lapack的最新源文件,解压。

tar -xvf lapack-3.4.2.tgz
cd lapack-3.4.2


这后面编译LAPACK的步骤就按照 这篇博文 《如何在Windows下使用LAPACK和ARPACK》来进行了。

(1). 打开解压缩文件夹,把lapack-3.4.2\BLAS\SRC里面的所有*.f文件 以及 lapack-3.4.2\INSTALL里面的dlamch.f和slamch.f两个文件 都复制到 lapack-3.4.2\SRC 文件夹里面。复制的时候提示有两个文件是重复的,我选择了不要覆盖。这样BLAS和LAPACK两个库的源文件都在一起了。
(2). DOS进入lapack-3.4.2\SRC目录,用gfortran 开始编译
cd SRC
gfortran -fimplicit-none -g -c *.f -O2
(等待十几分钟,可以在Windows下看到SRC文件夹里面源源不断地冒出.o文件)
然后以下文件需要无优化地重新编译
gfortran -fimplicit-none -g -c slaruv.f -O0
gfortran -fimplicit-none -g -c dlaruv.f -O0
gfortran -fimplicit-none -g -c sla_wwaddw.f -O0
gfortran -fimplicit-none -g -c dla_wwaddw.f   -O0
gfortran -fimplicit-none -g -c cla_wwaddw.f -O0
gfortran -fimplicit-none -g -c zla_wwaddw.f -O0
我是一行一行执行的……
(3). 用ar 连接成一个库
ar rcs liblapack.a *.o
这样就在SRC文件夹里得到LAPACK库文件 liblapack.a。
 

2,编译cvxopt

按照cvxopt的官方说明 进行。

(1)下载最新版cvxopt1.1.6源文件 。

(2)解压

tar -xvf cvxopt-1.1.6.tar.gz
cd cvxopt-1.1.6/src

(3)修改setup.py

    • 修改 变量 BLAS_LIB = [‘blas’,’gfortran’]
    • 修改变量 BLAS_LIB_DIR = ‘.’
(4)将之前编译好的libblas.a 和liblapack.a拷贝到cvxopt-1.1.6 文件夹下。
由于没有时间细读setup.py里面的内容,所以不能确定libblas.a 和liblapack.a具体应该在哪个文件夹下面,所以很疯狂的拷贝到了./ ,./src,./src/python , ./src/C 四个位置下面。
(5) 修改python的编译方式
sed 's/-mno-cygwin//g' -i'.bak' c:\Python27\Lib\distutils\cygwinccompiler.py

(6)在cvxopt-1.1.6文件夹下运行如下命令

python setup.py build --compiler=mingw32
python setup.py install

最终在 C:\Python27\Lib\site-packages 下看到了cvxopt-1.1.6-py2.7.egg-info,并且用ipython能够顺利的import cvxopt.   DONE!!!


(7)不要忘了把python的编译方式改回来

mv c:\Python27\Lib\distutils\cygwinccompiler.py.bak c:\Python27\Lib\distutils\cygwinccompiler.py

==================================================

下面运行cvxopt官方的一个线性规划的例子:


'''
Created on 2013-9-11
@author: Waleking
'''
from cvxopt import matrix, solvers

if __name__ == '__main__':

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

在eclipse+pydev中的运行结果:

     pcost       dcost       gap    pres   dres
 0:  1.8889e+00  7.7778e-01  1e+00  3e-16  2e+00
 1:  1.8769e+00  1.8320e+00  4e-02  1e-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  3e-16  5e-08
Optimal solution found.
[ 2.50e-01]
[ 7.50e-01]


附上cvxopt的setup.py:

from distutils.core import setup, Extension
from glob import glob

# Modifiy this if BLAS and LAPACK libraries are not in /usr/lib.
#BLAS_LIB_DIR = '/usr/lib'
BLAS_LIB_DIR = '.'

# Default names of BLAS and LAPACK libraries
BLAS_LIB = ['blas','gfortran']
#LAPACK_LIB = ['lapack','gfortran']
LAPACK_LIB = ['lapack']
BLAS_EXTRA_LINK_ARGS = []

# Set environment variable BLAS_NOUNDERSCORES=1 if your BLAS/LAPACK do
# not use trailing underscores
BLAS_NOUNDERSCORES = False

# Set to 1 if you are using the random number generators in the GNU
# Scientific Library.
BUILD_GSL = 0

# Directory containing libgsl (used only when BUILD_GSL = 1).
GSL_LIB_DIR = '/usr/lib'

# Directory containing the GSL header files (used only when BUILD_GSL = 1).
GSL_INC_DIR = '/usr/include/gsl'

# Set to 1 if you are installing the fftw module.
BUILD_FFTW = 0 

# Directory containing libfftw3 (used only when BUILD_FFTW = 1).
FFTW_LIB_DIR = '/usr/lib'

# Directory containing fftw.h (used only when BUILD_FFTW = 1).
FFTW_INC_DIR = '/usr/include'

# Set to 1 if you are installing the glpk module.
BUILD_GLPK = 0 

# Directory containing libglpk (used only when BUILD_GLPK = 1).
GLPK_LIB_DIR = '/usr/lib'

# Directory containing glpk.h (used only when BUILD_GLPK = 1).
GLPK_INC_DIR = '/usr/include'

# Set to 1 if you are installing the DSDP module.
BUILD_DSDP = 0

# Directory containing libdsdp (used only when BUILD_DSDP = 1).
DSDP_LIB_DIR = '/usr/lib'
 
# Directory containing dsdp5.h (used only when BUILD_DSDP = 1).
DSDP_INC_DIR = '/usr/include/dsdp'

# No modifications should be needed below this line.

extmods = []

# Macros
MACROS = []
if BLAS_NOUNDERSCORES: MACROS.append(('BLAS_NO_UNDERSCORE',''))

# optional modules

if BUILD_GSL:
    gsl = Extension('gsl', libraries = ['m', 'gsl'] + BLAS_LIB,
        include_dirs = [ GSL_INC_DIR ],
        library_dirs = [ GSL_LIB_DIR, BLAS_LIB_DIR ],
        extra_link_args = BLAS_EXTRA_LINK_ARGS,
        sources = ['src/C/gsl.c'] )
    extmods += [gsl];

if BUILD_FFTW:
    fftw = Extension('fftw', libraries = ['fftw3'] + BLAS_LIB,
        include_dirs = [ FFTW_INC_DIR ],
        library_dirs = [ FFTW_LIB_DIR, BLAS_LIB_DIR ],
        extra_link_args = BLAS_EXTRA_LINK_ARGS,
        sources = ['src/C/fftw.c'] )
    extmods += [fftw];

if BUILD_GLPK:
    glpk = Extension('glpk', libraries = ['glpk'],
        include_dirs = [ GLPK_INC_DIR ],
        library_dirs = [ GLPK_LIB_DIR ],
        sources = ['src/C/glpk.c'] )
    extmods += [glpk];

if BUILD_DSDP:
    dsdp = Extension('dsdp', libraries = ['dsdp'] + LAPACK_LIB + BLAS_LIB,
        include_dirs = [ DSDP_INC_DIR ],
        library_dirs = [ DSDP_LIB_DIR, BLAS_LIB_DIR ],
        extra_link_args = BLAS_EXTRA_LINK_ARGS,
        sources = ['src/C/dsdp.c'] )
    extmods += [dsdp];

# Required modules

base = Extension('base', libraries = ['m'] + LAPACK_LIB + BLAS_LIB,
    library_dirs = [ BLAS_LIB_DIR ],
    define_macros = MACROS,
    extra_link_args = BLAS_EXTRA_LINK_ARGS,
    sources = ['src/C/base.c','src/C/dense.c','src/C/sparse.c']) 

blas = Extension('blas', libraries = BLAS_LIB,
    library_dirs = [ BLAS_LIB_DIR ],
    define_macros = MACROS,
    extra_link_args = BLAS_EXTRA_LINK_ARGS,
    sources = ['src/C/blas.c'] )

lapack = Extension('lapack', libraries = LAPACK_LIB + BLAS_LIB,
    library_dirs = [ BLAS_LIB_DIR ],
    define_macros = MACROS,
    extra_link_args = BLAS_EXTRA_LINK_ARGS,
    sources = ['src/C/lapack.c'] )

umfpack = Extension('umfpack', 
    include_dirs = [ 'src/C/SuiteSparse/UMFPACK/Include',
        'src/C/SuiteSparse/AMD/Include', 
        'src/C/SuiteSparse/AMD/Source', 
        'src/C/SuiteSparse/SuiteSparse_config' ],
    library_dirs = [ BLAS_LIB_DIR ],
    define_macros = MACROS + [('NTIMER', '1'), ('NCHOLMOD', '1')],
    libraries = LAPACK_LIB + BLAS_LIB,
    extra_compile_args = ['-Wno-unknown-pragmas'],
    extra_link_args = BLAS_EXTRA_LINK_ARGS,
    sources = [ 'src/C/umfpack.c',
        'src/C/SuiteSparse/UMFPACK/Source/umfpack_global.c',
        'src/C/SuiteSparse/UMFPACK/Source/umfpack_tictoc.c' ] +
        ['src/C/SuiteSparse/SuiteSparse_config/SuiteSparse_config.c'] +
        glob('src/C/SuiteSparse_cvxopt_extra/umfpack/*'))

# Build for int or long? 
import sys
if sys.maxsize > 2**31: MACROS += [('DLONG',None)]

cholmod = Extension('cholmod',
    library_dirs = [ BLAS_LIB_DIR ],
    libraries = LAPACK_LIB + BLAS_LIB,
    include_dirs = [ 'src/C/SuiteSparse/CHOLMOD/Include', 
        'src/C/SuiteSparse/COLAMD', 
        'src/C/SuiteSparse/AMD/Include', 
        'src/C/SuiteSparse/COLAMD/Include',
        'src/C/SuiteSparse/SuiteSparse_config' ],
    define_macros = MACROS + [('NPARTITION', '1'), ('NTIMER', '1')],
    extra_link_args = BLAS_EXTRA_LINK_ARGS,
    sources = [ 'src/C/cholmod.c' ] + 
        ['src/C/SuiteSparse/AMD/Source/' + s for s in ['amd_global.c',
            'amd_postorder.c', 'amd_post_tree.c', 'amd_2.c']] +
        ['src/C/SuiteSparse/COLAMD/Source/' + s for s in ['colamd.c',
            'colamd_global.c']] +
        ['src/C/SuiteSparse/SuiteSparse_config/SuiteSparse_config.c'] +
        glob('src/C/SuiteSparse/CHOLMOD/Core/c*.c') +
        glob('src/C/SuiteSparse/CHOLMOD/Cholesky/c*.c') +
        ['src/C/SuiteSparse/CHOLMOD/Check/cholmod_check.c'] +
        glob('src/C/SuiteSparse/CHOLMOD/Supernodal/c*.c') )

amd = Extension('amd', 
    include_dirs = [ 'src/C/SuiteSparse/AMD/Include', 
        'src/C/SuiteSparse/SuiteSparse_config' ],
    define_macros = MACROS,
    sources = [ 'src/C/amd.c' ] + glob('src/C/SuiteSparse/AMD/Source/*.c') )

misc_solvers = Extension('misc_solvers',
    libraries = LAPACK_LIB + BLAS_LIB,
    library_dirs = [ BLAS_LIB_DIR ],
    define_macros = MACROS,
    extra_link_args = BLAS_EXTRA_LINK_ARGS,
    sources = ['src/C/misc_solvers.c'] )

extmods += [base, blas, lapack, umfpack, cholmod, amd, misc_solvers] 

setup (name = 'cvxopt', 
    description = 'Convex optimization package',
    version = '1.1.6', 
    long_description = '''
CVXOPT is a free software package for convex optimization based on the 
Python programming language. It can be used with the interactive Python 
interpreter, on the command line by executing Python scripts, or 
integrated in other software via Python extension modules. Its main 
purpose is to make the development of software for convex optimization 
applications straightforward by building on Python's extensive standard 
library and on the strengths of Python as a high-level programming 
language.''', 
    author = 'M. Andersen, J. Dahl, and L. Vandenberghe',
    author_email = '[email protected], [email protected], [email protected]',
    url = 'http://abel.ee.ucla.edu/cvxopt',
    license = 'GNU GPL version 3',
    ext_package = "cvxopt",
    ext_modules = extmods,
    package_dir = {"cvxopt": "src/python"},
    packages = ["cvxopt"])



你可能感兴趣的:(windows 7 安装最优化计算包cvxopt)