51#include <gtest/gtest.h>
70 using i64 = std::int64_t;
79 std::vector<std::vector<i64>> a;
83 [[nodiscard]]
size_t m()
const {
return a.
size(); }
85 [[nodiscard]] std::string str()
const
88 s << (maximize ?
"max" :
"min");
89 for (
size_t j = 0; j < n; ++j)
90 s <<
' ' << c[j] <<
"x" << j;
91 for (
size_t i = 0; i <
m(); ++i)
94 for (
size_t j = 0; j < n; ++j)
95 s <<
' ' << a[i][j] <<
"x" << j;
96 s << (type[i] == CT::LE ?
" <= " : type[i] == CT::GE ?
" >= " :
" = ") << b[i];
106 i64
magnitude(
const i64 x) {
return x < 0 ? -x : x; }
108 i64
mul(
const i64 x,
const i64
y)
110 if (x != 0
and y != 0
112 throw std::overflow_error(
"the oracle overflows");
116 i64
add(
const i64 x,
const i64
y)
118 if ((
y > 0
and x > std::numeric_limits<i64>::max() -
y)
119 or (
y < 0
and x < -std::numeric_limits<i64>::max() -
y))
120 throw std::overflow_error(
"the oracle overflows");
125 bool less(
const i64 p1,
const i64 q1,
const i64 p2,
const i64 q2)
127 return mul(p1, q2) <
mul(p2, q1);
131 i64 det(std::vector<std::vector<i64>> M)
133 const size_t n = M.size();
134 i64 sign = 1, prev = 1;
135 for (
size_t k = 0;
k + 1 < n; ++
k)
140 while (
r < n
and M[
r][
k] == 0)
144 std::swap(M[
k], M[
r]);
147 for (
size_t i =
k + 1; i < n; ++i)
148 for (
size_t j =
k + 1; j < n; ++j)
149 M[i][j] =
add(
mul(M[i][j], M[
k][
k]), -
mul(M[i][
k], M[
k][j])) / prev;
152 return sign * M[n - 1][n - 1];
158 enum Kind { Infeasible, Unbounded, Optimal } kind = Infeasible;
168 bool best_vertex(
const Program & p,
const i64 U, Optimum & best)
170 const size_t n = p.n,
m = p.m();
173 const size_t h =
m + 2 * n;
174 auto row = [&] (
size_t k,
size_t j) -> i64
178 return (
k -
m) % n == j ? 1 : 0;
180 auto rhs = [&] (
size_t k) -> i64
184 return k <
m + n ? 0 : U;
188 std::vector<size_t> pick(n);
189 std::vector<std::vector<i64>> M(n, std::vector<i64>(n));
190 std::vector<i64>
N(n);
193 for (
size_t i = 0; i < n; ++i)
197 for (
size_t r = 0;
r < n; ++
r)
198 for (
size_t j = 0; j < n; ++j)
199 M[
r][j] =
row(pick[
r], j);
201 if (i64 D = det(M); D != 0)
203 for (
size_t j = 0; j < n; ++j)
206 for (
size_t r = 0;
r < n; ++
r)
207 Mj[
r][j] = rhs(pick[
r]);
217 bool feasible =
true;
218 for (
size_t j = 0; j < n
and feasible; ++j)
219 feasible =
N[j] >= 0
and N[j] <=
mul(U, D);
220 for (
size_t i = 0; i <
m and feasible; ++i)
223 for (
size_t j = 0; j < n; ++j)
224 lhs =
add(lhs,
mul(p.a[i][j],
N[j]));
225 const i64 bd =
mul(p.b[i], D);
226 feasible = p.type[i] == CT::LE ? lhs <= bd
227 : p.type[i] == CT::GE ? lhs >= bd : lhs == bd;
233 for (
size_t j = 0; j < n; ++j)
236 or (p.maximize ?
less(best.num, best.den,
value, D)
250 while (i > 0
and pick[i - 1] ==
h - n + i - 1)
255 for (
size_t k = i;
k < n; ++
k)
256 pick[
k] = pick[
k - 1] + 1;
266 i64 vertex_bound(
const Program & p,
const i64 extra_b = 0)
268 i64 amax = 1, bmax = std::max<i64>(1, extra_b), f = 1;
269 for (
size_t i = 0; i < p.m(); ++i)
271 bmax = std::max(bmax,
magnitude(p.b[i]));
272 for (
size_t j = 0; j < p.n; ++j)
273 amax = std::max(amax,
magnitude(p.a[i][j]));
275 for (
size_t k = 2;
k <= p.n; ++
k)
277 i64 bound =
mul(f, bmax);
278 for (
size_t k = 1;
k < p.n; ++
k)
279 bound =
mul(bound, amax);
285 Optimum solve_exactly(
const Program & p, i64 U = 0)
288 U = 2 * vertex_bound(p) + 1;
289 Optimum small, large;
290 if (not best_vertex(p, U, small))
293 (void) best_vertex(p, 2 * U, large);
294 if (small.num != large.num or small.den != large.den)
295 if (
mul(small.num, large.den) !=
mul(large.num, small.den))
297 large.kind = Optimum::Unbounded;
301 small.kind = Optimum::Optimal;
310 bool to_rational(
const double v, i64 & p, i64 & q,
const i64 max_den = 20000)
312 if (not std::isfinite(v) or std::abs(v) > 1e9)
316 i64 p0 = 0, q0 = 1, p1 = 1, q1 = 0;
317 for (
int k = 0;
k < 64; ++
k)
319 const double f = std::floor(x);
320 const i64 a = i64(f);
321 const i64 p2 =
add(
mul(a, p1), p0), q2 =
add(
mul(a, q1), q0);
324 p0 = p1; q0 = q1; p1 = p2; q1 = q2;
325 if (std::abs(
double(p1) /
double(q1) - v) <= 1e-9 * std::max(1.0, std::abs(v)))
342 template <
typename T>
343 std::unique_ptr<Simplex<T>> make_simplex(
const Program & p,
const bool bland =
false)
345 auto lp = std::make_unique<Simplex<T>>(p.n, p.maximize ? OptimizationType::Maximize
346 : OptimizationType::Minimize);
347 for (
size_t j = 0; j < p.n; ++j)
348 lp->put_objetive_function_coef(j,
T(p.c[j]));
349 for (
size_t i = 0; i < p.m(); ++i)
351 std::vector<T>
row(p.n + 1);
352 for (
size_t j = 0; j < p.n; ++j)
353 row[j] =
T(p.a[i][j]);
354 row[p.n] =
T(p.b[i]);
355 lp->put_restriction(
row.data(), p.type[i]);
358 lp->enable_bland_rule();
359 lp->prepare_linear_program();
363 template <
typename T>
370 default:
return Optimum::Infeasible;
374 const char * name(
const Optimum::Kind
k)
376 return k == Optimum::Optimal ?
"Optimal" :
k == Optimum::Unbounded ?
"Unbounded"
381 double tol(
const double v,
const double base = 1e-8)
383 return base * std::max(1.0, std::abs(v));
392 size_t infeasible = 0;
393 size_t unbounded = 0;
394 size_t degenerate = 0;
396 size_t with_negative_rhs = 0;
397 size_t range_points = 0;
398 size_t range_tight = 0;
399 size_t reoptimized = 0;
404 std::string check_optimum(
const Program & p,
Simplex<double> & lp,
const Optimum & want)
406 std::ostringstream err;
407 const size_t n = p.n,
m = p.m();
409 std::vector<double> x(n);
411 for (
size_t j = 0; j < n; ++j)
414 value += double(p.c[j]) * x[j];
415 if (x[j] < -tol(x[j]))
416 err <<
" x" << j <<
" = " << x[j] <<
" < 0;";
419 if (std::abs(
value - want.value()) > tol(want.value()))
420 err <<
" value " <<
value <<
", want " << want.value() <<
';';
424 for (
size_t i = 0; i <
m; ++i)
427 for (
size_t j = 0; j < n; ++j)
428 lhs +=
double(p.a[i][j]) * x[j];
429 const double b = double(p.b[i]);
430 const bool ok = p.type[i] == CT::LE ? lhs <= b + tol(b)
431 : p.type[i] == CT::
GE ? lhs >= b - tol(b) :
std::
abs(lhs - b) <= tol(b);
433 err <<
" constraint " << i <<
" violated: " << lhs <<
" vs " << b <<
';';
436 err <<
" verify_solution() is false;";
440 double dual_value = 0;
441 for (
size_t i = 0; i <
m; ++i)
445 err <<
" get_all_shadow_prices()[" << i <<
"] differs;";
446 dual_value +=
y * double(p.b[i]);
447 const bool up = (p.type[i] == CT::LE) == p.maximize;
448 if (p.type[i] != CT::EQ
and (up ?
y < -tol(
y) :
y > tol(
y)))
449 err <<
" shadow price " << i <<
" = " <<
y <<
" of the wrong sign;";
451 if (std::abs(dual_value - want.value()) > tol(want.value(), 1e-7))
452 err <<
" dual value " << dual_value <<
" != " << want.value() <<
';';
455 for (
size_t j = 0; j < n; ++j)
457 double s = double(p.c[j]);
458 for (
size_t i = 0; i <
m; ++i)
459 s -= prices[i] *
double(p.a[i][j]);
462 err <<
" get_all_reduced_costs()[" << j <<
"] differs;";
463 if (std::abs(rc - s) > tol(s, 1e-7))
464 err <<
" reduced cost " << j <<
" = " << rc <<
", c - y A = " << s <<
';';
465 if (p.maximize ? rc > tol(rc, 1e-7) : rc < -tol(rc, 1e-7))
466 err <<
" reduced cost " << j <<
" = " << rc <<
" of the wrong sign;";
468 err <<
" basic x" << j <<
" has reduced cost " << rc <<
';';
479 for (
size_t j = 0; j < p.n; ++j)
482 for (
size_t i = 0; i < p.m(); ++i)
484 if (p.type[i] == CT::EQ)
487 for (
size_t j = 0; j < p.n; ++j)
489 if (std::abs(lhs -
double(p.b[i])) > 1e-7)
492 return positive < p.m();
498 for (
size_t j = 0; j < p.n; ++j)
501 for (
size_t i = 0; i < p.m(); ++i)
503 if (p.type[i] == CT::EQ)
506 for (
size_t j = 0; j < p.n; ++j)
508 if (std::abs(lhs -
double(p.b[i])) <= 1e-7
and std::abs(lp.
shadow_price(i)) <= 1e-7)
515 Program with_cost(
const Program & p,
const size_t j,
const i64 num,
const i64 den)
525 Program with_rhs(
const Program & p,
const size_t i,
const i64 num,
const i64 den)
528 for (
auto & v : q.a[i])
537 std::string check_cost_ranges(
const Program & p,
Simplex<double> & lp, Coverage & cov)
539 std::ostringstream err;
540 const bool tight = not degenerate(p, lp)
and not dual_degenerate(p, lp);
541 const i64 U = 2 * vertex_bound(p) + 1;
542 std::vector<double> x(p.n);
543 for (
size_t j = 0; j < p.n; ++j)
546 for (
size_t j = 0; j < p.n; ++j)
549 if (
r.current_value !=
double(p.c[j]))
550 err <<
" cost range " << j <<
": current " <<
r.current_value <<
';';
551 if (
r.lower_bound >
double(p.c[j]) + 1e-9 or
r.upper_bound <
double(p.c[j]) - 1e-9)
552 err <<
" cost range " << j <<
" [" <<
r.lower_bound <<
", " <<
r.upper_bound
553 <<
"] misses " << p.c[j] <<
';';
556 struct Point {
double v; };
557 std::vector<Point> points;
558 const double lo =
r.is_unbounded_below() ? double(p.c[j]) - 5 :
r.lower_bound;
559 const double hi =
r.is_unbounded_above() ? double(p.c[j]) + 5 :
r.upper_bound;
560 points.push_back({lo});
561 points.push_back({hi});
562 points.push_back({(lo + hi) / 2});
564 for (
const Point & pt : points)
567 if (not to_rational(pt.v, num, den, 4000))
569 const Program q = with_cost(p, j, num, den);
570 const Optimum o = solve_exactly(q, U);
572 for (
size_t k = 0;
k < p.n; ++
k)
573 value +=
double(q.c[
k]) * x[
k];
575 if (o.kind != Optimum::Optimal or std::abs(o.value() -
value) > tol(
value, 1e-7))
576 err <<
" cost " << j <<
" = " << pt.v <<
" inside its range: the solution gives "
577 <<
value <<
", the optimum is " << name(o.kind) <<
' ' << o.value() <<
';';
580 const bool is_lo = &pt == &points[0];
581 const bool is_hi = &pt == &points[1];
582 if (not tight or not ((is_lo
and not
r.is_unbounded_below())
583 or (is_hi
and not
r.is_unbounded_above())))
586 const i64 out_num =
mul(num, 2) + (is_lo ? -1 : 1), out_den =
mul(den, 2);
587 const Program qo = with_cost(p, j, out_num, out_den);
588 const Optimum oo = solve_exactly(qo, U);
589 double out_value = 0;
590 for (
size_t k = 0;
k < p.n; ++
k)
591 out_value +=
double(qo.c[
k]) * x[
k];
593 const bool still = oo.kind == Optimum::Optimal
594 and std::abs(oo.value() - out_value) <= tol(out_value, 1e-9);
596 err <<
" cost " << j <<
" just beyond " << pt.v <<
" keeps the solution optimal;";
608 std::string check_rhs_ranges(
const Program & p,
Simplex<double> & lp,
const Optimum & want,
611 std::ostringstream err;
612 const bool tight = not degenerate(p, lp)
and not dual_degenerate(p, lp);
614 for (
size_t i = 0; i < p.m(); ++i)
618 const double b = double(p.b[i]);
619 if (
r.current_value != b)
620 err <<
" rhs range " << i <<
": current " <<
r.current_value <<
';';
621 if (
r.lower_bound > b + 1e-9 or
r.upper_bound < b - 1e-9)
622 err <<
" rhs range " << i <<
" [" <<
r.lower_bound <<
", " <<
r.upper_bound
623 <<
"] misses " << b <<
';';
625 const double lo =
r.is_unbounded_below() ? b - 5 :
r.lower_bound;
626 const double hi =
r.is_unbounded_above() ? b + 5 :
r.upper_bound;
627 const double points[] = {lo, hi, (lo + hi) / 2};
628 for (
int k = 0;
k < 3; ++
k)
630 const double v = points[
k];
632 if (not to_rational(v, num, den, 4000))
636 const i64 U = 2 * vertex_bound(p, i64(std::ceil(std::abs(v))) + 1) + 1;
637 const Optimum o = solve_exactly(with_rhs(p, i, num, den), U);
638 const double predicted = want.value() +
y * (v - b);
640 if (o.kind != Optimum::Optimal or std::abs(o.value() - predicted) > tol(predicted, 1e-7))
641 err <<
" rhs " << i <<
" = " << v <<
" inside its range: the optimum is "
642 << name(o.kind) <<
' ' << o.value() <<
", the shadow price gives "
645 auto again = make_simplex<double>(p);
646 (void) again->solve();
647 const auto st = again->update_rhs_and_reoptimize(i, v);
648 again->load_solution();
651 or std::abs(again->objetive_value() - predicted) > tol(predicted, 1e-7))
652 err <<
" update_rhs_and_reoptimize(" << i <<
", " << v <<
") inside the range gives "
653 << name(kind_of<double>(st)) <<
' ' << again->objetive_value() <<
" with "
654 << again->get_pivot_count() <<
" pivots;";
656 const bool finite =
k == 0 ? not
r.is_unbounded_below()
657 :
k == 1 ? not
r.is_unbounded_above() :
false;
662 const i64 out_num =
mul(num, 2) + (
k == 0 ? -1 : 1), out_den =
mul(den, 2);
663 const double out = double(out_num) / double(out_den);
664 const Program qo = with_rhs(p, i, out_num, out_den);
665 const Optimum oo = solve_exactly(qo, U);
666 auto beyond = make_simplex<double>(p);
667 (void) beyond->solve();
668 const auto sto = beyond->update_rhs_and_reoptimize(i,
out);
669 beyond->load_solution();
672 if (kind_of<double>(sto) != oo.kind
673 or (oo.kind == Optimum::Optimal
674 and (std::abs(beyond->objetive_value() - oo.value()) > tol(oo.value(), 1e-7)
675 or beyond->get_pivot_count() == 0
676 or not beyond->verify_solution())))
677 err <<
" update_rhs_and_reoptimize(" << i <<
", " <<
out <<
") beyond the range gives "
678 << name(kind_of<double>(sto)) <<
' ' << beyond->objetive_value() <<
" with "
679 << beyond->get_pivot_count() <<
" pivots, the oracle " << name(oo.kind) <<
' '
680 << oo.value() <<
';';
682 const double out_predicted = want.value() +
y * (
out - b);
683 if (tight
and oo.kind == Optimum::Optimal
684 and std::abs(oo.value() - out_predicted) <= tol(out_predicted, 1e-9))
685 err <<
" rhs " << i <<
" just beyond " << v <<
" keeps the shadow price;";
693 std::string check_program(
const Program & p, Coverage & cov,
const bool deep)
696 for (
size_t i = 0; i < p.m(); ++i)
698 cov.with_eq += p.type[i] == CT::EQ;
699 cov.with_negative_rhs += p.b[i] < 0;
702 const Optimum want = solve_exactly(p);
703 auto lp = make_simplex<double>(p);
704 const auto st = lp->
solve();
706 if (kind_of<double>(st) != want.kind)
707 return std::string(
" state ") + name(kind_of<double>(st)) +
", want " + name(want.kind);
711 case Optimum::Infeasible:
714 case Optimum::Unbounded:
718 err +=
" the vertex of an unbounded program is not feasible;";
720 case Optimum::Optimal:
722 err += check_optimum(p, *lp, want);
723 if (degenerate(p, *lp))
727 err += check_cost_ranges(p, *lp, cov);
728 err += check_rhs_ranges(p, *lp, want, cov);
734 auto bland = make_simplex<double>(p,
true);
735 const auto stb = bland->solve();
737 err += std::string(
" with Bland's rule: ") + name(kind_of<double>(stb));
740 bland->load_solution();
741 if (std::abs(bland->objetive_value() - want.value()) > tol(want.value()))
742 err +=
" with Bland's rule the value is " + std::to_string(bland->objetive_value());
743 if (not bland->get_stats().bland_rule_used
and bland->get_pivot_count() > 0)
744 err +=
" Bland's rule is not reported as used;";
749 auto lf = make_simplex<float>(p);
750 const auto stf = lf->solve();
751 if (kind_of<float>(stf) != want.kind)
752 err += std::string(
" with float: ") + name(kind_of<float>(stf));
756 if (std::abs(
double(lf->objetive_value()) - want.value()) > tol(want.value(), 1e-3))
757 err +=
" with float the value is " + std::to_string(lf->objetive_value());
765 std::string check_revised(
const Program & p,
const size_t refactor_frequency)
768 for (
size_t j = 0; j < p.n; ++j)
769 rs.set_objective(j,
double(p.c[j]));
770 for (
size_t i = 0; i < p.m(); ++i)
772 std::vector<double>
row(p.n);
773 for (
size_t j = 0; j < p.n; ++j)
774 row[j] =
double(p.a[i][j]);
775 rs.set_constraint_row(i,
row.data(),
double(p.b[i]));
777 rs.set_refactorization_frequency(refactor_frequency);
779 const Optimum want = solve_exactly(p);
780 const auto st = rs.solve();
783 if (got != want.kind)
784 return std::string(
" RevisedSimplex: ") + name(got) +
", want " + name(want.kind);
785 if (got == Optimum::Optimal)
787 if (std::abs(rs.objective_value() - want.value()) > tol(want.value()))
788 return " RevisedSimplex value " + std::to_string(rs.objective_value()) +
", want "
789 + std::to_string(want.value());
790 if (not rs.verify_solution())
791 return " RevisedSimplex: verify_solution() is false";
801 Program random_program(std::mt19937_64 &
rng,
const size_t max_n,
const size_t max_m,
802 const i64 amax,
const i64 bmax,
const bool only_le =
false)
806 auto pick = [&] (i64 lo, i64 hi) {
return lo + i64(
rng() % std::uint64_t(hi - lo + 1)); };
808 p.n = size_t(pick(1, i64(max_n)));
809 const size_t m = size_t(pick(1, i64(max_m)));
810 p.maximize = only_le or pick(0, 1) == 1;
813 v = pick(-amax, amax);
815 const bool around_x0 = pick(0, 2) != 0;
816 std::vector<i64> x0(p.n);
820 for (
size_t i = 0; i <
m; ++i)
822 std::vector<i64>
row(p.n);
824 v = pick(0, 2) == 0 ? 0 : pick(-amax, amax);
825 const i64 t = pick(0, 9);
826 const CT type = only_le or t < 5 ? CT::LE : t < 8 ? CT::GE : CT::EQ;
827 i64 b = pick(-bmax, bmax);
831 for (
size_t j = 0; j < p.n; ++j)
832 ax +=
row[j] * x0[j];
833 const i64 room = pick(0, 3);
834 b = type == CT::LE ? ax + room : type == CT::GE ? ax - room : ax;
837 p.type.push_back(type);
848TEST(SimplexOracle, KnowsTheTextbookPrograms)
853 p.a = {{1, 1}, {2, 1}};
854 p.type = {CT::LE, CT::LE};
856 auto o = solve_exactly(p);
857 ASSERT_EQ(o.kind, Optimum::Optimal);
858 EXPECT_DOUBLE_EQ(o.value(), 1400.0);
860 p.a = {{1, 0}, {1, 0}};
861 p.type = {CT::LE, CT::GE};
863 EXPECT_EQ(solve_exactly(p).kind, Optimum::Infeasible);
868 EXPECT_EQ(solve_exactly(p).kind, Optimum::Unbounded);
872 p.a = {{1, 1}, {1, -1}};
873 p.type = {CT::EQ, CT::EQ};
875 o = solve_exactly(p);
876 ASSERT_EQ(o.kind, Optimum::Optimal);
877 EXPECT_DOUBLE_EQ(o.value(), 3.0);
880TEST(SimplexOracle, RationalApproximation)
883 ASSERT_TRUE(to_rational(2.5, p, q));
886 ASSERT_TRUE(to_rational(-1.0 / 3.0, p, q));
889 ASSERT_TRUE(to_rational(7.0, p, q));
892 EXPECT_FALSE(to_rational(std::numeric_limits<double>::infinity(), p, q));
901TEST(SimplexExhaustive, EveryProgramOfTheSmallDomain)
905 const CT types[] = {CT::LE, CT::GE, CT::EQ};
907 for (
size_t n = 1; n <= 2; ++n)
910 std::vector<std::pair<std::vector<i64>, std::pair<i64, CT>>>
rows;
911 const size_t combos = n == 1 ? 3 : 9;
914 std::vector<i64> a(n);
916 for (
size_t j = 0; j < n; ++j, rest /= 3)
917 a[j] = i64(rest % 3) - 1;
918 for (i64 b = -1; b <= 1; ++b)
920 rows.push_back({a, {b, t}});
923 std::vector<std::vector<i64>> objectives;
926 std::vector<i64> c(n);
928 for (
size_t j = 0; j < n; ++j, rest /= 3)
929 c[j] = i64(rest % 3) - 1;
930 objectives.push_back(c);
933 for (
size_t m = 1;
m <= 2; ++
m)
935 std::vector<size_t> idx(
m, 0);
938 for (
const auto & c : objectives)
939 for (bool maximize : {
true,
false})
943 p.maximize = maximize;
947 p.a.push_back(
rows[
k].first);
948 p.b.push_back(
rows[
k].second.first);
949 p.type.push_back(
rows[
k].second.second);
951 const std::string e = check_program(p, cov,
false);
952 if (not e.empty()
and ++failures <= 20)
953 ADD_FAILURE() << p.str() <<
":" << e;
965 EXPECT_EQ(failures, 0u);
966 EXPECT_GT(cov.programs, 100000u);
967 EXPECT_GT(cov.optimal, 10000u);
968 EXPECT_GT(cov.infeasible, 10000u);
969 EXPECT_GT(cov.unbounded, 10000u);
970 EXPECT_GT(cov.degenerate, 1000u);
977TEST(SimplexExhaustive, RandomProgramsWithTheirSensitivity)
979 std::mt19937_64
rng(20261104);
982 for (
int k = 0;
k < 1500; ++
k)
984 const Program p = random_program(
rng, 3, 4, 5, 10);
985 const std::string e = check_program(p, cov,
true);
986 if (not e.empty()
and ++failures <= 20)
987 ADD_FAILURE() << p.str() <<
":" << e;
990 EXPECT_EQ(failures, 0u);
991 EXPECT_GT(cov.optimal, 400u);
992 EXPECT_GT(cov.infeasible, 150u);
993 EXPECT_GT(cov.unbounded, 200u);
994 EXPECT_GT(cov.degenerate, 150u);
995 EXPECT_GT(cov.with_eq, 400u);
996 EXPECT_GT(cov.with_negative_rhs, 800u);
997 EXPECT_GT(cov.range_points, 5000u);
998 EXPECT_GT(cov.range_tight, 1500u);
999 EXPECT_GT(cov.reoptimized, 4000u);
1002TEST(SimplexExhaustive, LargerRandomPrograms)
1004 std::mt19937_64
rng(4111);
1006 size_t failures = 0;
1007 for (
int k = 0;
k < 400; ++
k)
1009 const Program p = random_program(
rng, 4, 5, 5, 10);
1010 const std::string e = check_program(p, cov,
false);
1011 if (not e.empty()
and ++failures <= 20)
1012 ADD_FAILURE() << p.str() <<
":" << e;
1015 EXPECT_EQ(failures, 0u);
1016 EXPECT_GT(cov.optimal, 100u);
1017 EXPECT_GT(cov.infeasible, 35u);
1018 EXPECT_GT(cov.unbounded, 55u);
1027TEST(SimplexExhaustive, ScaledProgramsGiveScaledResults)
1029 std::mt19937_64
rng(77);
1030 auto power = [&] {
return std::pow(10.0,
double(i64(
rng() % 13) - 6)); };
1031 size_t failures = 0, solved = 0, programs = 0;
1032 for (
int k = 0;
k < 400; ++
k)
1034 const Program p = random_program(
rng, 3, 4, 5, 10);
1035 const Optimum want = solve_exactly(p);
1036 for (
int how = 0; how < 3; ++how)
1040 std::vector<double>
row(p.m()),
col(p.n);
1044 const double all = how == 0 ? 1e6 : 1e-6;
1046 std::fill(
col.begin(),
col.end(), 1.0);
1051 for (
auto & v :
row)
1053 for (
auto & v :
col)
1059 : OptimizationType::Minimize);
1060 for (
size_t j = 0; j < p.n; ++j)
1062 std::vector<std::vector<double>> a(p.m(), std::vector<double>(p.n + 1));
1063 for (
size_t i = 0; i < p.m(); ++i)
1065 for (
size_t j = 0; j < p.n; ++j)
1066 a[i][j] =
double(p.a[i][j]) *
row[i] *
col[j];
1067 a[i][p.n] = double(p.b[i]) *
row[i];
1071 const auto st = lp.
solve();
1074 if (kind_of<double>(st) != want.kind)
1075 err = std::string(name(kind_of<double>(st))) +
", want " + name(want.kind);
1080 const double v = want.value() * obj;
1083 + std::to_string(v);
1085 err =
"verify_solution() is false";
1088 double dual = 0,
size = std::abs(v);
1089 for (
size_t i = 0; i < p.m(); ++i)
1092 dual +=
y * a[i][p.n];
1093 size += std::abs(
y * a[i][p.n]);
1094 const bool up = (p.type[i] == CT::LE) == p.maximize;
1095 if (p.type[i] != CT::EQ
and (up ?
y < -tol(
y) :
y > tol(
y)))
1096 err +=
" shadow price " + std::to_string(i) +
" of the wrong sign;";
1098 if (std::abs(dual - v) > 1e-7 * std::max(1.0,
size))
1099 err +=
" dual value " + std::to_string(dual) +
", want " + std::to_string(v);
1101 if (not err.empty()
and ++failures <= 20)
1102 ADD_FAILURE() << p.str() <<
" scaled (" << how <<
"): " << err;
1105 EXPECT_EQ(failures, 0u);
1106 EXPECT_EQ(programs, 1200u);
1107 EXPECT_GT(solved, 300u);
1114TEST(SimplexExhaustive, LatexSolveGivesWhatSolveGives)
1116 const auto dir = std::filesystem::temp_directory_path()
1117 / (
"aleph_simplex_latex_" + std::to_string(std::random_device{}()));
1118 std::filesystem::create_directories(dir);
1120 std::mt19937_64
rng(5);
1121 size_t failures = 0;
1122 for (
int k = 0;
k < 60; ++
k)
1124 const Program p = random_program(
rng, 3, 3, 5, 10);
1125 auto a = make_simplex<double>(p);
1126 auto b = make_simplex<double>(p);
1127 const auto sa = a->solve();
1128 const std::string base = (dir / (
"p" + std::to_string(
k))).string();
1129 const auto sb = b->latex_solve(base.c_str());
1130 bool ok = sa == sb
and std::filesystem::exists(base +
"-0.tex");
1135 ok = std::abs(a->objetive_value() - b->objetive_value()) <= tol(a->objetive_value())
1136 and a->get_pivot_count() == b->get_pivot_count();
1138 if (ok
and b->get_pivot_count() > 0)
1139 ok = std::filesystem::exists(base +
"-1.tex")
1140 and std::filesystem::exists(base +
"-1-after.tex");
1141 if (not ok
and ++failures <= 10)
1142 ADD_FAILURE() << p.str();
1144 EXPECT_EQ(failures, 0u);
1146 std::filesystem::remove_all(dir);
1153TEST(RevisedSimplexExhaustive, RandomProgramsOfLessOrEqualConstraints)
1155 std::mt19937_64
rng(1234);
1156 size_t failures = 0, optimal = 0, infeasible = 0, unbounded = 0;
1157 for (
int k = 0;
k < 800; ++
k)
1159 const Program p = random_program(
rng, 4, 5, 5, 10,
true);
1160 const Optimum want = solve_exactly(p);
1161 optimal += want.kind == Optimum::Optimal;
1162 infeasible += want.kind == Optimum::Infeasible;
1163 unbounded += want.kind == Optimum::Unbounded;
1164 for (
size_t f : {size_t(1), size_t(2), size_t(7), size_t(50)})
1165 if (
const std::string e = check_revised(p, f); not e.empty()
and ++failures <= 20)
1166 ADD_FAILURE() << p.str() <<
" refactorization every " << f <<
":" << e;
1168 EXPECT_EQ(failures, 0u);
1169 EXPECT_GT(optimal, 180u);
1170 EXPECT_GT(infeasible, 60u);
1171 EXPECT_GT(unbounded, 150u);
1178TEST(RevisedSimplexExhaustive, ScaledPrograms)
1180 std::mt19937_64
rng(4321);
1181 auto power = [&] {
return std::pow(10.0,
double(i64(
rng() % 13) - 6)); };
1182 size_t failures = 0, solved = 0;
1183 for (
int k = 0;
k < 400; ++
k)
1185 const Program p = random_program(
rng, 4, 5, 5, 10,
true);
1186 const Optimum want = solve_exactly(p);
1187 std::vector<double>
row(p.m()),
col(p.n);
1188 for (
auto & v :
row)
1190 for (
auto & v :
col)
1192 const double obj = power();
1195 for (
size_t j = 0; j < p.n; ++j)
1197 for (
size_t i = 0; i < p.m(); ++i)
1199 std::vector<double>
r(p.n);
1200 for (
size_t j = 0; j < p.n; ++j)
1201 r[j] =
double(p.a[i][j]) *
row[i] *
col[j];
1204 const auto st = rs.
solve();
1207 bool ok = got == want.kind;
1208 if (ok
and got == Optimum::Optimal)
1211 const double v = want.value() * obj;
1214 if (not ok
and ++failures <= 20)
1215 ADD_FAILURE() << p.str() <<
": " << name(got) <<
", want " << name(want.kind);
1217 EXPECT_EQ(failures, 0u);
1218 EXPECT_GT(solved, 100u);
1221TEST(RevisedSimplexExhaustive, AgreesWithSimplex)
1223 std::mt19937_64
rng(99);
1224 size_t failures = 0;
1225 for (
int k = 0;
k < 300; ++
k)
1227 const Program p = random_program(
rng, 4, 5, 5, 10,
true);
1228 auto lp = make_simplex<double>(p);
1229 const auto st = lp->
solve();
1231 for (
size_t j = 0; j < p.n; ++j)
1233 for (
size_t i = 0; i < p.m(); ++i)
1235 std::vector<double>
row(p.n);
1236 for (
size_t j = 0; j < p.n; ++j)
1237 row[j] =
double(p.a[i][j]);
1240 const auto sr = rs.
solve();
1241 bool ok = int(st) == int(sr);
1247 if (not ok
and ++failures <= 10)
1248 ADD_FAILURE() << p.str();
1250 EXPECT_EQ(failures, 0u);
Simplex algorithm for linear programming.
size_t size_t int32_t value
size_t size_t int32_t * out
Number Theoretic Transform over Z / MOD Z.
Represents a point with rectangular coordinates in a 2D plane.
Revised Simplex algorithm for linear programming.
void set_objective(size_t j, T coef)
Sets objective coefficient.
void set_constraint_row(size_t i, const T *coefs, T rhs)
Adds a complete constraint.
T objective_value() const
Computes objective function value.
State solve()
Solves the linear program.
bool verify_solution() const
Verifies solution satisfies all constraints.
Linear program solver using the Simplex method.
SensitivityRange< T > rhs_sensitivity(size_t rest_idx) const
Computes sensitivity range for a RHS value.
State
Solution states for the linear program.
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.
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.
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.
T shadow_price(size_t rest_idx) const
Gets the shadow price (dual value) for a constraint.
bool is_basic_variable(size_t var_idx) const
Checks if a variable is basic in the optimal solution.
const T & get_solution(size_t i) const noexcept
Gets the solution value for a specific variable.
void put_objetive_function_coef(size_t i, const T &coef)
Sets a coefficient in the objective function.
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 solve()
Solves the linear program.
std::vector< T > get_all_shadow_prices() const
Gets all shadow prices as a vector.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_abs_function > > abs(const __gmp_expr< T, U > &expr)
ca_size_t magnitude(const ca_index_t c) noexcept
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
std::decay_t< typename HeadC::Item_Type > T
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
and
Check uniqueness with explicit hash + equality functors.
ConstraintType
Constraint type for linear programming.
double add(double a, double b)
double mul(double a, double b)
Built_Graph_T< Graph > build_graph(const size_t n, const std::vector< Edge_Def > &edges)
int num(Node *p)
The number of a node of a built graph.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)