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

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

## All 2 Replies

Ok I am not going to deal with all of this mess but point out some of the basic errors and then ask how did you manage to get to this point.

-- Talk your first effort,

``````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;
}``````

Ok errors/mistakes:

(i) No use of const
(ii) No use of references .
Point 1+2 says that you should have written `double Individual::Schwefels(const std::vector<double>& PopVec)` (iii) General use of `using namespace std;` -- You are not writing your first c++ program so don't do this.
(iv) No use of iterators -- forgivable since you are learning but maybe think about these two pieces of code:.

``````sum=accumulate(Pop.begin(),Pop.end(),0.0);
// AND:
double sum=0.0;
std::vector<double>::const_iterator ITvec;
for(ITvec=Pop.begin();ITvec=Pop.end();ITvec++)
sum+=*ITvec;``````

(v) You are using square_of_sums without initializing it. Don't tell me you did it in the constructor. This function will be called 100s of times so it will give junk answers.
(vi) You write `for(int i=0 ;i<Pop.size();i++)` Well that gives you a warning on your compiler.... You didn't either
(a) turn warnings on or (b) fix it.
This is the quickest fix for that warning (assuming iterators don't appeal): `for(std::size_t i=0 ;i<Pop.size();i++)` (vii) If you want the square of the sum you need to acually square the sum at some point. You don't.

(vii) What were you thinking when you wrote this `sum_of_squares=sum_of_squares+pow(square_of_sums,2);` ???
Let me see.
-- you didn't initialize the varible
-- you square the result NOT the values in the vector
-- the result depends on the initial value and the length of the vector. You can skip the loop and write `sum_of_squared+=Pop.size()*(square_of_sum*square_of_sum)` -- This is numeric code , and pow is a function, so write
square*square. [Although many compilers will spot this]
I think you wanted this: `sum_of_squares+=Pop[j]*Pop[j]` The rest of your effort is equal junk. So in future.

(i) Break your code down into small units and complete them one by one.
-- Just it be clear code is ONLY complete when
(a) it compiles with no warnings
(b) there is test code that calls it and get the correct results.
(c) that test code HAS been RUN.
(d) You have had a second look at the code, added comments, sensible variable names and refactored if necessary.

(ii) This is numeric code : think very very carefully about initialzation/range/scalability.

Sorry if this is very very harsh but clearly you are actually semi-competent in c++ and programming BUT if you don't write defensive code in a well organized manor, you will spend forever trying to debug and fix it and leave a stupid fatal bug that will invalidate lots of work much later down the line.

So I suggest going away and re-writing this from ABSOLUTE scratch.
Write a small piece of code and then the test for it. (or the test first even). Only when all of that works do you try to put it together.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.