39 #include <visp3/core/vpLinProg.h> 135 for (
unsigned int j = 0; j < n; ++j)
138 A = IQQt.
extract(0, 0, n, n - m);
139 if (A.
qr(Q, R,
false,
false, tol) != n - m) {
142 for (j0 = 0; j0 < n; ++j0) {
149 unsigned int j = j0 + 1;
154 if (A.
qr(Q, R,
false,
false, tol) != A.
getCols())
178 for (
unsigned int j = 0; j < n; ++j)
181 A = IQQt.
extract(0, 0, n, n - r);
182 if (A.
qr(Q, R,
false,
false, tol) != n - r) {
185 for (j0 = 0; j0 < n; ++j0) {
192 unsigned int j = j0 + 1;
197 if (A.
qr(Q, R,
false,
false, tol) != A.
getCols())
262 const unsigned int r = A.
qrPivot(Q, R, P,
false,
false, tol);
277 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 340 vpColVector &x, std::vector<BoundedIndex> l, std::vector<BoundedIndex> u,
const double &tol)
344 const unsigned int p = C.
getRows();
347 const bool feasible =
350 (find_if(l.begin(), l.end(), [&](
BoundedIndex &i) {
return x[i.first] < i.second - tol; }) == l.end()) &&
351 (find_if(u.begin(), u.end(), [&](
BoundedIndex &i) {
return x[i.first] > i.second + tol; }) == u.end());
354 if (!feasible && m && l.size() == 0 && u.size() == 0) {
367 std::cout <<
"vpLinProg::simplex: equality constraints not feasible" << std::endl;
372 unsigned int s1 = 0, s2 = 0;
373 for (
unsigned int i = 0; i < n; ++i) {
374 const auto cmp = [&](
const BoundedIndex &bi) {
return bi.first == i; };
376 const bool has_low = find_if(l.begin(), l.end(), cmp) != l.end();
378 const bool has_up = find_if(u.begin(), u.end(), cmp) != u.end();
379 if (has_low == has_up) {
387 A.
resize(m + p + s2, n + p + s1,
false);
390 x.
resize(n + p + s1,
false);
394 for (
unsigned int i = 0; i < p; ++i) {
397 for (
unsigned int j = 0; j < n; ++j)
398 A[m + i][j] = C[i][j];
404 P.
eye(n, n + p + s1);
410 unsigned int k1 = 0, k2 = 0;
411 for (
unsigned int i = 0; i < n; ++i) {
413 const auto cmp = [&](
const BoundedIndex &bi) {
return bi.first == i; };
416 const auto low = find_if(l.begin(), l.end(), cmp);
418 const auto up = find_if(u.begin(), u.end(), cmp);
424 P[i][n + p + k1] = -1;
425 for (
unsigned int j = 0; j < m + p; ++j)
426 A[j][n + p + k1] = -A[j][i];
428 x[i] = std::max(x[i], 0.);
429 x[n + p + k1] = std::max(-x[i], 0.);
436 for (
unsigned int j = 0; j < m + p; ++j)
439 x[i] = up->second - x[i];
444 z0[i] = -low->second;
447 A[m + p + k2][i] = A[m + p + k2][n + p + k1] = 1;
448 b[m + p + k2] = up->second - low->second;
450 x[i] = up->second - x[i];
451 x[n + p + k1] = x[i] - low->second;
457 x[i] = x[i] - low->second;
539 unsigned int non0 = 0;
540 for (
unsigned int i = 0; i < n; ++i)
552 for (
unsigned int i = 0; i < m; ++i) {
564 for (
unsigned int j = 0; j < n; ++j)
571 std::cout <<
"vpLinProg::simplex: constraints not feasible" << std::endl;
582 std::cout <<
"vpLinProg::simplex: equality constraint not feasible" << std::endl;
593 vpMatrix Ab(m, m), An(m, n - m), Abi;
595 std::vector<unsigned int> B, N;
597 for (
unsigned int i = 0; i < n; ++i) {
598 if (std::abs(x[i]) > tol)
603 std::vector<unsigned int> null_rows;
604 for (
unsigned int i = 0; i < m; ++i) {
606 for (
unsigned int j = 0; j < B.size(); ++j) {
607 if (std::abs(A[i][B[j]]) > tol) {
613 null_rows.push_back(i);
618 for (
unsigned int j = n; j-- > 0;) {
619 if (std::abs(x[j]) < tol) {
623 for (
unsigned int i = 0; i < null_rows.size(); ++i) {
625 if (std::abs(A[null_rows[i]][j]) > tol) {
626 null_rows.erase(null_rows.begin() + i);
632 if (!in_N && null_rows.size()) {
636 for (
unsigned int i = 0; i < null_rows.size(); ++i) {
637 if (std::abs(A[null_rows[i]][j]) > tol) {
639 null_rows.erase(null_rows.begin() + i);
653 for (
unsigned int j = 0; j < m; ++j) {
655 for (
unsigned int i = 0; i < m; ++i)
656 Ab[i][j] = A[i][B[j]];
658 for (
unsigned int j = 0; j < n - m; ++j) {
660 for (
unsigned int i = 0; i < m; ++i)
661 An[i][j] = A[i][N[j]];
664 std::vector<std::pair<double, unsigned int> > a;
672 R = cn - An.
t() * Abi.
t() * cb;
674 for (j = 0; j < N.size(); ++j) {
691 for (
unsigned int k = 0; k < m; ++k) {
693 a.push_back({-x[B[k]] / db[k], k});
696 const auto amin = std::min_element(a.begin(), a.end());
697 const double alpha = amin->first;
698 const unsigned int k = amin->second;
702 for (
unsigned int i = 0; i < B.size(); ++i)
703 x[B[i]] += alpha * db[i];
708 std::swap(cb[k], cn[j]);
709 for (
unsigned int i = 0; i < m; ++i)
710 std::swap(Ab[i][k], An[i][j]);
713 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)
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)
double infinityNorm() const
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
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
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)
vpMatrix transpose() const
static bool rowReduction(vpMatrix &A, vpColVector &b, const double &tol=1e-6)
unsigned int getRows() const
vpMatrix inverseByQR() 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)
vpColVector getCol(unsigned int j) const