File: frankwolfe_test.cpp

package info (click to toggle)
ensmallen 3.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,952 kB
  • sloc: cpp: 44,963; sh: 186; makefile: 35
file content (206 lines) | stat: -rw-r--r-- 6,401 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
 * @file frankwolfe_test.cpp
 * @author Chenzhe Diao
 * @author Marcus Edel
 *
 * Test file for Frank-Wolfe type optimizer.
 *
 * ensmallen is free software; you may redistribute it and/or modify it under
 * the terms of the 3-clause BSD license.  You should have received a copy of
 * the 3-clause BSD license along with ensmallen.  If not, see
 * http://www.opensource.org/licenses/BSD-3-Clause for more information.
 */
#if defined(ENS_USE_COOT)
  #include <armadillo>
  #include <bandicoot>
#endif
#include <ensmallen.hpp>
#include "catch.hpp"
#include "test_function_tools.hpp"
#include "test_types.hpp"

using namespace arma;
using namespace ens;
using namespace ens::test;

/**
 * Simple test of Orthogonal Matching Pursuit algorithm.
 */
TEMPLATE_TEST_CASE("FrankWolfe_OMP", "[FrankWolfe]", arma::mat)
{
  typedef typename TestType::elem_type ElemType;

  const int k = 5;
  TestType B1 = arma::eye<TestType>(3, 3);
  TestType B2 = 0.1 * arma::randn<TestType>(3, k);
  // The dictionary is input as columns of A.
  TestType A = join_horiz(B1, B2);
  // Vector to be sparsely approximated.
  arma::Col<ElemType> b = {1.0, 1.0, 0.0};

  FuncSq f(A, b);
  ConstrLpBallSolver linearConstrSolver(1);
  UpdateSpan updateRule;

  OMP s(linearConstrSolver, updateRule);

  TestType coordinates = zeros<TestType>(k + 3, 1);
  ElemType result = s.Optimize(f, coordinates);

  const double margin = Tolerances<TestType>::Coord;

  REQUIRE(result == Approx(0.0).margin(margin));
  REQUIRE(coordinates(0) - 1 == Approx(0.0).margin(margin));
  REQUIRE(coordinates(1) - 1 == Approx(0.0).margin(margin));
  REQUIRE(coordinates(2) == Approx(0.0).margin(margin));
  for (int ii = 0; ii < k; ++ii)
  {
    REQUIRE(coordinates[ii + 3] == Approx(0.0).margin(margin));
  }
}

/**
 * Simple test of Orthogonal Matching Pursuit with regularization.
 */
TEMPLATE_TEST_CASE("FrankWolfe_RegularizedOMP", "[FrankWolfe]", arma::mat)
{
  typedef typename TestType::elem_type ElemType;

  const int k = 10;
  TestType B1 = 0.1 * arma::eye<TestType>(k, k);
  TestType B2 = 100 * arma::randn<TestType>(k, k);
  // The dictionary is input as columns of A.
  TestType A = join_horiz(B1, B2);
  // Vector to be sparsely approximated.
  arma::Col<ElemType> b(k, arma::fill::zeros);
  b(0) = 1;
  b(1) = 1;
  arma::Col<ElemType> lambda(A.n_cols);
  for (size_t ii = 0; ii < A.n_cols; ii++)
    lambda(ii) = norm(A.col(ii), 2);

  FuncSq f(A, b);
  ConstrLpBallSolver linearConstrSolver(1, lambda);
  UpdateSpan updateRule;

  OMP s(linearConstrSolver, updateRule);

  TestType coordinates = zeros<TestType>(2 * k, 1);
  ElemType result = s.Optimize(f, coordinates);

  REQUIRE(result == Approx(0.0).margin(Tolerances<TestType>::Coord));
}

/**
 * Simple test of Orthogonal Matching Pursuit with support prune.
 */
