51#include <gtest/gtest.h>
76 return std::fabs(a - b) < eps;
145 double coefs[] = {5.0, 4.0, 3.0};
175 ASSERT_NE(ptr,
nullptr);
176 EXPECT_DOUBLE_EQ(ptr[0], 7.0);
186 double rest[] = {1.0, 2.0, 10.0};
196 double r1[] = {1.0, 1.0, 4.0};
197 double r2[] = {2.0, 1.0, 5.0};
198 double r3[] = {1.0, 2.0, 6.0};
213 ASSERT_NE(rest,
nullptr);
217 EXPECT_DOUBLE_EQ(rest[0], 0.0);
218 EXPECT_DOUBLE_EQ(rest[1], 0.0);
219 EXPECT_DOUBLE_EQ(rest[2], 0.0);
232 EXPECT_DOUBLE_EQ(retrieved[0], 3.0);
233 EXPECT_DOUBLE_EQ(retrieved[1], 4.0);
234 EXPECT_DOUBLE_EQ(retrieved[2], 12.0);
240 double rest[] = {1.0, 1.0, 4.0};
250 double rest[] = {1.0, 2.0, 3.0, 10.0};
303 double rest[] = {1.0, 1.0, 4.0};
306 EXPECT_THROW(simplex.
solve(), std::logic_error);
314 double rest[] = {1.0, 1.0, 4.0};
320 EXPECT_THROW(simplex.
solve(), std::logic_error);
332 double labor[] = {1.0, 1.0, 40.0};
333 double machine[] = {2.0, 1.0, 60.0};
334 double extra[] = {1.0, 0.0, 30.0};
336 for (
size_t i = 0; i < 3; ++i)
337 extra_array[i] = extra[i];
342 auto expect_no_restriction_is_added = [&]
352 expect_no_restriction_is_added();
354 expect_no_restriction_is_added();
357 EXPECT_TRUE(nearly_equal(simplex.
get_solution(0), 20.0));
358 EXPECT_TRUE(nearly_equal(simplex.
get_solution(1), 20.0));
368 auto message_of = [](
auto && op) -> std::string
374 catch (
const std::out_of_range & e)
383 .
find(
"Variable index 2 out of range [0, 2)"), std::string::npos);
385 .
find(
"Restriction index 0 out of range [0, 0)"), std::string::npos);
389 .
find(
"Variable index 0 out of range [0, 0)"), std::string::npos);
410 double c1[] = {1.0, 1.0, 4.0};
411 double c2[] = {1.0, 0.0, 2.0};
412 double c3[] = {0.0, 1.0, 3.0};
419 auto state = simplex.
solve();
425 EXPECT_TRUE(nearly_equal(simplex.
get_solution(0), 2.0));
426 EXPECT_TRUE(nearly_equal(simplex.
get_solution(1), 2.0));
445 double c1[] = {2.0, 3.0, 1.0, 5.0};
446 double c2[] = {4.0, 2.0, 3.0, 11.0};
447 double c3[] = {3.0, 4.0, 2.0, 8.0};
454 auto state = simplex.
solve();
479 double c1[] = {1.0, 1.0, 40.0};
480 double c2[] = {2.0, 1.0, 60.0};
486 auto state = simplex.
solve();
492 EXPECT_TRUE(nearly_equal(simplex.
get_solution(0), 20.0));
493 EXPECT_TRUE(nearly_equal(simplex.
get_solution(1), 20.0));
512 double c1[] = {1.0, 1.0, 1.0, 10.0};
513 double c2[] = {2.0, 1.0, 3.0, 18.0};
519 auto state = simplex.
solve();
543 double c1[] = {1.0, 1.0, 0.0, 0.0, 4.0};
544 double c2[] = {0.0, 0.0, 1.0, 1.0, 6.0};
545 double c3[] = {1.0, 0.0, 1.0, 0.0, 5.0};
546 double c4[] = {0.0, 1.0, 0.0, 1.0, 5.0};
554 auto state = simplex.
solve();
575 double c1[] = {1.0, 10.0};
579 auto state = simplex.
solve();
584 EXPECT_TRUE(nearly_equal(simplex.
get_solution(0), 10.0));
602 double c1[] = {1.0, 1.0, 10.0};
603 double c2[] = {0.0, 1.0, 10.0};
608 auto state = simplex.
solve();
630 double c1[] = {1.0, 0.0, 5.0};
631 double c2[] = {0.0, 1.0, 5.0};
632 double c3[] = {1.0, 1.0, 10.0};
639 auto state = simplex.
solve();
644 EXPECT_TRUE(nearly_equal(simplex.
get_solution(0), 5.0));
645 EXPECT_TRUE(nearly_equal(simplex.
get_solution(1), 5.0));
663 double c1[] = {1.0, 1.0, 10.0};
664 double c2[] = {1.0, 0.0, 5.0};
665 double c3[] = {0.0, 1.0, 5.0};
672 auto state = simplex.
solve();
695 double c1[] = {1.0, 1.0, 4.0};
696 double c2[] = {2.0, 3.0, 9.0};
697 double c3[] = {1.0, 0.0, 4.0};
698 double c4[] = {0.0, 1.0, 4.0};
706 auto state = simplex.
solve();
728 float c1[] = {1.0f, 1.0f, 4.0f};
729 float c2[] = {1.0f, 0.0f, 2.0f};
735 auto state = simplex.
solve();
755 double c1[] = {1.0, 1.0, 10.0};
761 auto state = simplex.
solve();
777 double c1[] = {1.0, 0.0, 5.0};
778 double c2[] = {0.0, 1.0, 5.0};
805 double c1[] = {1.0, 1.0, 4.0};
809 double c2[] = {2.0, 1.0, 6.0};
826 for (
int i = 0; i < n; ++i)
830 for (
int j = 0; j < n; ++j)
833 for (
int i = 0; i < n; ++i)
834 rest[i] = (i == j) ? 1.0 : 0.0;
839 auto state = simplex.
solve();
858 double c1[] = {0.001, 0.001, 0.01};
859 double c2[] = {0.001, 0.0, 0.005};
860 double c3[] = {0.0, 0.001, 0.005};
866 auto state = simplex.
solve();
881 double c1[] = {1000.0, 1000.0, 10000.0};
882 double c2[] = {1000.0, 0.0, 5000.0};
883 double c3[] = {0.0, 1000.0, 5000.0};
889 auto state = simplex.
solve();
911 double c1[] = {1.0, 1.0, 40.0};
912 double c2[] = {2.0, 1.0, 60.0};
917 auto state_max = simplex_max.
solve();
932 auto state_min = simplex_min.
solve();
962 double c1[] = {1.0, 1.0, 10.0};
963 double c2[] = {1.0, 0.0, 6.0};
964 double c3[] = {0.0, 1.0, 8.0};
971 auto state = simplex.
solve();
991 double c1[] = {1.0, 1.0, 10.0};
992 double c2[] = {1.0, 0.0, 8.0};
993 double c3[] = {0.0, 1.0, 6.0};
1000 auto state = simplex.
solve();
1017 double c1[] = {1.0, 1.0, 40.0};
1018 double c2[] = {2.0, 1.0, 60.0};
1027 EXPECT_GT(stats.iterations, 0u);
1028 EXPECT_GT(stats.pivots, 0u);
1029 EXPECT_GE(stats.elapsed_ms, 0.0);
1042 double c1[] = {1.0, 0.0, 0.0, 1.0};
1043 double c2[] = {20.0, 1.0, 0.0, 100.0};
1044 double c3[] = {200.0, 20.0, 1.0, 10000.0};
1051 auto state = simplex.
solve();
1069 double c1[] = {1.0, 1.0, 40.0};
1070 double c2[] = {2.0, 1.0, 60.0};
1080 EXPECT_EQ(
range.current_value, 40.0);
1084 EXPECT_NEAR(
range.lower_bound, 30.0, 1e-9);
1085 EXPECT_NEAR(
range.upper_bound, 60.0, 1e-9);
1087 EXPECT_NEAR(
range.lower_bound, 20.0, 1e-9);
1088 EXPECT_NEAR(
range.upper_bound, 40.0, 1e-9);
1092 EXPECT_EQ(
range.current_value, 40.0);
1093 EXPECT_NEAR(
range.lower_bound, 30.0, 1e-9);
1094 EXPECT_NEAR(
range.upper_bound, 60.0, 1e-9);
1096 EXPECT_NEAR(
range.lower_bound, 40.0, 1e-9);
1097 EXPECT_NEAR(
range.upper_bound, 80.0, 1e-9);
1106 double c1[] = {1.0, 1.0, 40.0};
1107 double c2[] = {2.0, 1.0, 60.0};
1117 EXPECT_EQ(prices.size(), 2u);
1122 EXPECT_NEAR(prices[0], 20.0, 1e-9);
1123 EXPECT_NEAR(prices[1], 10.0, 1e-9);
1132 double c1[] = {1.0, 1.0, 40.0};
1133 double c2[] = {2.0, 1.0, 60.0};
1143 EXPECT_EQ(costs.size(), 2u);
1146 EXPECT_NEAR(costs[0], 0.0, 1e-9);
1147 EXPECT_NEAR(costs[1], 0.0, 1e-9);
1158 double c1[] = {1.0, 1.0, 40.0};
1159 double c2[] = {2.0, 1.0, 60.0};
1164 auto state1 = simplex.
solve();
1179 EXPECT_NEAR(simplex.
objetive_value(), original_obj + 20.0 * 10, 1e-9);
1186 const size_t n = 10;
1191 for (
size_t i = 0; i < n; ++i)
1195 for (
size_t i = 0; i < n; ++i)
1198 for (
size_t j = 0; j <= n; ++j)
1207 for (
size_t i = 0; i < n; ++i)
1209 sum_c[n] =
static_cast<double>(n) / 2.0;
1213 auto state = simplex.
solve();
1218 EXPECT_NEAR(simplex.
objetive_value(),
static_cast<double>(n) / 2.0, 0.1);
1245 double c1[] = {1.0, 1.0, 40.0};
1246 double c2[] = {2.0, 1.0, 60.0};
1256 EXPECT_TRUE(has_basic);
1270 return std::fabs(a - b) < eps;
1296 double c1[] = {1.0, 1.0};
1297 double c2[] = {2.0, 1.0};
1301 auto state = simplex.
solve();
1306 EXPECT_TRUE(nearly_equal(simplex.
get_solution(0), 20.0, 0.1));
1307 EXPECT_TRUE(nearly_equal(simplex.
get_solution(1), 20.0, 0.1));
1326 double c1[] = {2.0, 3.0, 1.0};
1327 double c2[] = {4.0, 2.0, 3.0};
1328 double c3[] = {3.0, 4.0, 2.0};
1334 auto state = simplex.
solve();
1354 double c1[] = {1.0, 1.0, 2.0, 4.0};
1355 double c2[] = {2.0, 1.0, 1.0, 5.0};
1360 std_simplex.
solve();
1370 double r1[] = {1.0, 1.0, 2.0};
1371 double r2[] = {2.0, 1.0, 1.0};
1375 rev_simplex.
solve();
1379 EXPECT_NEAR(std_obj, rev_obj, 0.1);
1385 const size_t n = 10;
1391 for (
size_t j = 0; j < n; ++j)
1392 simplex.
set_objective(j,
static_cast<double>((j + 1) * 3 % 7 + 1));
1394 for (
size_t i = 0; i <
m; ++i)
1396 for (
size_t j = 0; j < n; ++j)
1397 simplex.
set_constraint(i, j,
static_cast<double>((i + j + 1) % 5 + 1));
1398 simplex.
set_rhs(i,
static_cast<double>((i + 1) * 20));
1401 auto state = simplex.
solve();
1415 double c1[] = {1.0, 1.0};
1416 double c2[] = {2.0, 1.0};
1424 EXPECT_GT(stats.iterations, 0u);
1425 EXPECT_GT(stats.pivots, 0u);
1426 EXPECT_GE(stats.elapsed_ms, 0.0);
1432 const size_t n = 20;
1433 const size_t m = 10;
1436 std::vector<double> obj(n);
1437 std::vector<std::vector<double>> A(
m, std::vector<double>(n));
1438 std::vector<double> b(
m);
1440 for (
size_t j = 0; j < n; ++j)
1441 obj[j] =
static_cast<double>(j % 5 + 1);
1443 for (
size_t i = 0; i <
m; ++i)
1446 for (
size_t j = 0; j < n; ++j)
1448 A[i][j] =
static_cast<double>((i + j) % 3 + 1);
1456 for (
size_t j = 0; j < n; ++j)
1459 for (
size_t i = 0; i <
m; ++i)
1462 for (
size_t j = 0; j < n; ++j)
1469 auto std_state = std_simplex.
solve();
1471 auto std_stats = std_simplex.
get_stats();
1477 for (
size_t i = 0; i <
m; ++i)
1480 auto rev_state = rev_simplex.
solve();
1481 auto rev_stats = rev_simplex.
get_stats();
1489 EXPECT_GT(std_obj, 0.0);
1490 EXPECT_GT(rev_obj, 0.0);
1492 std::cout <<
"\n=== Performance Comparison (n=" << n <<
", m=" <<
m <<
") ===\n"
1493 <<
"Standard Simplex: " << std_stats.elapsed_ms <<
" ms, "
1494 << std_stats.pivots <<
" pivots, obj=" << std_obj <<
"\n"
1495 <<
"Revised Simplex: " << rev_stats.elapsed_ms <<
" ms, "
1496 << rev_stats.pivots <<
" pivots, obj=" << rev_obj <<
"\n";
1510 return std::fabs(a - b) < eps;
1532 double c1[] = {1.0, 1.0, 4.0};
1536 double c2[] = {1.0, 0.0, 6.0};
1537 double c3[] = {0.0, 1.0, 5.0};
1542 auto state = simplex.
solve();
1549 EXPECT_TRUE(nearly_equal(simplex.
get_solution(0), 6.0, 0.1));
1550 EXPECT_TRUE(nearly_equal(simplex.
get_solution(1), 5.0, 0.1));
1572 double c1[] = {1.0, 1.0, 10.0};
1576 double c2[] = {1.0, 0.0, 6.0};
1577 double c3[] = {0.0, 1.0, 8.0};
1582 auto state = simplex.
solve();
1589 EXPECT_TRUE(nearly_equal(simplex.
get_solution(0), 6.0, 0.1));
1590 EXPECT_TRUE(nearly_equal(simplex.
get_solution(1), 4.0, 0.1));
1596 EXPECT_TRUE(nearly_equal(
sum, 10.0, 0.1));
1622 double c1[] = {1.0, 1.0, 1.0, 5.0};
1623 double c2[] = {1.0, 2.0, 0.0, 10.0};
1624 double c3[] = {2.0, 1.0, 0.0, 8.0};
1625 double c4[] = {0.0, 0.0, 1.0, 20.0};
1633 auto state = simplex.
solve();
1662 double c1[] = {1.0, 1.0, 0.0, 5.0};
1663 double c2[] = {0.0, 1.0, 1.0, 6.0};
1669 auto state = simplex.
solve();
1680 EXPECT_TRUE(nearly_equal(x +
y, 5.0, 0.1));
1681 EXPECT_TRUE(nearly_equal(
y + z, 6.0, 0.1));
1705 double c1[] = {1.0, 1.0, 10.0};
1706 double c2[] = {1.0, 0.0, 3.0};
1707 double c3[] = {0.0, 1.0, 4.0};
1714 auto state = simplex.
solve();
1732 return std::fabs(a - b) < eps;
1753 double c1[] = {1.0, 1.0, 4.0};
1754 double c2[] = {1.0, 2.0, 6.0};
1760 auto state = simplex.
solve();
1766 EXPECT_TRUE(nearly_equal(simplex.
get_solution(0), 2.0, 0.1));
1767 EXPECT_TRUE(nearly_equal(simplex.
get_solution(1), 2.0, 0.1));
1785 double c1[] = {2.0, 1.0, 10.0};
1786 double c2[] = {1.0, 2.0, 8.0};
1792 auto state = simplex.
solve();
1802 EXPECT_GE(2*x +
y, 10.0 -
EPSILON);
1825 double c1[] = {1.0, 1.0, 0.0, 0.0, 10.0};
1826 double c2[] = {0.0, 0.0, 1.0, 1.0, 15.0};
1827 double c3[] = {1.0, 0.0, 1.0, 0.0, 20.0};
1828 double c4[] = {0.0, 1.0, 0.0, 1.0, 20.0};
1836 auto state = simplex.
solve();
1845 EXPECT_GT(cost, 0.0);
1846 EXPECT_LT(cost, 100.0);
1860 double c1[] = {1.0, 1.0, 8.0};
1864 auto state = simplex.
solve();
1892 double c1[] = {1.0, 1.0, 1.0, 10.0};
1893 double c2[] = {1.0, 0.0, 1.0, 4.0};
1894 double c3[] = {0.0, 1.0, 0.0, 2.0};
1901 auto state = simplex.
solve();
1912 EXPECT_TRUE(nearly_equal(x +
y + z, 10.0, 0.1));
1913 EXPECT_GE(x + z, 4.0 -
EPSILON);
1928 double c1[] = {1.0, 1.0, 5.0};
1932 simplex_min.
solve();
1945 simplex_max.
solve();
1960 using Rows = std::initializer_list<std::pair<std::vector<double>,
ConstraintType>>;
1965 for (
const auto & [
row, type] :
rows)
1966 lp.put_restriction(
row.data(), type);
1971 std::string temp_name(
const std::string & stem)
1973 return (std::filesystem::temp_directory_path()
1974 / (stem +
"_" + std::to_string(std::random_device{}()))).
string();
1977 std::string read_file(
const std::string & name)
1979 std::ifstream in(name);
1980 std::ostringstream s;
1986TEST(SimplexDefects, AnInfeasibleProgramIsReported)
1991 add_rows(lp, {{{1, 0}, ConstraintType::LE}, {{1, 1}, ConstraintType::GE}});
1995 EXPECT_THROW((
void) lp.
shadow_price(0), std::logic_error);
1999TEST(SimplexDefects, ColumnsWithTheSameUnitVectorAreNotBothBasic)
2006 add_rows(lp, {{{1, 1, 1}, ConstraintType::LE}});
2016TEST(SimplexDefects, ANegativeRightHandSideIsNotIgnored)
2022 add_rows(lp, {{{-1, -1}, ConstraintType::LE}, {{1, 3}, ConstraintType::LE}});
2037 add_rows(lp, {{{1, -1, -1}, ConstraintType::EQ}});
2048 add_rows(lp, {{{1, -1, -2}, ConstraintType::GE}});
2058 add_rows(lp, {{{1, 1, 5}, ConstraintType::LE}, {{-1, -1, -10}, ConstraintType::LE}});
2063TEST(SimplexDefects, AnObjectiveAboveTheBigMOfBefore)
2069 add_rows(lp, {{{1, 1}, ConstraintType::GE}});
2076TEST(SimplexDefects, SensitivityOfEqualitiesAndOfGreaterOrEqual)
2082 add_rows(lp, {{{1, 2}, ConstraintType::EQ}});
2092 add_rows(lp, {{{1, 1, 3}, ConstraintType::EQ}, {{1, -1, 1}, ConstraintType::EQ}});
2108 add_rows(lp, {{{1, 1, 4}, ConstraintType::GE}, {{1, 3, 6}, ConstraintType::GE}});
2116 EXPECT_NEAR(
r.lower_bound, 2.0, 1e-12);
2117 EXPECT_NEAR(
r.upper_bound, 6.0, 1e-12);
2119 EXPECT_NEAR(
r.lower_bound, 4.0, 1e-12);
2120 EXPECT_NEAR(
r.upper_bound, 12.0, 1e-12);
2122 EXPECT_NEAR(
r.lower_bound, 1.0, 1e-12);
2123 EXPECT_NEAR(
r.upper_bound, 3.0, 1e-12);
2125 EXPECT_NEAR(
r.lower_bound, 2.0, 1e-12);
2126 EXPECT_NEAR(
r.upper_bound, 6.0, 1e-12);
2133 add_rows(lp, {{{1, 0, 3}, ConstraintType::LE}, {{1, 1, 5}, ConstraintType::LE}});
2139 EXPECT_TRUE(
r.is_unbounded_below());
2140 EXPECT_NEAR(
r.upper_bound, 0.0, 1e-12);
2145TEST(SimplexDefects, ReoptimizationOfEqualitiesAndOfGreaterOrEqual)
2151 add_rows(lp, {{{1, 2}, ConstraintType::EQ}, {{1, 10}, ConstraintType::LE}});
2165 add_rows(lp, {{{1, 1, 2}, ConstraintType::GE}, {{1, -1, 0}, ConstraintType::EQ}});
2183 add_rows(lp, {{{-1, -1}, ConstraintType::LE}, {{1, 3}, ConstraintType::LE}});
2195 add_rows(lp, {{{1, 1, 1}, ConstraintType::EQ}, {{1, 1, 1}, ConstraintType::EQ}});
2205TEST(SimplexDefects, PrintAndLatexOutput)
2211 add_rows(lp, {{{1, 1, 4}, ConstraintType::GE}, {{1, 3, 6}, ConstraintType::EQ},
2212 {{1, 0, 9}, ConstraintType::LE}});
2214 testing::internal::CaptureStdout();
2216 const std::string
out = testing::internal::GetCapturedStdout();
2219 EXPECT_EQ(std::count(
out.begin(),
out.end(),
'\n'), 4);
2220 EXPECT_NE(
out.find(
"1.00"), std::string::npos);
2222 const std::string program = temp_name(
"aleph_simplex_program") +
".tex";
2224 const std::string text = read_file(program);
2225 EXPECT_NE(text.find(
"\\min"), std::string::npos);
2226 EXPECT_NE(text.find(
"\\geq"), std::string::npos);
2227 EXPECT_NE(text.find(
" = "), std::string::npos);
2228 EXPECT_NE(text.find(
"\\leq"), std::string::npos);
2229 std::filesystem::remove(program);
2231 const std::string base = temp_name(
"aleph_simplex_steps");
2233 EXPECT_NE(read_file(base +
"-1.tex").
find(
"\\circled"), std::string::npos);
2236 std::filesystem::remove(base +
"-" + std::to_string(
k) +
".tex");
2237 std::filesystem::remove(base +
"-" + std::to_string(
k) +
"-after.tex");
2243 EXPECT_THROW(unprepared.
print_matrix(), std::logic_error);
2246TEST(SimplexDefects, LargeCoefficientsDoNotHideTheLeavingRow)
2255 add_rows(lp, {{{2e8, -1e8, 2e8}, ConstraintType::LE}, {{5e8, 1e8, 8e8}, ConstraintType::LE}});
2263TEST(SimplexDefects, SmallDataDoNotMakeAPivot)
2275 std::vector<double> c;
2278 const Program programs[] = {
2280 {{{5, -3, -2, 10}, C::LE}, {{3, 1, 0, 7}, C::LE}, {{0, -1, 0, -1}, C::GE},
2281 {{0, 0, 2, -1}, C::GE}}},
2282 {{-4, 0}, {{{0, -3, -9}, C::EQ}, {{-4, 5, 4}, C::LE}, {{0, 0, 0}, C::EQ}, {{0, 0, 3}, C::LE}}},
2284 {{{0, -2, 5, 2}, C::LE}, {{0, -1, 4, 4}, C::LE}, {{0, 0, -3, 6}, C::LE},
2285 {{0, 0, 2, 3}, C::EQ}}}};
2286 const double u = 1e-6;
2287 for (
const auto & p : programs)
2290 for (
size_t j = 0; j < p.c.size(); ++j)
2292 for (
auto [
row, type] : p.rows)
2294 for (
double & x :
row)
2303TEST(SimplexScaling, TheSensitivityInOtherUnits)
2310 add_rows(lp, {{{1e5, 1e5, 40e5}, ConstraintType::LE}, {{2e5, 1e5, 60e5}, ConstraintType::LE}});
2315 EXPECT_NEAR(
r.lower_bound, 30e5, 1e-6);
2316 EXPECT_NEAR(
r.upper_bound, 60e5, 1e-6);
2318 EXPECT_NEAR(
r.lower_bound, 30e-3, 1e-15);
2319 EXPECT_NEAR(
r.upper_bound, 60e-3, 1e-15);
2326TEST(SimplexScaling, SubnormalMagnitudesAreNotScaled)
2332 const double t = std::ldexp(1.0, -1032);
2335 add_rows(lp, {{{t, 1}, ConstraintType::LE}, {{1, 5}, ConstraintType::LE}});
2343 const double r1[] = {t}, r2[] = {1};
2353 float f1[] = {std::ldexp(1.0f, -140), 1}, f2[] = {1, 5};
2364 add_rows(lo, {{{1, 5}, ConstraintType::LE}});
2374 add_rows(lc, {{{t, 0, 1}, ConstraintType::LE}, {{0, 1, 2}, ConstraintType::LE}});
2378TEST(SimplexClassic, BealesCyclingProgramEnds)
2383 for (
bool bland : {
false,
true})
2392 add_rows(lp, {{{0.25, -60, -0.04, 9, 0}, ConstraintType::LE},
2393 {{0.5, -90, -0.02, 3, 0}, ConstraintType::LE},
2394 {{0, 0, 1, 0, 1}, ConstraintType::LE}});
2403 EXPECT_GE(st.iterations, st.pivots);
2404 EXPECT_GE(st.pivots, st.degenerate_pivots);
2405 EXPECT_GT(st.degenerate_pivots, 0u);
2416 for (
bool bland : {
false,
true})
2419 for (
size_t j = 0; j < n; ++j)
2423 for (
size_t i = 0; i < n; ++i)
2425 std::vector<double>
row(n + 1, 0.0);
2426 for (
size_t j = 0; j < i; ++j)
2427 row[j] = std::pow(2.0,
double(i - j + 1));
2429 row[n] = std::pow(5.0,
double(i + 1));
2435 EXPECT_NEAR(lp.
objetive_value(), std::pow(5.0,
double(n)), 1e-6);
2436 EXPECT_NEAR(lp.
get_solution(n - 1), std::pow(5.0,
double(n)), 1e-6);
2441TEST(SimplexClassic, ZeroRowsAndZeroObjective)
2447 add_rows(lp, {{{0, 5}, ConstraintType::LE}, {{0, -5}, ConstraintType::GE},
2448 {{1, 3}, ConstraintType::LE}});
2453 for (
auto [rhs, type] : {std::pair{5.0, ConstraintType::GE}, std::pair{3.0, ConstraintType::EQ},
2454 std::pair{-1.0, ConstraintType::LE}})
2459 double row[] = {0, rhs};
2468 add_rows(lp, {{{0, 0}, ConstraintType::EQ}, {{1, 2}, ConstraintType::LE}});
2476 add_rows(lp, {{{1, 1, 4}, ConstraintType::LE}, {{1, 0, 1}, ConstraintType::GE}});
2484TEST(SimplexClassic, InconsistentEqualities)
2488 add_rows(lp, {{{1, 1, 1}, ConstraintType::EQ}, {{1, 1, 2}, ConstraintType::EQ}});
2492TEST(SimplexClassic, AMovedSolverSolves)
2497 add_rows(lp, {{{1, 1, 40}, ConstraintType::LE}, {{2, 1, 60}, ConstraintType::LE}});
2501 moved.load_solution();
2502 EXPECT_NEAR(moved.objetive_value(), 1400.0, 1e-9);
2505 assigned = std::move(moved);
2514 add_rows(lp, {{{1, 1}, ConstraintType::LE}});
2515 EXPECT_THROW((
void) lp.
shadow_price(0), std::logic_error);
2516 EXPECT_THROW((
void) lp.
reduced_cost(0), std::logic_error);
2519 EXPECT_THROW((
void) lp.
dual_solve(), std::logic_error);
2522 EXPECT_THROW((
void) lp.
solve(), std::logic_error);
2523 EXPECT_THROW((
void) lp.
latex_solve(), std::logic_error);
2524 EXPECT_THROW((
void) lp.
shadow_price(1), std::out_of_range);
2529TEST(RevisedSimplexDefects, ANegativeRightHandSideNeedsTheFirstPhase)
2544TEST(RevisedSimplexDefects, InfeasibleAndUnbounded)
2558 const double row[] = {1, -1};
2564TEST(RevisedSimplexDefects, RefactorizationEveryPivotGivesTheSame)
2570 for (
size_t f : {size_t(1), size_t(1000)})
2573 for (
size_t j = 0; j < n; ++j)
2575 for (
size_t i = 0; i < n; ++i)
2577 for (
size_t j = 0; j < i; ++j)
2580 rs.
set_rhs(i, std::pow(5.0,
double(i + 1)));
2586 EXPECT_NEAR(
value[0], std::pow(5.0,
double(n)), 1e-6);
2587 EXPECT_NEAR(
value[1], std::pow(5.0,
double(n)), 1e-6);
2593TEST(RevisedSimplexDefects, LargeCoefficientsDoNotHideTheLeavingRow)
2600 const double r1[] = {2e8, -1e8}, r2[] = {5e8, 1e8};
2607TEST(SimplexClassic, TheRestOfTheInterface)
2619 const double ge[] = {1, 1, 4},
eq[] = {1, 3, 6};
2637 testing::internal::CaptureStdout();
2640 const std::string
out = testing::internal::GetCapturedStdout();
2641 EXPECT_NE(
out.find(
"Pivots:"), std::string::npos);
2642 EXPECT_NE(
out.find(
"shadow price"), std::string::npos);
2644 const std::string name = temp_name(
"aleph_simplex_matrix") +
".tex";
2646 EXPECT_NE(read_file(name).
find(
"\\circled{"), std::string::npos);
2647 std::filesystem::remove(name);
2657 testing::internal::CaptureStdout();
2659 EXPECT_NE(testing::internal::GetCapturedStdout().
find(
"Refactorizations: 0"),
2669 ::testing::InitGoogleTest(&argc, argv);
2670 return RUN_ALL_TESTS();
Simplex algorithm for linear programming.
TEST_F(SimplexTest, ConstructorBasic)
size_t size_t int32_t value
size_t size_t int32_t * out
Revised Simplex algorithm for linear programming.
void set_refactorization_frequency(size_t freq)
Sets refactorization frequency.
void set_objective(size_t j, T coef)
Sets objective coefficient.
size_t get_num_vars() const noexcept
Gets number of variables.
void print_stats() const
Prints statistics to stdout.
size_t get_num_constraints() const noexcept
Gets number of constraints.
const SimplexStats & get_stats() const noexcept
Gets execution statistics.
void set_constraint_row(size_t i, const T *coefs, T rhs)
Adds a complete constraint.
State get_state() const noexcept
Gets current state.
T get_solution(size_t j) const
Gets solution value for variable j.
void set_constraint(size_t i, size_t j, T coef)
Sets constraint coefficient.
T objective_value() const
Computes objective function value.
State solve()
Solves the linear program.
void set_rhs(size_t i, T rhs)
Sets RHS value for a constraint.
bool verify_solution() const
Verifies solution satisfies all constraints.
Linear program solver using the Simplex method.
void set_optimization_type(OptimizationType type) noexcept
Sets the optimization type.
SensitivityRange< T > rhs_sensitivity(size_t rest_idx) const
Computes sensitivity range for a RHS value.
void print_matrix() const
Prints the simplex tableau to stdout.
OptimizationType get_optimization_type() const noexcept
Gets the current optimization type.
const SimplexStats & get_stats() const noexcept
Gets execution statistics.
T & get_restriction_coef(const size_t rest_num, size_t idx)
Gets a specific coefficient from a constraint.
void set_minimize() noexcept
Sets minimization mode.
T objetive_value() const noexcept
Computes and returns the objective function value.
std::vector< T > get_all_reduced_costs() const
Gets all reduced costs as a vector.
void print_sensitivity_analysis() const
Prints sensitivity analysis to stdout.
T * get_restriction(const size_t rest_num)
Gets a pointer to a constraint array.
T * get_objetive_function() noexcept
Gets the objective function coefficients array.
bool verify_solution() const
Verifies that the solution satisfies all constraints.
SensitivityRange< T > objective_sensitivity(size_t var_idx) const
Computes sensitivity range for an objective coefficient.
void put_restriction_coef(const size_t rest_num, const size_t idx, const T &coef)
Sets a specific coefficient in a constraint.
size_t get_pivot_count() const noexcept
Gets the number of pivot operations.
T * put_restriction(const T *coefs=nullptr, ConstraintType type=ConstraintType::LE)
Adds a constraint to the linear program.
void prepare_linear_program()
Prepares the linear program for solving.
void latex_linear_program(const std::string &name) const
Exports the linear program to a LaTeX file.
T shadow_price(size_t rest_idx) const
Gets the shadow price (dual value) for a constraint.
State update_rhs_and_reoptimize(size_t rest_idx, T new_rhs)
Updates RHS value and re-optimizes using dual simplex.
bool is_basic_variable(size_t var_idx) const
Checks if a variable is basic in the optimal solution.
void print_stats() const
Prints a summary of statistics to stdout.
void set_maximize() noexcept
Sets maximization mode.
size_t get_iteration_count() const noexcept
Gets the number of iterations performed.
size_t get_num_restrictions() const noexcept
Gets the number of constraints.
const T & get_solution(size_t i) const noexcept
Gets the solution value for a specific variable.
void latex_matrix(const std::string &name, const int d=2, const int p=-1, const int q=-1) const
Exports the simplex tableau to a LaTeX file.
void put_objetive_function_coef(size_t i, const T &coef)
Sets a coefficient in the objective function.
void set_pivot_rule(PivotRule rule) noexcept
Sets the pivot selection rule.
T * put_ge_restriction(const T *coefs)
Adds a ≥ constraint.
T reduced_cost(size_t var_idx) const
Gets the reduced cost for a variable.
void load_solution() noexcept
Loads the solution values into the solution array.
State dual_solve()
Performs dual simplex iteration.
void set_epsilon(T epsilon) noexcept
Sets the floating-point comparison tolerance.
void put_objetive_function(const DynArray< T > &coefs)
Sets all objective function coefficients from a DynArray.
size_t get_num_vars() const noexcept
Gets the number of decision variables.
State get_state() const noexcept
Gets the current state of the solver.
size_t get_degenerate_pivot_count() const noexcept
Gets the number of degenerate pivots.
T * put_eq_restriction(const T *coefs)
Adds an = constraint.
State latex_solve(const char *name=nullptr)
Solves with LaTeX output at each iteration.
void enable_bland_rule() noexcept
Enables Bland's anti-cycling rule.
State solve()
Solves the linear program.
std::vector< T > get_all_shadow_prices() const
Gets all shadow prices as a vector.
const T & get_objetive_function_coef(size_t i) const
Gets a coefficient from the objective function.
double get_elapsed_time_ms() const noexcept
Gets the elapsed solving time.
bool nearly_equal(double a, double b, double eps=EPSILON)
static constexpr double EPSILON
bool nearly_equal(double a, double b, double eps=EPSILON)
static constexpr double EPSILON
static constexpr double EPSILON
bool nearly_equal(double a, double b, double eps=EPSILON)
bool nearly_equal(double a, double b, double eps=EPSILON)
static constexpr double EPSILON
Main namespace for Aleph-w library functions.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
auto min_value(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute minimum value.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
ConstraintType
Constraint type for linear programming.
auto max_value(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute maximum value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
bool bland_rule_used
Whether Bland's rule was activated.
size_t pivots
Total pivot operations.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)