Exemplo n.º 1
0
def main():
    message = "***Direct task***"
    matrix, line_signs, free_vec, var_signs, target = parse(Path("config.txt"))
    print_task(matrix, line_signs, free_vec, var_signs, target, message)

    message = "***Dual task***"
    dual_matrix, dual_line_signs, dual_free_vec, dual_var_signs, dual_target = to_dual_task(
        matrix, line_signs, free_vec, var_signs, target)
    print_task(dual_matrix, dual_line_signs, dual_free_vec, dual_var_signs, dual_target, message)

    message = "***Canonical direct task***"
    N, B, A, b, c, v = to_canon(matrix, line_signs, free_vec, var_signs, target)
    print_task(matrix, line_signs, free_vec, var_signs, target, message)

    message = "***Canonical dual task***"
    dual_N, dual_B, dual_A, dual_b, dual_c, dual_v = to_canon(dual_matrix, dual_line_signs, dual_free_vec,
                                                              dual_var_signs, dual_target)
    print_task(dual_matrix, dual_line_signs, dual_free_vec, dual_var_signs, dual_target, message)

    print("***Solution of direct task by brute force method***")
    print(brute_force(A, b, c))
    print()
    print("***Solution of dual task by brute force method***")
    print(brute_force(dual_A, dual_b, dual_c))
    print("============================================================================")

    print("***Solution of direct task by simplex method***")
    N, B, A, b, c, v = initialize_simplex(matrix, free_vec, target)
    print(simplex(N, B, A, b, c, v))

    """
Exemplo n.º 2
0
    def test_simplex_1(self):
        (z, x) = simplex('min', np.array([[3, 1], [4, 3], [1, 2]]),
                         np.array([[3], [6], [4]]), np.array([[4], [1]]),
                         np.array([[0], [-1], [1]]), 100)

        self.assertTrue(
            np.allclose(z, 3.4)
            and np.allclose(x, np.array([[0.4], [1.8], [0], [1], [0], [0]])))
Exemplo n.º 3
0
    def test_simplex_3(self):
        (z, x) = simplex('min', np.array([[1 / 2, 1 / 4], [1, 3], [1, 1]]),
                         np.array([[4], [20], [10]]), np.array([[2], [3]]),
                         np.array([[1], [-1], [0]]), 100)

        self.assertTrue(
            np.allclose(z, 25)
            and np.allclose(x, np.array([[5], [5], [0.25], [0], [0], [0]])))
Exemplo n.º 4
0
    def test_simplex_2(self):
        (z, x) = simplex('max', np.array([[6, 4], [1, 2], [-1, 1], [0, 1]]),
                         np.array([[24], [6], [1], [2]]), np.array([[5], [4]]),
                         np.array([[1], [1], [1], [1]]), 100)

        self.assertTrue(
            np.allclose(z, 21)
            and np.allclose(x, np.array([[3], [1.5], [0], [0], [2.5], [0.5]])))
Exemplo n.º 5
0
    def test_simplex_4(self):
        (z, x) = simplex(
            'max', np.array([[8, 6, 1], [4, 2, 1.5], [2, 1.5, 0.5], [0, 1,
                                                                     0]]),
            np.array([[48], [20], [8], [5]]), np.array([[60], [30], [20]]),
            np.array([[1], [1], [1], [1]]), 100)

        self.assertTrue(
            np.allclose(z, 280)
            and np.allclose(x, np.array([[2], [0], [8], [24], [0], [0], [5]])))