I keep getting a stack fault error when trying to run my GA. It compiles fine. No idea what is causing it. Im using borland C++ 5.02.
Here is my code:
Code:
#include <iostream.h>
#include <fstream.h>
#include <cstring.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <ctype.h>
#define Population 100
#define Dimension 3
#define Chrome_Length 5
#define Lval 1.5
#define Mval 10
#define Crossover_Rate 700
#define Mutation_Rate 100
#define Lifecycle 100
#define file "C:\\Average.csv"
//Function Prototypes
void Random_Number_Generator(int MAX, float Numbers[Population][Dimension]);
//genrates poulation
void Binary_Conversion(int Binary[Population][Chrome_Length*Dimension],float
Positivenumberss[Population][Dimension]);
//conversion for decimal into binary
void Positive_Conversion (float Numbers[Population][Dimension],float
Positivenumberss[Population] [Dimension]);
//converts initial population into positive numbers
void Calculate_Fitness(float Numbers[Population][Dimension],float
fitness[Population]);
//calculates the fitness of the population
void Select_Parents(int Binary [Population][Chrome_Length*Dimension],float
fitness[Population],float total,
int parents [Population] [Chrome_Length * Dimension], int children
[Population][Chrome_Length*Dimension]);
//selects the parents
void Crossover (int children [Population] [Chrome_Length * Dimension]);
//Crossover function
void Mutation (int children [Population] [Chrome_Length * Dimension]);
//Mutation function
void Decimal_Converter(int children [Population] [Chrome_Length *
Dimension], float Numbers2 [Population] [Dimension]);
// converts children to decimal
void Negative_Conversion(float Numbers2 [Population][Dimension],float Numbers [Population][Dimension]);
//conversion for decimal numbers into negative numbers
float Average_Fitness(float fitness [Population]);
// calculates the average fitness
float Fittest_Chrome(float fitness [Population]);
// searches for the fittest chromasome
void main()
{
float Numbers [Population][Dimension];
// array called numbers to store the population
float Numbers2 [Population] [Dimension];
// array to store decimal numbers that need to be converted to negative
int Binary [Population][Chrome_Length*Dimension];
// two dimesional array called binary
int MAX=pow(2,Chrome_Length)-1;
// calculates the maximum value of a given Chrome_Length. Value is 2^cl -1
float Positivenumberss[Population][Dimension];
// array to store decimal numbers to be converted to binary
float Fitness[Population];
// an array for the fitness values
float Total = 0;
// variable used in calculating the total fitness
int Parents [Population] [Chrome_Length*Dimension];
// two dimensional array to store parents selected for breeding
int Children [Population] [Chrome_Length*Dimension];
// two dimensioanl array to store children bread from selected parents
float Average;
// variable for the average fitness of a population
float Fittest;
// variable for the fittest chromasome
int n = 1;
// variable used to count the number of generations
cout.setf(ios::fixed);
// function that tells the compiler to output numbers to the screen using fixed point notation
cout.setf(ios::showpoint);
// function that tells the compiler to always show the decimal point when outputting floating point numbers
cout.precision(1);
// sets the number of decimal places of floating point numbers
srand ( time(NULL) );
// seeds the random number generator using the system clock
Random_Number_Generator(MAX,Numbers);
// call to the function that generates the initial population
ofstream outfile(file, ios::app);
// specifies that when ouputing data to csv file, the file should be ammended and not overwritten
outfile<<"Generation"<<','<<"Average Fitness"<<','<<"Fittest Chromasome"<<endl;
// outputs three column headings to the csv file
outfile<<endl;
for (int i=0;i<Lifecycle; i++)
// loop that sets the numbers of time the GA will go though it's lifecycle
{
Binary_Conversion(Binary,Positivenumberss);
// function that converts decimal numbers into binary
Calculate_Fitness(Numbers,Fitness );
// function that measures the fitness of the members of the population
Select_Parents(Binary,Fitness,Total,Parents,Children);
// function that selects the parents for breeding
Crossover(Children);
// function that performs Crossover
Mutation(Children);
// function that performs Mutation
Decimal_Converter(Children,Numbers2);
// function that converts binary numbers into decimal
Positive_Conversion(Numbers,Positivenumberss);
// function that converts negative decimal into positive numbers
Negative_Conversion(Numbers2,Numbers);
// function that converts positve decimal numbers back into negative
outfile<<n<<',';
// writes the value of counter n to the csv file
Average = Average_Fitness(Fitness);
// sets the variable average equal to the value returned by the average fitness
outfile<<Average<<',';
// writes the value of average to the csv file
Fittest = Fittest_Chrome(Fitness);
// sets the varialbe fittest to equal the value returned by the fittest chromasome
outfile<<Fittest<<endl;
// writes the value of fittest to the csv file
n++;
//increments the variable n by one
}
}
//Function inplementations
void Random_Number_Generator(int MAX, float Numbers[Population][Dimension])
{
float random; // declaring float variable random
for (int i=0;i<Population; i++)
{
for (int j=0;j<Dimension;j++)
{
random = (rand()%MAX)*1.00; // generate a random number between 0 and MAX
random=random /Mval; // divide the random number by Mval
random=random -Lval; // take Lval away from the random number
Numbers[i][j] = random; //stores the random number in the numbers array
}
}
}
void Positive_Conversion (float Numbers[Population][Dimension],float Positivenumberss[Population] [Dimension])
{
float decimal; // declare float variable decimal
for (int i=0;i<Population; i++) // for each of the population
{
for (int j=0; j<Dimension;j++) // for each dimension
{
decimal = Numbers[i][j]; // decimal equals the value [i] [j] in the numbers array
decimal=decimal + Lval; // add Lval to the value decimal
decimal=decimal * Mval; // multiply decimal by Mval
Positivenumberss[i][j]= decimal; // set the value at position [i] [j] in the Positivenumberss array equal to the value of decimal
}
}
}
void Binary_Conversion(int Binary[Population][Chrome_Length*Dimension],float Positivenumberss[Population][Dimension])
{
int decci; //declaring integer variable
for (int i=0; i<Population; i++)
{
int pos=0; // declaring integer pos and initialising it to zero
for (int d =0; d < Dimension; d++)
{
decci = Positivenumberss[i][d]; // set decci equal to the value at position [i] [d] in the Positivenumberss array
for (int j = 0; j < Chrome_Length; j++) // for each bit in the chromasome
{
int CurrSigBit = pow (2,(Chrome_Length -j-1)); // declare an integer CurrSigBbit and set its value equal to 2^(cl-1)
if(CurrSigBit <= decci) // if the value of CurrSigBit is less than or equal to decci
{
Binary[i][pos] = 1; // set the [i] [pos] position in the binary array to a 1
decci = decci - CurrSigBit; // minus CurrSigBit from decci
}
else
{
Binary [i] [pos] = 0; // else set the [i] [pos] position in the binary array to a 0
}
pos++; // increment the pos variable by one
}
}
}
}
void Calculate_Fitness(float Numbers[Population][Dimension],float fitness[Population])
{
float total; // declare a float variable total
for (int j=0;j<Population; j++) // for each of the population
{
total = 0; // set value of total to zero
for (int i=0;i<Dimension; i++) // for each dimension
{
total=total + pow (Numbers [j][i],2); // add the square of the value at position [j] [i] in the Numbers array to total
}
1000/(total+1);
fitness[j]=10000/(total+1); // setting the value at position [j] in the fitness array to equal
cout<<fitness[j]<<"INDIVIDUAL FITNESS VALUES"; // outputs the value in the fitness array to the screen
cout<<endl; // start outputting data to the screen on a new line
}
}
void Select_Parents(int Binary [Population][Chrome_Length*Dimension] , float fitness[Population],
float total,int parents [Population] [Chrome_Length * Dimension], int children [Population][Chrome_Length*Dimension])
{
float random_number; // declaring a float variable random_number
long int score; // declaring a long integer variable score
float temp; // declaring a float variable temp
for (int j=0;j<Population; j++) // for each of the population
{
total = total+ fitness[j]; // add the value a position [j] in the fitness aray to total
}
cout<<endl; // output blank space to screen
cout<<" Total Fitness = " <<total<<endl; // outputs the value of total to the screen
for (int i=0;i<Population; i++) // for each of the population
{
temp=rand()%RAND_MAX; // setting the variable temp equal to a random number between 0 and RAND_MAX (the compilers maximum
random_number = (temp/RAND_MAX) * total; // setting the value of random_number equal to (temp divided by RNAD_MAX) multiplied by the value of total
cout<<" The Random Number Is "<<random_number<<endl; // ouputs the random number to the screen
score =0; // sets the value of score to zero
for (int j=0;j<Population; j++) // for each member of the population
{
score = score + fitness[j]; // add the value in the [j]th position in the fitness array to score
cout<<score; // output the value of score
cout<<endl; // output on new line
if (random_number<score) // if the value random_number less that the value score
{
for (int k=0;k<Chrome_Length * Dimension;k++) // for each bit in the child
{
parents [i] [k] = Binary [j][k]; // set the value at position [i] [k] in the parents array to the value at position [j] [k] in the binary array
children [i] [k] = Binary [j] [k]; // set the value at position [i] [k] in the children array to the value at position [j] [k] in the binary array
}
j = (Population) +1; // set the value of j to Population plus one
cout<<endl; // output data on new line
}
}
}
}
void Crossover (int children [Population] [Chrome_Length * Dimension])
{
float random_number; // declaring float variable random_number
float randy; // declaring float variable randy
int temp; // declaring integer variable temp
int crosspoint = Chrome_Length * Dimension -2; // declaring crosspoint and setting value to Chrome_Length multiplied by dimension minus two
srand ( time(NULL) ); // seeding the random number generator
for (int i=0; i< Population; i= i+2) // for every pair in population
{
random_number = (rand()%1000)*1.00; // set the value of random_number equal to a random number between 0 and 1000 multiplied by one
cout<<random_number<<" R"<<" i "<< i<<endl; // output random_number and i to the screen and then start anew line
if(random_number <= Crossover_Rate) // if random_number is less than or eqaul to the Crossover_Rate
{
randy = (rand()%crosspoint)*1.00; // set the value of randy to a random number between 0 and crosspoint
cout<<randy<<" Randy"; // output the value of randy to the screen
cout<<endl; // start a new line
for (int k=randy;k<Chrome_Length*Dimension;k++) // for the pair chosen above
{
temp = children [i] [k]; // set the value of temp to the value in the children array at position [i] [k]
children [i] [k] = children [i+1] [k]; // set the value at position [i] [k] in the children array equal to the value at position [i+1] [k] in the children array
children [i+1] [k] = temp; // set the value at position [i+1] [k] in the children array equal to temp
}
}
}
}
void Mutation (int children [Population] [Chrome_Length * Dimension])
{
float random; // declare float variable random
srand ( time(NULL) ); // seed the random number generator
for ( int i =0; i<Population; i++) // for each of the population
{
for ( int j=0; j<Chrome_Length*Dimension; j++) // for each bit in a child
{
random = (rand()%1000); // set random equal to a random number between 0 and 1000
if ((random <= Mutation_Rate) && (children [i] [j] ==0))
// if random is less than or equal to the Mutation_Rate and at position [i] [j] in the children array is a 0
{
children [i] [j] =1;
// set at position [i] [j] in the children array to a 1
}
else if ((random <= Mutation_Rate) && (children [i] [j]==1))
// else if the value of random is less than or equal to Mutation_Rate and the bit at position [i] [j] in the children array is a 1
{
children [i] [j] = 0;
// set the bit at position [i] [j] in the children array to a 0
}
}
}
}
void Decimal_Converter(int children [Population] [Chrome_Length*Dimension], float Numbers2 [Population] [Dimension])
{
for(int i = 0; i < Population; i++) // for each of the population
{
int pos = 0; // declare and set the integer variable position to zero
for(int k = 0; k < Dimension; k++) // for each dimension
{
int n = 1; // declare and set an integer variable n to 1
float Score = 0; // declare and set a float variable score to 0
float number; // declare a float variable number
for(int j = pos; j < Chrome_Length + pos; j++) // for each binary bit
{
if(children[i][j] == 1) // if the bit at position [i] [j] in the children array is a 1
{
number = pow(2,(Chrome_Length - n)); // set the value of number equal to 2^(Chrome_Length-n)
Score = Score + number; // add number to the value of score
}
n++; // increment the value of n by one
}
Numbers2[i][k] = Score; // set the value at position [i] [k] in the array Numbers2 equal to the value of score
pos = pos + Chrome_Length; // add the value of Chrome_Length to pos
}
}
}
void Negative_Conversion(float Numbers2 [Population][Dimension],float Numbers [Population][Dimension])
{
float decimal; // delcare floar variable decimal
cout<<endl; // start new line on screen
for (int i=0;i<Population; i++) // for each of the population
{
for (int j=0; j<Dimension;j++) // for each dimension
{
decimal = Numbers2[i][j]; // set the value of deciaml equal to the value at posiyion [i [j] in the Numbers2 array
decimal = (decimal / Mval) - Lval; // divide the value of deciaml by Mval then subtract Lval
Numbers2[i][j]=decimal; // set the value at position [i] [j] in the Numbers2 array equal to the value of decimal
Numbers [i] [j] = Numbers2 [i] [j]; // set the value at position [i] [j] in the numbers array equal to the value at position [i] [j] in the Numbers2 array
}
}
}
float Average_Fitness(float fitness [Population]) // function definition for Average_Fitness
{
float total=0; // declaring float variable total equal to zero
float average; // declaring float variable average
for (int j=0;j<Population; j++) // for each of the population
{
total = total + fitness[j]; // add the value at position [j] in fitness array to total
}
average = total/Population;
// set the value of average to the value of total divided by the value of Population
cout<<" Total Fitness: "<<total; // output the value of total to the screen
cout<<"Average Fitness: "<<average; // output the value of average to the screen
return average; // return the value of average to the main function
}
float Fittest_Chrome(float fitness [Population]) // function declaration for Fittest_Chrome
{
float fittest = 0; // declaring a float variable fittest and setting it to 0
for(int i = 0; i < Population; i++) // for each member of the population
{
if(fitness[i] > fittest) // if the value at position [i] in the fitness array is greater than the value of fittest
{
fittest = fitness[i]; // set the value of fittest equal to the value at position [i] in the fitness array
}
}
cout<<" Fittest Chromasome: "<<fittest; // output the value of fittest to the screen
return fittest; // return the value of fittest to the main function
}