add tests

This commit is contained in:
Suvarsha Chennareddy
2023-06-12 19:03:32 +05:30
parent 8baccf8990
commit 52c4d63b89
5 changed files with 114 additions and 11 deletions
@@ -138,8 +138,6 @@ class ActiveCMAES
MatType& iterate,
CallbackTypes&&... callbacks);
//typedef typename TransformationPolicyType::BaseMatType BMatType;
//! Get the population size.
size_t PopulationSize() const { return lambda; }
//! Modify the population size.
@@ -120,13 +120,10 @@ typename MatType::elem_type ActiveCMAES<SelectionPolicyType,
if (lambda == 0)
lambda = (4 + std::round(3 * std::log(iterate.n_elem))) * 10;
// Parent weights.
// Parent number.
const size_t mu = std::round(lambda / 4);
/*
BaseMatType w = std::log(mu + 0.5) - arma::log(
arma::linspace<BaseMatType>(0, mu - 1, mu) + 1.0);
*/
//Recombination weight (w = 1/ (parent number));
const double w = 1.0 / mu;
// Number of effective solutions.
@@ -343,12 +340,12 @@ typename MatType::elem_type ActiveCMAES<SelectionPolicyType,
}
// Output current objective function.
Info << "CMA-ES: iteration " << i << ", objective " << overallObjective
Info << "Active CMA-ES: iteration " << i << ", objective " << overallObjective
<< "." << std::endl;
if (std::isnan(overallObjective) || std::isinf(overallObjective))
{
Warn << "CMA-ES: converged to " << overallObjective << "; "
Warn << "Active CMA-ES: converged to " << overallObjective << "; "
<< "terminating with failure. Try a smaller step size?" << std::endl;
iterate = transformationPolicy.Transform(iterate);
@@ -359,7 +356,7 @@ typename MatType::elem_type ActiveCMAES<SelectionPolicyType,
if (std::abs(lastObjective - overallObjective) < tolerance)
{
Info << "CMA-ES: minimized within tolerance " << tolerance << "; "
Info << "Active CMA-ES: minimized within tolerance " << tolerance << "; "
<< "terminating optimization." << std::endl;
iterate = transformationPolicy.Transform(iterate);
+1
View File
@@ -1,6 +1,7 @@
# The tests that need to be compiled.
set(ENSMALLEN_TESTS_SOURCES
main.cpp
active_cmaes_test.cpp
ada_belief_test.cpp
ada_bound_test.cpp
ada_delta_test.cpp
+96
View File
@@ -0,0 +1,96 @@
/**
* @file active_cmaes_test.cpp
* @author Suvarsha Chennareddy
*
* 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_function_tools.hpp"
using namespace ens;
using namespace ens::test;
/**
* Run Active CMA-ES with the full selection policy on Rosenbrock function and
* make sure the results are acceptable.
* This test uses the deprecated constructor and therefore can be removed
* in a future version of ensmallen.
*/
TEST_CASE("ActiveCMAESDeprecatedConstructorRosenbrockFunctionTest", "[ActiveCMAESTest]")
{
ActiveCMAES<FullSelection, BoundaryBoxConstraint<>>
activecmaes(0, 0, 2, 32, 0, 1e-3);
activecmaes.StepSize() = 0.075;
FunctionTest<RosenbrockFunction>(activecmaes, 0.1, 0.15);
}
/**
* Run Active CMA-ES with the full selection policy on Rosenbrock function and
* make sure the results are acceptable.
*/
TEST_CASE("ActiveCMAESRosenbrockFunctionTest", "[ActiveCMAESTest]")
{
BoundaryBoxConstraint<> b(0, 2);
ActiveCMAES<FullSelection, BoundaryBoxConstraint<>>
activecmaes(0, b, 32, 0, 1e-3);
activecmaes.StepSize() = 0.075;
FunctionTest<RosenbrockFunction>(activecmaes, 0.1, 0.15);
}
/**
* Run Active CMA-ES with the random selection policy on Rosenbrock function and
* make sure the results are acceptable.
*/
TEST_CASE("ApproxActiveCMAESRosenbrockFunctionTest", "[ActiveCMAESTest]")
{
BoundaryBoxConstraint<> b(0, 2);
ApproxActiveCMAES<BoundaryBoxConstraint<arma::mat>>
activecmaes(0, b, 32, 0, 1e-3);
activecmaes.StepSize() = 0.075;
FunctionTest<RosenbrockFunction>(activecmaes, 0.1, 0.15);
}
/**
* Run Active CMA-ES with the full selection policy on Rosenbrock function and
* make sure the results are acceptable. Use arma::fmat.
*/
TEST_CASE("ActiveCMAESRosenbrockFunctionFMatTest", "[ActiveCMAESTest]")
{
BoundaryBoxConstraint<arma::fmat> b(0, 2);
ActiveCMAES<FullSelection, BoundaryBoxConstraint<arma::fmat>>
activecmaes(0, b, 32, 0, 1e-3);
activecmaes.StepSize() = 0.075;
FunctionTest<RosenbrockFunction, arma::fmat>(activecmaes, 0.1, 0.15);
}
/**
* Run Active CMA-ES with the random selection policy on Rosenbrock function and
* make sure the results are acceptable. Use arma::fmat.
*/
TEST_CASE("ApproxActiveCMAESRosenbrockFunctionFMatTest", "[ActiveCMAESTest]")
{
BoundaryBoxConstraint<arma::fmat> b(0, 2);
ApproxActiveCMAES<BoundaryBoxConstraint<arma::fmat>>
activecmaes(0, b, 32, 0, 1e-3);
activecmaes.StepSize() = 0.075;
FunctionTest<RosenbrockFunction, arma::fmat>(activecmaes, 0.1, 0.15);
}
/**
* Run Active CMA-ES with the random selection and empty transformation policies
* on Rosenbrock function and make sure the results are acceptable.
* Use arma::fmat.
*/
TEST_CASE("ApproxActiveCMAESEmptyTransformationRosenbrockFunctionFMatTest",
"[ActiveCMAESTest]")
{
ApproxActiveCMAES<EmptyTransformation<arma::fmat>>
activecmaes(0, EmptyTransformation<arma::fmat>(), 32, 0, 1e-3);
LogisticRegressionFunctionTest<arma::fmat>(activecmaes, 0.01, 0.02, 5);
}
+12 -1
View File
@@ -256,6 +256,17 @@ TEST_CASE("EarlyStopAtMinLossCustomLambdaTest", "[CallbacksTest]")
REQUIRE(std::abs(coordinates[i]) >= 3.0);
}
/**
* Make sure we invoke all callbacks (CMAES).
*/
TEST_CASE("ActiveCMAESCallbacksFullFunctionTest", "[CallbacksTest]")
{
BoundaryBoxConstraint<> b(-1, 1);
ActiveCMAES<FullSelection, BoundaryBoxConstraint<>> optimizer(0, b, 32, 3, 1e-3);
CallbacksFullFunctionTest(optimizer, true, false, false, false, true, true,
false, false, true);
}
/**
* Make sure we invoke all callbacks (AdaBound).
*/
@@ -315,7 +326,7 @@ TEST_CASE("CMAESCallbacksFullFunctionTest", "[CallbacksTest]")
BoundaryBoxConstraint<> b(-1, 1);
CMAES<FullSelection, BoundaryBoxConstraint<>> optimizer(0, b, 32, 3, 1e-3);
CallbacksFullFunctionTest(optimizer, true, false, false, false, true, true,
false, false, true);
false, false, true);
}
/**