Wednesday, February 10, 2010

Using GLPK in Python through pulp 1.4.7 in Windows Seven (32 bits)

There are several steps before we can use GLPK (GNU Linear Programming Kits) in Python
1. For those who are not familiar with python, you have to learn some of basic Python here (I recommend to use python v.2.6.4).
2. Install Pulp 1.4.7 from here (please also follow the install instruction in Windows)
3. Download the Microsoft Visual Studio Express 2008 from microsoft (Indeed, we just want only visual c compiler, but I've not tried it yet).
4. Download the GLPK from here
5. Goes deep into folder "your download directory"\glpk-4.42\glpk-4.42\w32. Find for the file name Build_GLPK_with_VC9.bat.
6. Open with text editor and check for the path of c compiler (in the case that you have changed the default installation directory of visual C compiler).
7. Run Build_GLPK_with_VC9.bat
8. If the installation was successfully, it will display the message 'OPTIMAL SOLUTION FOUND'.
9. Now, you test with the following script

Note that you have to add the path for all executable files (Python, Easy_install and glpsol) in the environment variables of Windows seven.

from pulp import *

prob = LpProblem("test1", LpMinimize)

# Variables x = LpVariable("x", 0, 4) y = LpVariable("y", -1, 1) z = LpVariable("z", 0)

# Objective prob += x + 4*y + 9*z

# Constraints prob += x+y <= 5 prob += x+z >= 10 prob += -y+z == 7

GLPK().solve(prob)

# Solution for v in prob.variables():    print v.name, "=", v.varValue

print "objective=", value(prob.objective)