Python GLPK interfaces

下载:

http://www.dcc.fc.up.pt/~jpp/code/python-glpk/

安装:

http://en.wikibooks.org/wiki/GLPK/Python

 

 

 

 

 

Google: python GLPK

 

http://spokutta.wordpress.com/the-gnu-linear-programming-kit-glpk/

 

There are several interfaces for Python:

Python: python-glpk (see also GLPK’s Wikipedia page ).
PyGLPK:
PyGLPK
PyMathProg:
Python MathProg (GMPL)

Java: GLPK-java is an interface for Java.

YALMIP: YALMIP is a free Matlab interface/Matlab modeling environment for various solvers which can also access GLPK using GLPKMEX.

Many more interface can be found on GLPK’s Wikipedia page .

 

python 开发工具介绍:

http://www.builder.com.cn/2007/0915/507553.shtml

SWIG Python

http://www.swig.org/Doc1.3/Python.html

 

 

 


just follow the steps of the makefile
bottom to upper, build sequentially

=======================================================================================
PYVERS := $(shell pyversions -d)
DIR := $(shell basename `pwd`)

all:    _glpkpi.so glpkpi.py

glpkpi.py:    glpkpi.i glpkpi.c glpkpi.h Makefile
   


_glpkpi.so:    glpkpi.py glpkpi.o glpkpi_wrap.o
    gcc -Wall -shared glpkpi.o  glpkpi_wrap.o -lm -lglpk  -o _glpkpi.so

glpkpi.o:    glpkpi.c
    gcc -Wall -c -fPIC glpkpi.c -DHAVE_CONFIG_H -I D:/Program Files/Python27
glpkpi_wrap.o:    glpkpi_wrap.c
    gcc -Wall -c -fPIC glpkpi_wrap.c -DHAVE_CONFIG_H -I D:/Program Files/Python27


clean:
    rm -f *.o *~ *.pyc *.pyo _*.so glpkpi_wrap.c glpkpi.py

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

D:/TDDownload/pros/_Eclipse Workspace/python-glpk-0.4.43/swigwin-2.0.1>swig -pyt
hon glpkpi.i
./glpk.h(916) : Warning 314: 'in' is a python keyword, renaming to '_in'

Pydev could also finish that.

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

when "swig" or "gcc" shows up, just copy all the relevant files to the folders containing "swig.exe" or "gcc.exe",
D:/TDDownload/pros/_Eclipse Workspace/python-glpk-0.4.43/swigwin-2.0.1>swig -python glpkpi.i
D:/Program Files/MinGW/bin>gcc -Wall -c -fPIC glpkpi_wrap.c

need to install MinGW and add D:/Program Files/MinGW/bin to the path environment variable
c.f.
解决“Unable to find vcvarsall.bat”错误
   1. 首先安装MinGW,在MinGW的安装目录下找到bin的文件夹,找到mingw32-make.exe,复制一份更名为make.exe;
   2. 把MinGW的路径添加到环境变量path中,比如我把MinGW安装到D:/MinGW/中,就把D:/MinGW/bin添加到path中;
   3. 打开命令行窗口,在命令行窗口中进入到要安装代码的目录下;
   4. 输入如下命令就可以安装了。
setup.py install build --compiler=mingw32
=======================================================================================

