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
|
/**
* @file grid_search_test.cpp
* @author Ryan Curtin
*
* Test file for the GridSearch optimizer.
*
* 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 ens;
using namespace ens::test;
// An implementation of a simple categorical function. The parameters can be
// understood as x = [c1 c2 c3]. When c1 = 0, c2 = 2, and c3 = 1, the value of
// f(x) is 0. In any other case, the value of f(x) is 10. Therefore, the
// optimum is found at [0, 2, 1].
class SimpleCategoricalFunction
{
public:
// Return the objective function f(x) as described above.
template<typename MatType>
typename MatType::elem_type Evaluate(const MatType& x)
{
if (size_t(x(0)) == 0 && size_t(x(1)) == 2 && size_t(x(2)) == 1)
return 0;
else
return 10;
}
};
#ifdef ENS_USE_COOT
TEMPLATE_TEST_CASE("GridSearch_SimpleCategoricalFunction", "[GridSearch]",
ENS_ALL_TEST_TYPES, arma::imat, coot::imat)
#else
TEMPLATE_TEST_CASE("GridSearch_SimpleCategoricalFunction", "[GridSearch]",
ENS_ALL_TEST_TYPES, arma::imat)
#endif
{
// Create and optimize the categorical function with the GridSearch
// optimizer. We must also create a std::vector<bool> that holds the types
// of each dimension, and an arma::Row<size_t> that holds the number of
// categories in each dimension.
SimpleCategoricalFunction c;
// We have three categorical dimensions only.
std::vector<bool> categoricalDimensions;
categoricalDimensions.push_back(true);
categoricalDimensions.push_back(true);
categoricalDimensions.push_back(true);
// The first category can take 5 values; the second can take 3; the third can
// take 12.
arma::Row<size_t> numCategories("5 3 12");
// The initial point for our optimization will be to set all categories to 0.
TestType params("0 0 0");
// Now create the GridSearch optimizer with default parameters, and run the
// optimization.
// The GridSearch type can be replaced with any ensmallen optimizer that
// is able to handle categorical functions.
GridSearch gs;
gs.Optimize(c, params, categoricalDimensions, numCategories);
REQUIRE(params(0) == 0);
REQUIRE(params(1) == 2);
REQUIRE(params(2) == 1);
}
|