enhanced math.cpp and made while loop for iterative evaluation

This commit is contained in:
emil28092005
2024-09-22 17:51:55 +03:00
parent 3cba9318ef
commit df12b44d35
3 changed files with 95 additions and 19 deletions
+31 -7
View File
@@ -1,6 +1,7 @@
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include "tools/matrix.h" #include "tools/matrix.h"
#include "tools/math.h"
enum solver_state { enum solver_state {
unbounded, unbounded,
@@ -10,7 +11,26 @@ enum solver_state {
struct Result { struct Result {
solver_state state; solver_state state;
ColumnVector *solution; 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) { 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 n = C.getRows();
int m = A.getColumns(); int m = A.getColumns();
ColumnVector ratio_vector(m); while (b[0] < eps) {
//3
int pivot_column_index = 0; int pivot_column_index = 0;
pivot_column_index = Math::max_index(C);
pivot_column_index = max(C); //4
ColumnVector ratio_vector(m);
for (int i = 0; i < m; i++) { for (int i = 0; i < m; i++) {
ratio_vector[i] = b[i] / A[i][pivot_column_index]; ratio_vector[i] = b[i] / A[i][pivot_column_index];
} }
int pivot_row_index = Math::min_index(ratio_vector);
int pivot_row_index = min(ratio_vector); //5
struct FracturedMatrix fractured_matrix();
fractured_matrix = eleminate(A, C, b, pivot_column_index, pivot_row_index);
}
eleminate(A, C, b, pivot_column_index, pivot_row_index);
/* /*
Result result; Result result;
std::vector<int> basicVars(A.getColumns() - A.getRows()); std::vector<int> basicVars(A.getColumns() - A.getRows());
+52 -4
View File
@@ -1,7 +1,7 @@
#include "math.h" #include "math.h"
#include "matrix.h" #include "matrix.h"
double Math::minVector(ColumnVector columnVector) { double Math::min(ColumnVector columnVector) {
double temp = columnVector[0]; double temp = columnVector[0];
for (int j = 0; j < columnVector.getColumns(); j++) { for (int j = 0; j < columnVector.getColumns(); j++) {
if (columnVector[j] < temp) { if (columnVector[j] < temp) {
@@ -11,7 +11,7 @@ double Math::minVector(ColumnVector columnVector) {
return temp; return temp;
} }
double Math::maxVector(ColumnVector columnVector) { double Math::max(ColumnVector columnVector) {
double temp = columnVector[0]; double temp = columnVector[0];
for (int j = 0; j < columnVector.getColumns(); j++) { for (int j = 0; j < columnVector.getColumns(); j++) {
if (columnVector[j] > temp) { if (columnVector[j] > temp) {
@@ -21,7 +21,31 @@ double Math::maxVector(ColumnVector columnVector) {
return temp; return temp;
} }
double Math::minArray(std::vector<double> 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<double> array) {
double temp = array[0]; double temp = array[0];
for (size_t i = 1; i < array.size(); i++) { for (size_t i = 1; i < array.size(); i++) {
if (array[i] < temp) { if (array[i] < temp) {
@@ -31,7 +55,7 @@ double Math::minArray(std::vector<double> array) {
return temp; return temp;
} }
double Math::maxArray(std::vector<double> array) { double Math::max(std::vector<double> array) {
double temp = array[0]; double temp = array[0];
for (size_t i = 1; i < array.size(); i++) { for (size_t i = 1; i < array.size(); i++) {
if (array[i] > temp) { if (array[i] > temp) {
@@ -40,3 +64,27 @@ double Math::maxArray(std::vector<double> array) {
} }
return temp; return temp;
} }
int Math::min_index(std::vector<double> 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<double> 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;
}
+8 -4
View File
@@ -7,10 +7,14 @@ class ColumnVector;
class Math { class Math {
public: public:
double minVector(ColumnVector columnVector); double min(ColumnVector columnVector);
double maxVector(ColumnVector columnVector); double max(ColumnVector columnVector);
double minArray(std::vector<double> array); int min_index(ColumnVector columnVector);
double maxArray(std::vector<double> array); int max_index(ColumnVector columnVector);
double min(std::vector<double> array);
double max(std::vector<double> array);
int min_index(std::vector<double> array);
int max_index(std::vector<double> array);
}; };
#endif // MATH_H #endif // MATH_H