this application has requested the runtime to terminate it in an unusual way

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!!!

ci-joint le code!

//----------------------------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string>

using std::string;

//----------------------------------------------------------------------------------------
// 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;

chromo_typ(): bits(""), fitness(0.0f){};
chromo_typ(string bts, float ftns): bits(bts), fitness(ftns){}
};
//----------------------------------------------------------------------------------------
// Prototype
//----------------------------------------------------------------------------------------
int BinToDec(string bits);
float AssignFitness(string bits);
string Roulette(float total_fitness, chromo_typ* Population);
void Mutate(string &bits);
void Crossover(string &offspring1, string &offspring2);

//----------------------------------------------------------------------------------------
// 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);

string t1 = offspring1.substr(0, crossover) + offspring2.substr(crossover, CHROMO_LENGHT);
string t2 = offspring2.substr(0, crossover) + offspring1.substr(crossover, CHROMO_LENGHT);

offspring1 = t1; offspring2 = t2;
}
}


//--------------------------------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 "";
}
Last edited on
http://www.cplusplus.com/forum/general/112111/

foo.cpp:24:44: warning: integer overflow in expression [-Woverflow]
foo.cpp:186:7: note: in expansion of macro ‘RANDOM_NUM’
#define RANDOM_NUM ((float)rand()/(RAND_MAX+1))


> std::ifstream fichier( "lire.txt" );
include all that is needed to run your program properly, like an example input.
I added the lire.txt:
0000
0001
0010
0011
0100
0101
0111
1000
1001
1010
1011
1100
1101
1110
1111

after execution I have:

0000
0001
0010
0011
0100
0101
0111
1000
1001
1010
1011
1100
1101
1110
1111



this application has requested the runtime to terminate it in an unusual way. please contact the application support team for more information

Process returned 3 (0x3) execution time : 3.303 s
Press any key to continu..

----------
how can I correct the expression :
#define RANDOM_NUM ((float)rand()/(RAND_MAX+1))

Thank you

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.
1
2
3
4
5
6
7
8
9
10
//void Crossover (string & offspring1, string & offspring2)
		int crossover = (int) (RANDOM_NUM * CHROMO_LENGHT);

		string t1 =
			offspring1.substr (0, crossover) 
			+ offspring2.substr (crossover, CHROMO_LENGHT);

		string t2 =
			offspring2.substr (0, crossover) 
			+ offspring1.substr (crossover, CHROMO_LENGHT);
RANDOM_NUM is in the range [0;1]
CHROMO_LENGHT is 300
so crossover is in the range [0;300]
offspring is, by instance, "0100" (four characters long)

You are trying to access out of bounds.


1
2
3
4
5
6
7
8
//main()
		chromo_typ temp[POP_SIZE];
		while (cPop < POP_SIZE)
		{
			//...
			temp[cPop++] = chromo_typ (offspring1, 0.0f);
			temp[cPop++] = chromo_typ (offspring2, 0.0f); //here cPop may be equal to POP_SIZE, out of bounds access
		}
Thank you ne555 and Computergeek01,

I change my code :

//----------------------------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string>

using std::string;

//----------------------------------------------------------------------------------------
// 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;

chromo_typ(): bits(""), fitness(0.0f){};
chromo_typ(string bts, float ftns): bits(bts), fitness(ftns){}
};
//----------------------------------------------------------------------------------------
// Prototype
//----------------------------------------------------------------------------------------
int BinToDec(string bits);
float AssignFitness(string bits);
string Roulette(float total_fitness, chromo_typ* Population);
void Mutate(string &bits);
void Crossover(string &offspring1, string &offspring2);

//----------------------------------------------------------------------------------------
// 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);

string t1 = offspring1.substr(0, crossover) + offspring2.substr(crossover, BIT);
string t2 = offspring2.substr(0, crossover) + offspring1.substr(crossover, BIT);

offspring1 = t1; offspring2 = t2;
}
}


//--------------------------------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 !

1
2
3
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! 
Last edited on
Topic archived. No new replies allowed.