File: fista_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 (140 lines) | stat: -rw-r--r-- 4,509 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
/**
 * @file fista_test.cpp
 * @author Ryan Curtin
 *
 * Tests for FISTA (Fast Iterative Shrinkage-Thresholding Algorithm).
 *
 * 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"

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

TEMPLATE_TEST_CASE("FISTASimpleTest", "[FISTA]", ENS_TEST_TYPES)
{
  // Make sure that we can get a decent result with no g(x) constraint.
  FISTA<L1Penalty> fista(L1Penalty(0.0), 10000);
  GeneralizedRosenbrockFunction f(20);
  FunctionTest<GeneralizedRosenbrockFunction, TestType>(fista, f,
      100 * Tolerances<TestType>::Obj,
      100 * Tolerances<TestType>::Coord);
}

TEMPLATE_TEST_CASE("FISTASphereFunctionTest", "[FISTA]", ENS_ALL_TEST_TYPES,
    ENS_SPARSE_TEST_TYPES)
{
  // The sphere function optimizes to the origin anyway, so the L1 penalty does
  // not affect the result.
  FISTA<L1Penalty> fista(L1Penalty(0.1));
  FunctionTest<SphereFunction, TestType>(fista,
      Tolerances<TestType>::Obj,
      Tolerances<TestType>::Coord);
}

// FISTA struggles with the fmat type on the Wood function, so we skip it.  Even
// a regular matrix can be a bit tricky so we allow a few trials.
TEMPLATE_TEST_CASE("FISTAWoodFunctionTest", "[FISTA]", arma::mat)
{
  // Set the L1 constraint to be sufficiently large that the final solution is
  // just inside the ball.
  FISTA<L1Constraint> fista(L1Constraint(5.2));
  fista.Tolerance() = 1e-10;
  FunctionTest<WoodFunction, TestType>(fista,
      Tolerances<TestType>::LargeObj,
      Tolerances<TestType>::LargeCoord,
      3);
}

TEMPLATE_TEST_CASE("FISTALogisticRegressionFunctionTest", "[FISTA]",
    ENS_ALL_TEST_TYPES)
{
  FISTA<L1Penalty> fista(L1Penalty(0.001));
  LogisticRegressionFunctionTest<TestType>(fista,
      5 * Tolerances<TestType>::LargeObj,
      5 * Tolerances<TestType>::LargeCoord,
      5);
}

// Check that maxIterations does anything.
TEST_CASE("FISTAMaxIterationsTest", "[FISTA]")
{
  FISTA<L1Penalty> fista1(L1Penalty(0.001)), fista2(L1Penalty(0.001));
  fista1.MaxIterations() = 5;
  fista2.MaxIterations() = 100;

  BoothFunction f;
  mat coordinates1 = f.GetInitialPoint<mat>();
  mat coordinates2 = coordinates1;

  fista1.Optimize(f, coordinates1);
  fista2.Optimize(f, coordinates2);

  // The second optimization should have proceeded further.
  REQUIRE(f.Evaluate(coordinates1) >= f.Evaluate(coordinates2));
}

// Check that the step size estimate works at least reasonably.
TEST_CASE("FISTAStepSizeEstimateTest", "[FISTA]")
{
  QuadraticFunction f;
  FISTA<L1Penalty> fista1;

  mat coordinates1 = f.GetInitialPoint<mat>();
  fista1.Optimize(f, coordinates1);

  // Check the step size to ensure that it's reasonable.  If the guess is
  // perfect, then the step size will be 10 / L, where L = 1.
  REQUIRE(fista1.MaxStepSize() >= 1.0);
  REQUIRE(fista1.MaxStepSize() <= 11.0);
}

// Check what happens when 0 estimate trials are used.
TEST_CASE("FISTAZeroEstimateTrialsTest", "[FISTA]")
{
  QuadraticFunction f;
  FISTA<L1Penalty> fista1;
  REQUIRE_THROWS(fista1 = FISTA<L1Penalty>(1000, 1e-10, 50, 2.0, true, 0));

  FISTA<L1Penalty> fista2;
  fista2.EstimateTrials() = 0;

  mat coordinates = f.GetInitialPoint<mat>();
  REQUIRE_THROWS(fista2.Optimize(f, coordinates));
}

// Check what happens when the step size is manually set to something much
// smaller than the estimate would give.
TEST_CASE("FISTATooSmallManualStepSizeTest", "[FISTA]")
{
  QuadraticFunction f;
  FISTA<L1Penalty> fista(L1Penalty(0.0), 1000, 1e-10, 50, 2.0, false, 10,
      1e-10);

  mat coordinates = f.GetInitialPoint<mat>();
  fista.Optimize(f, coordinates);

  // We should be far away from the optimum.
  REQUIRE(std::abs(coordinates[0]) >= 1.0);
}

// Check that we can converge even when a gigantic manual maximum step size is
// specified.
TEST_CASE("FISTATooLargeManualStepSizeTest", "[FISTA]")
{
  // Use a huge step size.  We should still successfully optimize the function.
  FISTA<L1Penalty> fista(L1Penalty(0.0), 1000, 1e-10, 50, 2.0, false, 10,
      10000.0);
  GeneralizedRosenbrockFunction f(20);
  FunctionTest<GeneralizedRosenbrockFunction, mat>(fista, f);
}