So I am confused as how to reference the indexes of the array of individuals. There is a class of individuals each witha a set of genes and fitness. Then a class called population that is an array of individuals. When making a Population object how do you refer to each individual?
The error occurs in MatingAlgorithm " tournamentPopulation[i] = oldPopulation[random % POPULATION_SIZE]; " and main "nextGen[i] = child;"
An explanation as what I'm doing wrong syntactically would be really helpful.
This is a research project so I cannot share all of the source code. But will provide as much as possible.
//GENETIC ALGORITHM PARAMETERS
const static int POPULATION_SIZE = 50;
const static double MUTATION_RATE = 1;//in percent
const static double CROSSOVER_RATE = 50;//in percent
const static int TOURNAMENT_SIZE = 4;
class Individual
{
//define characteristics of an individual
public:
unsigned long long genes;
double fitness;
//Getters and Setters
long long getGenes()
{
return genes;
}//getGenes
void setGenes(unsigned long long value)
{
genes = value;
}//SetGenes
double getFitness()
{
return fitness;
}//getFitness
void setFitness(double value)
{
fitness = value;
}//setFitness
};//Individual
class Population
{
public:
//CREATE A POPULATION OF INDIVIDUALS
Individual population[POPULATION_SIZE];
//initialize population
void initializePopulation()
{
//CODE HIDDEN
}//initialize population
};//Population
class MatingAlgorithm
{
public:
int random = rand();
Individual tournamentSelection(Population oldPopulation)
{
//MAKE A SUBPOPULATION OF TOURNAMENT INDIVIDUALS
Individual tournamentPopulation[TOURNAMENT_SIZE];
error in this block:
//RANDOMLY PICK FOUR INDIVIDUALS FROM THE POPULATION TO COMPETE
int i;
for(i = 0; i < TOURNAMENT_SIZE; i++)
{
tournamentPopulation[i] = oldPopulation[random % POPULATION_SIZE];
}//for
continues:
//TAKE ONLY THE FITTEST INDIVIDUAL
Individual fittest = tournamentPopulation[0];
for(i = 0; i < TOURNAMENT_SIZE; i++)
{
//IF THE CURRENT FITTEST IS WEAKER THAN THE NEXT INDIVIDUAL IN THE TOURNAMENT SUB POPULATION. DECLARE THE OTHER INDIVIDUAL THE NEW FITTEST
if(fittest.getFitness() < tournamentPopulation[i].getFitness())
{
fittest= tournamentPopulation[i];
}
}//for
return fittest;
}
//LOOP OVER POPULATION AND CREATE NEW INDIVIDUALS WITH CROSSOVER
Individual Crossover(Individual parent1, Individual parent2)
{
unsigned long long bit;
//Initialize Child
Individual child;
child.setGenes(0);
//CODE HIDDEN
return child;
}//crossover
void Mutate(Individual child)
{
//CODE HIDDEN
}//mutate
};//matingAlgorithm
int main()
{
cout<<"Starting...";
Population firstGen;
Population nextGen;
firstGen.initializePopulation();
MatingAlgorithm MA;
//PLACE NEW INDIVIDUALS IN NEW POPULATION;
for (int i = 0; i < POPULATION_SIZE; i++)
{
Individual parent1 = MA.tournamentSelection(firstGen);
Individual parent2 = MA.tournamentSelection(firstGen);
Individual child = MA.Crossover(parent1,parent2);
MA.Mutate(child);
error on this line:
nextGen[i] = child;
continues
cout<<child.getGenes();
}
cout<<"code executed"<<endl;
return 0;
}//main
Aucun commentaire:
Enregistrer un commentaire