File: lrsdp_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 (315 lines) | stat: -rw-r--r-- 9,238 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
 * @file lrsdp_test.cpp
 * @author Ryan Curtin
 * @author Marcus Edel
 * @author Conrad Sanderson
 *
 * 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.
 */

#include <ensmallen.hpp>
#include "catch.hpp"
#include "test_types.hpp"

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

/**
 * Create a Lovasz-Theta initial point.
 */
template<typename MatType>
void CreateLovaszThetaInitialPoint(const MatType& edges,
                                   MatType& coordinates)
{
  // Get the number of vertices in the problem.
  const size_t vertices = max(max(edges)) + 1;

  const size_t m = edges.n_cols + 1;
  float r = 0.5 + sqrt(0.25 + 2 * m);
  if (ceil(r) > vertices)
    r = vertices; // An upper bound on the dimension.

  coordinates.set_size(vertices, ceil(r));

  // Now we set the entries of the initial matrix according to the formula given
  // in Section 4 of Monteiro and Burer.
  for (size_t i = 0; i < vertices; ++i)
  {
    for (size_t j = 0; j < ceil(r); ++j)
    {
      if (i == j)
        coordinates(i, j) = sqrt(1.0 / r) + sqrt(1.0 / (vertices * m));
      else
        coordinates(i, j) = sqrt(1.0 / (vertices * m));
    }
  }
}

/**
 * Prepare an LRSDP object to solve the Lovasz-Theta SDP in the manner detailed
 * in Monteiro + Burer 2004.  The list of edges in the graph must be given; that
 * is all that is necessary to set up the problem.  A matrix which will contain
 * initial point coordinates should be given also.
 */
template<typename MatType>
void SetupLovaszTheta(const MatType& edges,
                      LRSDP<SDP<MatType>>& lovasz,
                      typename ForwardType<MatType>::bvec& lambda)
{
  // Get the number of vertices in the problem.
  const size_t vertices = max(max(edges)) + 1;

  // C = -(e e^T) = -ones().
  lovasz.SDP().C().ones(vertices, vertices);
  lovasz.SDP().C() *= -1;

  // b_0 = 1; else = 0.
  lovasz.SDP().SparseB().zeros(edges.n_cols + 1);
  lovasz.SDP().SparseB()[0] = 1;

  // A_0 = I_n.
  lovasz.SDP().SparseA()[0].eye(vertices, vertices);

  // A_ij only has ones at (i, j) and (j, i) and 0 elsewhere.
  for (size_t i = 0; i < edges.n_cols; ++i)
  {
    lovasz.SDP().SparseA()[i + 1].zeros(vertices, vertices);
    lovasz.SDP().SparseA()[i + 1](edges(0, i), edges(1, i)) = 1.;
    lovasz.SDP().SparseA()[i + 1](edges(1, i), edges(0, i)) = 1.;
  }

  // Set the Lagrange multipliers right.
  lambda.ones(edges.n_cols + 1);
  lambda *= -1;
  lambda[0] = -((typename MatType::elem_type) vertices);
}

/**
 * johnson8-4-4.co test case for Lovasz-Theta LRSDP.
 * See Monteiro and Burer 2004.
 */
TEMPLATE_TEST_CASE("Johnson844LovaszThetaSDP", "[LRSDP]", ENS_TEST_TYPES)
{
  typedef typename TestType::elem_type ElemType;

  // Load the edges.
  TestType edges;

  if (edges.load("data/johnson8-4-4.csv", csv_ascii) == false)
  {
    FAIL("couldn't load data");
    return;
  }

  edges = edges.t();

  // The LRSDP itself and the initial point.
  TestType coordinates;

  CreateLovaszThetaInitialPoint(edges, coordinates);

  LRSDP<SDP<TestType>> lovasz(edges.n_cols + 1, 0, coordinates);

  typedef typename ForwardType<TestType>::bvec VecType;
  VecType lambda;
  SetupLovaszTheta(edges, lovasz, lambda);

  double sigma = 10;
  ElemType finalValue = lovasz.Optimize(coordinates, lambda, sigma);

  // Final value taken from Monteiro + Burer 2004.
  REQUIRE(finalValue == Approx(-14.0).epsilon(Tolerances<TestType>::Obj));

  // Now ensure that all the constraints are satisfied.
  TestType rrt = coordinates * trans(coordinates);
  REQUIRE(trace(rrt) == Approx(1.0).epsilon(Tolerances<TestType>::Obj));

  // All those edge constraints...
  for (size_t i = 0; i < edges.n_cols; ++i)
  {
    REQUIRE(rrt(edges(0, i), edges(1, i)) ==
        Approx(0.0).margin(100 * Tolerances<TestType>::Obj));
    REQUIRE(rrt(edges(1, i), edges(0, i)) ==
        Approx(0.0).margin(100 * Tolerances<TestType>::Obj));
  }
}

/**
 * Create an unweighted graph laplacian from the edges.
 */
template<typename ElemType>
void CreateSparseGraphLaplacian(const Mat<ElemType>& edges,
                                SpMat<ElemType>& laplacian)
{
  // Get the number of vertices in the problem.
  const size_t vertices = max(max(edges)) + 1;

  laplacian.zeros(vertices, vertices);

  for (size_t i = 0; i < edges.n_cols; ++i)
  {
    laplacian(edges(0, i), edges(1, i)) = ElemType(-1);
    laplacian(edges(1, i), edges(0, i)) = ElemType(-1);
  }

  for (size_t i = 0; i < vertices; ++i)
  {
    laplacian(i, i) = -accu(laplacian.row(i));
  }
}

