35 #ifndef SHARK_ALGORITHMS_GRIDSEARCH_H 36 #define SHARK_ALGORITHMS_GRIDSEARCH_H 41 #include <boost/foreach.hpp> 43 #include <boost/serialization/vector.hpp> 88 for (
size_t i = 0; i < numSections; i++)
90 double section = min + i * (max -
min) / (numSections - 1.0);
92 node.push_back(section);
101 void configure(
const std::vector<double>&
min,
const std::vector<double>&
max,
const std::vector<size_t>& sections)
103 size_t params = min.size();
109 for (
size_t dimension = 0; dimension < params; dimension++)
111 size_t numSections = sections[dimension];
113 node.resize(numSections);
115 if ( numSections == 1 )
117 node[0] = (( min[dimension] + max[dimension] ) / 2.0);
119 else for (
size_t section = 0; section < numSections; section++)
121 node[section] = (min[dimension] + section * (max[dimension] - min[dimension]) / (numSections - 1.0));
135 void configure(
double min1,
double max1,
size_t sections1,
double min2,
double max2,
size_t sections2)
144 if ( sections1 == 1 ) {
147 for (
size_t section = 0; section < sections1; section++)
148 m_nodeValues[0].push_back(min1 + section * (max1 - min1) / (sections1 - 1.0));
151 if ( sections2 == 1 ) {
154 for (
size_t section = 0; section < sections2; section++)
155 m_nodeValues[1].push_back(min2 + section * (max2 - min2) / (sections2 - 1.0));
163 void configure(
double min1,
double max1,
size_t sections1)
170 if ( sections1 == 1 ) {
173 for (
size_t section = 0; section < sections1; section++)
174 m_nodeValues[0].push_back(min1 + section * (max1 - min1) / (sections1 - 1.0));
182 void configure(
size_t params,
const std::vector<double>& values)
194 void configure(
const std::vector<std::vector<double> >& values)
224 objectiveFunction.
init();
246 if ( noOfSections == 1 ) {
251 for (
size_t section = 0; section < noOfSections; section++)
252 m_nodeValues[index].push_back(min + section*( max-min ) / ( noOfSections-1.0 ));
272 for (
int section = 0; section <= (max-
min); section++)
273 m_nodeValues[index].push_back( factor * std::pow( exp_base, section+min ));
281 std::vector<size_t> index(dimensions, 0);
283 RealVector point(dimensions);
289 for (
size_t dimension = 0; dimension < dimensions; dimension++)
290 point(dimension) =
m_nodeValues[dimension][index[dimension]];
295 double error = objectiveFunction.
eval(point);
297 #ifdef SHARK_CV_VERBOSE_1 298 std::cout <<
"." << std::flush;
300 #ifdef SHARK_CV_VERBOSE 301 std::cout << point <<
"\t" << error << std::endl;
311 size_t dimension = 0;
312 for (; dimension < dimensions; dimension++)
316 index[dimension] = 0;
318 if (dimension == dimensions)
break;
320 #ifdef SHARK_CV_VERBOSE_1 321 std::cout << std::endl;
387 return "NestedGridSearch";
402 size_t dimensions = min.size();
408 m_stepsize.resize(dimensions);
410 for (
size_t dimension = 0; dimension < dimensions; dimension++)
412 m_best.
point(dimension)=0.5 *(min[dimension] + max[dimension]);
413 m_stepsize[dimension] = 0.25 * (max[dimension] - min[dimension]);
435 m_stepsize=std::vector<double>(
parameters,0.25 * (max -
min));
439 double start=0.5 *(min +
max);
468 objectiveFunction.
init();
473 SIZE_CHECK(m_stepsize.size()==startingPoint.size());
484 size_t dimensions = m_stepsize.size();
489 std::vector<size_t> index(dimensions,0);
500 for (
size_t d = 0; d < dimensions; d++)
502 point(d) += (index[d] - 2.0) * m_stepsize[d];
503 if (point(d) < m_minimum[d] || point(d) > m_maximum[d])
511 if (compute && objectiveFunction.
isFeasible(point))
513 double error = objectiveFunction.
eval(point);
524 for (; d < dimensions; d++)
527 if (index[d] <= 4)
break;
530 if (d == dimensions)
break;
533 BOOST_FOREACH(
double&
step,m_stepsize)
578 return "PointSearch";
591 m_points.resize(samples);
592 for(
size_t sample=0; sample!=samples; ++sample)
594 m_points[sample].resize(parameters);
595 for(
size_t param=0; param!=
parameters; ++param)
597 m_points[sample](param)=
Rng::uni(min,max);
621 objectiveFunction.
init();
627 size_t samples=
std::min(
sqr(parameters),(
size_t)20);
636 size_t numPoints = m_points.size();
641 for (
size_t point = 0; point < numPoints; point++)
644 if (objectiveFunction.
isFeasible(m_points[point]))
646 double error = objectiveFunction.
eval(m_points[point]);