Evolutionary Programming Framework
All Evolutionary algorithms use the same scheme to evolve populations. The files ea.c and ea.h provide a framework that covers the standard tasks of evolutionary programming. This includes creation of populations, application of evolutionary operations, support for fitness functions, and copy functions for individuals. The framework supports variable genome and population sizes as well as the PLUS and the COMMA strategy. The population is stored internally and no additional
Command line parameter parsing
argc.h and argc.c provide basic parameter parsing. The default parameters are parsed by std_opts(). They are either taken from the command line or the .defaults file that resides in the current working directory. The command line parameters take priority over the parameters stored in the .defaults file. Parsed arguments are stored in the variables that were supplied to std_opts().
int std_opts(int argc, char **argv, int *mu, int *lambda, int *plus,
int *prob_size, int *generations, int *modulo, int *prt_opts, double *sigma, double *x0)
Parameters:
• sigma – step size
• generations – number of generations to compute
• prob_size – number of free parameters in an individual
• mu – parent count
• lambda – children count
• plus – evolution strategy
• x0 – parameter range
• modulo – print every n-th line
• prt_opts – debugging options Example for command line parameters:
./main sigma=0.1 generations=100 prob_size=3 mu=1 lambda=10 plus=1 x0=10.0 modulo=5
Example for a .defaults file:
x0=10 generations=400 sigma=10 prob_size=2
EA operators
#include “ea.h”
int make_populations( int mu, int lambda, int problem_size ) Generates a population.
mu: number of parents
lambda: number of offspring
size: size of genome
int destroy_populations()
Removes a previously generated population.
int init_parents( double min_x, double max_x, double sigma )
Initializes all genes of the parent population with random values within min_x and max_x. Also sets the step size sigma in each individual.
double get_parameter( int pop_type, int o, int i ) Returns the parameter i of individual o.
pop_type: PARENT or OFFSPRING
int set_parameter( int pop_type, int o, int i, double val )
Sets parameter i of individual o to value val.
pop_type: PARENT or OFFSPRING double get_sigma( int pop_type, int o ) Retrieves the current sigma of an offspring.
pop_type: PARENT or OFFSPRING
o: number of offspring
int set_sigma( int pop_type, int o, double sigma ) Sets the sigma of offspring o.
pop_type: PARENT or OFFSPRING
o: number of offspring
sigma: sigma to set
double get_fitness( int pop_type, int o ) Retrieves the fitness of offspring o.
pop_type: PARENT or OFFSPRING
o: number of offspring
int set_fitness( int pop_type, int o, double fitness ) Sets the fitness of individual o.
pop_type: PARENT or OFFSPRING
o: number of offspring
fitness: fitness to be set double get_faverage()
returns the average fitness of the population.
int copy_objects( int from_pop, int from_index, int to_pop, int to_index ) Copies an individual from_index to to_index.
from_pop: PARENT or OFFSPRING from_index: index of source individual to_pop: PARENT or OFFSPRING to_index: destination index
int selection( int smode ) performs the selection operation.
smode:PLUS or COMMA int mutate_offspring( int i ) Performs the mutation operation.
i: index of offspring to mutate
Random Number Generators
#include “rand.h”
long uRand()
Returns a random number between 0 and 65535.
double drand()
Returns a random number between 0.0 and 1.0.
int choice()
Returns 0 or 1 randomly int n_choice( int n )
Returns random numbers from 0 to n-1.
double gauss()
Returns Gaussian distributed random numbers between 0.0 and 1.0.
double bga_mut()
Returns sigma used in the Breeder Genetic Algorithm.
Tools
#include “tools.h”
int ferr( char *txt, char *cmd, int para ) Prints a fatal error message to stderr txt: Error message
cmd: function name in which the error occurred para: an additional integer paramter
char *m_alloc( int size, char *name ) Allocates memory from the system.
size: number of bytes to be allocated
name: an additional name for the memory area (used for debugging) char *m_free( char *p, char *name )
Frees system memory, previously allocated with m_alloc.
p: pointer to the allocated memory area name: additional name of the memory area int prt_dot( int run )
Prints a dot. In case run is dividable by 10, a vertical line is printed.
run: count
char *tst_opt( char *opt, char *name )
Checks if the string opt starts with name and has a '=' after it.
Returns the pointer to the start position of the option's value or 0.
int i_arg( char *opt, int *var, char *name )
Checks for the argument name in the string opt and stores its integer value into var. Returns 0 in case the option is not found. In case the value can not be converted, 0 is stored into var.
int f_arg( char *opt, double *var, char *name )
Checks for the argument name in the string opt and stores its floating value into var. Returns 0 in case the option is not found.
char *mk_name( char *n1, char *n2 )
Returns a new string that contains the strings n1 and n2 separated by a dot.
Example Code
#include <stdio.h>
#include <math.h>
#include "argc.h"
#include "ea.h"
/* global variables */
int generations = 100 int prt_opts = 0;
int mu = 1 int lambda = 6 int prob_size = 10;
double sigma = 0.1;
int plus = 0;
double x0 = 1.0;
int modulo = 10;
// Example of a fitness function
static double fitness( int pop_type, int o, int prob_size ) {
double f;
f=...
return f;
}
// Main program
main( int argc, char **argv ) {
int i, g;
argc = std_opts( argc, argv, &mu, &lambda, &plus, &prob_size, &generations, &modulo, &prt_opts, &sigma, &x0 );
if ( argc > 0 ) {
// room to process own options for( i = 0; i < argc; i++ )
fprintf( stderr, "warning, unprocessed option: '%s'\n", argv[ i ] );
fprintf( stderr, "\n" );
}
make_populations( mu, lambda, prob_size );
// init parents // loop generation // copy operation // mutate operation // evaluate fitness // selection
}