SOPT
Sparse OPTimisation
euclidian_norm.cc
Go to the documentation of this file.
1 #include <exception>
2 #include "sopt/padmm.h"
3 #include "sopt/proximal.h"
5 #include "sopt/types.h"
6 
7 // We will minimize ||x - x_0|| + ||x - x_1||, ||.|| the euclidian norm
8 int main(int, char const **) {
9 
10  // Some type aliases for simplicity
11  using t_Scalar = sopt::t_real;
14 
15  // Creates the target vectors
16  auto constexpr N = 5;
17  t_Vector const target0 = t_Vector::Random(N);
18  t_Vector const target1 = t_Vector::Random(N) * 4;
19 
20  // Creates the resulting proximal
21  // In practice g_0 and g_1 are any functions with the signature
22  // void(t_Vector &output, t_Vector::Scalar regulariser_strength, t_Vector const &input)
23  // They are the proximal of ||x - x_0|| and ||x - x_1||
24  auto prox_g0 = sopt::proximal::translate(sopt::proximal::EuclidianNorm(), -target0);
25  auto prox_g1 = sopt::proximal::translate(sopt::proximal::EuclidianNorm(), -target1);
26 
27  auto padmm = sopt::algorithm::ProximalADMM<t_Scalar>(prox_g0, prox_g1, t_Vector::Zero(N))
28  .itermax(5000)
30  .regulariser_strength(0.01)
31  // Phi == -1, so that we can minimize f(x) + g(x), as per problem definition in
32  // padmm.
33  .Phi(-t_Matrix::Identity(N, N));
34 
35  // Alternatively, padmm can be called with a tuple (x, residual) as argument
36  // Here, we default to (Φ^Ty/ν, ΦΦ^Ty/ν - y)
37  auto const diagnostic = padmm();
38 
39  // diagnostic should tell us the function converged
40  // it also contains diagnostic.niters - the number of iterations, and cg_diagnostic - the
41  // diagnostic from the last call to the conjugate gradient.
42  if (not diagnostic.good) throw std::runtime_error("Did not converge!");
43 
44  // x should be any point on the segment linking x_0 and x_1
45  t_Vector const segment = (target1 - target0).normalized();
46  t_Scalar const alpha = (diagnostic.x - target0).transpose() * segment;
47  if ((target1 - target0).transpose() * segment < alpha)
48  throw std::runtime_error("Point beyond x_1 plane");
49  if (alpha < 0e0) throw std::runtime_error("Point before x_0 plane");
50  if ((diagnostic.x - target0 - alpha * segment).stableNorm() > 1e-8)
51  throw std::runtime_error("Point not on (x_0, x_1) line");
52 
53  return 0;
54 }
constexpr auto N
Definition: wavelets.cc:57
sopt::Vector< Scalar > t_Vector
sopt::Matrix< Scalar > t_Matrix
Proximal Alternate Direction method of mutltipliers.
Definition: padmm.h:19
ProximalADMM< Scalar > & is_converged(std::function< bool(t_Vector const &x)> const &func)
Convergence function that takes only the output as argument.
Definition: padmm.h:112
Proximal of euclidian norm.
Definition: proximal.h:18
Translation< FUNCTION, VECTOR > translate(FUNCTION const &func, VECTOR const &translation)
Translates given proximal by given vector.
Definition: proximal.h:362
double t_real
Root of the type hierarchy for real numbers.
Definition: types.h:17
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector of a given type.
Definition: types.h:24
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > Matrix
A matrix of a given type.
Definition: types.h:29
int main(int, char const **)