Lors de l’exécution en utilisant code::blocks, j'ai l'erreur suivante:
this application has requested the runtime to terminate it in an unusual way. please contact the application support team for more information!!!
//----------------------------------------------------------------------------------------
// Defines
//----------------------------------------------------------------------------------------
#define POP_SIZE 15
#define BIT 4
#define CROSSOVER_RATE 0.7
#define MUTATION_RATE 0.001
#define MAX_ALLOWABLE_GENERATIONS 400
#define CHROMO_LENGHT 300
#define RANDOM_NUM ((float)rand()/(RAND_MAX+1))
//----------------------------------------------------------------------------------------
// define a data structure which will define a chromosome
//----------------------------------------------------------------------------------------
struct chromo_typ
{
//the binary bit string is held in a std::string
string bits;
float fitness;
//----------------------------------------------------------------------------------------
// Main
//----------------------------------------------------------------------------------------
int main()
{
string ligne; // variable contenant chaque ligne lue
int i=0, j, k, l;
string offspring1;
string offspring2;
//storage for our population of chedcromosomes.
chromo_typ Population[POP_SIZE];
// le constructeur de ifstream permet d'ouvrir un fichier en lecture
std::ifstream fichier( "lire.txt" );
if ( fichier ) // ce test échoue si le fichier n'est pas ouvert
{
// cette boucle s'arrête dès qu'une erreur de lecture survient
while ( std::getline( fichier, ligne ) )
{
Population[i].bits= ligne;
i++;
}
}
//Write population on console
for(i=0; i<POP_SIZE; i++)
std::cout << Population[i].bits << std::endl;
//Initialise fitness function
for(i=0; i<POP_SIZE; i++)
Population[i].fitness =0.0f;
bool bFound = false;
//enter the main GA loop
while(!bFound)
{
//this is used during roulette wheel sampling
float TotalFitness = 0.0f;
// test and update the fitness of every chromosome in the
// population
for (j=0; j<POP_SIZE; j++)
{
Population[j].fitness = AssignFitness(Population[j].bits);
TotalFitness += Population[j].fitness;
}
// check to see if we have found any solutions (fitness will be 999)
for (k=1; k<POP_SIZE; k++)
{
if (Population[k].fitness < Population[k-1].fitness)
{
std::cout << "\nSolution found in " << Population[k].bits << " generations!" << std::endl ;
bFound = true;
break;
}
}
// create a new population by selecting two parents at a time and creating offspring
// by applying crossover and mutation. Do this until the desired number of offspring
// have been created.
//define some temporary storage for the new population we are about to create
chromo_typ temp[POP_SIZE];
int cPop = 0;
//loop until we have created POP_SIZE new chromosomes
while (cPop < POP_SIZE)
{
// we are going to create the new population by grabbing members of the old population
// two at a time via roulette wheel selection.
offspring1 = Roulette(TotalFitness, Population);
offspring2 = Roulette(TotalFitness, Population);
//add crossover dependent on the crossover rate
Crossover(offspring1, offspring2);
//now mutate dependent on the mutation rate
Mutate(offspring1);
Mutate(offspring2);
//add these offspring to the new population. (assigning zero as their
//fitness scores)
temp[cPop++] = chromo_typ(offspring1, 0.0f);
temp[cPop++] = chromo_typ(offspring2, 0.0f);
}//end loop
//copy temp population into main population array
for (l=0; l<POP_SIZE; l++)
{
Population[l] = temp[l];
}
}
return 0;
}
//---------------------------------AssignFitness--------------------------------------
//
// given a string of bits and a target value this function will calculate its
// representation and return a fitness score accordingly
//------------------------------------------------------------------------------------
float AssignFitness(string bits)
{
float result = 0;
int i;
float T_soft, T_Hard;
for (i=0; i<BIT; i++)
{
if (bits.at(i) == '1')
result +=1;
}
//Execution software et hardware if bit.at(i) =1 ou 0
T_soft = (BIT - result) * 0.3;
T_Hard = result * 0.1;
return T_soft+ T_Hard;
}
//------------------------------------Mutate---------------------------------------
//
// Mutates a chromosome's bits dependent on the MUTATION_RATE
//-------------------------------------------------------------------------------------
void Mutate(string &bits)
{
int i;
for (i=0; i< BIT; i++)
{
if (RANDOM_NUM < MUTATION_RATE)
{
if (bits.at(i) == '1')
bits.at(i) = '0';
else
bits.at(i) = '1';
}
}
return;
}
//---------------------------------- Crossover ---------------------------------------
//
// Dependent on the CROSSOVER_RATE this function selects a random point along the
// lenghth of the chromosomes and swaps all the bits after that point.
//------------------------------------------------------------------------------------
void Crossover(string &offspring1, string &offspring2)
{
//dependent on the crossover rate
if (RANDOM_NUM < CROSSOVER_RATE)
{
//create a random crossover point
int crossover = (int) (RANDOM_NUM * CHROMO_LENGHT);
//--------------------------------Roulette-------------------------------------------
//
// selects a chromosome from the population via roulette wheel selection
//------------------------------------------------------------------------------------
string Roulette(float total_fitness, chromo_typ* Population)
{
int i;
//generate a random number between 0 & total fitness count
float Slice = (float)(RANDOM_NUM * total_fitness);
//go through the chromosones adding up the fitness so far
float FitnessSoFar = 0.0f;
for (i=0; i<POP_SIZE; i++)
{
FitnessSoFar += Population[i].fitness;
//if the fitness so far > random number return the chromo at this point
if (FitnessSoFar >= Slice)
how can I correct the expression :
#define RANDOM_NUM ((float)rand()/(RAND_MAX+1))
You don't. There is so much wrong with that approach that you should just abandon it and come at it from a different angle. Look over this: http://en.cppreference.com/w/cpp/numeric/random to see how you should approach this problem.
//----------------------------------------------------------------------------------------
// Defines
//----------------------------------------------------------------------------------------
#define POP_SIZE 15
#define BIT 4
#define CROSSOVER_RATE 0.7
#define MUTATION_RATE 0.001
#define MAX_ALLOWABLE_GENERATIONS 100
#define RANDOM_NUM ((float)rand()/(RAND_MAX+1))
//----------------------------------------------------------------------------------------
// define a data structure which will define a chromosome
//----------------------------------------------------------------------------------------
struct chromo_typ
{
//the binary bit string is held in a std::string
string bits;
float fitness;
//----------------------------------------------------------------------------------------
// Main
//----------------------------------------------------------------------------------------
int main()
{
string ligne; // variable contenant chaque ligne lue
int i=0, j, k, l;
string offspring1;
string offspring2;
//storage for our population of chedcromosomes.
chromo_typ Population[POP_SIZE];
// le constructeur de ifstream permet d'ouvrir un fichier en lecture
std::ifstream fichier( "lire.txt" );
if ( fichier ) // ce test échoue si le fichier n'est pas ouvert
{
// cette boucle s'arrête dès qu'une erreur de lecture survient
while ( std::getline( fichier, ligne ) )
{
Population[i].bits= ligne;
i++;
}
}
//Write population on console
for(i=0; i<POP_SIZE; i++)
std::cout << Population[i].bits << std::endl;
//Initialise fitness function
for(i=0; i<POP_SIZE; i++)
Population[i].fitness =0.0f;
bool bFound = false;
//enter the main GA loop
while(!bFound)
{
//this is used during roulette wheel sampling
float TotalFitness = 0.0f;
// test and update the fitness of every chromosome in the
// population
for (j=0; j<POP_SIZE; j++)
{
Population[j].fitness = AssignFitness(Population[j].bits);
TotalFitness += Population[j].fitness;
}
// check to see if we have found any solutions (fitness will be 999)
for (k=1; k<POP_SIZE; k++)
{
if (Population[k].fitness < Population[k-1].fitness)
{
std::cout << "\nSolution found in " << Population[k].bits << " generations!" << std::endl ;
bFound = true;
break;
}
}
// create a new population by selecting two parents at a time and creating offspring
// by applying crossover and mutation. Do this until the desired number of offspring
// have been created.
//define some temporary storage for the new population we are about to create
chromo_typ temp[POP_SIZE];
int cPop = 0;
//loop until we have created POP_SIZE new chromosomes
while (cPop < POP_SIZE)
{
// we are going to create the new population by grabbing members of the old population
// two at a time via roulette wheel selection.
offspring1 = Roulette(TotalFitness, Population);
offspring2 = Roulette(TotalFitness, Population);
//add crossover dependent on the crossover rate
Crossover(offspring1, offspring2);
//now mutate dependent on the mutation rate
Mutate(offspring1);
Mutate(offspring2);
//add these offspring to the new population. (assigning zero as their
//fitness scores)
temp[cPop++] = chromo_typ(offspring1, 0.0f);
temp[cPop++] = chromo_typ(offspring2, 0.0f);
}//end loop
//copy temp population into main population array
for (l=0; l<POP_SIZE; l++)
{
Population[l] = temp[l];
}
}
return 0;
}
//---------------------------------AssignFitness--------------------------------------
//
// given a string of bits and a target value this function will calculate its
// representation and return a fitness score accordingly
//------------------------------------------------------------------------------------
float AssignFitness(string bits)
{
float result = 0;
int i;
float T_soft, T_Hard;
for (i=0; i<BIT; i++)
{
if (bits.at(i) == '1')
result +=1;
}
//Execution software et hardware if bit.at(i) =1 ou 0
T_soft = (BIT - result) * 0.3;
T_Hard = result * 0.1;
return T_soft+ T_Hard;
}
//------------------------------------Mutate---------------------------------------
//
// Mutates a chromosome's bits dependent on the MUTATION_RATE
//-------------------------------------------------------------------------------------
void Mutate(string &bits)
{
int i;
for (i=0; i< BIT; i++)
{
if (RANDOM_NUM < MUTATION_RATE)
{
if (bits.at(i) == '1')
bits.at(i) = '0';
else
bits.at(i) = '1';
}
}
return;
}
//---------------------------------- Crossover ---------------------------------------
//
// Dependent on the CROSSOVER_RATE this function selects a random point along the
// lenghth of the chromosomes and swaps all the bits after that point.
//------------------------------------------------------------------------------------
void Crossover(string &offspring1, string &offspring2)
{
//dependent on the crossover rate
if (RANDOM_NUM < CROSSOVER_RATE)
{
//create a random crossover point
int crossover = (int) (RANDOM_NUM * BIT);
//--------------------------------Roulette-------------------------------------------
//
// selects a chromosome from the population via roulette wheel selection
//------------------------------------------------------------------------------------
string Roulette(float total_fitness, chromo_typ* Population)
{
int i;
//generate a random number between 0 & total fitness count
float Slice = (float)(RANDOM_NUM * total_fitness);
//go through the chromosones adding up the fitness so far
float FitnessSoFar = 0.0f;
for (i=0; i<POP_SIZE; i++)
{
FitnessSoFar += Population[i].fitness;
//if the fitness so far > random number return the chromo at this point
if (FitnessSoFar >= Slice)
return Population[i].bits;
}
return "";
}
I change the value of CHROMO_LENGHT to 4! the range of the crossover become in the range[0;4]. but nothing! the same error !
temp[cPop++] = chromo_typ(offspring1, 0.0f); //What if cPop is equal to POP_SIZE - 1?
temp[cPop++] = chromo_typ(offspring2, 0.0f); //Then there it will be equal to POP_SIZE
//We have out of bounds access here!