14template<
class Scalar,
class D1,
class D2,
class D3,
class D4>
16 const Eigen::DenseBase<D1>& _f,
17 const Eigen::DenseBase<D2>& _C,
18 const Eigen::DenseBase<D3>& _d,
19 Eigen::DenseBase<D4>& x,
20 std::string& halt_reason,
21 const Scalar& small_number,
22 const Scalar& large_number
29 eigen_assert(small_number>0 &&
"PARAMETER 'small_number' MUST BE POSITIVE");
44 eigen_assert(n>0 &&
"THE PROBLEM DOES NOT HAVE ANY VARIABLE");
45 f = VectorXs::Zero(n);
50 eigen_assert(_C.rows()==_d.rows() &&
"C MATRIX AND D VECTOR HAVE DIFFERENT NUMBER OF ROWS");
51 eigen_assert(_C.cols()==n &&
"C MATRIX HAS WRONG NUMBER OF COLUMNS");
57 halt_reason =
"No constraints given, the problem is ill-defined";
63 MatrixXs C(_C.rows(), _C.cols());
64 VectorXs d(_d.rows());
68 for(
unsigned int i=0; i<_C.rows(); i++) {
69 if(!_C.row(i).isZero(small_number)) {
71 C.row(m).noalias() = _C.row(i);
77 halt_reason =
"Found infeasible degenerate constraint (row " + std::to_string(i) +
").";
83 C.conservativeResize(m, C.cols());
84 d.conservativeResize(m);
98 VectorXs fs = T.transpose()*f;
100 const unsigned int nv = T.cols();
107 std::vector<int> basic_variables;
114 const unsigned int na = tableau.cols() - nv - m - 1;
116#ifdef EIGEN_SIMPLEX_DEBUG_ON
118 for(
unsigned int i=0; i<m; i++) {
119 EigenOpt_SIMPLEX_DBG(
"- " << basic_variables[i] <<
" (" << (basic_variables[i]<nv+m ?
"slack" :
"artificial") <<
")");
124 if(large_number > 0) {
136 VectorXs xv = VectorXs::Zero(nv);
137 for(
unsigned int i = 0; i<m; i++) {
138 if(basic_variables[i] < nv) {
139 xv(basic_variables[i]) = tableau(i, tableau.cols()-1);
145 eigen_assert((C*VectorXs(x) - d).maxCoeff() < small_number &&
"Something went horribly wrong: Simplex optimization was completed 'successfully' but constraints are not respected.");
146 halt_reason =
"Optimization completed successfully";
151template<
class Scalar,
class D1,
class D2,
class D3,
class D4,
class D5,
class D6>
153 const Eigen::MatrixBase<D1>& f,
154 const Eigen::MatrixBase<D2>& A,
155 const Eigen::MatrixBase<D3>& b,
156 const Eigen::MatrixBase<D4>& C,
157 const Eigen::MatrixBase<D5>& d,
158 Eigen::DenseBase<D6>& x,
159 std::string& halt_reason,
160 const Scalar& small_number,
161 const Scalar& large_number
170 #ifdef EigenOpt_SIMPLEX_USE_QR_INSTEAD_OF_SVD
178 if(!(A*xeq-b).isZero(small_number)) {
179 halt_reason =
"Equality constraints are infeasible.";
191 halt_reason =
"The solution is fully determined by equality constraints";
206 bool ok =
minimize(Z.transpose()*f, C*Z, d-C*xeq, y, halt_reason, small_number, large_number);
208 halt_reason =
"Failed to solve the inequality constrained sub-problem: " + halt_reason;
bool simplex(Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &tableau, std::vector< int > &basic_variables, const Scalar &small_number, std::string &halt_reason)
Perform successive pivot operations until a termination condition is met.
bool transformation_matrix(const Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &C, const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &d, const Scalar &small_number, Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &T, std::string &halt_reason)
Calculate a transformation matrix so that , .
void create_tableau(const Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &C, const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &d, Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &tableau, std::vector< int > &basic_variables)
Create a Simplex Tableau given a set of inequality constraints.
bool two_steps_method(const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &objective, Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &tableau, std::vector< int > &basic_variables, unsigned int na, const Scalar &small_number, std::string &halt_reason)
Solve a minimization problem using the two-steps Simplex method.
bool penalty_method(const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &objective, Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &tableau, std::vector< int > &basic_variables, unsigned int na, const Scalar &small_number, const Scalar &large_number, std::string &halt_reason)
Solve a minimization problem using the penalty Simplex method.
bool minimize(const Eigen::MatrixBase< D1 > &f, const Eigen::MatrixBase< D2 > &A, const Eigen::MatrixBase< D3 > &b, const Eigen::MatrixBase< D4 > &C, const Eigen::MatrixBase< D5 > &d, Eigen::DenseBase< D6 > &x, std::string &halt_reason, const Scalar &small_number, const Scalar &large_number=-1)
Solve a constrained linear optimization problem.
Main namespace of this project, containing all utilities.
void svd_projection(const Eigen::MatrixBase< D1 > &A, const Eigen::MatrixBase< D2 > &b, Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &Z, Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &xeq)
Return all solutions to a linear system, using a kernel-based parameterization.
void qr_projection(const Eigen::MatrixBase< D1 > &A, const Eigen::MatrixBase< D2 > &b, Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &Z, Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &xeq)
Return all solutions to a linear system, using a kernel-based parameterization.
#define EigenOpt_SIMPLEX_DBG(x)
#define EigenOptTypedefs(ScalarType)