35 #ifndef SHARK_ALGORITHMS_GRIDSEARCH_H 36 #define SHARK_ALGORITHMS_GRIDSEARCH_H 42 #include <boost/serialization/vector.hpp> 81 void configure(
size_t params,
double min,
double max,
size_t numSections)
87 for (
size_t i = 0; i < numSections; i++)
89 double section = min + i * (max - min) / (numSections - 1.0);
91 node.push_back(section);
100 void configure(
const std::vector<double>& min,
const std::vector<double>& max,
const std::vector<size_t>& sections)
102 size_t params = min.size();
108 for (
size_t dimension = 0; dimension < params; dimension++)
110 size_t numSections = sections[dimension];
112 node.resize(numSections);
114 if ( numSections == 1 )
116 node[0] = (( min[dimension] + max[dimension] ) / 2.0);
118 else for (
size_t section = 0; section < numSections; section++)
120 node[section] = (min[dimension] + section * (max[dimension] - min[dimension]) / (numSections - 1.0));
134 void configure(
double min1,
double max1,
size_t sections1,
double min2,
double max2,
size_t sections2)
143 if ( sections1 == 1 ) {
146 for (
size_t section = 0; section < sections1; section++)
147 m_nodeValues[0].push_back(min1 + section * (max1 - min1) / (sections1 - 1.0));
150 if ( sections2 == 1 ) {
153 for (
size_t section = 0; section < sections2; section++)
154 m_nodeValues[1].push_back(min2 + section * (max2 - min2) / (sections2 - 1.0));
162 void configure(
double min1,
double max1,
size_t sections1)
169 if ( sections1 == 1 ) {
172 for (
size_t section = 0; section < sections1; section++)
173 m_nodeValues[0].push_back(min1 + section * (max1 - min1) / (sections1 - 1.0));
181 void configure(
size_t params,
const std::vector<double>& values)
193 void configure(
const std::vector<std::vector<double> >& values)
244 if ( noOfSections == 1 ) {
249 for (
size_t section = 0; section < noOfSections; section++)
250 m_nodeValues[index].push_back(min + section*( max-min ) / ( noOfSections-1.0 ));
270 for (
int section = 0; section <= (max-min); section++)
271 m_nodeValues[index].push_back( factor * std::pow( exp_base, section+min ));
279 std::vector<size_t> index(dimensions, 0);
281 RealVector point(dimensions);
287 for (
size_t dimension = 0; dimension < dimensions; dimension++)
288 point(dimension) =
m_nodeValues[dimension][index[dimension]];
293 double error = objectiveFunction.
eval(point);
295 #ifdef SHARK_CV_VERBOSE_1 296 std::cout <<
"." << std::flush;
298 #ifdef SHARK_CV_VERBOSE 299 std::cout << point <<
"\t" << error << std::endl;
309 size_t dimension = 0;
310 for (; dimension < dimensions; dimension++)
313 if (index[dimension] <
m_nodeValues[dimension].size())
break;
314 index[dimension] = 0;
316 if (dimension == dimensions)
break;
318 #ifdef SHARK_CV_VERBOSE_1 319 std::cout << std::endl;
385 return "NestedGridSearch";
398 void configure(
const std::vector<double>& min,
const std::vector<double>& max)
400 size_t dimensions = min.size();
406 m_stepsize.resize(dimensions);
408 for (
size_t dimension = 0; dimension < dimensions; dimension++)
410 m_best.
point(dimension)=0.5 *(min[dimension] + max[dimension]);
411 m_stepsize[dimension] = 0.25 * (max[dimension] - min[dimension]);
427 void configure(
size_t parameters,
double min,
double max)
431 m_minimum=std::vector<double>(parameters,min);
432 m_maximum=std::vector<double>(parameters,max);
433 m_stepsize=std::vector<double>(parameters,0.25 * (max - min));
437 double start=0.5 *(min + max);
470 SIZE_CHECK(m_stepsize.size()==startingPoint.size());
481 size_t dimensions = m_stepsize.size();
486 std::vector<size_t> index(dimensions,0);
497 for (
size_t d = 0; d < dimensions; d++)
499 point(d) += (index[d] - 2.0) * m_stepsize[d];
500 if (point(d) < m_minimum[d] || point(d) > m_maximum[d])
508 if (compute && objectiveFunction.
isFeasible(point))
510 double error = objectiveFunction.
eval(point);
521 for (; d < dimensions; d++)
524 if (index[d] <= 4)
break;
527 if (d == dimensions)
break;
530 for(
double&
step: m_stepsize)
575 return "PointSearch";
586 void configure(
size_t parameters,
size_t samples,
double min,
double max) {
588 m_points.resize(samples);
589 for(
size_t sample=0; sample!=samples; ++sample)
591 m_points[sample].resize(parameters);
592 for(
size_t param=0; param!=parameters; ++param)
622 size_t parameters=startingPoint.size();
623 size_t samples=std::min(
sqr(parameters),(
size_t)20);
632 size_t numPoints = m_points.size();
637 for (
size_t point = 0; point < numPoints; point++)
640 if (objectiveFunction.
isFeasible(m_points[point]))
642 double error = objectiveFunction.
eval(m_points[point]);