51#include <gtest/gtest.h>
68 return std::fabs(a - b) < eps;
110 simplex.put_objetive_function_coef(0, 3.0);
111 simplex.put_objetive_function_coef(1, 2.0);
112 simplex.put_objetive_function_coef(2, 1.0);
137 double coefs[] = {5.0, 4.0, 3.0};
164 simplex.put_objetive_function_coef(0, 7.0);
166 double* ptr =
simplex.get_objetive_function();
178 double rest[] = {1.0, 2.0, 10.0};
188 double r1[] = {1.0, 1.0, 4.0};
189 double r2[] = {2.0, 1.0, 5.0};
190 double r3[] = {1.0, 2.0, 6.0};
232 double rest[] = {1.0, 1.0, 4.0};
242 double rest[] = {1.0, 2.0, 3.0, 10.0};
253 simplex.put_restriction(
nullptr);
255 simplex.put_restriction_coef(0, 0, 5.0);
256 simplex.put_restriction_coef(0, 1, 6.0);
284 simplex.put_objetive_function_coef(0, 3.0);
285 simplex.put_objetive_function_coef(1, 2.0);
293 simplex.put_objetive_function_coef(0, 3.0);
294 simplex.put_objetive_function_coef(1, 2.0);
295 double rest[] = {1.0, 1.0, 4.0};
304 simplex.put_objetive_function_coef(0, 3.0);
305 simplex.put_objetive_function_coef(1, 2.0);
306 double rest[] = {1.0, 1.0, 4.0};
309 simplex.prepare_linear_program();
330 simplex.put_objetive_function_coef(0, 3.0);
331 simplex.put_objetive_function_coef(1, 2.0);
333 double c1[] = {1.0, 1.0, 4.0};
334 double c2[] = {1.0, 0.0, 2.0};
335 double c3[] = {0.0, 1.0, 3.0};
341 simplex.prepare_linear_program();
364 simplex.put_objetive_function_coef(0, 5.0);
365 simplex.put_objetive_function_coef(1, 4.0);
366 simplex.put_objetive_function_coef(2, 3.0);
368 double c1[] = {2.0, 3.0, 1.0, 5.0};
369 double c2[] = {4.0, 2.0, 3.0, 11.0};
370 double c3[] = {3.0, 4.0, 2.0, 8.0};
376 simplex.prepare_linear_program();
399 simplex.put_objetive_function_coef(0, 40.0);
400 simplex.put_objetive_function_coef(1, 30.0);
402 double c1[] = {1.0, 1.0, 40.0};
403 double c2[] = {2.0, 1.0, 60.0};
408 simplex.prepare_linear_program();
431 simplex.put_objetive_function_coef(0, 10.0);
432 simplex.put_objetive_function_coef(1, 15.0);
433 simplex.put_objetive_function_coef(2, 12.0);
435 double c1[] = {1.0, 1.0, 1.0, 10.0};
436 double c2[] = {2.0, 1.0, 3.0, 18.0};
441 simplex.prepare_linear_program();
461 simplex.put_objetive_function_coef(0, 2.0);
462 simplex.put_objetive_function_coef(1, 3.0);
463 simplex.put_objetive_function_coef(2, 1.0);
464 simplex.put_objetive_function_coef(3, 4.0);
466 double c1[] = {1.0, 1.0, 0.0, 0.0, 4.0};
467 double c2[] = {0.0, 0.0, 1.0, 1.0, 6.0};
468 double c3[] = {1.0, 0.0, 1.0, 0.0, 5.0};
469 double c4[] = {0.0, 1.0, 0.0, 1.0, 5.0};
476 simplex.prepare_linear_program();
496 simplex.put_objetive_function_coef(0, 5.0);
498 double c1[] = {1.0, 10.0};
501 simplex.prepare_linear_program();
522 simplex.put_objetive_function_coef(0, 0.0);
523 simplex.put_objetive_function_coef(1, 5.0);
525 double c1[] = {1.0, 1.0, 10.0};
526 double c2[] = {0.0, 1.0, 10.0};
530 simplex.prepare_linear_program();
550 simplex.put_objetive_function_coef(0, 1.0);
551 simplex.put_objetive_function_coef(1, 1.0);
553 double c1[] = {1.0, 0.0, 5.0};
554 double c2[] = {0.0, 1.0, 5.0};
555 double c3[] = {1.0, 1.0, 10.0};
561 simplex.prepare_linear_program();
583 simplex.put_objetive_function_coef(0, 1.0);
584 simplex.put_objetive_function_coef(1, 1.0);
586 double c1[] = {1.0, 1.0, 10.0};
587 double c2[] = {1.0, 0.0, 5.0};
588 double c3[] = {0.0, 1.0, 5.0};
594 simplex.prepare_linear_program();
615 simplex.put_objetive_function_coef(0, 3.0);
616 simplex.put_objetive_function_coef(1, 5.0);
618 double c1[] = {1.0, 1.0, 4.0};
619 double c2[] = {2.0, 3.0, 9.0};
620 double c3[] = {1.0, 0.0, 4.0};
621 double c4[] = {0.0, 1.0, 4.0};
628 simplex.prepare_linear_program();
648 simplex.put_objetive_function_coef(0, 3.0f);
649 simplex.put_objetive_function_coef(1, 2.0f);
651 float c1[] = {1.0f, 1.0f, 4.0f};
652 float c2[] = {1.0f, 0.0f, 2.0f};
657 simplex.prepare_linear_program();
675 simplex.put_objetive_function_coef(0, 1.0);
676 simplex.put_objetive_function_coef(1, 1.0);
678 double c1[] = {1.0, 1.0, 10.0};
681 simplex.prepare_linear_program();
697 simplex.put_objetive_function_coef(0, 1.0);
698 simplex.put_objetive_function_coef(1, 1.0);
700 double c1[] = {1.0, 0.0, 5.0};
701 double c2[] = {0.0, 1.0, 5.0};
706 simplex.prepare_linear_program();
728 double c1[] = {1.0, 1.0, 4.0};
732 double c2[] = {2.0, 1.0, 6.0};
749 for (
int i = 0; i < n; ++i)
750 simplex.put_objetive_function_coef(i, 1.0);
753 for (
int j = 0; j < n; ++j)
756 for (
int i = 0; i < n; ++i)
757 rest[i] = (i == j) ? 1.0 : 0.0;
761 simplex.prepare_linear_program();
778 simplex.put_objetive_function_coef(0, 0.001);
779 simplex.put_objetive_function_coef(1, 0.002);
781 double c1[] = {0.001, 0.001, 0.01};
782 double c2[] = {0.001, 0.0, 0.005};
783 double c3[] = {0.0, 0.001, 0.005};
788 simplex.prepare_linear_program();
801 simplex.put_objetive_function_coef(0, 1000.0);
802 simplex.put_objetive_function_coef(1, 2000.0);
804 double c1[] = {1000.0, 1000.0, 10000.0};
805 double c2[] = {1000.0, 0.0, 5000.0};
806 double c3[] = {0.0, 1000.0, 5000.0};
811 simplex.prepare_linear_program();
834 double c1[] = {1.0, 1.0, 40.0};
835 double c2[] = {2.0, 1.0, 60.0};
882 simplex.put_objetive_function_coef(0, 5.0);
883 simplex.put_objetive_function_coef(1, 4.0);
885 double c1[] = {1.0, 1.0, 10.0};
886 double c2[] = {1.0, 0.0, 6.0};
887 double c3[] = {0.0, 1.0, 8.0};
893 simplex.prepare_linear_program();
911 simplex.put_objetive_function_coef(0, 2.0);
912 simplex.put_objetive_function_coef(1, 3.0);
914 double c1[] = {1.0, 1.0, 10.0};
915 double c2[] = {1.0, 0.0, 8.0};
916 double c3[] = {0.0, 1.0, 6.0};
918 simplex.put_restriction(
c1, ConstraintType::LE);
919 simplex.put_restriction(
c2, ConstraintType::LE);
920 simplex.put_restriction(
c3, ConstraintType::LE);
922 simplex.prepare_linear_program();
937 simplex.put_objetive_function_coef(0, 40.0);
938 simplex.put_objetive_function_coef(1, 30.0);
940 double c1[] = {1.0, 1.0, 40.0};
941 double c2[] = {2.0, 1.0, 60.0};
945 simplex.prepare_linear_program();
948 auto stats =
simplex.get_stats();
961 simplex.put_objetive_function_coef(0, 100.0);
962 simplex.put_objetive_function_coef(1, 10.0);
963 simplex.put_objetive_function_coef(2, 1.0);
965 double c1[] = {1.0, 0.0, 0.0, 1.0};
966 double c2[] = {20.0, 1.0, 0.0, 100.0};
967 double c3[] = {200.0, 20.0, 1.0, 10000.0};
973 simplex.prepare_linear_program();
989 simplex.put_objetive_function_coef(0, 40.0);
990 simplex.put_objetive_function_coef(1, 30.0);
992 double c1[] = {1.0, 1.0, 40.0};
993 double c2[] = {2.0, 1.0, 60.0};
997 simplex.prepare_linear_program();
1011 simplex.put_objetive_function_coef(0, 40.0);
1012 simplex.put_objetive_function_coef(1, 30.0);
1014 double c1[] = {1.0, 1.0, 40.0};
1015 double c2[] = {2.0, 1.0, 60.0};
1019 simplex.prepare_linear_program();
1032 simplex.put_objetive_function_coef(0, 40.0);
1033 simplex.put_objetive_function_coef(1, 30.0);
1035 double c1[] = {1.0, 1.0, 40.0};
1036 double c2[] = {2.0, 1.0, 60.0};
1040 simplex.prepare_linear_program();
1052 simplex.put_objetive_function_coef(0, 40.0);
1053 simplex.put_objetive_function_coef(1, 30.0);
1055 double c1[] = {1.0, 1.0, 40.0};
1056 double c2[] = {2.0, 1.0, 60.0};
1060 simplex.prepare_linear_program();
1082 const size_t n = 10;
1087 for (
size_t i = 0; i < n; ++i)
1088 simplex.put_objetive_function_coef(i, 1.0);
1091 for (
size_t i = 0; i < n; ++i)
1094 for (
size_t j = 0; j <= n; ++j)
1103 for (
size_t i = 0; i < n; ++i)
1105 sum_c[n] =
static_cast<double>(n) / 2.0;
1108 simplex.prepare_linear_program();
1117 auto stats =
simplex.get_stats();
1138 simplex.put_objetive_function_coef(0, 40.0);
1139 simplex.put_objetive_function_coef(1, 30.0);
1141 double c1[] = {1.0, 1.0, 40.0};
1142 double c2[] = {2.0, 1.0, 60.0};
1146 simplex.prepare_linear_program();
1166 return std::fabs(a - b) < eps;
1188 simplex.set_objective(0, 40.0);
1189 simplex.set_objective(1, 30.0);
1192 double c1[] = {1.0, 1.0};
1193 double c2[] = {2.0, 1.0};
1194 simplex.set_constraint_row(0,
c1, 40.0);
1195 simplex.set_constraint_row(1,
c2, 60.0);
1218 simplex.set_objective(0, 5.0);
1219 simplex.set_objective(1, 4.0);
1220 simplex.set_objective(2, 3.0);
1222 double c1[] = {2.0, 3.0, 1.0};
1223 double c2[] = {4.0, 2.0, 3.0};
1224 double c3[] = {3.0, 4.0, 2.0};
1227 simplex.set_constraint_row(1,
c2, 11.0);
1250 double c1[] = {1.0, 1.0, 2.0, 4.0};
1251 double c2[] = {2.0, 1.0, 1.0, 5.0};
1266 double r1[] = {1.0, 1.0, 2.0};
1267 double r2[] = {2.0, 1.0, 1.0};
1281 const size_t n = 10;
1287 for (
size_t j = 0; j < n; ++j)
1288 simplex.set_objective(j,
static_cast<double>((j + 1) * 3 % 7 + 1));
1290 for (
size_t i = 0; i < m; ++i)
1292 for (
size_t j = 0; j < n; ++j)
1293 simplex.set_constraint(i, j,
static_cast<double>((i + j + 1) % 5 + 1));
1294 simplex.set_rhs(i,
static_cast<double>((i + 1) * 20));
1308 simplex.set_objective(0, 40.0);
1309 simplex.set_objective(1, 30.0);
1311 double c1[] = {1.0, 1.0};
1312 double c2[] = {2.0, 1.0};
1313 simplex.set_constraint_row(0,
c1, 40.0);
1314 simplex.set_constraint_row(1,
c2, 60.0);
1318 auto stats =
simplex.get_stats();
1328 const size_t n = 20;
1329 const size_t m = 10;
1332 std::vector<double>
obj(n);
1333 std::vector<std::vector<double>> A(m, std::vector<double>(n));
1334 std::vector<double> b(m);
1336 for (
size_t j = 0; j < n; ++j)
1337 obj[j] =
static_cast<double>(j % 5 + 1);
1339 for (
size_t i = 0; i < m; ++i)
1342 for (
size_t j = 0; j < n; ++j)
1344 A[i][j] =
static_cast<double>((i + j) % 3 + 1);
1352 for (
size_t j = 0; j < n; ++j)
1355 for (
size_t i = 0; i < m; ++i)
1358 for (
size_t j = 0; j < n; ++j)
1373 for (
size_t i = 0; i < m; ++i)
1374 rev_simplex.set_constraint_row(i, A[i].data(), b[i]);
1388 std::cout <<
"\n=== Performance Comparison (n=" << n <<
", m=" << m <<
") ===\n"
1389 <<
"Standard Simplex: " <<
std_stats.elapsed_ms <<
" ms, "
1391 <<
"Revised Simplex: " <<
rev_stats.elapsed_ms <<
" ms, "
1406 return std::fabs(a - b) < eps;
1424 simplex.put_objetive_function_coef(0, 3.0);
1425 simplex.put_objetive_function_coef(1, 2.0);
1428 double c1[] = {1.0, 1.0, 4.0};
1429 simplex.put_restriction(
c1, ConstraintType::GE);
1432 double c2[] = {1.0, 0.0, 6.0};
1433 double c3[] = {0.0, 1.0, 5.0};
1434 simplex.put_restriction(
c2, ConstraintType::LE);
1435 simplex.put_restriction(
c3, ConstraintType::LE);
1437 simplex.prepare_linear_program();
1464 simplex.put_objetive_function_coef(0, 5.0);
1465 simplex.put_objetive_function_coef(1, 4.0);
1468 double c1[] = {1.0, 1.0, 10.0};
1469 simplex.put_restriction(
c1, ConstraintType::EQ);
1472 double c2[] = {1.0, 0.0, 6.0};
1473 double c3[] = {0.0, 1.0, 8.0};
1474 simplex.put_restriction(
c2, ConstraintType::LE);
1475 simplex.put_restriction(
c3, ConstraintType::LE);
1477 simplex.prepare_linear_program();
1514 simplex.put_objetive_function_coef(0, 2.0);
1515 simplex.put_objetive_function_coef(1, 3.0);
1516 simplex.put_objetive_function_coef(2, 1.0);
1518 double c1[] = {1.0, 1.0, 1.0, 5.0};
1519 double c2[] = {1.0, 2.0, 0.0, 10.0};
1520 double c3[] = {2.0, 1.0, 0.0, 8.0};
1521 double c4[] = {0.0, 0.0, 1.0, 20.0};
1523 simplex.put_restriction(
c1, ConstraintType::GE);
1524 simplex.put_restriction(
c2, ConstraintType::LE);
1525 simplex.put_restriction(
c3, ConstraintType::EQ);
1526 simplex.put_restriction(
c4, ConstraintType::LE);
1528 simplex.prepare_linear_program();
1554 simplex.put_objetive_function_coef(0, 1.0);
1555 simplex.put_objetive_function_coef(1, 1.0);
1556 simplex.put_objetive_function_coef(2, 1.0);
1558 double c1[] = {1.0, 1.0, 0.0, 5.0};
1559 double c2[] = {0.0, 1.0, 1.0, 6.0};
1561 simplex.put_restriction(
c1, ConstraintType::EQ);
1562 simplex.put_restriction(
c2, ConstraintType::EQ);
1564 simplex.prepare_linear_program();
1571 double x =
simplex.get_solution(0);
1572 double y =
simplex.get_solution(1);
1573 double z =
simplex.get_solution(2);
1598 simplex.put_objetive_function_coef(0, 1.0);
1599 simplex.put_objetive_function_coef(1, 1.0);
1601 double c1[] = {1.0, 1.0, 10.0};
1602 double c2[] = {1.0, 0.0, 3.0};
1603 double c3[] = {0.0, 1.0, 4.0};
1605 simplex.put_restriction(
c1, ConstraintType::GE);
1606 simplex.put_restriction(
c2, ConstraintType::LE);
1607 simplex.put_restriction(
c3, ConstraintType::LE);
1609 simplex.prepare_linear_program();
1618 double x =
simplex.get_solution(0);
1619 double y =
simplex.get_solution(1);
1632 std::cout <<
"WARNING: Simplex returned 'Solved' for infeasible problem\n";
1633 std::cout <<
" x=" << x <<
", y=" <<
y <<
", x+y=" << (x+
y) <<
"\n";
1638 <<
"All constraints appear satisfied but problem is mathematically infeasible";
1657 return std::fabs(a - b) < eps;
1675 simplex.put_objetive_function_coef(0, 2.0);
1676 simplex.put_objetive_function_coef(1, 3.0);
1678 double c1[] = {1.0, 1.0, 4.0};
1679 double c2[] = {1.0, 2.0, 6.0};
1681 simplex.put_restriction(
c1, ConstraintType::GE);
1682 simplex.put_restriction(
c2, ConstraintType::GE);
1684 simplex.prepare_linear_program();
1707 simplex.put_objetive_function_coef(0, 3.0);
1708 simplex.put_objetive_function_coef(1, 5.0);
1710 double c1[] = {2.0, 1.0, 10.0};
1711 double c2[] = {1.0, 2.0, 8.0};
1713 simplex.put_restriction(
c1, ConstraintType::GE);
1714 simplex.put_restriction(
c2, ConstraintType::GE);
1716 simplex.prepare_linear_program();
1723 double x =
simplex.get_solution(0);
1724 double y =
simplex.get_solution(1);
1745 simplex.put_objetive_function_coef(0, 4.0);
1746 simplex.put_objetive_function_coef(1, 2.0);
1747 simplex.put_objetive_function_coef(2, 3.0);
1748 simplex.put_objetive_function_coef(3, 1.0);
1750 double c1[] = {1.0, 1.0, 0.0, 0.0, 10.0};
1751 double c2[] = {0.0, 0.0, 1.0, 1.0, 15.0};
1752 double c3[] = {1.0, 0.0, 1.0, 0.0, 20.0};
1753 double c4[] = {0.0, 1.0, 0.0, 1.0, 20.0};
1755 simplex.put_restriction(
c1, ConstraintType::GE);
1756 simplex.put_restriction(
c2, ConstraintType::GE);
1757 simplex.put_restriction(
c3, ConstraintType::LE);
1758 simplex.put_restriction(
c4, ConstraintType::LE);
1760 simplex.prepare_linear_program();
1769 double cost =
simplex.objetive_value();
1782 simplex.put_objetive_function_coef(0, 5.0);
1783 simplex.put_objetive_function_coef(1, 4.0);
1785 double c1[] = {1.0, 1.0, 8.0};
1786 simplex.put_restriction(
c1, ConstraintType::GE);
1788 simplex.prepare_linear_program();
1797 double x =
simplex.get_solution(0);
1798 double y =
simplex.get_solution(1);
1813 simplex.put_objetive_function_coef(0, 1.0);
1814 simplex.put_objetive_function_coef(1, 2.0);
1815 simplex.put_objetive_function_coef(2, 3.0);
1817 double c1[] = {1.0, 1.0, 1.0, 10.0};
1818 double c2[] = {1.0, 0.0, 1.0, 4.0};
1819 double c3[] = {0.0, 1.0, 0.0, 2.0};
1821 simplex.put_restriction(
c1, ConstraintType::EQ);
1822 simplex.put_restriction(
c2, ConstraintType::GE);
1823 simplex.put_restriction(
c3, ConstraintType::GE);
1825 simplex.prepare_linear_program();
1832 double x =
simplex.get_solution(0);
1833 double y =
simplex.get_solution(1);
1834 double z =
simplex.get_solution(2);
1853 double c1[] = {1.0, 1.0, 5.0};
1885 ::testing::InitGoogleTest(&
argc,
argv);
Simplex algorithm for linear programming.
TEST_F(SimplexTest, ConstructorBasic)
size_t size() const noexcept
Count the number of elements of the list.
Revised Simplex algorithm for linear programming.
Linear program solver using the Simplex method.
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.
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.
auto max_value(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute maximum value.
DynList< T > maps(const C &c, Op op)
Classic map operation.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.