TEMPLATE_TEST_CASE("FrankWolfe_PruneSupportOMP", "[FrankWolfe]", arma::mat)
{
  typedef typename TestType::elem_type ElemType;

  // The dictionary is input as columns of A.
  const int k = 3;
  TestType B1 = { { 1.0, 0.0, 1.0 },
                  { 0.0, 1.0, 1.0 },
                  { 0.0, 0.0, 1.0 } };
  TestType B2 = arma::randu<TestType>(k, k);
  // The dictionary is input as columns of A.
  TestType A = join_horiz(B1, B2);
  // Vector to be sparsely approximated.
  arma::Col<ElemType> b = { 1.0, 1.0, 0.0 };

  FuncSq f(A, b);
  ConstrLpBallSolver linearConstrSolver(1);
  UpdateSpan updateRule(true);

  OMP s(linearConstrSolver, updateRule);

  TestType coordinates = zeros<TestType>(k + 3, 1);
  ElemType result = s.Optimize(f, coordinates);

  REQUIRE(result == Approx(0.0).margin(Tolerances<TestType>::Coord));
}

/**
 * Simple test of sparse soluton in atom domain with atom norm constraint.
 */
TEMPLATE_TEST_CASE("FrankWolfe_AtomNormConstraint", "[FrankWolfe]",
    arma::mat)
{
  typedef typename TestType::elem_type ElemType;

  const int k = 5;
  TestType B1 = arma::eye<TestType>(3, 3);
  TestType B2 = 0.1 * arma::randn<TestType>(3, k);
  // The dictionary is input as columns of A.
  TestType A = join_horiz(B1, B2);
  // Vector to be sparsely approximated.
  arma::Col<ElemType> b = { 1.0, 1.0, 0.0 };

  FuncSq f(A, b);
  ConstrLpBallSolver linearConstrSolver(1);
  UpdateFullCorrection updateRule(2, 0.2);

  FrankWolfe<ConstrLpBallSolver, UpdateFullCorrection>
      s(linearConstrSolver, updateRule);

  TestType coordinates = zeros<TestType>(k + 3, 1);
  ElemType result = s.Optimize(f, coordinates);

  REQUIRE(result == Approx(0.0).margin(Tolerances<TestType>::Coord));
}

/**
 * A very simple test of classic Frank-Wolfe algorithm.
 * The constrained domain used is unit lp ball.
 */
TEMPLATE_TEST_CASE("FrankWolfe_Classic", "[FrankWolfe]", arma::mat)
{
  typedef typename TestType::elem_type ElemType;

  TestFuncFW<TestType> f;
  double p = 2;   // Constraint set is unit lp ball.
  ConstrLpBallSolver linearConstrSolver(p);
  UpdateClassic updateRule;

  FrankWolfe<ConstrLpBallSolver, UpdateClassic>
      s(linearConstrSolver, updateRule);

  TestType coordinates = arma::randu<TestType>(3, 1);
  ElemType result = s.Optimize(f, coordinates);

  REQUIRE(result == Approx(0.0).margin(Tolerances<TestType>::Obj));
  const double coordTol = Tolerances<TestType>::Coord;
  REQUIRE(coordinates(0) - 0.1 == Approx(0.0).margin(coordTol));
  REQUIRE(coordinates(1) - 0.2 == Approx(0.0).margin(coordTol));
  REQUIRE(coordinates(2) - 0.3 == Approx(0.0).margin(coordTol));
}

/**
 * Exactly the same problem with ClassicFW.
 * The update step performs a line search now.
 * It converges much faster.
 */
TEMPLATE_TEST_CASE("FrankWolfe_LineSearch", "[FrankWolfe]",
    ENS_FULLPREC_TEST_TYPES)
{
  typedef typename TestType::elem_type ElemType;
  typedef typename ForwardType<TestType>::brow RowType;

  TestFuncFW<TestType> f;
  double p = 2;   // Constraint set is unit lp ball.
  ConstrLpBallSolverType<RowType> linearConstrSolver(p);
  UpdateLineSearch updateRule;

  FrankWolfe<decltype(linearConstrSolver), UpdateLineSearch>
      s(linearConstrSolver, updateRule);

  TestType coordinates = randu<TestType>(3);
  ElemType result = s.Optimize(f, coordinates);

  REQUIRE(result == Approx(0.0).margin(Tolerances<TestType>::Obj));
  const double coordTol = Tolerances<TestType>::Coord;
  REQUIRE(coordinates(0) - 0.1 == Approx(0.0).margin(coordTol));
  REQUIRE(coordinates(1) - 0.2 == Approx(0.0).margin(coordTol));
  REQUIRE(coordinates(2) - 0.3 == Approx(0.0).margin(coordTol));
}