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)

33 comments:

  1. 看到大家都留言,我也來留個言支持!!!加油囉 ........................................

    ReplyDelete
  2. hello~welcome my world~<. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    ReplyDelete
  3. 卡爾.桑得柏:「除非先有夢,否則一切皆不成。」共勉!..................................................

    ReplyDelete
  4. 文章是心情的反應~~祝妳天天寫的都是讓人開心的好文章哦!!............................................................

    ReplyDelete
  5. 我們這一代最偉大的發現是,人類可以藉由改變心中的態度來改變人生。.......................................................

    ReplyDelete
  6. 人生有些波折,才能有些成長,所以不論順逆,凡是成長、成功的助緣,都應該心存感激。.................................................

    ReplyDelete
  7. 聰明人之所以不會成功,是因為他們缺乏了堅忍的毅力。.................................................

    ReplyDelete
  8. 河水永遠是相同的,可是每一剎那又都是新的。..................................................

    ReplyDelete