TEMPLATE_TEST_CASE("ErdosRenyiRandomGraphMaxCutSDP", "[LRSDP]", ENS_TEST_TYPES)
{
  typedef typename TestType::elem_type ElemType;

  // Load the edges.
  TestType edges;

  if (edges.load("data/erdosrenyi-n100.csv", csv_ascii) == false)
  {
    FAIL("couldn't load data");
    return;
  }

  edges = edges.t();

  SpMat<ElemType> laplacian;
  CreateSparseGraphLaplacian(edges, laplacian);

  float r = 0.5 + sqrt(0.25 + 2 * edges.n_cols);
  if (ceil(r) > laplacian.n_rows)
    r = laplacian.n_rows;

  // initialize coordinates to a feasible point
  TestType coordinates(laplacian.n_rows, ceil(r));
  coordinates.zeros();
  for (size_t i = 0; i < coordinates.n_rows; ++i)
  {
    coordinates(i, i % coordinates.n_cols) = ElemType(1);
  }

  LRSDP<SDP<SpMat<ElemType>>> maxcut(laplacian.n_rows, 0, coordinates);
  maxcut.SDP().C() = laplacian;
  maxcut.SDP().C() *= -1.; // need to minimize the negative
  maxcut.SDP().SparseB().ones(laplacian.n_rows);
  for (size_t i = 0; i < laplacian.n_rows; ++i)
  {
    maxcut.SDP().SparseA()[i].zeros(laplacian.n_rows, laplacian.n_rows);
    maxcut.SDP().SparseA()[i](i, i) = ElemType(1);
  }

  const ElemType finalValue = maxcut.Optimize(coordinates);
  const TestType rrt = coordinates * trans(coordinates);

  for (size_t i = 0; i < laplacian.n_rows; ++i)
  {
    REQUIRE(rrt(i, i) == Approx(1.0).epsilon(Tolerances<TestType>::Obj));
  }

  // Final value taken by solving with Mosek
  REQUIRE(finalValue ==
      Approx(-3672.7).epsilon(100 * Tolerances<TestType>::Coord));
}

/*
 * Test a nuclear norm minimization SDP.
 *
 * Specifically, fix an unknown m x n matrix X. Our goal is to recover X from p
 * measurements of X, where the i-th measurement is of the form
 *
 *    b_i = dot(A_i, X)
 *
 * where the A_i's have iid entries from Normal(0, 1/p). We do this by solving
 * the the following semi-definite program
 *
 *    min ||X||_* subj to dot(A_i, X) = b_i, i=1,...,p
 *
 * where ||X||_* denotes the nuclear norm (sum of singular values) of X. The
 * equivalent SDP is
 *
 *    min tr(W1) + tr(W2) : [ W1, X ; X', W2 ] is PSD,
 *                          dot(A_i, X) = b_i, i = 1, ..., p
 *
 * For more details on matrix sensing and nuclear norm minimization, see
 *
 *    Guaranteed Minimum-Rank Solutions of Linear Matrix Equations via Nuclear
 *    Norm Minimization.
 *    Benjamin Recht, Maryam Fazel, Pablo Parrilo.
 *    SIAM Review 2010.
 *
 */
TEMPLATE_TEST_CASE("GaussianMatrixSensingSDP", "[LRSDP]", ENS_TEST_TYPES)
{
  typedef typename TestType::elem_type ElemType;

  TestType Xorig, A;

  // read the unknown matrix X and the measurement matrices A_i in

  if (Xorig.load("data/sensing_X.csv", csv_ascii) == false)
  {
    FAIL("couldn't load data");
  }

  if (A.load("data/sensing_A.csv", csv_ascii) == false)
  {
    FAIL("couldn't load data");
  }

  const size_t m = Xorig.n_rows;
  const size_t n = Xorig.n_cols;
  const size_t p = A.n_rows;
  assert(A.n_cols == m * m);

  Col<ElemType> b(p);
  for (size_t i = 0; i < p; ++i)
  {
    const TestType Ai = reshape(A.row(i), n, m);
    b(i) = dot(trans(Ai), Xorig);
  }

  float r = 0.5 + sqrt(0.25 + 2 * p);
  if (ceil(r) > m + n)
    r = m + n;

  TestType coordinates;
  coordinates.eye(m + n, ceil(r));

  LRSDP<SDP<SpMat<ElemType>>> sensing(0, p, coordinates, 15);
  sensing.SDP().C().eye(m + n, m + n);
  sensing.SDP().DenseB() = 2. * b;

  const span blockRows(0, m - 1);
  const span blockCols(m, m + n - 1);

  for (size_t i = 0; i < p; ++i)
  {
    const TestType Ai = reshape(A.row(i), n, m);
    sensing.SDP().DenseA()[i].zeros(m + n, m + n);
    sensing.SDP().DenseA()[i](blockRows, blockCols) = trans(Ai);
    sensing.SDP().DenseA()[i](blockCols, blockRows) = Ai;
  }

  ElemType finalValue = sensing.Optimize(coordinates);
  REQUIRE(finalValue == Approx(44.7550132629).epsilon(
      Tolerances<TestType>::LargeObj));

  const TestType rrt = coordinates * trans(coordinates);
  for (size_t i = 0; i < p; ++i)
  {
    const TestType Ai = reshape(A.row(i), n, m);
    const ElemType measurement = dot(trans(Ai), rrt(blockRows, blockCols));
    // Custom tolerances because floats can do very bad here.
    const ElemType eps = std::is_same<ElemType, float>::value ? 0.1 : 0.001;
    REQUIRE(measurement == Approx(b(i)).epsilon(eps));
  }

  // check matrix recovery
  const ElemType err = norm(Xorig - rrt(blockRows, blockCols), "fro") /
      norm(Xorig, "fro");
  REQUIRE(err == Approx(0.0).margin(10 * Tolerances<TestType>::LargeObj));
}