5 #ifndef COSAN_RANDOMGRIDSEARCH_H
6 #define COSAN_RANDOMGRIDSEARCH_H
12 Derived<CosanModel> Model,
13 Derived<CosanMetric<NumericType>> Metric,
14 Derived<Splitter> Split,
15 typename =
typename std::enable_if<std::is_arithmetic<NumericType>::value,
NumericType>::type>
23 const std::vector<NumericType> & paramGrid,
long unsigned int nsamples= 100):
Search() {
24 NumericType minError = std::numeric_limits<NumericType>::infinity();
27 std::vector<NumericType> RandomChoice;
28 std::sample(paramGrid.begin(), paramGrid.end(), std::back_inserter(RandomChoice),
29 std::min({paramGrid.size(),nsamples}), std::mt19937{std::random_device{}()});
32 for (gsl::index i = 0; i < RandomChoice.size(); ++i){
33 currParam = paramGrid[i];
34 estimator.SetParams(paramGrid[i]);
36 if (currError < minError)
50 Derived<CosanModel> Model,
51 Derived<CosanMetric<NumericType>> Metric,
52 Derived<Splitter> Split,
53 typename =
typename std::enable_if<std::is_arithmetic<NumericType>::value,
NumericType>::type>
61 const std::vector<NumericType> & paramGrid,
long unsigned int nsamples = 100,
int nthreads = -1):
Search() {
62 NumericType minError = std::numeric_limits<NumericType>::infinity();
65 std::vector<NumericType> RandomChoice;
66 std::sample(paramGrid.begin(), paramGrid.end(), std::back_inserter(RandomChoice),
67 std::min({paramGrid.size(),nsamples}), std::mt19937{std::random_device{}()});
69 std::vector<NumericType> allError(RandomChoice.size());
71 omp_set_num_threads(omp_get_max_threads());
74 omp_set_num_threads(nthreads);
76 #pragma omp parallel for
77 for (gsl::index i = 0; i < RandomChoice.size(); ++i){
78 estimator.SetParams(RandomChoice[i]);
81 bestParam =RandomChoice[std::distance(allError.begin(), std::min_element(allError.begin(), allError.end()))];
91 Derived<CosanModel> Model,
92 Derived<CosanMetric<NumericType>> Metric,
93 Derived<Splitter> Split,
94 typename =
typename std::enable_if<std::is_arithmetic<NumericType>::value,
NumericType>::type>
102 const std::vector<std::vector<NumericType>> & paramGrid,
103 long unsigned int nsamples = 100):
Search() {
104 NumericType minError = std::numeric_limits<NumericType>::infinity();
106 std::vector<std::vector<NumericType>> RandomChoice;
107 std::sample(paramGrid.begin(), paramGrid.end(), std::back_inserter(RandomChoice),
108 std::min({paramGrid.size(),nsamples}), std::mt19937{std::random_device{}()});
110 decltype(bestParam) currParam;
112 for (gsl::index i = 0; i < RandomChoice.size(); ++i){
113 currParam = paramGrid[i];
114 estimator.SetParams(paramGrid[i]);
116 if (currError < minError)
118 minError = currError;
119 bestParam = currParam;
130 Derived<CosanModel> Model,
131 Derived<CosanMetric<NumericType>> Metric,
132 Derived<Splitter> Split,
133 typename =
typename std::enable_if<std::is_arithmetic<NumericType>::value,
NumericType>::type>
141 const std::vector<std::vector<NumericType>> & paramGrid,
142 long unsigned int nsamples = 100,
int nthreads = -1):
Search() {
143 NumericType minError = std::numeric_limits<NumericType>::infinity();
145 std::vector<NumericType> RandomChoice;
146 std::sample(paramGrid.begin(), paramGrid.end(), std::back_inserter(RandomChoice),
147 std::min({paramGrid.size(),nsamples}), std::mt19937{std::random_device{}()});
148 std::vector<std::vector<NumericType>> allError(paramGrid.size());
150 omp_set_num_threads(omp_get_max_threads());
153 omp_set_num_threads(nthreads);
155 #pragma omp parallel for
156 for (gsl::index i = 0; i < paramGrid.size(); ++i){
157 estimator.SetParams(paramGrid[i]);
160 bestParam =paramGrid[std::distance(allError.begin(), std::min_element(allError.begin(), allError.end()))];
168 #endif //COSAN_RANDOMGRIDSEARCH_H