Point2f GoalPostDetector::findTheShift () { CannyThreshold(); createPaddedImg(); getPointsAlongTheGoalBar(); CMAES<float> evo; float *arFunvals, *xfinal, *const*pop; // Initialize everything const int dim = 2; float xstart[dim]; for(int i=0; i<dim; i++) xstart[i] = 0.0; float stddev[dim]; for(int i=0; i<dim; i++) stddev[i] = 0.05; Parameters<float> parameters; parameters.init(dim, xstart, stddev); arFunvals = evo.init(parameters); // Iterate until stop criterion holds while(!evo.testForTermination()) { // Generate lambda new search points, sample population pop = evo.samplePopulation(); // condition: solution dx and dy should not be bigger than squareSize for (int i = 0; i < evo.get(CMAES<float>::PopSize); ++i) while (abs(pop[i][0])*scale>=windowSearchSize||abs(pop[i][1])*scale>=windowSearchSize) evo.reSampleSingle(i); // evaluate the new search points using objectiveFunction from above for (int i = 0; i < evo.get(CMAES<float>::Lambda); ++i) { arFunvals[i]= objectiveFunction(pop[i]); } evo.updateDistribution(arFunvals); } return Point2f(evo.getNew(CMAES<float>::XMean)[0]*scale, evo.getNew(CMAES<float>::XMean)[1]*scale); }
/** * The optimization loop. */ int main(int, char**) { CMAES<double> evo; double *arFunvals, *const*pop, *xfinal; // Initialize everything const int dim = 22; double xstart[dim]; for(int i=0; i<dim; i++) xstart[i] = 0.5; double stddev[dim]; for(int i=0; i<dim; i++) stddev[i] = 0.3; Parameters<double> parameters; // TODO Adjust parameters here parameters.init(dim, xstart, stddev); arFunvals = evo.init(parameters); std::cout << evo.sayHello() << std::endl; // Iterate until stop criterion holds while(!evo.testForTermination()) { // Generate lambda new search points, sample population pop = evo.samplePopulation(); // Do not change content of pop /* Here you may resample each solution point pop[i] until it becomes feasible, e.g. for box constraints (variable boundaries). function is_feasible(...) needs to be user-defined. Assumptions: the feasible domain is convex, the optimum is not on (or very close to) the domain boundary, initialX is feasible and initialStandardDeviations are sufficiently small to prevent quasi-infinite looping. */ /* for (i = 0; i < evo.get(CMAES<double>::PopSize); ++i) while (!is_feasible(pop[i])) evo.reSampleSingle(i); */ // evaluate the new search points using fitfun from above for (int i = 0; i < evo.get(CMAES<double>::Lambda); ++i) arFunvals[i] = fitfun(pop[i], (int) evo.get(CMAES<double>::Dimension)); // update the search distribution used for sampleDistribution() evo.updateDistribution(arFunvals); } std::cout << "Stop:" << std::endl << evo.getStopMessage(); evo.writeToFile(CMAES<double>::WKResume, "resumeevo1.dat"); // write resumable state of CMA-ES // get best estimator for the optimum, xmean xfinal = evo.getNew(CMAES<double>::XMean); // "XBestEver" might be used as well // do something with final solution and finally release memory delete[] xfinal; return 0; }