Hi,
When i run my program below, i recieve this error:
Debug assertion failed!
Expression: BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
this exception occurs when returning from fitness function.
#include <map>
#include <algorithm>
#include <vector>
#include <fstream>
#include<time.h>
#include <math.h>
using namespace std;
#define generations 10000
#define popSize 120 // population size
#define makeRandNumber ((double)rand()/RAND_MAX)
int b=2;
const int l= 16;
struct mychromo
{
mychromo()
{
}
~mychromo()
{
//delete genes;
delete [] genes;
}
double makeRandNum(int n)
{
srand(time(NULL));
return ((double)rand()/RAND_MAX)*(n-1);
}
inline double makeRandNum()
{
srand(time(NULL));
return ((double)rand()/RAND_MAX);
}
int genes[l];
double fit;
};
bool comval (std::pair<int,int> p1, std::pair<int,int> p2)
{
return p1.second<p2.second;
}
double fitness(mychromo gen) // error accurs when returning from this function!!
{
int fitness=0;
bool flag;
for (int i=0; i< l; i=i+2)
{
flag=true;
for (int j=0; j< b; j++)
if(gen.genes[i+j]==0)
flag= false;
if(flag)
fitness+=b;
}
return (double)fitness;
}
void mutate(vector <mychromo> & pop)
{
int mi1,temp;
for (int i=0; i< popSize; i++)
{
mi1 = (int)(ceil(makeRandNumber*(l-1)));
if(pop[i].genes[mi1]==0)
pop[i].genes[mi1]=1;
else
pop[i].genes[mi1]=0;
pop[i].fit=fitness(pop[i]);
}
}
void twoPointCrossOver(mychromo& c1,mychromo& c2)
{
int ind1, ind2,tmp;
ind1= makeRandNumber*(l-1);
ind2= makeRandNumber*(l-1);
while (ind1== ind2)
ind2= makeRandNumber*(l-1);
if(ind1> ind2){
tmp=ind2;ind2=ind1;ind1=tmp;}
mychromo temp;
for (int i=ind1; i<ind2+1 ; i++)
{
temp.genes[i]= c1.genes[i];
c1.genes[i]=c2.genes[i];
c2.genes[i]= temp.genes[i];
}
c1.fit= fitness(c1);
c2.fit= fitness(c2);
return;
}
bool compfit(mychromo chromo1,mychromo chromo2)// sort nozooli
{
return chromo1.fit > chromo2.fit;
}
int chance(const double probability) {
if(makeRandNumber<probability)
return 1; // crossover
else
return 0; // mutation
}
void make_population_greedy(vector <mychromo> chromoPop)
{
mychromo chromosome;
for ( int i = 0; i < popSize; i++ )
{
for (int j=0;j<l; j++){
if(makeRandNumber<0.5) chromosome.genes[j]=0;
else chromosome.genes[j]=1;
}
chromosome.fit= fitness(chromosome);
chromoPop.push_back(chromosome);
}
return;
}
void SelectPopulation(vector <mychromo> chromoPop,vector <mychromo>& selectedPop)
{
// calculate agrigate Probability
double fitSum=0;
for (int i=0;i< popSize; i++)
fitSum+= chromoPop[i].fit;
chromoPop[0].fit= chromoPop[0].fit/fitSum;
for (int i=1;i< popSize; i++)
chromoPop[i].fit= chromoPop[i].fit/fitSum+ chromoPop[i-1].fit;
// selection part
for (int i=0;i< popSize; i++)
{
double cmpVal= makeRandNumber;
if(cmpVal<= chromoPop[0].fit){
selectedPop.push_back(chromoPop[0]);continue;}
for(int j=1; j< popSize; j++)
if(cmpVal> chromoPop[j-1].fit && cmpVal<= chromoPop[j].fit )
selectedPop.push_back(chromoPop[j]);
}
sort(selectedPop.begin(), selectedPop.end(), compfit);
}
void replacePopulation_eliteReplacement(vector <mychromo> &chromoPop,vector <mychromo>& selectedPop)
{
for (int i=0; i<popSize; i++ )
{
int index= makeRandNumber*(popSize-1);
if(selectedPop[i].fit> chromoPop[index].fit)
{
chromoPop[index].fit= selectedPop[i].fit;
for(int j=0; j<l; j++)
chromoPop[index].genes[j]=selectedPop[i].genes[j];
}
}
return;
}
void main()
{
int n=16;
srand(time(NULL));
int idx=0;
int count=0;
int minx = 0, finx = 0; //parent (father & mother) indexes
double crp = 0.6; // crossover probability
int parentDistance=0;
vector <mychromo> chromoPop;
vector <mychromo> selectedPop;
int bestfit=999;
make_population_greedy(chromoPop);
for(int p=0;p<generations;p++)
{
sort(chromoPop.begin(), chromoPop.end(), compfit);
//Selection
SelectPopulation(chromoPop,selectedPop);
//CrossOver
minx = ceil((popSize-1)* makeRandNumber);
finx = ceil((popSize-1)* makeRandNumber);
while(finx==minx)
finx = ceil((popSize-1)* makeRandNumber);
if(makeRandNumber< crp)
twoPointCrossOver(selectedPop[minx], selectedPop[minx]);
//Replacement
replacePopulation_eliteReplacement(chromoPop, selectedPop);
//Mutation
mutate(chromoPop);
}
return ;
}
I have no idea whats wrong with it!
thanks for any advice in advance.