34 #include <visp3/core/vpLinProg.h>
137 for (
unsigned int j = 0; j < n; ++j)
140 A = IQQt.
extract(0, 0, n, n - m);
141 if (A.
qr(Q, R,
false,
false, tol) != n - m) {
144 for (j0 = 0; j0 < n; ++j0) {
151 unsigned int j = j0 + 1;
156 if (A.
qr(Q, R,
false,
false, tol) != A.
getCols())
180 for (
unsigned int j = 0; j < n; ++j)
183 A = IQQt.
extract(0, 0, n, n - r);
184 if (A.
qr(Q, R,
false,
false, tol) != n - r) {
187 for (j0 = 0; j0 < n; ++j0) {
194 unsigned int j = j0 + 1;
199 if (A.
qr(Q, R,
false,
false, tol) != A.
getCols())
269 const unsigned int r = A.
qrPivot(Q, R, P,
false,
false, tol);
284 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
350 vpColVector &x, std::vector<BoundedIndex> l, std::vector<BoundedIndex> u,
const double &tol)
354 const unsigned int p = C.
getRows();
357 const bool feasible =
360 (find_if(l.begin(), l.end(), [&](
BoundedIndex &i) { return x[i.first] < i.second - tol; }) == l.end()) &&
361 (find_if(u.begin(), u.end(), [&](
BoundedIndex &i) { return x[i.first] > i.second + tol; }) == u.end());
364 if (!feasible && m && l.size() == 0 && u.size() == 0) {
378 std::cout <<
"vpLinProg::simplex: equality constraints not feasible" << std::endl;
383 unsigned int s1 = 0, s2 = 0;
384 for (
unsigned int i = 0; i < n; ++i) {
386 return bi.first == i;
389 const bool has_low = find_if(l.begin(), l.end(), cmp) != l.end();
391 const bool has_up = find_if(u.begin(), u.end(), cmp) != u.end();
392 if (has_low == has_up) {
400 A.
resize(m + p + s2, n + p + s1,
false);
403 x.
resize(n + p + s1,
false);
407 for (
unsigned int i = 0; i < p; ++i) {
410 for (
unsigned int j = 0; j < n; ++j)
411 A[m + i][j] = C[i][j];
417 P.
eye(n, n + p + s1);
423 unsigned int k1 = 0, k2 = 0;
424 for (
unsigned int i = 0; i < n; ++i) {
427 return bi.first == i;
431 const auto low = find_if(l.begin(), l.end(), cmp);
433 const auto up = find_if(u.begin(), u.end(), cmp);
439 P[i][n + p + k1] = -1;
440 for (
unsigned int j = 0; j < m + p; ++j)
441 A[j][n + p + k1] = -A[j][i];
443 x[i] = std::max<double>(x[i], 0.);
444 x[n + p + k1] = std::max<double>(-x[i], 0.);
452 for (
unsigned int j = 0; j < m + p; ++j)
455 x[i] = up->second - x[i];
461 z0[i] = -low->second;
464 A[m + p + k2][i] = A[m + p + k2][n + p + k1] = 1;
465 b[m + p + k2] = up->second - low->second;
467 x[i] = up->second - x[i];
468 x[n + p + k1] = x[i] - low->second;
475 x[i] = x[i] - low->second;
560 unsigned int non0 = 0;
561 for (
unsigned int i = 0; i < n; ++i) {
575 for (
unsigned int i = 0; i < m; ++i) {
588 for (
unsigned int j = 0; j < n; ++j)
595 std::cout <<
"vpLinProg::simplex: constraints not feasible" << std::endl;
606 std::cout <<
"vpLinProg::simplex: equality constraint not feasible" << std::endl;
617 vpMatrix Ab(m, m), An(m, n - m), Abi;
619 std::vector<unsigned int> B, N;
621 for (
unsigned int i = 0; i < n; ++i) {
622 if (std::abs(x[i]) > tol)
627 std::vector<unsigned int> null_rows;
628 for (
unsigned int i = 0; i < m; ++i) {
630 for (
unsigned int j = 0; j < B.size(); ++j) {
631 if (std::abs(A[i][B[j]]) > tol) {
637 null_rows.push_back(i);
642 for (
unsigned int j = n; j-- > 0;) {
643 if (std::abs(x[j]) < tol) {
647 for (
unsigned int i = 0; i < null_rows.size(); ++i) {
649 if (std::abs(A[null_rows[i]][j]) > tol) {
650 null_rows.erase(null_rows.begin() + i);
656 if (!in_N && null_rows.size()) {
660 for (
unsigned int i = 0; i < null_rows.size(); ++i) {
661 if (std::abs(A[null_rows[i]][j]) > tol) {
663 null_rows.erase(null_rows.begin() + i);
677 for (
unsigned int j = 0; j < m; ++j) {
679 for (
unsigned int i = 0; i < m; ++i)
680 Ab[i][j] = A[i][B[j]];
682 for (
unsigned int j = 0; j < n - m; ++j) {
684 for (
unsigned int i = 0; i < m; ++i)
685 An[i][j] = A[i][N[j]];
688 std::vector<std::pair<double, unsigned int> > a;
696 R = cn - An.
t() * Abi.
t() * cb;
698 for (j = 0; j < N.size(); ++j) {
715 for (
unsigned int k = 0; k < m; ++k) {
717 a.push_back({ -x[B[k]] / db[k], k });
720 const auto amin = std::min_element(a.begin(), a.end());
721 const double alpha = amin->first;
722 const unsigned int k = amin->second;
726 for (
unsigned int i = 0; i < B.size(); ++i)
727 x[B[i]] += alpha * db[i];
732 std::swap(cb[k], cn[j]);
733 for (
unsigned int i = 0; i < m; ++i)
734 std::swap(Ab[i][k], An[i][j]);
737 std::swap(B[k], N[j]);
unsigned int getCols() const
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
unsigned int getRows() const
Implementation of column vector and the associated operations.
vpColVector extract(unsigned int r, unsigned int colsize) const
double infinityNorm() const
void resize(unsigned int i, bool flagNullify=true)
static bool allGreater(const vpColVector &x, const double &thr=1e-6)
static bool rowReduction(vpMatrix &A, vpColVector &b, const double &tol=1e-6)
static bool solveLP(const vpColVector &c, vpMatrix A, vpColVector b, const vpMatrix &C, const vpColVector &d, vpColVector &x, std::vector< BoundedIndex > l={}, std::vector< BoundedIndex > u={}, const double &tol=1e-6)
static bool allZero(const vpColVector &x, const double &tol=1e-6)
static bool allLesser(const vpMatrix &C, const vpColVector &x, const vpColVector &d, const double &thr=1e-6)
static bool allClose(const vpMatrix &A, const vpColVector &x, const vpColVector &b, const double &tol=1e-6)
static bool colReduction(vpMatrix &A, vpColVector &b, bool full_rank=false, const double &tol=1e-6)
static bool simplex(const vpColVector &c, vpMatrix A, vpColVector b, vpColVector &x, const double &tol=1e-6)
std::pair< unsigned int, double > BoundedIndex
Implementation of a matrix and operations on matrices.
unsigned int qr(vpMatrix &Q, vpMatrix &R, bool full=false, bool squareR=false, double tol=1e-6) const
void stack(const vpMatrix &A)
vpMatrix inverseTriangular(bool upper=true) const
static vpMatrix juxtaposeMatrices(const vpMatrix &A, const vpMatrix &B)
vpRowVector getRow(unsigned int i) const
unsigned int qrPivot(vpMatrix &Q, vpMatrix &R, vpMatrix &P, bool full=false, bool squareR=false, double tol=1e-6) const
double infinityNorm() const
vpColVector getCol(unsigned int j) const
vpMatrix inverseByQR() const
vpMatrix transpose() const
vpMatrix extract(unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols) const