Make some linting

This commit is contained in:
emil
2024-10-10 02:17:54 +03:00
parent 0920abc31b
commit d8735c3d44
2 changed files with 5 additions and 55 deletions
+4 -16
View File
@@ -22,7 +22,6 @@ void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maxim
bool lastNonZero = false; bool lastNonZero = false;
for (int i = 0; i < C.size(); i++) for (int i = 0; i < C.size(); i++)
{ {
bool isNegative = false; bool isNegative = false;
for (int k = i; k < C.size(); k++) for (int k = i; k < C.size(); k++)
@@ -41,8 +40,6 @@ void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maxim
std::cout << " + "; std::cout << " + ";
} }
if (C[i] != 0){ if (C[i] != 0){
if (C[i] != 1) { if (C[i] != 1) {
if (C[i] < 0){ if (C[i] < 0){
@@ -68,6 +65,7 @@ void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maxim
{ {
bool previousIsZero = true; bool previousIsZero = true;
bool lastNonZero = false; bool lastNonZero = false;
for (int j = 0; j < A.getColumns(); j++) for (int j = 0; j < A.getColumns(); j++)
{ {
bool isNegative = false; bool isNegative = false;
@@ -88,9 +86,6 @@ void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maxim
std::cout << " + "; std::cout << " + ";
} }
if (A[i][j] != 0) { if (A[i][j] != 0) {
if (A[i][j] != 1) { if (A[i][j] != 1) {
if (A[i][j] < 0) { if (A[i][j] < 0) {
@@ -108,24 +103,24 @@ void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maxim
}else { }else {
previousIsZero = true; previousIsZero = true;
} }
} }
std::cout << " <= " << b[i] << std::endl; std::cout << " <= " << b[i] << std::endl;
} }
} }
int printResult(Result result) int printResult(Result result)
{ {
if (result.state == unsolvable) if (result.state == unsolvable)
{ {
std::cout << "The method is not applicable!" << std::endl; std::cout << "The method is not applicable!" << std::endl;
}else if (result.state == unbounded ) {
std::cout << "Unbounded problem!" << std::endl;
} }
else else
{ {
std::cout << "SOLVED!" << std::endl; std::cout << "SOLVED!" << std::endl;
std::cout << "Decision variables: ["; std::cout << "Decision variables: [";
for (int i = 0; i < result.solution.size(); i++) for (int i = 0; i < result.solution.size(); i++)
{ {
std::cout << result.solution[i]; std::cout << result.solution[i];
@@ -146,7 +141,6 @@ int printResult(Result result)
} }
std::cout << "objective function value: " << result.objective_function_value << std::endl; std::cout << "objective function value: " << result.objective_function_value << std::endl;
} }
return 0; return 0;
} }
@@ -163,7 +157,6 @@ bool check_eq(double a, double b, double relativeEpsilon = 0.0001)
int TEST_GENERAL_CASE() int TEST_GENERAL_CASE()
{ {
std::cout << "----------------------------RUNNING_TEST_GENERAL_CASE----------------------------" << std::endl; std::cout << "----------------------------RUNNING_TEST_GENERAL_CASE----------------------------" << std::endl;
Vector C = {5, 4}; Vector C = {5, 4};
Matrix A = { Matrix A = {
{6, 4}, {6, 4},
@@ -172,7 +165,6 @@ int TEST_GENERAL_CASE()
{0, 1}}; {0, 1}};
Vector b = {24, 6, 1, 2}; Vector b = {24, 6, 1, 2};
_printInitialInputs(C, A, b, 0.01, true); _printInitialInputs(C, A, b, 0.01, true);
auto result = simplex(C, A, b); auto result = simplex(C, A, b);
if (!(result.state == bounded)) if (!(result.state == bounded))
@@ -209,14 +201,12 @@ int TEST_GENERAL_CASE()
} }
printResult(result); printResult(result);
return 1; return 1;
} }
int TEST_MINIMIZE_CASE() int TEST_MINIMIZE_CASE()
{ {
std::cout << "----------------------------RUNNING_TEST_MINIMIZE_CASE----------------------------" << std::endl; std::cout << "----------------------------RUNNING_TEST_MINIMIZE_CASE----------------------------" << std::endl;
Vector C = {-2, 2, -6}; Vector C = {-2, 2, -6};
Matrix A = { Matrix A = {
{2, 1, -2}, {2, 1, -2},
@@ -224,7 +214,6 @@ int TEST_MINIMIZE_CASE()
{1, -1, 2}}; {1, -1, 2}};
Vector b = {24, 23, 10}; Vector b = {24, 23, 10};
_printInitialInputs(C, A, b, 0.01, false); _printInitialInputs(C, A, b, 0.01, false);
auto result = simplex(C, A, b, 0.01, false); auto result = simplex(C, A, b, 0.01, false);
if (!(result.state == bounded)) if (!(result.state == bounded))
@@ -399,7 +388,6 @@ int TEST_UNSOLVABLE_CASE()
int main() int main()
{ {
std::vector<std::function<int(void)>> tests = { std::vector<std::function<int(void)>> tests = {
TEST_GENERAL_CASE, TEST_GENERAL_CASE,
TEST_MINIMIZE_CASE, TEST_MINIMIZE_CASE,
-38
View File
@@ -142,41 +142,3 @@ Result simplex(Vector &C, Matrix &A, Vector &b, double eps = 0.01, bool maximize
} }
return result; return result;
} }
/*
Function_name(C, A, b, eps = eps_default)
Input:
- C: A vector of coefficients of the objective function
- A: A matrix of coefficients of the constraint functions
- b: A vector of right-hand side values
- eps: Approximation accuracy (optional, default = eps_default)
Steps:
1. Print the optimization problem:
- max (or min) z = C[0] * x1 + C[1] * x2 + ... + C[n] * xn
- subject to the constraints:
- A[0] * x <= b[0]
- A[1] * x <= b[1]
- ...
- A[m] * x <= b[m]
2. Initialize:
- Form the initial tableau by introducing slack variables to convert inequalities into equalities.
3. Iteratively apply the Simplex method:
- Step 1: Identify the entering variable (most negative coefficient in the objective row).
- Step 2: Identify the leaving variable (smallest positive ratio of RHS to pivot column).
- Step 3: Perform pivot operations to update the tableau.
4. Check for optimality or unboundedness:
- If all coefficients in the objective function row are non-negative, the solution is optimal.
- If no leaving variable exists, the problem is unbounded.
5. Return:
- solver_state: {solved, unbounded}
- x*: Optimal vector of decision variables (if solved)
- z: Maximum (or minimum) value of the objective function (if solved)
End Function
*/