A close look at the code.
The above code is very similar to gettingStarted.c seen in the Getting Started tutorial and shall not be explained again. Here the specifics relating to assessing the average behaviour of CGP towards a given task shall be highlighted.
When CGP is repeatedly applied to a given task using repeatCGP the results are stored in a returned results structure. Therefore we need to define a results structure variable to store the returned results. An averageFitness variable is also defined which will later store the average fitness achieved when repeatedly applying CGP to the given task multiple times.
struct results *rels = NULL;
When assessing the average behaviour of CGP towards a given task CGP must be applied to that task a number of times. Each instance of applying CGP towards the task is called a run. Here the numRuns variable stores the number of runs CGP will be applied towards the given task.
int numRuns = 10;
In the Getting Started tutorial we applied CGP to the given task once using runCGP. Here we are repeatedly applying CGP to a give task using repeatCGP. The parameters of repeatCGP are the same as for runCGP except we must also specify the number of runs.
When repeatCGP is called it prints high level information relating to each run to the terminal and returns an initialised results structure.
rels = repeatCGP(params, trainingData, numGens, numRuns);
In order to access the result of applying CGP to a given task multiple times the returned results structure must be queried for information. For instance, getAverageFitness, getAverageActiveNodes and getAverageGenerations can be used to get the average fitness, average number of active nodes and average number of generations respectively.
In the example code getAverageFitness is used to get the average fitness of all the runs which is then printed to the terminal.
averageFitness = getAverageFitness(rels);
printf("The average chromosome fitness is: %f\n", averageFitness);
As well as getting average statistics the best chromosome found on any given run can be accessed using getChromosome.
In the example code getChromosome is used to get the best chromosome found on run four which is then printed to the terminal using printChromosome.
chromo = getChromosome(rels, 4);
printf("The best chromosome found on run 4:\n");
In cases where the results need to be saved to be inspected later the saveResults function can be used. saveResults saves the given results structure to the given file. The format of the results is comma separated in the same form as is displayed to the terminal during repeatCGP; except the averages are not included.
Finally all of the initialised structures should be free’d including the results using freeResults and the chromosome returned from getChromosome using freeChromosome.
And that’s it, running CGP multiple times to assess its average performance is not that much more complicated that running it once.