File: aug_lagrangian_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 (94 lines) | stat: -rw-r--r-- 2,825 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
/**
 * @file aug_lagrangian_test.cpp
 * @author Ryan Curtin
 * @author Marcus Edel
 * @author Conrad Sanderson
 *
 * Test of the AugmentedLagrangian class using the test functions defined in
 * aug_lagrangian_test_functions.hpp.
 *
 * 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_types.hpp"

using namespace ens;
using namespace ens::test;

/**
 * Tests the Augmented Lagrangian optimizer using the
 * AugmentedLagrangianTestFunction class.
 */
TEMPLATE_TEST_CASE("AugLagrangian_AugLagrangianTestFunction", "[AugLagrangian]",
    ENS_ALL_CPU_TEST_TYPES)
{
  typedef typename TestType::elem_type ElemType;

  // The choice of 10 memory slots is arbitrary.
  AugLagrangianTestFunction<TestType> f;
  AugLagrangian aug;

  arma::Col<ElemType> coords = f.GetInitialPoint();

  if (!aug.Optimize(f, coords))
    FAIL("Optimization reported failure.");

  ElemType finalValue = f.Evaluate(coords);

  double objTol = Tolerances<TestType>::Obj;
  double coordTol = Tolerances<TestType>::Coord;

  // Low-precision optimization requires wider tolerances than usual.
  if (sizeof(ElemType) < 4)
  {
    objTol = Tolerances<TestType>::LargeObj;
    coordTol = Tolerances<TestType>::LargeCoord;
  }

  REQUIRE(finalValue == Approx(70.0).epsilon(objTol));
  REQUIRE(coords(0) == Approx(1.0).epsilon(coordTol));
  REQUIRE(coords(1) == Approx(4.0).epsilon(coordTol));
}

/**
 * Tests the Augmented Lagrangian optimizer using the Gockenbach function.
 */
TEMPLATE_TEST_CASE("AugLagrangian_GockenbachFunction", "[AugLagrangian]",
    ENS_ALL_TEST_TYPES, ENS_SPARSE_TEST_TYPES)
{
  typedef typename TestType::elem_type ElemType;

  GockenbachFunction f;
  AugLagrangian aug;

  TestType coords = f.GetInitialPoint<TestType>();

  if (!aug.Optimize(f, coords))
    FAIL("Optimization reported failure.");

  ElemType finalValue = f.Evaluate(coords);

  double objTol = 100 * Tolerances<TestType>::Obj;
  double coordTol = 10 * Tolerances<TestType>::Coord;

  // Low-precision optimization requires wider tolerances than usual.
  if (sizeof(ElemType) < 4)
  {
    objTol = Tolerances<TestType>::LargeObj;
    coordTol = 10 * Tolerances<TestType>::LargeCoord;
  }

  // Higher tolerance for smaller values.
  REQUIRE(finalValue == Approx(29.633926).epsilon(objTol));
  REQUIRE(coords(0) == Approx(0.12288178).epsilon(coordTol));
  REQUIRE(coords(1) == Approx(-1.10778185).epsilon(coordTol));
  REQUIRE(coords(2) == Approx(0.015099932).epsilon(coordTol));
}