diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..eeb0991 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "makefile.launchConfigurations": [ + { + "cwd": "/home/emil/Coding/Assignments/SimplexTASK", + "binaryPath": "/home/emil/Coding/Assignments/SimplexTASK/simplex.out", + "binaryArgs": [] + } + ] +} \ No newline at end of file diff --git a/Makefile b/Makefile.txt similarity index 100% rename from Makefile rename to Makefile.txt diff --git a/main.cpp b/main.cpp index f4a38ed..444c48f 100644 --- a/main.cpp +++ b/main.cpp @@ -3,13 +3,43 @@ #include "tools/math.h" #include "simplex.h" +void manualInput() { + int ZLength; + std::cout << "Write how many x's the objective function has:" << std::endl; + std::cin >> ZLength; + Vector Z = {}; +} +void printInitialInputs(Vector C, Matrix A, Vector b) { + +} int main() { // TODO: Initially should be positive - Vector C = {-5, -4, 0, 0, 0, 0}; - Matrix A = {{6, 4, 1, 0, 0, 0}, {1, 2, 0, 1, 0, 0}, {-1, 1, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 1}}; + std::string doManual = ""; + std::cout << "Enable manual input? (y/n)" << std::endl; + std::cin >> doManual ; + if (doManual == "y" or doManual == "Y") { + manualInput(); + } else if (doManual == "n"){ + + } + + Vector C = {5, 4, 0, 0, 0, 0}; + + Matrix A = { + {6, 4, 1, 0, 0, 0}, + {1, 2, 0, 1, 0, 0}, + {-1, 1, 0, 0, 1, 0}, + {0, 1, 0, 0, 0, 1} + }; + Vector b = {24, 6, 1, 2}; - Result result = Simplex(C, A, b); + //Matrix test = {{1, -1, -2}, {1, 1, -2}, {1, -1, 2}}; + + //showMatrix(test); + + + Result result = Simplex(C, A, b, 0.1, true); if(result.state == bounded) { if(result.solution == nullptr) { @@ -20,6 +50,9 @@ int main() { delete result.solution; } + //std::cout << "asdasd" << std::endl; return 0; } + + diff --git a/simplex.cpp b/simplex.cpp index 3d2b421..d0dbf38 100644 --- a/simplex.cpp +++ b/simplex.cpp @@ -15,42 +15,81 @@ struct Result { double objective_function_value; }; -Result Simplex(Vector C, Matrix A, Vector b, double eps = 0.01, bool maximize=true) { - Result result{}; - Matrix generalMatrix = createGeneralMatrix(A, C, b); + +Result Simplex(Vector C, Matrix A, Vector b, double eps = 0.01, bool maximize=true) { + if (maximize == true) { + for (int i = 0; i < C.size(); i++) { + C[i] = -C[i]; + } + } + Result result{}; + Matrix generalMatrix = createGeneralMatrix(A, C, b); + std::cout << "Before:" << std::endl; + showMatrix(generalMatrix); std::vector basicVars(generalMatrix.getRows()); basicVars[0] = -1; for (size_t i = 1; i < basicVars.size(); i++) { basicVars[i] = static_cast(basicVars.size()) + i; } - + int iterationCount = 0; while (true) { //3 + iterationCount++; + std::cout << "Iteration: "; + std::cout << iterationCount << std::endl; int pivot_column_index = 0; - pivot_column_index = min_index(generalMatrix[0]); + if (maximize) { - if (generalMatrix[0][pivot_column_index] >= 0) { - DestroyMatrix destroyedGeneralMatrix = destroyGeneralMatrix(generalMatrix); - Matrix _A = destroyedGeneralMatrix.A; - Vector _C = destroyedGeneralMatrix.C; - Vector _b = destroyedGeneralMatrix.b; + pivot_column_index = min_index(generalMatrix[0]); - result.state = bounded; - result.solution = new Vector(C.size()); - for (int i = 0; i < C.size(); i++) { - result.solution->operator[](i) = 0; - } - for (size_t i = 1; i < basicVars.size(); i++) { - if (basicVars[i] <= C.size()) { - result.solution->operator[](basicVars[i]) = _b[i]; + if (generalMatrix[0][pivot_column_index] >= 0) { + DestroyMatrix destroyedGeneralMatrix = disassembleGeneralMatrix(generalMatrix); + Matrix _A = destroyedGeneralMatrix.A; + Vector _C = destroyedGeneralMatrix.C; + Vector _b = destroyedGeneralMatrix.b; + + result.state = bounded; + result.solution = new Vector(C.size()); + for (int i = 0; i < C.size(); i++) { + result.solution->operator[](i) = 0; } + for (size_t i = 1; i < basicVars.size(); i++) { + if (basicVars[i] <= C.size()) { + result.solution->operator[](basicVars[i]) = _b[i]; + } + } + result.objective_function_value = _C[_C.size()]; + return result; } - result.objective_function_value = b[0]; - return result; } + if (maximize == false) { + pivot_column_index = max_index(generalMatrix[0]); + + if (generalMatrix[0][pivot_column_index] < 0) { + DestroyMatrix destroyedGeneralMatrix = disassembleGeneralMatrix(generalMatrix); + Matrix _A = destroyedGeneralMatrix.A; + Vector _C = destroyedGeneralMatrix.C; + Vector _b = destroyedGeneralMatrix.b; + + result.state = bounded; + result.solution = new Vector(C.size()); + for (int i = 0; i < C.size(); i++) { + result.solution->operator[](i) = 0; + } + for (size_t i = 1; i < basicVars.size(); i++) { + if (basicVars[i] <= C.size()) { + result.solution->operator[](basicVars[i]) = _b[i]; + } + } + result.objective_function_value = _C[_C.size()]; + return result; + } + } + + //4 Vector ratio_vector(generalMatrix.getRows()); for (int i = 1; i < generalMatrix.getRows(); i++) { @@ -68,6 +107,50 @@ Result Simplex(Vector C, Matrix A, Vector b, double eps = 0.01, bool maximize=tr //5 elimination(generalMatrix, pivot_row_index, pivot_column_index); + std::cout << "After:" << std::endl; + showMatrix(generalMatrix); + + if (maximize) { + bool thereIsNegative = false; + for (int j = 0; j < generalMatrix.getColumns()-1; ++j) { + if (generalMatrix[0][j] < 0) { + thereIsNegative = true; + } + } + if (thereIsNegative) { + for (int j = 0; j < generalMatrix.getColumns()-1; ++j) { + if (generalMatrix[0][j] > 0) { + if (generalMatrix[0][j] < (eps * (-1))) { + showMatrix(generalMatrix); + std::cout << generalMatrix[0][j] << std::endl; + return result; + } + } + } + } + } + if (maximize == false) { + bool thereIsPositive = false; + for (int j = 0; j < generalMatrix.getColumns()-1; ++j) { + if (generalMatrix[0][j] > 0) { + thereIsPositive = true; + } + } + if (thereIsPositive) { + for (int j = 0; j < generalMatrix.getColumns()-1; ++j) { + if (generalMatrix[0][j] > 0) { + if (generalMatrix[0][j] < eps) { + showMatrix(generalMatrix); + std::cout << generalMatrix[0][j] << std::endl; + return result; + } + } + } + } + } + + + } return result; diff --git a/tools/elimination.cpp b/tools/elimination.cpp index e3cbb1a..9b4ee4c 100644 --- a/tools/elimination.cpp +++ b/tools/elimination.cpp @@ -12,7 +12,7 @@ FracturedMatrix& FracturedMatrix::operator=(const FracturedMatrix& other) { return *this; } -DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix) { +DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix) { int rows = generalMatrix.getRows(); int cols = generalMatrix.getColumns(); diff --git a/tools/elimination.h b/tools/elimination.h index 67f4480..2c71100 100644 --- a/tools/elimination.h +++ b/tools/elimination.h @@ -22,7 +22,7 @@ struct DestroyMatrix { Vector b; }; -DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix); +DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix); void elimination(Matrix&, int pivot_row_index, int pivot_column_index); Matrix createGeneralMatrix(Matrix& A, Vector& C, Vector& b); diff --git a/tools/matrix.cpp b/tools/matrix.cpp index 26dcb80..cac1d78 100644 --- a/tools/matrix.cpp +++ b/tools/matrix.cpp @@ -1,5 +1,47 @@ #include "matrix.h" +void showMatrix(Matrix matrix) { + int maxNumberLength = 1; + for (size_t y = 0; y < matrix.getRows(); y++) { + for (size_t x = 0; x < matrix.getColumns(); x++) { + if (std::to_string(matrix[y][x]).length() > maxNumberLength) { + maxNumberLength = std::to_string(matrix[y][x]).length(); + } + } + } + //std::cout << maxNumberLength << std::endl; + std::cout << std::endl; + for (size_t y = 0; y < matrix.getRows(); y++) { + std::string row = ""; + for (size_t x = 0; x < matrix.getColumns(); x++) { + std::string strNumber = std::to_string(matrix[y][x]); + bool spaceRight = true; + while (strNumber.length() < maxNumberLength) { + if (spaceRight) { + strNumber += " "; + } + else { + strNumber = " " + strNumber; + } + spaceRight = !spaceRight; + strNumber = "" + strNumber; + + } + if (matrix[y][x] == 0) { + row += "[\033[0m" + strNumber + "\033[0m] "; + } + else if(matrix[y][x] > 0) { + row += "[\033[32m" + strNumber + "\033[0m] "; + }else { + row += "[\033[31m" + strNumber + "\033[0m] "; + } + + } + std::cout << row << std::endl; + } + std::cout << std::endl; +} + Vector::Vector(int n) { rows = n; columns = 1; diff --git a/tools/matrix.h b/tools/matrix.h index 6cc2983..af29d65 100644 --- a/tools/matrix.h +++ b/tools/matrix.h @@ -9,6 +9,8 @@ * Vector is a class to represent * a column vector with n rows. */ + + class Vector { protected: // Number of rows in vector @@ -95,4 +97,6 @@ public: friend std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj); }; +void showMatrix(Matrix); + #endif // TOOLS_MATRIX_H