]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - src/LinearProgramming.py
Added the LinearProgramming module.
[dead/census-tools.git] / src / LinearProgramming.py
1 """
2 Classes to create, solve, and make dinner for linear programs. Handles
3 integration with lp_solve.
4 """
5
6 import os
7 import site
8 import sys
9
10 # Add LP_SOLVE_PATH to our path. There is no point to this variable
11 # other than to make the site.addsitedir() line fit within 80
12 # characters.
13 LP_SOLVE_PATH = '/../lib/lp_solve'
14 site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + LP_SOLVE_PATH)
15
16 from lp_solve import *
17
18
19 # Constants denoting the three different types of (in)equalities.
20 # These are defined by lp_solve.
21 LEQ = -1 # Less than or equal to.
22 EQ = 0 # Equal to
23 GEQ = 1 # Greater than or Equal to.
24
25
26 class LinearProgram(object):
27 """
28 Represents an instance of an lp_solve linear program.
29 """
30
31 def __init__(self):
32 self.objective_function_coefficients = []
33 self.constraint_matrix = []
34 self.rhs = []
35 self.inequalities = []
36
37
38 def solve(self):
39 [v,x,duals] = lp_solve(self.objective_function_coefficients,
40 self.constraint_matrix,
41 self.rhs,
42 self.inequalities)
43 return [v,x,duals]
44