Move general matrix to simplex and fix bugs

This commit is contained in:
Ilya Grigorev
2024-09-22 23:17:49 +05:00
parent 9cf37ec9e4
commit 98d5fc9da9
7 changed files with 101 additions and 46 deletions
+4 -24
View File
@@ -43,8 +43,9 @@ Matrix createGeneralMatrix(Matrix& A, Vector& C, Vector& b) {
generalMatrix[0][i] = C[i];
}
for (int j = 0; j < A.getRows(); j++) {
generalMatrix[j][A.getColumns()] = b[j];
// For objective function (j=0) the value is set to zero automatically
for (int j = 1; j < A.getRows() + 1; j++) {
generalMatrix[j][A.getColumns()] = b[j-1];
}
for (int i = 0; i < A.getRows(); i++) {
@@ -56,8 +57,7 @@ Matrix createGeneralMatrix(Matrix& A, Vector& C, Vector& b) {
return generalMatrix;
}
FracturedMatrix elimination(Matrix A, Vector C, Vector b, int pivot_column_index, int pivot_row_index) {
Matrix generalMatrix = createGeneralMatrix(A, C, b);
void elimination(Matrix& generalMatrix, int pivot_row_index, int pivot_column_index) {
int rows = generalMatrix.getRows();
int cols = generalMatrix.getColumns();
@@ -78,25 +78,5 @@ FracturedMatrix elimination(Matrix A, Vector C, Vector b, int pivot_column_index
generalMatrix[i][j] -= pivotColumnCoefficient * generalMatrix[pivot_row_index][j];
}
}
DestroyMatrix destroyedMatrix = destroyGeneralMatrix(generalMatrix);
pivot_column_index = max_index(destroyedMatrix.C);
Vector ratio_vector(A.getRows());
for (int i = 0; i < A.getRows(); i++) {
if (A[i][pivot_column_index] != 0) {
ratio_vector[i] = b[i] / A[i][pivot_column_index];
}
else {
ratio_vector[i] = 0;
}
}
pivot_row_index = min_index(ratio_vector);
return {A, C, b, pivot_column_index, pivot_row_index};
}
+1 -1
View File
@@ -23,7 +23,7 @@ struct DestroyMatrix {
};
DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix);
FracturedMatrix elimination(Matrix A, Vector C, Vector b, int pivot_column_index, int pivot_row_index);
void elimination(Matrix&, int pivot_row_index, int pivot_column_index);
Matrix createGeneralMatrix(Matrix& A, Vector& C, Vector& b);
+20
View File
@@ -20,6 +20,26 @@ double max(Vector vector) {
return temp;
}
int min_index_positive(Vector vector) {
int i = 0;
while (i < vector.size() && vector[i] <= 0) {
++i;
}
if (i >= vector.size()) {
throw std::runtime_error("No positive min found");
}
int temp_index = i;
double temp = vector[i];
for (int j = i + 1; j < vector.size(); j++) {
if (vector[j] < temp && vector[j] > 0) {
temp_index = j;
temp = vector[j];
}
}
return temp_index;
}
int min_index(Vector vector) {
int temp_index = 0;
double temp = vector[0];
+1
View File
@@ -7,6 +7,7 @@
double min(Vector vector);
double max(Vector vector);
int min_index(Vector vector);
int min_index_positive(Vector vector);
int max_index(Vector vector);
double min(std::vector<double> array);
double max(std::vector<double> array);