def task_8(self):
        A = [[2, 1, 0, 3, -1, -1], [0, 1, -2, 1, 0, 3], [3, 0, 1, 1, 1, 1]]
        b = [2, 2, 5]
        c = [0, -1, 1, 0, 4, 3]
        d_lo = [2, 0, -1, -3, 2, 1]
        d_hi = [7, 3, 2, 3, 4, 5]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = DualSimplex(task)

        self.assertRaises(NoPlanException, dual.solve)
    def task_6(self):
        A = [[1, 7, 2, 0, 1, -1, 4], [0, 5, 6, 1, 0, -3, -2],
             [3, 2, 2, 1, 1, 1, 5]]
        b = [1, 4, 7]
        c = [1, 2, 1, -3, 3, 1, 0]
        d_lo = [-1, 1, -2, 0, 1, 2, 4]
        d_hi = [3, 2, 2, 5, 3, 4, 5]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = DualSimplex(task)
        self.assertRaises(NoPlanException, dual.solve)
    def test_task5(self):
        A = [[1, -5, 3, 1, 0, 0], [4, -1, 1, 0, 1, 0], [2, 4, 2, 0, 0, 1]]
        b = [-8, 22, 30]
        c = [7, -2, 6, 0, 5, 2]
        d_lo = [2, 1, 0, 0, 1, 1]
        d_hi = [6, 6, 5, 2, 4, 6]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = BranchAndBound(task)
        dual.solve()

        x_expected = [6, 3, 0, 1, 1, 6]
        for i, xi in enumerate(dual.x):
            self.assertAlmostEqual(xi, x_expected[i], 4)
        self.assertAlmostEqual(dual.mu, 53, 5)
    def task_1(self):
        A = [[2, 1, -1, 0, 0, 1], [1, 0, 1, 1, 0, 0], [0, 1, 0, 0, 1, 0]]
        b = [2, 5, 0]
        c = [3, 2, 0, 3, -2, -4]
        d_lo = [0, -1, 2, 1, -1, 0]
        d_hi = [2, 4, 4, 3, 3, 5]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = DualSimplex(task)
        x, J, f_val = dual.solve([3, 4, 5])

        x_expected = [3 / 2, 1, 2, 3 / 2, -1, 0]
        for i, xi in enumerate(x):
            self.assertAlmostEqual(xi, x_expected[i], 3)
        self.assertAlmostEqual(f_val, 13, 5)
    def task_3(self):
        A = [[1, 0, 2, 2, -3, 3], [0, 1, 0, -1, 0, 1], [1, 0, 1, 3, 2, 1]]
        b = [15, 0, 13]
        c = [3, 0.5, 4, 4, 1, 5]
        d_lo = [0, 0, 0, 0, 0, 0]
        d_hi = [3, 5, 4, 3, 3, 4]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = DualSimplex(task)
        x, J, f_val = dual.solve()

        x_expected = [3, 0, 4, 1.1818, 0.6364, 1.1818]
        for i, xi in enumerate(x):
            self.assertAlmostEqual(xi, x_expected[i], 4)
        self.assertAlmostEqual(f_val, 36.2727, 4)
    def task_2(self):
        A = [[1, -5, 3, 1, 0, 0], [4, -1, 1, 0, 1, 0], [2, 4, 2, 0, 0, 1]]
        b = [-7, 22, 30]
        c = [7, -2, 6, 0, 5, 2]
        d_lo = [2, 1, 0, 0, 1, 1]
        d_hi = [6, 6, 5, 2, 4, 6]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = DualSimplex(task)
        x, J, f_val = dual.solve()

        x_expected = [5, 3, 1, 0, 4, 6]
        for i, xi in enumerate(x):
            self.assertAlmostEqual(xi, x_expected[i], 5)
        self.assertAlmostEqual(f_val, 67, 5)
    def test_task1(self):
        A = [[1, 0, 0, 12, 1, -3, 4, -1], [0, 1, 0, 11, 12, 3, 5, 3],
             [0, 0, 1, 1, 0, 22, -2, 1]]
        b = [40, 107, 61]
        c = [2, 1, -2, -1, 4, -5, 5, 5]
        d_lo = [0, 0, 0, 0, 0, 0, 0, 0]
        d_hi = [3, 5, 5, 3, 4, 5, 6, 3]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = BranchAndBound(task)
        dual.solve()

        x_expected = [1, 1, 2, 2, 3, 3, 6, 3]
        for i, xi in enumerate(dual.x):
            self.assertAlmostEqual(xi, x_expected[i], 4)
        self.assertAlmostEqual(dual.mu, 39, 5)
    def task_4(self):
        A = [[1, 0, 0, 12, 1, -3, 4, -1], [0, 1, 0, 11, 12, 3, 5, 3],
             [0, 0, 1, 1, 0, 22, -2, 1]]
        b = [40, 107, 61]
        c = [2, 1, -2, -1, 4, -5, 5, 5]
        d_lo = [0, 0, 0, 0, 0, 0, 0, 0]
        d_hi = [3, 5, 5, 3, 4, 5, 6, 3]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = DualSimplex(task)
        x, J, f_val = dual.solve()

        x_expected = [3, 5, 0, 1.8779, 2.7545, 3.0965, 6, 3]
        for i, xi in enumerate(x):
            self.assertAlmostEqual(xi, x_expected[i], 4)
        self.assertAlmostEqual(f_val, 49.6577, 4)
    def task_9(self):
        A = [[1, 3, 1, -1, 0, -3, 2, 1], [2, 1, 3, -1, 1, 4, 1, 1],
             [-1, 0, 2, -2, 2, 1, 1, 1]]
        b = [4, 12, 4]
        c = [2, -1, 2, 3, -2, 3, 4, 1]
        d_lo = [-1] * 8
        d_hi = [2, 3, 1, 4, 3, 2, 4, 4]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = DualSimplex(task)
        x, J, f_val = dual.solve()

        x_expected = [-1, 0.4074, 1, 4, -0.3704, 1.7407, 4, 4]
        for i, xi in enumerate(x):
            self.assertAlmostEqual(xi, x_expected[i], 4)
        self.assertAlmostEqual(f_val, 37.5556, 4)
    def task_7(self):
        A = [[2, -1, 1, 0, 0, -1, 3], [0, 4, -1, 2, 3, -2, 2],
             [3, 1, 0, 1, 0, 1, 4]]
        b = [1.5, 9, 2]
        c = [0, 1, 2, 1, -3, 4, 7]
        d_lo = [0, 0, -3, 0, -1, 1, 0]
        d_hi = [3, 3, 4, 7, 5, 3, 2]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = DualSimplex(task)
        x, J, f_val = dual.solve()

        x_expected = [0, 1, 3.5, 0, 3.5, 1, 0]
        for i, xi in enumerate(x):
            self.assertAlmostEqual(xi, x_expected[i], 4)
        self.assertAlmostEqual(f_val, 1.5, 4)
    def test_task4(self):
        A = [[4, 0, 0, 0, 0, -3, 4, -1, 2, 3], [0, 1, 0, 0, 0, 3, 5, 3, 4, 5],
             [0, 0, 1, 0, 0, 22, -2, 1, 6, 7], [0, 0, 0, 1, 0, 6, -2, 7, 5, 6],
             [0, 0, 0, 0, 1, 5, 5, 1, 6, 7]]
        b = [8, 5, 4, 7, 8]
        c = [2, 1, -2, -1, 4, -5, 5, 5, 1, 2]
        d_lo = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        d_hi = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = BranchAndBound(task)
        dual.solve()

        x_expected = [2, 5, 4, 7, 8, 0, 0, 0, 0, 0]
        for i, xi in enumerate(dual.x):
            self.assertAlmostEqual(xi, x_expected[i], 4)
        self.assertAlmostEqual(dual.mu, 26, 5)
    def test_task3(self):
        A = [[1, 0, 0, 12, 1, -3, 4, -1, 2.5, 3],
             [0, 1, 0, 11, 12, 3, 5, 3, 4, 5.1],
             [0, 0, 1, 1, 0, 22, -2, 1, 6.1, 7]]
        b = [43.5, 107.3, 106.3]
        c = [2, 1, -2, -1, 4, -5, 5, 5, 1, 2]
        d_lo = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        d_hi = [2, 4, 5, 3, 4, 5, 4, 4, 5, 6]

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = BranchAndBound(task)
        dual.solve()

        x_expected = [1, 1, 2, 2, 2, 3, 3, 3, 3, 3]
        for i, xi in enumerate(dual.x):
            self.assertAlmostEqual(xi, x_expected[i], 4)
        self.assertAlmostEqual(dual.mu, 29, 5)
    def task_5(self):
        A = [[1, -3, 2, 0, 1, -1, 4, -1, 0], [1, -1, 6, 1, 0, -2, 2, 2, 0],
             [2, 2, -1, 1, 0, -3, 8, -1, 1], [4, 1, 0, 0, 1, -1, 0, -1, 1],
             [1, 1, 1, 1, 1, 1, 1, 1, 1]]
        b = [3, 9, 9, 5, 9]
        c = [-1, 5, -2, 4, 3, 1, 2, 8, 3]
        d_lo = [0] * 9
        d_hi = [5] * 9

        task = DualTask(A, b, c, d_lo, d_hi)
        dual = DualSimplex(task)
        x, J, f_val = dual.solve()

        x_expected = [1.1579, 0.6942, 0, 0, 2.8797, 0, 1.0627, 3.2055, 0]
        for i, xi in enumerate(x):
            self.assertAlmostEqual(xi, x_expected[i], 4)
        self.assertAlmostEqual(f_val, 38.7218, 4)
Пример #14
0
    def __split_task(self, task, x):
        pos, x_val = get_not_int(x)
        # print('split by pos', pos, x_val)
        split_val = math.floor(x_val)

        task1 = DualTask(task.A, task.b, task.c, task.d_lo, task.d_hi)
        task1.d_hi[pos] = split_val

        task2 = DualTask(task.A, task.b, task.c, task.d_lo, task.d_hi)
        task2.d_lo[pos] = split_val + 1

        return task1, task2