#include<math.h>
#include<stdlib.h>
{
for (int i=0;i<(length-1);i++) {
result+=
pow(1-xx[i],2)+100*
pow(xx[i+1]-
pow(xx[i],2),2);
}
return result;
}
{
for (int i=0;i<length;i++) {
result+=xx[i]*xx[i]-10*
cos(6.2831853*xx[i]);
}
return result;
}
void GlobalMinimization()
{
Bool_t installed=r.
Eval(
"is.element('DEoptim', installed.packages()[,1])");
if (!installed) {
std::cout<<"Package DEoptim no installed in R"<<std::endl;
std::cout<<"Run install.packages('DEoptim') in R's environment"<<std::endl;
return;
}
r<<"suppressMessages(library(DEoptim, quietly = TRUE))";
r["MaxIter"]<<5000;
r["n"]<<3;
r<<"ll<-rep(-25, n)";
r<<"ul<-rep(25, n)";
bench.
Start(
"GlobalMinimizationRosenBrock");
r<<"result1<-DEoptim(fn=GenRosenBrock,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))";
std::cout<<"-----------------------------------------"<<std::endl;
std::cout<<"RosenBrock's minimum in: "<<std::endl;
r<<"print(result1$optim$bestmem)";
std::cout<<"Bechmark Times"<<std::endl;
bench.
Show(
"GlobalMinimizationRosenBrock");
r["MaxIter"]<<2000;
r["n"]<<3;
r<<"ll<-rep(-5, n)";
r<<"ul<-rep(5, n)";
bench.
Start(
"GlobalMinimizationRastrigin");
r<<"result2<-DEoptim(fn=Rastrigin,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))";
std::cout<<"-----------------------------------------"<<std::endl;
std::cout<<"Rastrigin's minimum in: "<<std::endl;
r<<"print(result2$optim$bestmem)";
std::cout<<"Bechmark Times"<<std::endl;
bench.
Show(
"GlobalMinimizationRastrigin");
r<<"dev.new(title='RosenBrock Convergence')";
r<<"plot(result1,type='o',pch='.')";
r<<"dev.off()";
r<<"dev.new(title='Rastrigin Convergence')";
r<<"plot(result2,type='o',pch='.')";
r<<"dev.off()";
}
}