From 3cba9318efb3de4e6f79d7fa13fc1c6360095d4e Mon Sep 17 00:00:00 2001 From: emil28092005 <65846814+emil28092005@users.noreply.github.com> Date: Sat, 21 Sep 2024 20:22:36 +0300 Subject: [PATCH 1/2] make abstract steps of simplex evaluation before elimination process --- main.cpp | 29 +++++++++++++---------------- simplex.cpp | 26 +++++++++++++++++++++++++- simplex.h | 20 ++++++++++++++++++++ 3 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 simplex.h diff --git a/main.cpp b/main.cpp index df1ab18..b4eadcd 100644 --- a/main.cpp +++ b/main.cpp @@ -1,24 +1,21 @@ #include #include "tools/matrix.h" #include "tools/math.h" +#include "simplex.h" int main() { - int n, m; - std::cin >> n; - std::cin >> m; + ColumnVector C(6); + Matrix A(6, 4); + int Carr[C.getColumns()] = {-5, -4, 0, 0, 0, 0}; + for (int i = 0; i < C.getRows(); ++i) { + C[i] = Carr[i]; + } + 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}}; + ColumnVector b(A.getRows()); + b = {24, 6, 1, 2}; + std::cout << A << std::endl; + std::cout << C << std::endl; - ColumnVector C(n); - Matrix A(n, m); - ColumnVector b(n); - double eps; - double eps_default; - std::cin >> A; - - std::cout << "Вектор C: " << C << std::endl; - - std::cout << "Матрица A: " << A << std::endl; - - Matrix B(2, 2); - std::cout << B << std::endl; + Simplex(C, A, b, 0.01, true); return 0; } diff --git a/simplex.cpp b/simplex.cpp index c9a4b93..70e7c2f 100644 --- a/simplex.cpp +++ b/simplex.cpp @@ -13,7 +13,29 @@ struct Result { double objective_fucntion_value; }; -Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize) { +Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize=true) { + + std::cout << "C: " << C << std::endl; + std::cout << "A: " << A << std::endl; + std::cout << "b: " << b << std::endl; + + int n = C.getRows(); + int m = A.getColumns(); + + ColumnVector ratio_vector(m); + + int pivot_column_index = 0; + + pivot_column_index = max(C); + + for (int i = 0; i < m; i++) { + ratio_vector[i] = b[i] / A[i][pivot_column_index]; + } + + int pivot_row_index = min(ratio_vector); + + eleminate(A, C, b, pivot_column_index, pivot_row_index); + /* Result result; std::vector basicVars(A.getColumns() - A.getRows()); basicVars[0] = -1; @@ -43,7 +65,9 @@ Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool } } result.objective_fucntion_value = b[0]; + } + */ } /* diff --git a/simplex.h b/simplex.h new file mode 100644 index 0000000..d5fcd47 --- /dev/null +++ b/simplex.h @@ -0,0 +1,20 @@ +#ifndef SIMPLEX_H +#define SIMPLEX_H + +#include +#include "tools/matrix.h" + +enum solver_state { + unbounded, + bounded +}; + +struct Result { + solver_state state; + ColumnVector *solution; + double objective_fucntion_value; +}; + +Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize = true); + +#endif // SIMPLEX_H \ No newline at end of file From df12b44d354213ae5d0f7dc08298c0c9a0d8fe9e Mon Sep 17 00:00:00 2001 From: emil28092005 <65846814+emil28092005@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:51:55 +0300 Subject: [PATCH 2/2] enhanced math.cpp and made while loop for iterative evaluation --- simplex.cpp | 46 +++++++++++++++++++++++++++++++---------- tools/math.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++---- tools/math.h | 12 +++++++---- 3 files changed, 95 insertions(+), 19 deletions(-) diff --git a/simplex.cpp b/simplex.cpp index 70e7c2f..6dceb3a 100644 --- a/simplex.cpp +++ b/simplex.cpp @@ -1,6 +1,7 @@ #include #include #include "tools/matrix.h" +#include "tools/math.h" enum solver_state { unbounded, @@ -10,7 +11,26 @@ enum solver_state { struct Result { solver_state state; ColumnVector *solution; - double objective_fucntion_value; + double objective_function_value; +}; + +struct FracturedMatrix { + Matrix A; + ColumnVector C; + ColumnVector b; + int pivot_column_index; + int pivot_row_index; + FracturedMatrix(); + FracturedMatrix(const FracturedMatrix& other) : A(other.A), C(other.C), b(other.b), pivot_column_index(other.pivot_column_index), pivot_row_index(other.pivot_row_index) {} + FracturedMatrix(Matrix A, ColumnVector C, ColumnVector b, int pivot_column_index, int pivot_row_index) : A(A), C(C), b(b), pivot_column_index(pivot_column_index), pivot_row_index(pivot_row_index) {} + FracturedMatrix& FracturedMatrix::operator=(const FracturedMatrix& other) { + A = other.A; + C = other.C; + b = other.b; + pivot_column_index = other.pivot_column_index; + pivot_row_index = other.pivot_row_index; + return *this; + } }; Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize=true) { @@ -22,19 +42,23 @@ Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool int n = C.getRows(); int m = A.getColumns(); - ColumnVector ratio_vector(m); + while (b[0] < eps) { + //3 + int pivot_column_index = 0; + pivot_column_index = Math::max_index(C); - int pivot_column_index = 0; + //4 + ColumnVector ratio_vector(m); + for (int i = 0; i < m; i++) { + ratio_vector[i] = b[i] / A[i][pivot_column_index]; + } + int pivot_row_index = Math::min_index(ratio_vector); - pivot_column_index = max(C); + //5 + struct FracturedMatrix fractured_matrix(); + fractured_matrix = eleminate(A, C, b, pivot_column_index, pivot_row_index); + } - for (int i = 0; i < m; i++) { - ratio_vector[i] = b[i] / A[i][pivot_column_index]; - } - - int pivot_row_index = min(ratio_vector); - - eleminate(A, C, b, pivot_column_index, pivot_row_index); /* Result result; std::vector basicVars(A.getColumns() - A.getRows()); diff --git a/tools/math.cpp b/tools/math.cpp index 255124d..d6a5dae 100644 --- a/tools/math.cpp +++ b/tools/math.cpp @@ -1,7 +1,7 @@ #include "math.h" #include "matrix.h" -double Math::minVector(ColumnVector columnVector) { +double Math::min(ColumnVector columnVector) { double temp = columnVector[0]; for (int j = 0; j < columnVector.getColumns(); j++) { if (columnVector[j] < temp) { @@ -11,7 +11,7 @@ double Math::minVector(ColumnVector columnVector) { return temp; } -double Math::maxVector(ColumnVector columnVector) { +double Math::max(ColumnVector columnVector) { double temp = columnVector[0]; for (int j = 0; j < columnVector.getColumns(); j++) { if (columnVector[j] > temp) { @@ -21,7 +21,31 @@ double Math::maxVector(ColumnVector columnVector) { return temp; } -double Math::minArray(std::vector array) { +int Math::min_index(ColumnVector columnVector) { + int temp_index = 0; + double temp = columnVector[0]; + for (int j = 0; j < columnVector.getColumns(); j++) { + if (columnVector[j] < temp) { + temp_index = j; + temp = columnVector[j]; + } + } + return temp_index; +} + +int Math::max_index(ColumnVector columnVector) { + int temp_index = 0; + double temp = columnVector[0]; + for (int j = 0; j < columnVector.getColumns(); j++) { + if (columnVector[j] > temp) { + temp_index = j; + temp = columnVector[j]; + } + } + return temp_index; +} + +double Math::min(std::vector array) { double temp = array[0]; for (size_t i = 1; i < array.size(); i++) { if (array[i] < temp) { @@ -31,7 +55,7 @@ double Math::minArray(std::vector array) { return temp; } -double Math::maxArray(std::vector array) { +double Math::max(std::vector array) { double temp = array[0]; for (size_t i = 1; i < array.size(); i++) { if (array[i] > temp) { @@ -40,3 +64,27 @@ double Math::maxArray(std::vector array) { } return temp; } + +int Math::min_index(std::vector array) { + int temp_index = 0; + double temp = array[0]; + for (size_t i = 1; i < array.size(); i++) { + if (array[i] < temp) { + temp_index = i; + temp = array[i]; + } + } + return temp_index; +} + +int Math::max_index(std::vector array) { + int temp_index = 0; + double temp = array[0]; + for (size_t i = 1; i < array.size(); i++) { + if (array[i] > temp) { + temp_index = i; + temp = array[i]; + } + } + return temp_index; +} \ No newline at end of file diff --git a/tools/math.h b/tools/math.h index b641471..1afd176 100644 --- a/tools/math.h +++ b/tools/math.h @@ -7,10 +7,14 @@ class ColumnVector; class Math { public: - double minVector(ColumnVector columnVector); - double maxVector(ColumnVector columnVector); - double minArray(std::vector array); - double maxArray(std::vector array); + double min(ColumnVector columnVector); + double max(ColumnVector columnVector); + int min_index(ColumnVector columnVector); + int max_index(ColumnVector columnVector); + double min(std::vector array); + double max(std::vector array); + int min_index(std::vector array); + int max_index(std::vector array); }; #endif // MATH_H \ No newline at end of file