]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - bin/example-lp
Add the example linear programming problem solution via lp_solve.
[dead/census-tools.git] / bin / example-lp
1 #!/usr/bin/env python
2
3 """
4 Solve an example problem with lp_solve.
5 """
6
7 import os
8 import site
9 import sys
10
11 # Add LP_SOLVE_PATH to our path.
12 LP_SOLVE_PATH = '../lib/lp_solve'
13 site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/' + LP_SOLVE_PATH)
14
15 from lp_solve import *
16
17 c = [5, 100, 30, 10, 20, 300]
18
19 A = [ [1,1,1,0,0,0],
20 [0,0,0,1,1,1],
21 [1,0,0,1,0,0],
22 [0,1,0,0,1,0],
23 [0,0,1,0,0,1] ]
24
25 e = [ -1, -1, 0, 0, 0 ]
26
27 b = [ 500, 600, 400, 300, 200 ]
28
29 [v,x,duals] = lp_solve(c,A,b,e)
30
31 print 'Optimal objective function value: ', v
32 print 'Optimal solution vector: ', x
33 print 'Duals: ', duals