Sources are available on
During my PhD and postdoc, I became quite well-acquainted with numerical optimization. For some round-tables with fellow students, I prepared some slides and codes to illustrate how some optimization algorithms work. While most of the code was rather unpolished, the Simplex and QP optimization routines were acceptably written, and I decided to gather them here for future reference - and showcase my ability to code :computer:
For now, I included only two solvers - one for linear programming, one for quadratic programming - but I will hopefully have the time in the future to polish others I had created and add them as well. Below are two examples, one using EigenOpt::simplex::minimize() and the other featuring EigenOpt::quadratic_programming::Solver. You can find more details on them in the respective documentation.
simplex_example.cpp:
++
#include <Eigen/Dense>
#include <iostream>
int main(
int argc,
char** argv) {
Eigen::VectorXd f(2); f << -1, 1;
Eigen::MatrixXd C(5, 2); C << -4,-1,
1,-4,
2,-1,
-1, 0,
0,-1;
Eigen::VectorXd d(5); d << -5, -3, 8, 0, 0;
double tolerance = 1e-6;
Eigen::VectorXd x;
std::string message;
std::cout << "Solution: " << x.transpose() << std::endl;
return 0;
}
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.
int main(int argc, char **argv)
qp_example.cpp:
++
#include <Eigen/Dense>
#include <iostream>
int main(
int argc,
char** argv) {
Eigen::MatrixXd Q(1, 2); Q << 1, 1;
Eigen::VectorXd r(1); r << 5;
Eigen::MatrixXd A(1, 2); A << 1, -1;
Eigen::VectorXd b(1); b << 10;
Eigen::MatrixXd C(1, 2); C << 1, 4;
Eigen::VectorXd d(1); d << 0;
double tolerance = 1e-6;
qp::Solver<double> solver(Q, r, tolerance);
solver.setConstraints(A, b, C, d);
Eigen::VectorXd x;
solver.solve(x);
std::cout << "Solution: " << x.transpose() << std::endl;
return 0;
}
Contains the solver used for linearly constrained quadratic optimization problems.
A more interesting example can be found in example_pixel_robot.cpp, which contains the code to create a very simple simulator for a planar mobile robot that has to move in a rather cluttered environment. Quadratic Programming is used to find the command to be sent to the robot so that it tracks a target (the mouse) while avoiding obstacles.
Target-following robot