diff --git a/Makefile b/Makefile index 4fe2380..f9ef662 100644 --- a/Makefile +++ b/Makefile @@ -36,3 +36,32 @@ test: simplex.out clean: rm -rf $(BUILD) + rm -rf simplex.out + +rebuild: + rm -rf $(BUILD) + rm -rf simplex.out + + mkdir -p $(BUILD) + $(GXX) -c $(flags) $(TOOLS)/elimination.cpp -o $(BUILD)/elimination.obj + $(GXX) -c $(flags) $(TOOLS)/math.cpp -o $(BUILD)/math.obj + $(GXX) -c $(flags) $(TOOLS)/matrix.cpp -o $(BUILD)/matrix.obj + $(GXX) -c $(flags) simplex.cpp -o $(BUILD)/simplex.obj + $(GXX) -c $(flags) main.cpp -o $(BUILD)/main.obj + + $(GXX) $(BUILD)/*.obj $(flags) -o simplex.out + + $(BUILD)/elimination.obj: $(TOOLS)/elimination.cpp + $(GXX) -c $(flags) $(TOOLS)/elimination.cpp -o $(BUILD)/elimination.obj + + $(BUILD)/math.obj: $(TOOLS)/math.cpp + $(GXX) -c $(flags) $(TOOLS)/math.cpp -o $(BUILD)/math.obj + + $(BUILD)/matrix.obj: $(TOOLS)/matrix.cpp + $(GXX) -c $(flags) $(TOOLS)/matrix.cpp -o $(BUILD)/matrix.obj + + $(BUILD)/simplex.obj: simplex.cpp + $(GXX) -c $(flags) simplex.cpp -o $(BUILD)/simplex.obj + + $(BUILD)/main.obj: main.cpp + $(GXX) -c $(flags) main.cpp -o $(BUILD)/main.obj diff --git a/main.cpp b/main.cpp index e91beae..785d88d 100644 --- a/main.cpp +++ b/main.cpp @@ -4,8 +4,36 @@ #include "tools/math.h" #include "simplex.h" -bool check_eq(double a, double b, - double relativeEpsilon = 0.0001) { + +int printResult(Result result) { + + if (result.state == unsolvable){ + std::cout << "The method is not applicable!" << std::endl; + }else{ + std::cout << "SOLVED!" << std::endl; + std::cout << "Decision variables: ["; + for (size_t i = 0; i < result.solution.size(); i++) + { + std::cout << result.solution[i]; + if (i != result.solution.size()-1){ + std::cout << ", "; + } + } + std::cout << "]"; + std::cout << std::endl; + if (result.maximize) + { + std::cout << "Maximum "; + }else{ + std::cout << "Minimum "; + } + std::cout << "objective function value: " << result.objective_function_value << std::endl; + } + + return 0; +} + +bool check_eq(double a, double b, double relativeEpsilon = 0.0001) { double diff = std::abs(a - b); a = std::abs(a); b = std::abs(b); @@ -46,17 +74,19 @@ int TEST_GENERAL_CASE() { } if (!check_eq(result.objective_function_value, 21)) { - std::cout << "Incorrect objective function value. Expected 21. Got " + std::cout << "Incorrect objective function value. Expected 21. Got " //ЭТО УДАЛИТЬ? << result.objective_function_value << std::endl; return 0; } if (!( check_eq(result.solution[0],3) && check_eq(result.solution[1], 1.5) )) { - std::cout << "Incorrect desire variables. Expected 3 and 1.5. Got " + std::cout << "Incorrect desire variables. Expected 3 and 1.5. Got " //ЭТО УДАЛИТЬ? << result.solution; return 0; } + printResult(result); + return 1; } @@ -104,6 +134,8 @@ int TEST_MINIMIZE_CASE() { return 0; } + printResult(result); + return 1; } @@ -149,6 +181,9 @@ int TEST_WITH_SLACK_CASE() { << result.solution[0] << result.solution[1]; return 0; } + + printResult(result); + return 1; } @@ -181,6 +216,8 @@ int TEST_UNBOUNDED_CASE() { std::cout << "Incorrect state type. Expected unbounded. Got " << state_name << std::endl; return 0; } + + printResult(result); return 1; } @@ -215,10 +252,14 @@ int TEST_UNSOLVABLE_CASE() { std::cout << "Incorrect state type. Expected unsolvable. Got " << state_name << std::endl; return 0; } + + printResult(result); return 1; } + + int main() { std::vector> tests = { diff --git a/simplex.cpp b/simplex.cpp index f55f4ef..3006ef4 100644 --- a/simplex.cpp +++ b/simplex.cpp @@ -14,6 +14,7 @@ struct Result { solver_state state; Vector solution; double objective_function_value; + bool maximize; }; void _printInitialInputs(Vector& C, Matrix& A, Vector& b) { @@ -56,8 +57,10 @@ Result simplex(Vector& C, Matrix& A, Vector& b, double eps = 0.01, bool maximize } } Result result{}; + result.maximize = maximize; + Matrix generalMatrix = createGeneralMatrix(A, C, b); - std::cout << "Before:" << std::endl << generalMatrix; + std::cout << generalMatrix; std::vector basicVars(generalMatrix.getRows()); basicVars[0] = -1; @@ -87,8 +90,7 @@ Result simplex(Vector& C, Matrix& A, Vector& b, double eps = 0.01, bool maximize return result; } - std::cout << "Iteration: "; - std::cout << iterationCount << std::endl; + //4 Vector ratio_vector(generalMatrix.getRows()); @@ -116,9 +118,11 @@ Result simplex(Vector& C, Matrix& A, Vector& b, double eps = 0.01, bool maximize //5 elimination(generalMatrix, pivot_row_index, pivot_column_index); - std::cout << "After:" << std::endl << generalMatrix; + std::cout << "Iteration "<< iterationCount << " " << std::endl;; + std::cout << generalMatrix; } - + //result.state = solved; + //std::cout << result.state; return result; } diff --git a/simplex.h b/simplex.h index 283eddd..cdd5f12 100644 --- a/simplex.h +++ b/simplex.h @@ -14,6 +14,7 @@ struct Result { solver_state state; Vector solution; double objective_function_value; + bool maximize; }; void _printInitialInputs(Vector& C, Matrix& A, Vector& b);