39 #include <visp3/core/vpLinProg.h> 140 for(
unsigned int j = 0; j < n; ++j)
144 if(A.
qr(Q, R,
false,
false, tol) != n-m)
148 for(j0 = 0; j0 < n; ++j0)
157 unsigned int j = j0+1;
164 if(A.
qr(Q, R,
false,
false, tol) != A.
getCols())
191 for(
unsigned int j = 0; j < n; ++j)
195 if(A.
qr(Q, R,
false,
false, tol) != n-r)
199 for(j0 = 0; j0 < n; ++j0)
208 unsigned int j = j0+1;
215 if(A.
qr(Q, R,
false,
false, tol) != A.
getCols())
283 const unsigned int r = A.
qrPivot(Q, R, P,
false,
false, tol);
300 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 363 std::vector<BoundedIndex> l, std::vector<BoundedIndex> u,
368 const unsigned int p = C.
getRows();
371 const bool feasible =
375 && (find_if(l.begin(), l.end(),
376 [&](
BoundedIndex &i){
return x[i.first] < i.second-tol;})
378 && (find_if(u.begin(), u.end(),
379 [&](
BoundedIndex &i){
return x[i.first] > i.second+tol;})
383 if(!feasible && m && l.size() == 0 && u.size() == 0)
402 std::cout <<
"vpLinProg::simplex: equality constraints not feasible" << std::endl;
407 unsigned int s1 = 0, s2 = 0;
408 for(
unsigned int i = 0; i < n; ++i)
411 return bi.first == i;
414 const bool has_low = find_if(l.begin(), l.end(), cmp) != l.end();
416 const bool has_up = find_if(u.begin(), u.end(), cmp) != u.end();
417 if(has_low == has_up)
426 A.
resize(m+p+s2, n+p+s1,
false);
433 for(
unsigned int i = 0; i < p; ++i)
437 for(
unsigned int j = 0; j < n; ++j)
449 unsigned int k1 = 0, k2 = 0;
450 for(
unsigned int i = 0; i < n; ++i)
454 return bi.first == i;
458 const auto low = find_if(l.begin(), l.end(), cmp);
460 const auto up = find_if(u.begin(), u.end(), cmp);
467 for(
unsigned int j = 0; j < m+p; ++j)
468 A[j][n+p+k1] = -A[j][i];
471 x[i] = std::max(x[i], 0.);
472 x[n+p+k1] = std::max(-x[i], 0.);
480 for(
unsigned int j = 0; j < m+p; ++j)
483 x[i] = up->second - x[i];
489 z0[i] = -low->second;
492 A[m+p+k2][i] = A[m+p+k2][n+p+k1] = 1;
493 b[m+p+k2] = up->second - low->second;
496 x[i] = up->second - x[i];
497 x[n+p+k1] = x[i] - low->second;
504 x[i] = x[i] - low->second;
588 (m != 0 && !
allClose(A, x, b, tol)) ||
589 [&x,&n](){
unsigned int non0 = 0;
590 for(
unsigned int i = 0; i < n; ++i)
602 for(
unsigned int i = 0; i < m; ++i)
618 for(
unsigned int j = 0; j < n; ++j)
625 std::cout <<
"vpLinProg::simplex: constraints not feasible" << std::endl;
637 std::cout <<
"vpLinProg::simplex: equality constraint not feasible" << std::endl;
651 std::vector<unsigned int> B, N;
653 for(
unsigned int i = 0; i < n; ++i)
655 if(std::abs(x[i]) > tol)
660 std::vector<unsigned int> null_rows;
661 for(
unsigned int i = 0; i < m; ++i)
664 for(
unsigned int j = 0; j < B.size(); ++j)
666 if(std::abs(A[i][B[j]]) > tol)
673 null_rows.push_back(i);
678 for(
unsigned int j = n; j-- > 0; )
680 if(std::abs(x[j]) < tol)
686 for(
unsigned int i = 0; i < null_rows.size(); ++i)
689 if(std::abs(A[null_rows[i]][j]) > tol)
691 null_rows.erase(null_rows.begin()+i);
697 if(!in_N && null_rows.size())
703 for(
unsigned int i = 0; i < null_rows.size(); ++i)
705 if(std::abs(A[null_rows[i]][j]) > tol)
708 null_rows.erase(null_rows.begin()+i);
722 for(
unsigned int j = 0; j < m; ++j)
725 for(
unsigned int i = 0; i < m; ++i)
726 Ab[i][j] = A[i][B[j]];
728 for(
unsigned int j = 0; j < n-m; ++j)
731 for(
unsigned int i = 0; i < m; ++i)
732 An[i][j] = A[i][N[j]];
735 std::vector<std::pair<double, unsigned int>> a;
744 R = cn - An.
t()*Abi.
t()*cb;
746 for(j = 0; j < N.size(); ++j)
764 for(
unsigned int k = 0; k < m; ++k)
767 a.push_back({-x[B[k]]/db[k], k});
770 const auto amin = std::min_element(a.begin(), a.end());
771 const double alpha = amin->first;
772 const unsigned int k = amin->second;
776 for(
unsigned int i = 0; i < B.size(); ++i)
777 x[B[i]] += alpha * db[i];
782 std::swap(cb[k], cn[j]);
783 for(
unsigned int i = 0; i < m; ++i)
784 std::swap(Ab[i][k], An[i][j]);
787 std::swap(B[k],N[j]);
vpMatrix extract(unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols) const
Implementation of a matrix and operations on matrices.
static vpMatrix juxtaposeMatrices(const vpMatrix &A, const vpMatrix &B)
static bool colReduction(vpMatrix &A, vpColVector &b, bool full_rank=false, const double &tol=1e-6)
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
static bool allClose(const vpMatrix &A, const vpColVector &x, const vpColVector &b, const double &tol=1e-6)
vpMatrix inverseByQR() const
unsigned int qrPivot(vpMatrix &Q, vpMatrix &R, vpMatrix &P, bool full=false, bool squareR=false, double tol=1e-6) const
std::pair< unsigned int, double > BoundedIndex
void stack(const vpMatrix &A)
vpColVector extract(unsigned int r, unsigned int colsize) const
unsigned int getRows() const
static bool allGreater(const vpColVector &x, const double &thr=1e-6)
vpMatrix inverseTriangular(bool upper=true) const
double infinityNorm() const
unsigned int getCols() const
static bool simplex(const vpColVector &c, vpMatrix A, vpColVector b, vpColVector &x, const double &tol=1e-6)
double infinityNorm() const
unsigned int qr(vpMatrix &Q, vpMatrix &R, bool full=false, bool squareR=false, double tol=1e-6) const
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)
vpRowVector getRow(unsigned int i) const
static bool rowReduction(vpMatrix &A, vpColVector &b, const double &tol=1e-6)
vpColVector getCol(unsigned int j) const
vpMatrix transpose() const
void resize(unsigned int i, bool flagNullify=true)
Implementation of column vector and the associated operations.
static bool allLesser(const vpMatrix &C, const vpColVector &x, const vpColVector &d, const double &thr=1e-6)
static bool allZero(const vpColVector &x, const double &tol=1e-6)