Hi everyone, I'm having problems on my current project. The project consists of optimizing a given function using either the Hill Climber or Genetic Algorithm. The functions I'm going to be optimizing are the Schwefel, Rosenbrock, and Griewangk problems defined here
http://www.cs.cmu.edu/afs/cs/project/jair/pub/volume24/ortizboyer05a-html/node6.html
My code for each of the three function is
double Individual:: SchwefelsProblem(vector <double> schwefel_population)
{
for (int i=0; i <schwefel_population.size();i++)
square_of_sums=square_of_sums+ schwefel_population[i];
cout <<"The Sum of all elements in the vector is " << square_of_sums <<endl;
for(int j=0 ; j<schwefel_population.size();j++)
sum_of_squares=sum_of_squares+pow(square_of_sums,2);
cout <<"The sum of the entire sumation squared is "<<sum_of_squares<<endl;
return sum_of_squares;
}
double Individual:: RosenbrocksProblem(vector <double> rosenbrock_population)
{
double sum=0;
for (int i=0; i < rosenbrock_population.size(); i++)
sum=sum+((100*pow(rosenbrock_population[i+1]-pow(rosenbrock_population[i],2),2))+pow(rosenbrock_population[i]-1,2));
cout <<sum<<endl;
return sum;
}
double Individual:: GriewangkProblem(vector <double> griewangk_population)
{
double sum=0;
for(int i=0; i < griewangk_population.size(); i++)
{
sum=((pow(griewangk_population[i],2))/4000)-((PI*cos(griewangk_population[i]/sqrt(griewangk_population[i]))));
sum=1+sum;
}
cout<<sum<<endl;
return sum;
}
and for my Hill climber Algorithm for the Schwefel problem is
int HillClimberAlgorithm::SchwefelHillClimberAlgorithm (int& mutation_chance, int& mutation_amount)
{
Individual individual1;
vector<double> xi=individual1.CreateIndividual();
vector<double> xnew;
int problemChoice;
while (individual1.SchwefelsProblem(xi) >=0.1)
{
iterations=0;
for (int i=0; i < 30;i++)
{
xnew[i]=individual1.SchwefelsProblem(xi);
xnew[i]= xi[i]+xi[i]*(rand()%+1-1)*mutation_amount;
iterations++;
}
if (xnew < xi)
xi=xnew;
}
cout <<iterations;
return iterations;
}
iterations is just the number of runs the loop goes through before getting to 0.1 of the answer (zero).each .CreateIndividualMethod is creating a vector of doubles in a given range defined in the above website. Here is the CreateIndividualMethod()
vector <double> Individual:: CreateIndividual()
{
int number =GetProblemChoice();
switch(number)
{
case 1:
{
random_double=rand()%65-65;
for (int j=0; j < 30; j++)
{
individual.push_back(random_double);
cout <<individual[j] <<endl;
}
break;
}
case 2:
{
random_double=rand()%2-2;
for (int j=0; j < 30; j++)
{
individual.push_back(random_double);
cout << individual[j] <<endl;
}
break;
}
case 3:
{
random_double=rand()%600-600;
for (int j=0; j < 30; j++)
{
individual.push_back(random_double);
cout << individual[j] <<endl;
}
break;
}
default:
cout<<"Unknown Input" ;
break;
}
return individual;
}
My Output is....
The Sum of all elements in the vector is -0.321
The sum of the entire sumation squared is -0.321
-12112197121
which is all wrong. Please Help Me.