Apply clang-format-11
[alexxy/gromacs.git] / src / gromacs / math / tests / neldermead.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020,2021, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  *
37  * \brief Tests routines in neldermead.h .
38  *
39  * \author Christian Blau <blau@kth.se>
40  */
41
42 #include "gmxpre.h"
43
44 #include "gromacs/math/neldermead.h"
45
46 #include <gtest/gtest.h>
47
48 #include "testutils/testasserts.h"
49
50 namespace gmx
51 {
52 namespace test
53 {
54 namespace
55 {
56
57
58 /* \brief Test the NelderMeadSimplex class.
59  *
60  * Use a one-dimensional function to create a simplex with two vertices.
61  * The simplex is created at [1, 1.05] with function values [2, 2.1]
62  * respectively.
63  */
64 class NelderMeadSimplexTest : public ::testing::Test
65 {
66 public:
67     //! Set up a one-d, two-vertices Nelder-Mead simplex
68     NelderMeadSimplexTest() :
69         initialGuess_{ 1 }, simplex_{ NelderMeadSimplexTest::doubleFirstCoordinateValue, initialGuess_ }
70     {
71     }
72
73     //! Function that the simplex is meant help optimise
74     static real doubleFirstCoordinateValue(ArrayRef<const real> x) { return 2 * x[0]; }
75
76 protected:
77     std::vector<real> initialGuess_;
78     NelderMeadSimplex simplex_;
79 };
80
81 TEST_F(NelderMeadSimplexTest, BestVertex)
82 {
83     EXPECT_REAL_EQ(simplex_.bestVertex().coordinate_[0], 1);
84     EXPECT_REAL_EQ(simplex_.bestVertex().value_, 2);
85 }
86
87 TEST_F(NelderMeadSimplexTest, WorstVertex)
88 {
89     EXPECT_REAL_EQ(simplex_.worstVertex().coordinate_[0], 1.05);
90     EXPECT_REAL_EQ(simplex_.worstVertex().value_, 2.1);
91 }
92
93 TEST_F(NelderMeadSimplexTest, SecondWorstValue)
94 {
95     EXPECT_REAL_EQ(simplex_.secondWorstValue(), 2.00);
96 }
97
98 TEST_F(NelderMeadSimplexTest, ReflectionPoint)
99 {
100     EXPECT_REAL_EQ(
101             simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).coordinate_[0],
102             0.95);
103     EXPECT_REAL_EQ(
104             simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).value_, 1.9);
105 }
106
107 TEST_F(NelderMeadSimplexTest, EvaluateExpansionPoint)
108 {
109     EXPECT_REAL_EQ(
110             simplex_.evaluateExpansionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).coordinate_[0],
111             0.9);
112     EXPECT_REAL_EQ(
113             simplex_.evaluateExpansionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).value_, 1.8);
114 }
115
116 TEST_F(NelderMeadSimplexTest, EvaluateContractionPoint)
117 {
118     EXPECT_REAL_EQ(
119             simplex_.evaluateContractionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).coordinate_[0],
120             1.025);
121     EXPECT_REAL_EQ(
122             simplex_.evaluateContractionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).value_,
123             2.05);
124 }
125
126
127 TEST_F(NelderMeadSimplexTest, SwapOutWorst)
128 {
129     // introduce a new vertex that we know is better than any in the
130     // intial simplex
131     RealFunctionvalueAtCoordinate newVertex = { { 0 }, 0 };
132     simplex_.swapOutWorst(newVertex);
133     EXPECT_REAL_EQ(simplex_.bestVertex().coordinate_[0], 0);
134     EXPECT_REAL_EQ(simplex_.bestVertex().value_, 0);
135     // introduce a new vertex that we know is worse than any in the
136     // intial simplex
137     newVertex = { { 3 }, 6 };
138     simplex_.swapOutWorst(newVertex);
139     EXPECT_REAL_EQ(simplex_.worstVertex().coordinate_[0], 3);
140     EXPECT_REAL_EQ(simplex_.worstVertex().value_, 6);
141     // check that also the reflection point has changed now
142     EXPECT_REAL_EQ(
143             simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).coordinate_[0],
144             -3);
145     EXPECT_REAL_EQ(
146             simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).value_, -6);
147 }
148
149 TEST_F(NelderMeadSimplexTest, ShrinkSimplexPointsExceptBest)
150 {
151     simplex_.shrinkSimplexPointsExceptBest(NelderMeadSimplexTest::doubleFirstCoordinateValue);
152     EXPECT_REAL_EQ(simplex_.worstVertex().coordinate_[0], 1.025);
153     EXPECT_REAL_EQ(simplex_.worstVertex().value_, 2.05);
154     // check that also the reflection point has changed now
155     EXPECT_REAL_EQ(
156             simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).coordinate_[0],
157             0.975);
158     EXPECT_REAL_EQ(
159             simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).value_, 1.95);
160 }
161
162 TEST_F(NelderMeadSimplexTest, OrientedLength)
163 {
164     // here, the distance between the two vertices
165     EXPECT_REAL_EQ(simplex_.orientedLength(), 0.05);
166 }
167
168 } // namespace
169 } // namespace test
170 } // namespace gmx