Make appropriate outputting.
Fix Makefile and add optional rebuild shorcut function. Fix outputs in simplex.cpp
This commit is contained in:
@@ -36,3 +36,32 @@ test: simplex.out
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(BUILD)
|
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
|
||||||
|
|||||||
@@ -4,8 +4,36 @@
|
|||||||
#include "tools/math.h"
|
#include "tools/math.h"
|
||||||
#include "simplex.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);
|
double diff = std::abs(a - b);
|
||||||
a = std::abs(a);
|
a = std::abs(a);
|
||||||
b = std::abs(b);
|
b = std::abs(b);
|
||||||
@@ -46,17 +74,19 @@ int TEST_GENERAL_CASE() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!check_eq(result.objective_function_value, 21)) {
|
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;
|
<< result.objective_function_value << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!( check_eq(result.solution[0],3) && check_eq(result.solution[1], 1.5) )) {
|
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;
|
<< result.solution;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printResult(result);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,6 +134,8 @@ int TEST_MINIMIZE_CASE() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printResult(result);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,6 +182,9 @@ int TEST_WITH_SLACK_CASE() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printResult(result);
|
||||||
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,6 +217,8 @@ int TEST_UNBOUNDED_CASE() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printResult(result);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,9 +253,13 @@ int TEST_UNSOLVABLE_CASE() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printResult(result);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
std::vector<std::function<int(void)>> tests = {
|
std::vector<std::function<int(void)>> tests = {
|
||||||
|
|||||||
+9
-5
@@ -14,6 +14,7 @@ struct Result {
|
|||||||
solver_state state;
|
solver_state state;
|
||||||
Vector solution;
|
Vector solution;
|
||||||
double objective_function_value;
|
double objective_function_value;
|
||||||
|
bool maximize;
|
||||||
};
|
};
|
||||||
|
|
||||||
void _printInitialInputs(Vector& C, Matrix& A, Vector& b) {
|
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 result{};
|
||||||
|
result.maximize = maximize;
|
||||||
|
|
||||||
Matrix generalMatrix = createGeneralMatrix(A, C, b);
|
Matrix generalMatrix = createGeneralMatrix(A, C, b);
|
||||||
std::cout << "Before:" << std::endl << generalMatrix;
|
std::cout << generalMatrix;
|
||||||
std::vector<int> basicVars(generalMatrix.getRows());
|
std::vector<int> basicVars(generalMatrix.getRows());
|
||||||
basicVars[0] = -1;
|
basicVars[0] = -1;
|
||||||
|
|
||||||
@@ -87,8 +90,7 @@ Result simplex(Vector& C, Matrix& A, Vector& b, double eps = 0.01, bool maximize
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Iteration: ";
|
|
||||||
std::cout << iterationCount << std::endl;
|
|
||||||
|
|
||||||
//4
|
//4
|
||||||
Vector ratio_vector(generalMatrix.getRows());
|
Vector ratio_vector(generalMatrix.getRows());
|
||||||
@@ -116,9 +118,11 @@ Result simplex(Vector& C, Matrix& A, Vector& b, double eps = 0.01, bool maximize
|
|||||||
|
|
||||||
//5
|
//5
|
||||||
elimination(generalMatrix, pivot_row_index, pivot_column_index);
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ struct Result {
|
|||||||
solver_state state;
|
solver_state state;
|
||||||
Vector solution;
|
Vector solution;
|
||||||
double objective_function_value;
|
double objective_function_value;
|
||||||
|
bool maximize;
|
||||||
};
|
};
|
||||||
|
|
||||||
void _printInitialInputs(Vector& C, Matrix& A, Vector& b);
|
void _printInitialInputs(Vector& C, Matrix& A, Vector& b);
|
||||||
|
|||||||
Reference in New Issue
Block a user