Ok.. So I'm working on this code on C++. Code is below..
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
using namespace std;
vector<int> makePerson(int n)
{
vector<int>person;
for(int i=0;i<3;i++)
{
person.push_back(rand()%255+1);
}
return person;
}
vector<vector<int> > makeGeneration(int n)
{
vector<vector<int> > generation;
for(int i=0;i<n;i++)
{
generation.push_back(makePerson(i));
}
return generation;
}
vector<int> createChild(vector<vector<int> > &parentGeneration, double mutationRate)
{
int maleParent = (rand()%parentGeneration.size())+1;
int femaleParent = (rand()%parentGeneration.size())+1;
bool checkGender = true;
while(checkGender)
{
if(maleParent==femaleParent)
{
checkGender = true;
}
else
{
checkGender = false;
break;
}
}
vector<int> child;
vector<int> temp;
for(int i=0;i<3;i++)
{
temp.push_back(parentGeneration.at(maleParent).at(i));
temp.push_back(parentGeneration.at(femaleParent).at(i));
}
for(int i=0;i<3;i++)
{
child.push_back(temp.at((rand()%3)+1));
}
temp.clear();
for(int i=0;i<3;i++)
{
if((mutationRate*100) > (rand()%100)+1)
{
child.at(i)=(rand()%255)+1;
}
}
return child;
}
int main()
{
int N,k,g;
N=1000;
k=2;
double m=0.05;
double d=0.05;
srand(static_cast<unsigned int>(time(0)));
vector<vector<int> > parentGeneration;
vector<vector<int> > childGeneration;
parentGeneration = makeGeneration(N);
for(int i=0;i<parentGeneration.size();i++)
{
for(int j=0;j<3;j++)
{
cout<<parentGeneration.at(i).at(j)<<endl;
}
cout<<endl;
}
cout<<"reach"<<endl;
for(int i=0;i<(1000);i++)
{
childGeneration.push_back(createChild(parentGeneration,m));
}
cout<<"***CHILD GENERATION***"<<endl;
for(int i=0;i<childGeneration.size();i++)
{
for(int j=0;j<3;j++)
{
cout<<childGeneration.at(i).at(j)<<endl;
}
cout<<"i="<<i<<endl;
cout<<endl;
}
return 0;
}
So, the errors are:
- Microsoft C++ exception: std::out_of_range at memory location 0x002cf6c4.
- Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer dec
I think it's some problem with the vector and the random generator because this error is random. It sometimes works till 1000 and sometimes stops in between.
Any suggestions. Really urgent..
PLEASEEEE Thanks in advane :D