2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * \brief Tests routines in neldermead.h .
39 * \author Christian Blau <blau@kth.se>
44 #include "gromacs/math/neldermead.h"
46 #include <gtest/gtest.h>
48 #include "testutils/testasserts.h"
58 /* \brief Test the NelderMeadSimplex class.
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]
64 class NelderMeadSimplexTest : public ::testing::Test
67 //! Set up a one-d, two-vertices Nelder-Mead simplex
68 NelderMeadSimplexTest() :
69 initialGuess_{ 1 }, simplex_{ NelderMeadSimplexTest::doubleFirstCoordinateValue, initialGuess_ }
73 //! Function that the simplex is meant help optimise
74 static real doubleFirstCoordinateValue(ArrayRef<const real> x) { return 2 * x[0]; }
77 std::vector<real> initialGuess_;
78 NelderMeadSimplex simplex_;
81 TEST_F(NelderMeadSimplexTest, BestVertex)
83 EXPECT_REAL_EQ(simplex_.bestVertex().coordinate_[0], 1);
84 EXPECT_REAL_EQ(simplex_.bestVertex().value_, 2);
87 TEST_F(NelderMeadSimplexTest, WorstVertex)
89 EXPECT_REAL_EQ(simplex_.worstVertex().coordinate_[0], 1.05);
90 EXPECT_REAL_EQ(simplex_.worstVertex().value_, 2.1);
93 TEST_F(NelderMeadSimplexTest, SecondWorstValue)
95 EXPECT_REAL_EQ(simplex_.secondWorstValue(), 2.00);
98 TEST_F(NelderMeadSimplexTest, ReflectionPoint)
101 simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).coordinate_[0],
104 simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).value_, 1.9);
107 TEST_F(NelderMeadSimplexTest, EvaluateExpansionPoint)
110 simplex_.evaluateExpansionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).coordinate_[0],
113 simplex_.evaluateExpansionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).value_, 1.8);
116 TEST_F(NelderMeadSimplexTest, EvaluateContractionPoint)
119 simplex_.evaluateContractionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).coordinate_[0],
122 simplex_.evaluateContractionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).value_,
127 TEST_F(NelderMeadSimplexTest, SwapOutWorst)
129 // introduce a new vertex that we know is better than any in the
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
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
143 simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).coordinate_[0],
146 simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).value_, -6);
149 TEST_F(NelderMeadSimplexTest, ShrinkSimplexPointsExceptBest)
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
156 simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).coordinate_[0],
159 simplex_.evaluateReflectionPoint(NelderMeadSimplexTest::doubleFirstCoordinateValue).value_, 1.95);
162 TEST_F(NelderMeadSimplexTest, OrientedLength)
164 // here, the distance between the two vertices
165 EXPECT_REAL_EQ(simplex_.orientedLength(), 0.05);