]> gitweb.michael.orlitzky.com - dead/census-tools.git/commitdiff
Added the LinearProgramming module.
authorMichael Orlitzky <michael@orlitzky.com>
Mon, 19 Apr 2010 04:55:24 +0000 (00:55 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 19 Apr 2010 04:55:24 +0000 (00:55 -0400)
src/LinearProgramming.py [new file with mode: 0644]

diff --git a/src/LinearProgramming.py b/src/LinearProgramming.py
new file mode 100644 (file)
index 0000000..1d72ad5
--- /dev/null
@@ -0,0 +1,44 @@
+"""
+Classes to create, solve, and make dinner for linear programs. Handles
+integration with lp_solve.
+"""
+
+import os
+import site
+import sys
+
+# Add LP_SOLVE_PATH to our path. There is no point to this variable
+# other than to make the site.addsitedir() line fit within 80
+# characters.
+LP_SOLVE_PATH = '/../lib/lp_solve'
+site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + LP_SOLVE_PATH)
+
+from lp_solve import *
+
+
+# Constants denoting the three different types of (in)equalities.
+# These are defined by lp_solve.
+LEQ = -1 # Less than or equal to.
+EQ  =  0 # Equal to
+GEQ =  1 # Greater than or Equal to.
+
+
+class LinearProgram(object):
+    """
+    Represents an instance of an lp_solve linear program.    
+    """
+    
+    def __init__(self):
+        self.objective_function_coefficients = []
+        self.constraint_matrix = []
+        self.rhs = []
+        self.inequalities = []
+
+
+    def solve(self):
+        [v,x,duals] = lp_solve(self.objective_function_coefficients,
+                               self.constraint_matrix,
+                               self.rhs,
+                               self.inequalities)
+        return [v,x,duals]
+