update test cases and format code

This commit is contained in:
Ilya Grigorev
2024-10-09 14:09:48 +05:00
parent a41c15f3ee
commit 60bd99e570
8 changed files with 485 additions and 363 deletions
+133 -123
View File
@@ -4,94 +4,91 @@
#include "tools/math.h"
#include "simplex.h"
void _printInitialInputs(Vector& C, Matrix& A, Vector& b, double eps, bool maximize) {
std::cout << "Running for the following inputs:" << std::endl << std::endl;
void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maximize)
{
std::cout << "Running for the following inputs:" << std::endl
<< std::endl;
std::cout << "epsilon: " << eps << std::endl;
if (maximize)
{
std::cout << "Maximize" << std::endl;
}
else {
else
{
std::cout << "Minimize" << std::endl;
}
std::cout << "z = ";
bool previousIsNegative = false;
for (size_t i = 0; i < C.size(); i++)
for (int i = 0; i < C.size(); i++)
{
bool isNegative = false;
if (C[i] != 0){
if (i != 0 && !previousIsNegative){
std::cout << " + ";
if (i != 0)
{
std::cout << " + ";
}
if (C[i] != 1)
{
if (C[i] < 0)
{
isNegative = true;
std::cout << "(";
}
if (C[i] != 1) {
if (C[i] < 0){
isNegative = true;
std::cout << "(";
}
std::cout << C[i] << " * ";
}
std::cout << "x" << i + 1;
if (isNegative){
std::cout << ")";
}
}else {
previousIsNegative = true;
std::cout << C[i] << " * ";
}
std::cout << "x" << i + 1;
if (isNegative)
{
std::cout << ")";
}
}
std::cout << std::endl << "subject to the constrains:" << std::endl;
std::cout<< std::endl;
for (size_t i = 0; i < b.size(); i++)
std::cout << std::endl
<< "subject to the constrains:" << std::endl;
std::cout << std::endl;
for (int i = 0; i < b.size(); i++)
{
bool previousIsZero = false;
for (size_t j = 0; j < A.getColumns(); j++)
for (int j = 0; j < A.getColumns(); j++)
{
bool isNegative = false;
if (A[i][j] != 0) {
if (j != 0 && !previousIsZero){
std::cout << " + ";
previousIsZero = false;
if (j != 0)
{
std::cout << " + ";
}
if (A[i][j] != 1)
{
if (A[i][j] < 0)
{
isNegative = true;
std::cout << "(";
}
if (A[i][j] != 1) {
if (A[i][j] < 0) {
isNegative = true;
std::cout << "(";
}
std::cout << A[i][j] << " * ";
}
std::cout << "x" << j + 1;
if (isNegative) {
std::cout << ")";
}
}else {
previousIsZero = true;
std::cout << A[i][j] << " * ";
}
std::cout << "x" << j + 1;
if (isNegative)
{
std::cout << ")";
}
}
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;
}else {
}
else
{
std::cout << "SOLVED!" << std::endl;
std::cout << "Decision variables: [";
for (size_t i = 0; i < result.solution.size(); i++)
for (int i = 0; i < result.solution.size(); i++)
{
std::cout << result.solution[i];
if (i != result.solution.size()-1){
if (i != result.solution.size() - 1)
{
std::cout << ", ";
}
}
@@ -100,16 +97,19 @@ int printResult(Result result) {
if (result.maximize)
{
std::cout << "Maximum ";
}else{
}
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) {
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);
@@ -118,7 +118,8 @@ bool check_eq(double a, double b, double relativeEpsilon = 0.0001) {
return diff <= largest * relativeEpsilon;
}
int TEST_GENERAL_CASE() {
int TEST_GENERAL_CASE()
{
std::cout << "----------------------------RUNNING_TEST_GENERAL_CASE----------------------------" << std::endl;
Vector C = {5, 4};
@@ -126,16 +127,17 @@ int TEST_GENERAL_CASE() {
{6, 4},
{1, 2},
{-1, 1},
{0, 1}
};
{0, 1}};
Vector b = {24, 6, 1, 2};
_printInitialInputs(C, A, b, 0.01, true);
auto result = simplex(C, A, b);
if (!(result.state == bounded)) {
if (!(result.state == bounded))
{
std::string state_name;
switch (result.state) {
switch (result.state)
{
case unsolvable:
state_name = "unsolvable";
break;
@@ -150,40 +152,44 @@ int TEST_GENERAL_CASE() {
return 0;
}
if (!check_eq(result.objective_function_value, 21)) {
std::cout << "Incorrect objective function value. Expected 21. Got " //ЭТО УДАЛИТЬ?
if (!check_eq(result.objective_function_value, 21))
{
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 " //ЭТО УДАЛИТЬ?
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 " // ЭТО УДАЛИТЬ?
<< result.solution;
return 0;
}
printResult(result);
return 1;
}
int TEST_MINIMIZE_CASE() {
int TEST_MINIMIZE_CASE()
{
std::cout << "----------------------------RUNNING_TEST_MINIMIZE_CASE----------------------------" << std::endl;
Vector C = {-2, 2, -6};
Matrix A = {
{2, 1, -2},
{1, 2, 4},
{1, -1, 2}
};
{1, -1, 2}};
Vector b = {24, 23, 10};
_printInitialInputs(C, A, b, 0.01, false);
auto result = simplex(C, A, b, 0.01, false);
if (!(result.state == bounded)) {
if (!(result.state == bounded))
{
std::string state_name;
switch (result.state) {
switch (result.state)
{
case unsolvable:
state_name = "unsolvable";
break;
@@ -198,15 +204,15 @@ int TEST_MINIMIZE_CASE() {
return 0;
}
if (!check_eq(result.objective_function_value, -30.75)) {
if (!check_eq(result.objective_function_value, -30.75))
{
std::cout << "Incorrect objective function value. Expected -30.75. Got "
<< result.objective_function_value << std::endl;
return 0;
}
if (!( check_eq(result.solution[0], 0) && check_eq(result.solution[1], 0.75)
&& check_eq(result.solution[2],5.375)
)) {
if (!(check_eq(result.solution[0], 0) && check_eq(result.solution[1], 0.75) && check_eq(result.solution[2], 5.375)))
{
std::cout << "Incorrect desire variables. Expected 3 and 1.5. Got "
<< result.solution;
return 0;
@@ -217,25 +223,26 @@ int TEST_MINIMIZE_CASE() {
return 1;
}
int TEST_WITH_SLACK_CASE() {
int TEST_WITH_SLACK_CASE()
{
std::cout << "----------------------------RUNNING_TEST_WITH_SLACK_CASE----------------------------" << std::endl;
Vector C = {5, 4};
Vector C = {2, -1, 0, -1};
Matrix A = {
{6, 4, 1},
{1, 2, 0},
{-1, 1, 0},
{0, 1, 0}
};
Vector b = {24, 6, 1, 2};
{1, -2, 1, 0},
{-2, -1, 0, -2},
{3, 2, 0, 1}};
Vector b = {10, 18, 36};
_printInitialInputs(C, A, b, 0.01, true);
auto result = simplex(C, A, b);
if (!(result.state == bounded)) {
if (!(result.state == bounded))
{
std::string state_name;
switch (result.state) {
switch (result.state)
{
case unsolvable:
state_name = "unsolvable";
break;
@@ -250,41 +257,45 @@ int TEST_WITH_SLACK_CASE() {
return 0;
}
if (result.objective_function_value != 21) {
std::cout << "Incorrect objective function value. Expected 21. Got "
if (result.objective_function_value != 22.25)
{
std::cout << "Incorrect objective function value. Expected 22.25. Got "
<< result.objective_function_value << std::endl;
return 0;
}
if (!((result.solution[0] == 3) || (result.solution[1] == 1.5))) {
std::cout << "Incorrect desire variables. Expected 3 and 1.5. Got "
<< result.solution[0] << result.solution[1];
if (!((result.solution[0] == 11.5) || (result.solution[1] == 0.75) ||
(result.solution[2] == 0) || (result.solution[3] == 0)))
{
std::cout << "Incorrect desire variables. Expected 11.5, 0.75, 0, and 0. Got "
<< result.solution;
return 0;
}
printResult(result);
printResult(result);
return 1;
}
int TEST_UNBOUNDED_CASE() {
int TEST_UNBOUNDED_CASE()
{
std::cout << "----------------------------RUNNING_TEST_UNBOUNDED_CASE----------------------------" << std::endl;
Vector C = {2, 1};
Matrix A = {
{1, -1},
{2, 0}
};
{2, 0}};
Vector b = {10, 40};
_printInitialInputs(C, A, b, 0.01, true);
auto result = simplex(C, A, b);
if (!(result.state == unbounded)) {
if (!(result.state == unbounded))
{
std::string state_name;
switch (result.state) {
switch (result.state)
{
case unsolvable:
state_name = "unsolvable";
break;
@@ -298,31 +309,33 @@ int TEST_UNBOUNDED_CASE() {
std::cout << "Incorrect state type. Expected unbounded. Got " << state_name << std::endl;
return 0;
}
printResult(result);
return 1;
}
int TEST_UNSOLVABLE_CASE() {
int TEST_UNSOLVABLE_CASE()
{
std::cout << "----------------------------RUNNING_TEST_UNSOLVABLE_CASE----------------------------" << std::endl;
Vector C = {5, 4};
Vector C = {5, 4, 0, -5, 13};
Matrix A = {
{6, 4},
{1, 2},
{-1, 1},
{0, 1}
};
{6, 4, 1, 3, 4},
{1, 2, 0, 0, 2},
{-1, 0, 0, 10, 0},
{0, 1, 1, -5, 1}};
Vector b = {-24, 6, 1, 2};
_printInitialInputs(C, A, b, 0.01, true);
auto result = simplex(C, A, b);
if (!(result.state == unsolvable)) {
if (!(result.state == unsolvable))
{
std::string state_name;
switch (result.state) {
switch (result.state)
{
case unsolvable:
state_name = "unsolvable";
break;
@@ -336,27 +349,26 @@ int TEST_UNSOLVABLE_CASE() {
std::cout << "Incorrect state type. Expected unsolvable. Got " << state_name << std::endl;
return 0;
}
printResult(result);
return 1;
}
int main() {
int main()
{
std::vector<std::function<int(void)>> tests = {
TEST_GENERAL_CASE,
TEST_MINIMIZE_CASE,
TEST_WITH_SLACK_CASE,
TEST_UNBOUNDED_CASE,
TEST_UNSOLVABLE_CASE
};
TEST_UNSOLVABLE_CASE};
int counter = 0;
for (auto& test : tests) {
for (auto &test : tests)
{
counter += test();
}
@@ -366,5 +378,3 @@ int main() {
return 0;
}