Download GLPK
edit Build_GLPK_with_VC9.bat as (c.f. http://glpk.blogspot.com/):
----------------------------------------------------------------------------------------
rem Build GLPK with Microsoft Visual Studio Express 2008

rem NOTE: Make sure that HOME variable specifies correct path
set HOME="D:/Program Files/Microsoft Visual Studio 2005/VC"

call %HOME%/bin/vcvars32.bat
copy config_VC config.h
%HOME%/bin/nmake.exe /f Makefile_VC
%HOME%/bin/nmake.exe /f Makefile_VC check

pause
----------------------------------------------------------------------------------------



It should be OK now (glpk.lib and glpsol.exe are obtained)
----------------------------------------------------------------------------------------
        ./glpsol.exe --mps ../examples/plan.mps
GLPSOL: GLPK LP/MIP Solver, v4.45
Parameter(s) specified in the command line:
 --mps ../examples/plan.mps
Reading problem data from `../examples/plan.mps'...
Problem: PLAN
Objective: VALUE
8 rows, 7 columns, 48 non-zeros
54 records were read
GLPK Simplex Optimizer, v4.45
8 rows, 7 columns, 48 non-zeros
Preprocessing...
7 rows, 7 columns, 41 non-zeros
Scaling...
 A: min|aij| = 1.000e-002  max|aij| = 1.000e+000  ratio = 1.000e+002
GM: min|aij| = 2.534e-001  max|aij| = 3.946e+000  ratio = 1.557e+001
EQ: min|aij| = 6.422e-002  max|aij| = 1.000e+000  ratio = 1.557e+001
Constructing initial basis...
Size of triangular part = 7
      0: obj =  6.500000000e+002  infeas = 3.788e+003 (0)
*     2: obj =  4.376770833e+002  infeas = 0.000e+000 (0)
*    10: obj =  2.962166065e+002  infeas = 0.000e+000 (0)
OPTIMAL SOLUTION FOUND
Time used:   0.0 secs
Memory used: 0.0 Mb (51150 bytes)

D:/TDDownload/pros/_Eclipse Workspace/python-glpk-0.4.43/glpk-4.45/w32>pause
----------------------------------------------------------------------------------------



whenever there are path problems, simply change it to the absolute path...
setup.py
==========================================================================================
# Directory containing libglpk
GLPK_LIB_DIR = 'D:/TDDownload/pros/_Eclipse Workspace/python-glpk-0.4.43/glpk-4.45/lib'      

# Directory containing glpk.h
GLPK_INC_DIR = 'D:/TDDownload/pros/_Eclipse Workspace/python-glpk-0.4.43/glpk-4.45/include' 

# Directory containing Python.h
D:/Program Files/Python27/include/Python.h
==========================================================================================

install PLY
setup.py --help-commands

Standard commands:
  build            build everything needed to install
  build_py         "build" pure Python modules (copy to build directory)
  build_ext        build C/C++ extensions (compile/link to build directory)
  build_clib       build C/C++ libraries used by Python extensions
  build_scripts    "build" scripts (copy and fixup #! line)
  clean            clean up temporary files from 'build' command
  install          install everything from build directory
  install_lib      install all Python modules (extensions and pure Python)
  install_headers  install C/C++ header files
  install_scripts  install scripts (Python or otherwise)
  install_data     install data files
  sdist            create a source distribution (tarball, zip file, etc.)
  register         register the distribution with the Python package index
  bdist            create a built (binary) distribution
  bdist_dumb       create a "dumb" built distribution
  bdist_rpm        create an RPM distribution
  bdist_wininst    create an executable installer for MS Windows
  upload           upload binary package to PyPI
  check            perform some checks on the package

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help
  
  
   run ply/setup.py install

==========================================================================================
run src/setup.py install


run test.py
errro
Traceback (most recent call last):
  File "D:/TDDownload/pros/_Eclipse Workspace/python-glpk-0.4.43/examples/diet.py", line 1, in <module>
    from glpk import *
  File "D:/Program Files/Python27/lib/site-packages/glpk/__init__.py", line 27, in <module>
    from glpkpi import *
  File "D:/Program Files/Python27/lib/site-packages/glpk/glpkpi.py", line 25, in <module>
    _glpkpi = swig_import_helper()
  File "D:/Program Files/Python27/lib/site-packages/glpk/glpkpi.py", line 21, in swig_import_helper
    _mod = imp.load_module('_glpkpi', fp, pathname, description)
ImportError: DLL load failed: 找不到指定的模块。  


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

make
_glpkpi.so:    glpkpi.py glpkpi.o glpkpi_wrap.o
    gcc -Wall -shared glpkpi.o  glpkpi_wrap.o -lm -lglpk  -o _glpkpi.so

make -k _glpkpi.so
gcc -Wall -shared glpkpi.o  glpkpi_wrap.o -lm -lglpk  -o _glpkpi.so
process_begin: CreateProcess(NULL, pyversions -d, ...) failed.
process_begin: CreateProcess(NULL, basename `pwd`, ...) failed.
d:/program files/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -lglpk
collect2: ld returned 1 exit status
make: *** [_glpkpi.so] Error 1

just copy the glpk.lib file (c.f. http://glpk.blogspot.com/) to the following directory
d:/program files/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe
D:/Program Files/MinGW/bin
D:/Program Files/MinGW/mingw32/bin
D:/Program Files/MinGW/lib/gcc/mingw32/4.5.2

==========================================================================================
make _glpkpi.so
a lot of undefined reference

d:/program files/mingw/bin/../lib/gcc/mingw32/4.5.2/glpk.lib(../src/amd/amd_2.obj):(.text[__glp_amd_2]+0xb2): undefined reference to `_ftol2_sse'
collect2: ld returned 1 exit status
make: *** [_glpkpi.so] Error 1


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

Build with MinGW

GLPK 4.37 or before

 

rem Build GLPK with MinGW

rem NOTE: Make sure that HOME variable specifies correct path.
set HOME="D:/Program Files/MinGW"

set PATH=%HOME%/bin;%HOME%/libexec/gcc/mingw32/4.5.2;%PATH%
copy config_MinGW config.h
%HOME%/bin/mingw32-make.exe -f Makefile_MinGW
%HOME%/bin/mingw32-make.exe -f Makefile_MinGW check

pause

 

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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值