39 #include <visp3/core/vpLinProg.h> 99 const unsigned int m = A.
getRows();
100 const unsigned int n = A.
getCols();
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())
266 const unsigned int m = A.
getRows();
267 const unsigned int n = A.
getCols();
270 const unsigned int r = A.
qrPivot(Q, R, P,
false,
false, tol);
287 #ifdef VISP_HAVE_CPP11_COMPATIBILITY 350 std::vector<BoundedIndex> l, std::vector<BoundedIndex> u,
353 const unsigned int n = c.
getRows();
354 const unsigned int m = A.
getRows();
355 const unsigned int p = C.
getRows();
358 const bool feasible =
362 && (find_if(l.begin(), l.end(),
363 [&](
BoundedIndex &i){
return x[i.first] < i.second-tol;})
365 && (find_if(u.begin(), u.end(),
366 [&](
BoundedIndex &i){
return x[i.first] > i.second+tol;})
370 if(!feasible && m && l.size() == 0 && u.size() == 0)
389 std::cout <<
"vpLinProg::simplex: equality constraints not feasible" << std::endl;
394 unsigned int s1 = 0, s2 = 0;
395 for(
unsigned int i = 0; i < n; ++i)
397 const auto cmp = [&](
const BoundedIndex &p){
return p.first == i;};
399 const bool has_low = find_if(l.begin(), l.end(), cmp) != l.end();
401 const bool has_up = find_if(u.begin(), u.end(), cmp) != u.end();
402 if(has_low == has_up)
411 A.
resize(m+p+s2, n+p+s1,
false);
418 for(
unsigned int i = 0; i < p; ++i)
422 for(
unsigned int j = 0; j < n; ++j)
434 unsigned int k1 = 0, k2 = 0;
435 for(
unsigned int i = 0; i < n; ++i)
439 {
return p.first == i;};
442 const auto low = find_if(l.begin(), l.end(), cmp);
444 const auto up = find_if(u.begin(), u.end(), cmp);
451 for(
unsigned int j = 0; j < m+p; ++j)
452 A[j][n+p+k1] = -A[j][i];
455 x[i] = std::max(x[i], 0.);
456 x[n+p+k1] = std::max(-x[i], 0.);
464 for(
unsigned int j = 0; j < m+p; ++j)
467 x[i] = up->second - x[i];
473 z0[i] = -low->second;
476 A[m+p+k2][i] = A[m+p+k2][n+p+k1] = 1;
477 b[m+p+k2] = up->second - low->second;
480 x[i] = up->second - x[i];
481 x[n+p+k1] = x[i] - low->second;
488 x[i] = x[i] - low->second;
565 const unsigned int n = c.
getRows();
571 (m != 0 && !
allClose(A, x, b, tol)) ||
572 [&x,&n](){
unsigned int non0 = 0;
573 for(
unsigned int i = 0; i < n; ++i)
585 for(
unsigned int i = 0; i < m; ++i)
601 for(
unsigned int j = 0; j < n; ++j)
608 std::cout <<
"vpLinProg::simplex: constraints not feasible" << std::endl;
620 std::cout <<
"vpLinProg::simplex: equality constraint not feasible" << std::endl;
634 std::vector<unsigned int> B, N;
636 for(
unsigned int i = 0; i < n; ++i)
638 if(std::abs(x[i]) > tol)
643 std::vector<unsigned int> null_rows;
644 for(
unsigned int i = 0; i < m; ++i)
647 for(
unsigned int j = 0; j < B.size(); ++j)
649 if(std::abs(A[i][B[j]]) > tol)
656 null_rows.push_back(i);
661 for(
unsigned int j = n; j-- > 0; )
663 if(std::abs(x[j]) < tol)
669 for(
unsigned int i = 0; i < null_rows.size(); ++i)
672 if(std::abs(A[null_rows[i]][j]) > tol)
674 null_rows.erase(null_rows.begin()+i);
680 if(!in_N && null_rows.size())
686 for(
unsigned int i = 0; i < null_rows.size(); ++i)
688 if(std::abs(A[null_rows[i]][j]) > tol)
691 null_rows.erase(null_rows.begin()+i);
705 for(
unsigned int j = 0; j < m; ++j)
708 for(
unsigned int i = 0; i < m; ++i)
709 Ab[i][j] = A[i][B[j]];
711 for(
unsigned int j = 0; j < n-m; ++j)
714 for(
unsigned int i = 0; i < m; ++i)
715 An[i][j] = A[i][N[j]];
718 std::vector<std::pair<double, unsigned int>> a;
727 R = cn - An.
t()*Abi.
t()*cb;
729 for(j = 0; j < N.size(); ++j)
747 for(
unsigned int k = 0; k < m; ++k)
750 a.push_back({-x[B[k]]/db[k], k});
753 const auto amin = std::min_element(a.begin(), a.end());
754 const double alpha = amin->first;
755 const unsigned int k = amin->second;
759 for(
unsigned int i = 0; i < B.size(); ++i)
760 x[B[i]] += alpha * db[i];
765 std::swap(cb[k], cn[j]);
766 for(
unsigned int i = 0; i < m; ++i)
767 std::swap(Ab[i][k], An[i][j]);
770 std::swap(B[k],N[j]);
Implementation of a matrix and operations on matrices.
static vpMatrix juxtaposeMatrices(const vpMatrix &A, const vpMatrix &B)
vpColVector extract(unsigned int r, unsigned int colsize) const
static bool colReduction(vpMatrix &A, vpColVector &b, bool full_rank=false, const double &tol=1e-6)
static bool allClose(const vpMatrix &A, const vpColVector &x, const vpColVector &b, const double &tol=1e-6)
double infinityNorm() const
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
std::pair< unsigned int, double > BoundedIndex
void stack(const vpMatrix &A)
vpMatrix inverseTriangular(bool upper=true) const
static bool allGreater(const vpColVector &x, const double &thr=1e-6)
unsigned int getCols() const
unsigned int qrPivot(vpMatrix &Q, vpMatrix &R, vpMatrix &P, bool full=false, bool squareR=false, double tol=1e-6) const
vpRowVector getRow(const unsigned int i) const
unsigned int qr(vpMatrix &Q, vpMatrix &R, bool full=false, bool squareR=false, double tol=1e-6) const
static bool simplex(const vpColVector &c, vpMatrix A, vpColVector b, vpColVector &x, const double &tol=1e-6)
double infinityNorm() const
vpMatrix extract(unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols) 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)
vpColVector getCol(const unsigned int j) const
vpMatrix transpose() const
static bool rowReduction(vpMatrix &A, vpColVector &b, const double &tol=1e-6)
unsigned int getRows() const
vpMatrix inverseByQR() const
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)
void resize(const unsigned int i, const bool flagNullify=true)