diff --git a/main.cpp b/main.cpp index dc98ca4..29320fb 100644 --- a/main.cpp +++ b/main.cpp @@ -22,7 +22,6 @@ void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maxim bool lastNonZero = false; for (int i = 0; i < C.size(); i++) { - bool isNegative = false; for (int k = i; k < C.size(); k++) @@ -41,8 +40,6 @@ void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maxim std::cout << " + "; } - - if (C[i] != 0){ if (C[i] != 1) { if (C[i] < 0){ @@ -68,6 +65,7 @@ void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maxim { bool previousIsZero = true; bool lastNonZero = false; + for (int j = 0; j < A.getColumns(); j++) { bool isNegative = false; @@ -88,9 +86,6 @@ void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maxim std::cout << " + "; } - - - if (A[i][j] != 0) { if (A[i][j] != 1) { if (A[i][j] < 0) { @@ -108,24 +103,24 @@ void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maxim }else { previousIsZero = true; } - } std::cout << " <= " << b[i] << std::endl; - } } int printResult(Result result) { - if (result.state == unsolvable) { std::cout << "The method is not applicable!" << std::endl; + }else if (result.state == unbounded ) { + std::cout << "Unbounded problem!" << std::endl; } else { std::cout << "SOLVED!" << std::endl; std::cout << "Decision variables: ["; + for (int i = 0; i < result.solution.size(); i++) { std::cout << result.solution[i]; @@ -146,7 +141,6 @@ int printResult(Result result) } std::cout << "objective function value: " << result.objective_function_value << std::endl; } - return 0; } @@ -156,14 +150,13 @@ bool check_eq(double a, double b, double relativeEpsilon = 0.0001) a = std::abs(a); b = std::abs(b); double largest = (b > a) ? b : a; - + return diff <= largest * relativeEpsilon; } int TEST_GENERAL_CASE() { std::cout << "----------------------------RUNNING_TEST_GENERAL_CASE----------------------------" << std::endl; - Vector C = {5, 4}; Matrix A = { {6, 4}, @@ -172,7 +165,6 @@ int TEST_GENERAL_CASE() {0, 1}}; Vector b = {24, 6, 1, 2}; _printInitialInputs(C, A, b, 0.01, true); - auto result = simplex(C, A, b); if (!(result.state == bounded)) @@ -209,14 +201,12 @@ int TEST_GENERAL_CASE() } printResult(result); - return 1; } int TEST_MINIMIZE_CASE() { std::cout << "----------------------------RUNNING_TEST_MINIMIZE_CASE----------------------------" << std::endl; - Vector C = {-2, 2, -6}; Matrix A = { {2, 1, -2}, @@ -224,7 +214,6 @@ int TEST_MINIMIZE_CASE() {1, -1, 2}}; Vector b = {24, 23, 10}; _printInitialInputs(C, A, b, 0.01, false); - auto result = simplex(C, A, b, 0.01, false); if (!(result.state == bounded)) @@ -399,7 +388,6 @@ int TEST_UNSOLVABLE_CASE() int main() { - std::vector> tests = { TEST_GENERAL_CASE, TEST_MINIMIZE_CASE, diff --git a/simplex.cpp b/simplex.cpp index 4303a35..c85bc28 100644 --- a/simplex.cpp +++ b/simplex.cpp @@ -142,41 +142,3 @@ Result simplex(Vector &C, Matrix &A, Vector &b, double eps = 0.01, bool maximize } return result; } - -/* -Function_name(C, A, b, eps = eps_default) - -Input: -- C: A vector of coefficients of the objective function -- A: A matrix of coefficients of the constraint functions -- b: A vector of right-hand side values -- eps: Approximation accuracy (optional, default = eps_default) - -Steps: -1. Print the optimization problem: - - max (or min) z = C[0] * x1 + C[1] * x2 + ... + C[n] * xn - - subject to the constraints: - - A[0] * x <= b[0] - - A[1] * x <= b[1] - - ... - - A[m] * x <= b[m] - -2. Initialize: - - Form the initial tableau by introducing slack variables to convert inequalities into equalities. - -3. Iteratively apply the Simplex method: - - Step 1: Identify the entering variable (most negative coefficient in the objective row). - - Step 2: Identify the leaving variable (smallest positive ratio of RHS to pivot column). - - Step 3: Perform pivot operations to update the tableau. - -4. Check for optimality or unboundedness: - - If all coefficients in the objective function row are non-negative, the solution is optimal. - - If no leaving variable exists, the problem is unbounded. - -5. Return: - - solver_state: {solved, unbounded} - - x*: Optimal vector of decision variables (if solved) - - z: Maximum (or minimum) value of the objective function (if solved) - -End Function -*/