From a41c15f3eec949ee59ed89b7f61f3b839cc34b9e Mon Sep 17 00:00:00 2001 From: emil Date: Wed, 9 Oct 2024 01:13:45 +0300 Subject: [PATCH] Make appropriate outputs of inputs. --- main.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++-- simplex.cpp | 6 ---- 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index 785d88d..ff16e53 100644 --- a/main.cpp +++ b/main.cpp @@ -4,12 +4,88 @@ #include "tools/math.h" #include "simplex.h" +void _printInitialInputs(Vector& C, Matrix& A, Vector& b, double eps, bool maximize) { + std::cout << "Running for the following inputs:" << std::endl << std::endl; + std::cout << "epsilon: " << eps << std::endl; + if (maximize) + { + std::cout << "Maximize" << std::endl; + } + else { + std::cout << "Minimize" << std::endl; + } + std::cout << "z = "; + bool previousIsNegative = false; + for (size_t i = 0; i < C.size(); i++) + { + bool isNegative = false; + if (C[i] != 0){ + if (i != 0 && !previousIsNegative){ + std::cout << " + "; + } + if (C[i] != 1) { + if (C[i] < 0){ + isNegative = true; + std::cout << "("; + } + std::cout << C[i] << " * "; + } + + std::cout << "x" << i + 1; + if (isNegative){ + std::cout << ")"; + } + + }else { + previousIsNegative = true; + } + + } + std::cout << std::endl << "subject to the constrains:" << std::endl; + std::cout<< std::endl; + for (size_t i = 0; i < b.size(); i++) + { + bool previousIsZero = false; + for (size_t j = 0; j < A.getColumns(); j++) + { + bool isNegative = false; + + if (A[i][j] != 0) { + if (j != 0 && !previousIsZero){ + std::cout << " + "; + previousIsZero = false; + } + if (A[i][j] != 1) { + if (A[i][j] < 0) { + isNegative = true; + std::cout << "("; + } + std::cout << A[i][j] << " * "; + } + std::cout << "x" << j + 1; + if (isNegative) { + std::cout << ")"; + } + + }else { + previousIsZero = true; + } + + } + std::cout << " <= " << b[i] << std::endl; + + } + + + +} + int printResult(Result result) { - if (result.state == unsolvable){ + if (result.state == unsolvable) { std::cout << "The method is not applicable!" << std::endl; - }else{ + }else { std::cout << "SOLVED!" << std::endl; std::cout << "Decision variables: ["; for (size_t i = 0; i < result.solution.size(); i++) @@ -53,6 +129,7 @@ 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); @@ -100,6 +177,7 @@ 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); @@ -151,6 +229,8 @@ int TEST_WITH_SLACK_CASE() { }; Vector b = {24, 6, 1, 2}; + _printInitialInputs(C, A, b, 0.01, true); + auto result = simplex(C, A, b); if (!(result.state == bounded)) { @@ -197,6 +277,8 @@ int TEST_UNBOUNDED_CASE() { {2, 0} }; Vector b = {10, 40}; + + _printInitialInputs(C, A, b, 0.01, true); auto result = simplex(C, A, b); @@ -234,6 +316,8 @@ int TEST_UNSOLVABLE_CASE() { }; Vector b = {-24, 6, 1, 2}; + _printInitialInputs(C, A, b, 0.01, true); + auto result = simplex(C, A, b); if (!(result.state == unsolvable)) { diff --git a/simplex.cpp b/simplex.cpp index 3006ef4..d6dae65 100644 --- a/simplex.cpp +++ b/simplex.cpp @@ -17,10 +17,6 @@ struct Result { bool maximize; }; -void _printInitialInputs(Vector& C, Matrix& A, Vector& b) { - -} - void _stopIterating(Matrix& generalMatrix, Vector& C, std::vector& basicVars, solver_state state, Result& result) { DestroyMatrix destroyedGeneralMatrix = disassembleGeneralMatrix(generalMatrix); Matrix _A = destroyedGeneralMatrix.A; @@ -121,8 +117,6 @@ Result simplex(Vector& C, Matrix& A, Vector& b, double eps = 0.01, bool maximize std::cout << "Iteration "<< iterationCount << " " << std::endl;; std::cout << generalMatrix; } - //result.state = solved; - //std::cout << result.state; return result; }