add test coverage and fix input

This commit is contained in:
Ilya Grigorev
2024-10-08 00:57:25 +05:00
parent bf714eed92
commit 8007275709
9 changed files with 389 additions and 244 deletions
+55 -12
View File
@@ -18,14 +18,14 @@ DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix) {
Matrix A(rows - 1, cols - 1);
Vector C(cols - 1);
Vector b(rows - 1);
Vector b(rows);
for (int j = 0; j < cols - 1; ++j) {
C[j] = generalMatrix[0][j];
}
for (int i = 1; i < rows; ++i) {
b[i - 1] = generalMatrix[i - 1][cols - 1];
for (int i = 0; i < rows; ++i) {
b[i] = generalMatrix[i][cols - 1];
}
for (int i = 1; i < rows; ++i) {
@@ -38,19 +38,62 @@ DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix) {
}
Matrix createGeneralMatrix(Matrix& A, Vector& C, Vector& b) {
Matrix generalMatrix(A.getRows() + 1, A.getColumns() + 1);
for (int i = 0; i < A.getColumns(); i++) {
generalMatrix[0][i] = C[i];
int m = A.getRows() + 1;
int n = A.getColumns() + 1;
// States if equation has a slack variable
std::vector<bool> has_slack(A.getColumns(), false);
int number_of_slack = 0;
for (int j = 0; j < A.getColumns(); ++j) {
int basic_var_index = 0;
int ones = 0;
int zeros = 0;
for (int i = 0; i < A.getRows(); ++i) {
if (A[i][j] == 1) {
basic_var_index = i;
++ones;
} else if (A[i][j] == 0) {
++zeros;
}
}
if (ones + zeros == A.getRows()) {
has_slack[basic_var_index] = true;
number_of_slack += 1;
}
}
// 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];
n = n + A.getRows() - number_of_slack;
Matrix generalMatrix(m, n);
for (int i = 0; i < n; i++) {
if (i < C.size()) {
generalMatrix[0][i] = C[i];
} else {
generalMatrix[0][i] = 0;
}
}
for (int i = 0; i < A.getRows(); i++) {
for (int j = 0; j < A.getColumns(); j++) {
generalMatrix[i + 1][j] = A[i][j];
// For objective function (j=0) the value is set to zero one step before
for (int j = 1; j < m; j++) {
generalMatrix[j][n-1] = b[j-1];
}
int k = 0;
for (int i = 0; i < m - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (j < A.getColumns()) {
generalMatrix[i + 1][j] = A[i][j];
} else {
generalMatrix[i + 1][j] = 0;
}
}
if (!has_slack[i]) {
generalMatrix[i + 1][A.getColumns() + k] = 1;
++k;
}
}
+2 -1
View File
@@ -26,7 +26,8 @@ int min_index_positive(Vector vector) {
++i;
}
if (i >= vector.size()) {
throw std::runtime_error("No positive min found");
// No positive value found -> iterations stop
return -1;
}
int temp_index = i;
double temp = vector[i];
+37 -49
View File
@@ -1,47 +1,5 @@
#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;
@@ -263,15 +221,45 @@ std::istream& operator>>(std::istream& cin, Matrix& matrixObj) {
}
std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj) {
for (int i = 0; i < matrixObj.rows; ++i) {
for (int j = 0; j < matrixObj.columns; ++j) {
if (j == matrixObj.columns - 1) {
cout << matrixObj[i][j] << std::endl;
}
else {
cout << matrixObj[i][j] << ' ';
size_t maxNumberLength = 1;
for (int y = 0; y < matrixObj.getRows(); y++) {
for (int x = 0; x < matrixObj.getColumns(); x++) {
if (std::to_string(matrixObj[y][x]).length() > maxNumberLength) {
maxNumberLength = std::to_string(matrixObj[y][x]).length();
}
}
}
//std::cout << maxNumberLength << std::endl;
std::cout << std::endl;
for (int y = 0; y < matrixObj.getRows(); y++) {
std::string row = "";
for (int x = 0; x < matrixObj.getColumns(); x++) {
std::string strNumber = std::to_string(matrixObj[y][x]);
bool spaceRight = true;
while (strNumber.length() < maxNumberLength) {
if (spaceRight) {
strNumber += " ";
}
else {
strNumber = " " + strNumber;
}
spaceRight = !spaceRight;
strNumber = "" + strNumber;
}
if (matrixObj[y][x] == 0) {
row += "[\033[0m" + strNumber + "\033[0m] ";
}
else if(matrixObj[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;
return cout;
}
-2
View File
@@ -97,6 +97,4 @@ public:
friend std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj);
};
void showMatrix(Matrix);
#endif // TOOLS_MATRIX_H