Exemplo n.º 1
0
 def check_parser_with_borders(self, str_fun, num_of_cof, exp_left_cof,
                               exp_left_free, exp_right_cof, exp_right_free,
                               exp_border):
     cof_array, free_array, border = Parser.parse_function_with_borders(
         str_fun, num_of_cof)
     self.assertEqual(exp_left_cof, cof_array[0])
     self.assertEqual(exp_left_free, free_array[0])
     self.assertEqual(exp_right_cof, cof_array[1])
     self.assertEqual(exp_right_free, free_array[1])
     self.assertEqual(exp_border, border)
Exemplo n.º 2
0
    def find_solution(function, bounds_array, optimal_model):
        """
        Поиск решения симплек-методом
        :param optimal_model: True — MAX / False — MIN
        :param function: Функция
        :param bounds_array: Условия
        :return: Решение симплекс-методом
        """
        max_num_of_func = Parser.find_max_constant(function)
        max_num_of_bounds = Parser.find_max_constant(bounds_array)
        num_of_const = max_num_of_func if max_num_of_func > max_num_of_bounds else max_num_of_bounds

        x = [
            LpVariable("x" + str(i + 1), lowBound=0)
            for i in range(num_of_const)
        ]
        problem = LpProblem("0", LpMaximize) if optimal_model else LpProblem(
            "0", LpMinimize)

        # Считаем main функцию
        fun_cof = Parser.parse_alone_function(function, num_of_const)
        problem += sum([fun_cof[i] * x[i] for i in range(num_of_const)])

        coefficients = []
        arguments = []
        borders = []

        # Считаем коэфициенты
        for bound in bounds_array:
            cof_arr, free_arr, border = Parser.parse_function_with_borders(
                bound, num_of_const)

            # Запоминаем всю информацию о начальных условиях для дальнейшей проверки
            coefficients.append(cof_arr)
            arguments.append(free_arr)
            borders.append(border)

            if border == Parser.MORE_OR_EQUAL:
                problem += \
                    sum([cof_arr[0][i] * x[i] for i in range(num_of_const)]) + (
                        0 if free_arr[0] is None else free_arr[0]) >= \
                    sum([cof_arr[1][i] * x[i] for i in range(num_of_const)]) + (
                        0 if free_arr[1] is None else free_arr[1])
            elif border == Parser.LESS_OR_EQUAL:
                problem += \
                    sum([cof_arr[0][i] * x[i] for i in range(num_of_const)]) + (
                        0 if free_arr[0] is None else free_arr[0]) <= \
                    sum([cof_arr[1][i] * x[i] for i in range(num_of_const)]) + (
                        0 if free_arr[1] is None else free_arr[1])
            elif border == Parser.EQUAL:
                problem += \
                    sum([cof_arr[0][i] * x[i] for i in range(num_of_const)]) + (
                        0 if free_arr[0] is None else free_arr[0]) == \
                    sum([cof_arr[1][i] * x[i] for i in range(num_of_const)]) + (
                        0 if free_arr[1] is None else free_arr[1])

        problem.solve()
        variables = [var.varValue for var in problem.variables()]

        if SimplexMethodSolver.__check_to_normal(coefficients, arguments,
                                                 borders, variables):
            return problem.objective.value(), variables
        return None, []
Exemplo n.º 3
0
 def test_bound_BCE_Exception(self):
     with self.assertRaises(ParserException):
         Parser.parse_function_with_borders("-x1 => -3", 1)