def read_file(self, filepath, verbose_mode): A = [] c = [] b = [] signals = [] non_negativity = [] try: file_pointer = open(filepath, 'r') except IOError: print("Error: File Doesn't Exists!") return -1 num_variables = int(file_pointer.readline()) num_constraints = int(file_pointer.readline()) non_negativity = file_pointer.readline().replace("\r\n", "").split(" ") c = file_pointer.readline().replace("\r\n", "").split(" ") i = 0 line = file_pointer.readline() while (line): line = line.split(" ") b.append(float(line[len(line) - 1])) signals.append(line[len(line) - 2]) A.append([line[k] for k in (range(len(line) - 2))]) line = file_pointer.readline() i += 1 file_pointer.close() return linear_programming.LinearProgramming(num_variables, num_constraints, A, b, c, signals, non_negativity, verbose_mode)
def setUp(self): self.A1 = matrix.Matrix([ [3, 2, 1], [2, 5, 3] ]) self.b1 = [10, 15] self.c1 = [2, 3, 4] self.lp1 = linear_programming.LinearProgramming(self.c1, self.A1, self.b1)
def test_unfeasible(self): A = matrix.Matrix([ [1, 1, 1], [-1, -1, 1] ]) b = [-5, -5] c = [-1, 2, -2] lp = linear_programming.LinearProgramming(c, A, b) result = lp.run() self.assertEqual(result.status, "UNFEASIBLE")
def test_unbounded(self): A = matrix.Matrix([ [-1, 1], [1, -2] ]) b = [1, 2] c = [2, 1] lp = linear_programming.LinearProgramming(c, A, b) result = lp.run() # self.assertAlmostEqual(result.value, 4) # self.assertAlmostEqual(result.x_values[0]*c[0]+result.x_values[1]*c[1], 4) self.assertEqual(result.status, "UNBOUNDED")
def test_success(self): A = matrix.Matrix([ [2, -1], [1, 2], [-1, 2] ]) b = [4, 9, 3] c = [2, 5] lp = linear_programming.LinearProgramming(c, A, b) result = lp.run() self.assertAlmostEqual(result.value, 21) self.assertAlmostEqual(result.x_values[0]*c[0]+result.x_values[1]*c[1], 21) self.assertEqual(result.status, "SUCCESSFUL") for i in range(len(b)): s = 0 for j in range(len(result.x_values)): s += result.x_values[j]*A.elements[i][j] self.assertLessEqual(round(s,7), b[i])
def test_degenerate(self): A = matrix.Matrix([ [3, 1], [1, -1], [0, 1] ]) b = [6, 2, 3] c = [2, 1] lp = linear_programming.LinearProgramming(c, A, b) result = lp.run() self.assertAlmostEqual(result.value, 5) self.assertAlmostEqual(result.x_values[0]*c[0]+result.x_values[1]*c[1], 5) self.assertEqual(result.status, "SUCCESSFUL") for i in range(len(b)): s = 0 for j in range(len(result.x_values)): s += result.x_values[j]*A.elements[i][j] self.assertLessEqual(s, b[i])
def test_repr(self): primal = """max (-1*x0)+(2*x1)+(-2*x2) (1*x0)+(1*x1)+(-1*x2) <= 5 (-1*x0)+(-1*x1)+(1*x2) <= -5 x0, x1, x2 >= 0""" dual = """min (5*y0)+(-5*y1) (1*y0)+(-1*y1) >= -1 (1*y0)+(-1*y1) >= 2 (-1*y0)+(1*y1) >= -2 y0, y1 >= 0""" A = matrix.Matrix([ [1, 1, -1], [-1, -1, 1] ]) b = [5, -5] c = [-1, 2, -2] lp = linear_programming.LinearProgramming(c, A, b) self.assertEqual(str(lp), primal) self.assertEqual(lp.dual(), dual)
def test_negative_b(self): A = matrix.Matrix([ [-1,0,-1,0,0], [-1,0,0,-1,0], [-1,-1,0,0,-1], [0,-1,0,0,0], [0,-1,0,0,0] ]).transposed() b = [-1,-1,-1,-1,-1] c = [-1,-1,-1,-1,-1] lp = linear_programming.LinearProgramming(c, A, b) result = lp.run() self.assertAlmostEqual(result.value, -3) s = 0 for j in range(len(result.x_values)): s += result.x_values[j]*c[0] self.assertAlmostEqual(s, -3) self.assertEqual(result.status, "SUCCESSFUL") for i in range(len(b)): s = 0 for j in range(len(result.x_values)): s += result.x_values[j]*A.elements[i][j] self.assertLessEqual(s, b[i])