libfgen  0.1.15
Library for optimization using a genetic algorithm or particle swarm optimization
Data Structures | Defines | Typedefs | Functions
fgen (Genetic Algorithm) API

Data Structures

struct  FgenIndividual
struct  FgenCacheEntry
struct  FgenCache
struct  FgenRNG
struct  FgenPopulation

Defines

#define FGEN_RNG_STATE_SIZE   4096
#define FGEN_FITNESS_PROPORTIONAL   1
#define FGEN_SUBTRACT_MIN_FITNESS   2
#define FGEN_SUBTRACT_MIN_FITNESS_DIV_2   3
#define FGEN_STOCHASTIC_TYPE_MASK   3
#define FGEN_ELITIST_ELEMENT   4
#define FGEN_EXTINCTION_ELEMENT   8
#define FGEN_STOCHASTIC   0
#define FGEN_SUS   1
#define FGEN_TOURNAMENT   2
#define FGEN_RANK   3
#define FGEN_KILL_TOURNAMENT_ELEMENT   16
#define FGEN_ELITIST_STOCHASTIC   (FGEN_STOCHASTIC | FGEN_ELITIST_ELEMENT)
#define FGEN_ELITIST_SUS   (FGEN_SUS | FGEN_ELITIST_ELEMENT)
#define FGEN_ELITIST_SUS_WITH_EXTINCTION   (FGEN_SUS | FGEN_ELITIST_ELEMENT | FGEN_EXTINCTION_ELEMENT)
#define FGEN_ELITIST_TOURNAMENT   (FGEN_TOURNAMENT | FGEN_ELITIST_ELEMENT)
#define FGEN_ELITIST_TOURNAMENT_WITH_EXTINCTION   (FGEN_TOURNAMENT | FGEN_ELITIST_ELEMENT | FGEN_EXTINCTION_ELEMENT)
#define FGEN_ELITIST_RANK   (FGEN_RANK | FGEN_ELITIST_ELEMENT)
#define FGEN_KILL_TOURNAMENT   (FGEN_TOURNAMENT | FGEN_KILL_TOURNAMENT_ELEMENT)
#define FGEN_INITIALIZATION_SEED   0
#define FGEN_INITIALIZATION_CONTINUE   1

Typedefs

typedef void(* FgenGenerationCallbackFunc )(FgenPopulation *pop, int generation)
typedef double(* FgenCalculateFitnessFunc )(const FgenPopulation *pop, const unsigned char *bitstring)
typedef void(* FgenSeedFunc )(FgenPopulation *pop, unsigned char *bitstring)
typedef void(* FgenMutationFunc )(FgenPopulation *pop, const unsigned char *parent, unsigned char *child)
typedef void(* FgenCrossoverFunc )(FgenPopulation *pop, const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1, unsigned char *child2)

Functions

FGEN_API FgenPopulation * fgen_create (int population_size, int individual_size_in_bits, int data_element_size, FgenGenerationCallbackFunc fgen_generation_callback_func, FgenCalculateFitnessFunc fgen_calculate_fitness_func, FgenSeedFunc fgen_seed_func, FgenMutationFunc fgen_mutation_func, FgenCrossoverFunc fgen_crossover_func)
FGEN_API void fgen_initialize (FgenPopulation *pop, int population_size, int individual_size_in_bits, int data_element_size, FgenGenerationCallbackFunc fgen_generation_callback_func, FgenCalculateFitnessFunc fgen_calculate_fitness_func, FgenSeedFunc fgen_seed_func, FgenMutationFunc fgen_mutation_func, FgenCrossoverFunc fgen_crossover_func)
FGEN_API void fgen_set_parameters (FgenPopulation *pop, int selection_type, int selection_fitness_type, float crossover_probability_float, float mutation_per_bit_probability_float, float macro_mutation_probability_float)
FGEN_API void fgen_run (FgenPopulation *pop, int max_generation)
FGEN_API void fgen_run_threaded (FgenPopulation *pop, int max_generation)
FGEN_API void fgen_destroy (FgenPopulation *pop)
FGEN_API void fgen_run_archipelago (int nu_pops, FgenPopulation **pops, int max_generation)
FGEN_API void fgen_run_archipelago_threaded (int nu_pops, FgenPopulation **pops, int max_generation)
FGEN_API void fgen_run_steady_state (FgenPopulation *pop, int max_generation)
FGEN_API void fgen_run_steady_state_archipelago (int nu_pops, FgenPopulation **pops, int max_generation)
FGEN_API void fgen_run_steady_state_archipelago_threaded (int nu_pops, FgenPopulation **pops, int max_generation)
FGEN_API void fgen_set_mutation_probability (FgenPopulation *pop, float)
FGEN_API void fgen_set_macro_mutation_probability (FgenPopulation *pop, float)
FGEN_API void fgen_set_crossover_probability (FgenPopulation *pop, float)
FGEN_API void fgen_set_selection_fitness_type (FgenPopulation *pop, int)
FGEN_API void fgen_set_selection_type (FgenPopulation *pop, int)
FGEN_API void fgen_set_tournament_size (FgenPopulation *pop, int)
FGEN_API void fgen_set_data_element_size (FgenPopulation *pop, int)
FGEN_API void fgen_set_number_of_elites (FgenPopulation *pop, int)
FGEN_API void fgen_set_permutation_size (FgenPopulation *pop, int)
FGEN_API void fgen_set_user_data (FgenPopulation *pop, void *user_data)
FGEN_API void fgen_enable_cache (FgenPopulation *pop, int cache_size)
FGEN_API void fgen_enable_cache_on_archipelago (int nu_pops, FgenPopulation **pops, int cache_size)
FGEN_API void fgen_invalidate_cache (FgenPopulation *pop)
FGEN_API void fgen_set_migration_probability (FgenPopulation *pop, float)
FGEN_API void fgen_set_migration_interval (FgenPopulation *pop, int)
FGEN_API void fgen_set_generation_callback_interval (FgenPopulation *pop, int)
FGEN_API void fgen_set_initialization_type (FgenPopulation *pop, int)
FGEN_API void fgen_seed_random (FgenPopulation *pop, unsigned char *bitstring)
FGEN_API void fgen_mutation_per_bit (FgenPopulation *pop, const unsigned char *parent, unsigned char *child)
FGEN_API void fgen_mutation_per_bit_plus_macro_mutation (FgenPopulation *pop, const unsigned char *parent, unsigned char *child)
FGEN_API void fgen_mutation_per_bit_fast (FgenPopulation *pop, const unsigned char *parent, unsigned char *child)
FGEN_API void fgen_mutation_per_bit_plus_macro_mutation_fast (FgenPopulation *pop, const unsigned char *parent, unsigned char *child)
FGEN_API void fgen_crossover_one_point_per_bit (FgenPopulation *pop, const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1, unsigned char *child2)
FGEN_API void fgen_crossover_one_point_per_element (FgenPopulation *pop, const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1, unsigned char *child2)
FGEN_API void fgen_crossover_two_point_per_bit (FgenPopulation *pop, const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1, unsigned char *child2)
FGEN_API void fgen_crossover_two_point_per_element (FgenPopulation *pop, const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1, unsigned char *child2)
FGEN_API void fgen_crossover_uniform_per_bit (FgenPopulation *pop, const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1, unsigned char *child2)
FGEN_API void fgen_crossover_uniform_per_element (FgenPopulation *pop, const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1, unsigned char *child2)
FGEN_API void fgen_seed_permutation_random (FgenPopulation *pop, unsigned char *bitstring)
FGEN_API void fgen_mutation_permutation_swap (FgenPopulation *pop, const unsigned char *parent, unsigned char *child)
FGEN_API void fgen_mutation_permutation_insert (FgenPopulation *pop, const unsigned char *parent, unsigned char *child)
FGEN_API void fgen_mutation_permutation_invert (FgenPopulation *pop, const unsigned char *parent, unsigned char *child)
FGEN_API void fgen_crossover_permutation_order_based (FgenPopulation *pop, const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1, unsigned char *child2)
FGEN_API void fgen_crossover_permutation_position_based (FgenPopulation *pop, const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1, unsigned char *child2)
FGEN_API void fgen_crossover_noop (FgenPopulation *pop, const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1, unsigned char *child2)
FGEN_API FgenIndividualfgen_best_individual_of_population (FgenPopulation *pop)
FGEN_API FgenIndividualfgen_worst_individual_of_population (FgenPopulation *pop)
FGEN_API FgenIndividualfgen_best_individual_of_archipelago (int nu_pops, FgenPopulation **pops)
FGEN_API void fgen_update_population_fitness (FgenPopulation *pop)
FGEN_API void fgen_invalidate_population_fitness (FgenPopulation *pop)
FGEN_API void fgen_signal_stop (FgenPopulation *pop)
FGEN_API int fgen_individual_size_in_bytes (const FgenPopulation *pop)
FGEN_API int fgen_get_island (const FgenPopulation *pop)
FGEN_API int fgen_is_cached (const FgenPopulation *pop)
FGEN_API double fgen_get_cache_hit_rate (const FgenPopulation *pop)
FGEN_API FgenRNGfgen_get_rng (const FgenPopulation *pop)
FGEN_API int fgen_get_generation (const FgenPopulation *pop)
FGEN_API double fgen_bitstring_uint16_to_double (const unsigned char *bitstring, double domain_min, double domain_max)
FGEN_API double fgen_bitstring_uint32_to_double (const unsigned char *bitstring, double domain_min, double domain_max)
FGEN_API double fgen_bitstring_uint64_to_double (const unsigned char *bitstring, double domain_min, double domain_max)
FGEN_API void fgen_decode_from_gray (const FgenPopulation *pop, const unsigned char *src_bitstring, unsigned char *dest_bitstring)
FGEN_API void fgen_encode_to_gray (const FgenPopulation *pop, const unsigned char *src_bitstring, unsigned char *dest_bitstring)
FGEN_API void fgen_mutate_bit (unsigned char *bitstring, int n)
FGEN_API void fgen_set_random_bitstring (FgenRNG *rng, unsigned char *bitstring, int offset, int nu_bits)
FGEN_API int fgen_get_bit (const unsigned char *bitstring, int n)
FGEN_API void fgen_copy_partial_bitstring (const unsigned char *src_bitstring, unsigned char *dest_bitstring, int offset, int nu_bits)
FGEN_API FgenRNGfgen_random_create_rng ()
FGEN_API void fgen_random_destroy_rng (FgenRNG *rng)
FGEN_API void fgen_random_seed_rng (FgenRNG *rng, unsigned int seed)
FGEN_API void fgen_random_seed_with_timer (FgenRNG *rng)
FGEN_API int fgen_random_2 (FgenRNG *rng)
FGEN_API int fgen_random_8 (FgenRNG *rng)
FGEN_API int fgen_random_16 (FgenRNG *rng)
FGEN_API unsigned int fgen_random_32 (FgenRNG *rng)
FGEN_API int fgen_random_n (FgenRNG *rng, int n)
FGEN_API float fgen_random_f (FgenRNG *rng, float range)
FGEN_API double fgen_random_d (FgenRNG *rng, double range)
FGEN_API double fgen_random_from_range_d (FgenRNG *rng, double min_bound, double max_bound)

Typedef Documentation

typedef void(* FgenGenerationCallbackFunc)(FgenPopulation *pop, int generation)

The generation callback function. The current population and the current generation are passed as arguments.

Definition at line 126 of file fgen.h.

typedef double(* FgenCalculateFitnessFunc)(const FgenPopulation *pop, const unsigned char *bitstring)

The fitness evaluation function. The current population and the bitstring to be evaluated are passed as arguments. See fgen_create().

Definition at line 129 of file fgen.h.

typedef void(* FgenSeedFunc)(FgenPopulation *pop, unsigned char *bitstring)

The seeding operator function type. bitstring is already allocated.

Definition at line 131 of file fgen.h.

typedef void(* FgenMutationFunc)(FgenPopulation *pop, const unsigned char *parent, unsigned char *child)

The mutation operator function type. When this is called the child is allocated and already contains a copy of the parent. This function is called for every individual of the population.

Definition at line 134 of file fgen.h.

typedef void(* FgenCrossoverFunc)(FgenPopulation *pop, const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1, unsigned char *child2)

The crossover operator function type. The children are allocated but not filled in. This function is called depending on the crossover probability.

Definition at line 138 of file fgen.h.


Function Documentation

FGEN_API FgenPopulation* fgen_create ( int  population_size,
int  individual_size_in_bits,
int  data_element_size,
FgenGenerationCallbackFunc  fgen_generation_callback_func,
FgenCalculateFitnessFunc  fgen_calculate_fitness_func,
FgenSeedFunc  fgen_seed_func,
FgenMutationFunc  fgen_mutation_func,
FgenCrossoverFunc  fgen_crossover_func 
)

Create an fgen population. The random number generator belonging to the population is seeded with zero.

Parameters:
population_sizeSize of the population.
individual_size_in_bitsSize of each individual in bits, must be a multiple of 8.
data_element_sizeSize of each data element in bits, preferably a multiple of 8 or equal to 1. This affects several types of crossover and mutation operators, as well as Gray coding. The library is optimized for values of 16, 32 or 64.
fgen_generation_callback_funcThe callback function that is called by fgen after every n generations, where n is defined by the generation callback interval.
fgen_calculate_fitness_funcThe fitness evaluation function that calculates the fitness of an individual. It should return a double floating point value where higher values indicate higher fitness. In general fitness values greater or equal to zero are advisable, although negative fitness values are allowed in some cases (when FGEN_SUBTRACT_MIN_FITNESS is set with SUS selection or with any kind of tournament selection). Infinity is allowed (INFINITY in Linux), negative infinity and NaN are not. In Windows positive infinity is defined as DBL_MAX. When a fitness evaluation returns this value, the algorithm will continue but evolution may stop, and in the case of threaded concurrent islands skip generations and generate an early generation_callback.
fgen_seed_funcThe seeding operator. This is called for every member of the population when the algorithm starts (see fgen_run()).
fgen_mutation_funcThe mutation operator. When this is called the child is allocated and already contains a copy of the parent. This function is called for every individual of the population; logic reflecting the mutation probability should be used within the mutation operator function.
fgen_crossover_funcThe crossover operator. The children are allocated but not filled in. This function is called depending on the crossover probability (so in general there is no need to use the crossover probability within the operator).
Returns:
The created population.

Definition at line 144 of file ga.c.

FGEN_API void fgen_initialize ( FgenPopulation *  pop,
int  population_size,
int  individual_size_in_bits,
int  data_element_size,
FgenGenerationCallbackFunc  fgen_generation_callback_func,
FgenCalculateFitnessFunc  fgen_calculate_fitness_func,
FgenSeedFunc  fgen_seed_func,
FgenMutationFunc  fgen_mutation_func,
FgenCrossoverFunc  fgen_crossover_func 
)

Initialize an fgen population. pop must be allocated first. The random number generator belonging to the population is seeded with zero. For binary compatibility between different versions of libfgen, use fgen_create() instead of this function.

Parameters:
popThe population to initialize.
population_sizeSize of the population.
individual_size_in_bitsSize of each individual in bits, must be a multiple of 8.
data_element_sizeSize of each data element in bits, preferably a multiple of 8 or 1.
fgen_generation_callback_funcThe callback function that is called by fgen after each generation.
fgen_calculate_fitness_funcThe fitness evaluation function that calculates the fitness of an individual.
fgen_seed_funcThe seeding operator.
fgen_mutation_funcThe mutation operator.
fgen_crossover_funcThe crossover operator.
Returns:
The created population.

Definition at line 171 of file ga.c.

FGEN_API void fgen_set_parameters ( FgenPopulation *  pop,
int  selection_type,
int  selection_fitness_type,
float  crossover_probability_float,
float  mutation_probability_float,
float  macro_mutation_probability_float 
)

Set the genetic algorithm parameters for an fgen population. These values are not initialized by fgen_create(), so in general this function must be called after every population is created with fgen_create(). It is also possible to use functions that set parameters seperately.

Parameters:
popThe population. It should be created or initialized before this function is called.
selection_typeThe selection type. See fgen_set_selection_type().
selection_fitness_typeThe fitness selection type. See fgen_set_selection_fitness_type().
crossover_probability_floatThe crossover probability per individual.
mutation_probability_floatThe mutation probability (per bit for bitstrings, per individual for permutations).
macro_mutation_probability_floatThe macro-mutation probability (per data element).

Definition at line 212 of file ga.c.

FGEN_API void fgen_run ( FgenPopulation *  pop,
int  max_generation 
)

Run the genetic algorithm. First the initial population is created using the seeding function. For every generation selection, crossover and mutation are applied in that order. With a frequency defined by the generation callback interval, the generation callback function is called. Use the generation callback function to keep track of evolution (use fgen_best_individual_of_population() to get the individual with the best fitness) and print out results or draw graphics reflecting the best solution, and use fgen_signal_stop() to let the algorithm terminate.

Parameters:
popThe population.
max_generationThe maximum generation. When this is reached (or when stop is signalled) the function exits. If it is equal to - 1 the algorithm runs indefinitely and only stops when fgen_signal_stop() is called.

Definition at line 233 of file ga.c.

FGEN_API void fgen_run_threaded ( FgenPopulation *  pop,
int  max_generation 
)

Run the genetic algorithm. Threaded version. Because the amount of parallellism is limited to concurrent fitness calculations, the performance is generally better with the unthreaded version for simple problems. However, with larger populations and more expensive fitness functions the speed-up can be respectable. In Linux the number of threads to use is obtained from a sysconf() system call returning the number of online processor cores in the system. On windows the number of threads is set to 4. The fitness cache should not be enabled when using this function.

Parameters:
popThe population.
max_generationThe maximum generation. When this is reached (or when stop is signalled) the function exits. A value of - 1 means the function only returns when stop is signalled.

Definition at line 304 of file ga.c.

FGEN_API void fgen_destroy ( FgenPopulation *  pop)

Free all data structures associated with a population. This includes the cache, if any, the random number generator, the data structures for individuals, and the FgenPopulation data structure itself.

Parameters:
popThe population.

Definition at line 267 of file ga.c.

FGEN_API void fgen_run_archipelago ( int  nu_pops,
FgenPopulation **  pops,
int  max_generation 
)

Run the genetic algorithm on an archipelago of populations. The seeds of the random number generators of the islands after the first one are initialized with random seeds derived from the first island's random number generator. Each population in the pops[] array must have been created using fgen_create() and parameters set (usually the same parameters for each population) before calling this function. The generation callback function will be called individually for each island in the archipelago.

Parameters:
nu_popsNumber of islands.
popsAn array of populations.
max_generationThe maximum generation. When this is reached (or when stop is signalled) the function exits. A value of - 1 means the function only returns when stop is signalled.

Definition at line 359 of file ga.c.

FGEN_API void fgen_run_archipelago_threaded ( int  nu_pops,
FgenPopulation **  pops,
int  max_generation 
)

Run the genetic algorithm on an archipelago of populations. Threaded version, uses one thread per island. The seeds of the random number generators of the islands after the first one are initialized with random seeds derived from the first island's random number generator. The generation callback function will be called individually for each island in the archipelago.

Parameters:
nu_popsNumber of islands.
popsAn array of populations.
max_generationThe maximum generation. When this is reached (or when stop is signalled) the function exits. A value of - 1 means the function only returns when stop is signalled.

Definition at line 492 of file ga.c.

FGEN_API void fgen_run_steady_state ( FgenPopulation *  pop,
int  max_generation 
)

Run the steady state version of the genetic algorithm. In steady state evolution, only two individuals are selected for possible crossover and mutation in each "generation" and replace two random individuals in the population. Only tournament selection is supported, and elitism and extinction are not supported. The selection type must be FGEN_TOURNAMENT or FGEN_KILL_TOURNAMENT; in the latter case, the random individuals selected for replacement are selected with tournament selection of the worst individual. The generation callback interval should be set to a high value for efficiency, as a rule of thumb generation intervals should multiplied by half the population size compared to the regular genetic algorithm.

Parameters:
popThe population.
max_generationThe maximum generation. When this is reached (or when stop is signalled) the function exits. A value of - 1 means the function only returns when stop is signalled.

Definition at line 161 of file steady_state.c.

FGEN_API void fgen_run_steady_state_archipelago ( int  nu_pops,
FgenPopulation **  pops,
int  max_generation 
)

Run the steady state version of the genetic algorithm on an archipelago. In steady state evolution, only two individuals are selected for possible crossover and mutation in each "generation" and replace two random individuals in the population. Only tournament selection is supported, and elitism and extinction are not supported. The selection type must be FGEN_TOURNAMENT or FGEN_KILL_TOURNAMENT; in the latter case, the random individuals selected for replacement are selected with tournament selection of the worst individual. The generation callback interval should be set to a high value for efficiency. The seeds of the random number generators of the islands after the first one are initialized with random seeds derived from the first island's random number generator.

Parameters:
nu_popsNumber of islands.
popsAn array of populations.
max_generationThe maximum generation. When this is reached (or when stop is signalled) the function exits. A value of - 1 means the function only returns when stop is signalled.

Definition at line 205 of file steady_state.c.

FGEN_API void fgen_run_steady_state_archipelago_threaded ( int  nu_pops,
FgenPopulation **  pops,
int  max_generation 
)

Run the steady state genetic algorithm on an archipelago of populations. Threaded version, uses one thread per island. The seeds of the random number generators of the islands after the first one are initialized with random seeds derived from the first island's random number generator.

Parameters:
nu_popsNumber of islands.
popsAn array of populations.
max_generationThe maximum generation. When this is reached (or when stop is signalled) the function exits. A value of - 1 means the function only returns when stop is signalled.

Definition at line 340 of file steady_state.c.

FGEN_API void fgen_set_mutation_probability ( FgenPopulation *  pop,
float  p 
)

Set the mutation probability to a floating value from 0 to 1.0. Note that the mutation operator function is called for every individual regardless of the mutation probability. The operator itself uses the probability to apply mutations. The precise meaning of the probability (per bit, or per individual for example) varies between operators.

Definition at line 41 of file parameters.c.

FGEN_API void fgen_set_macro_mutation_probability ( FgenPopulation *  pop,
float  p 
)

Set the macro-mutation probability to a floating point value from 0 to 1.0. Only has meaning if fgen_mutation_per_bit_plus_macro_mutation() or fgen_mutation_per_bit_plus_macro_mutation_fast() is the mutation operator. May also be used as a parameter for a custom operator.

Definition at line 55 of file parameters.c.

FGEN_API void fgen_set_crossover_probability ( FgenPopulation *  pop,
float  p 
)

Set the crossover probability to a floating point value forom 0 to 1.0.

Definition at line 64 of file parameters.c.

FGEN_API void fgen_set_selection_fitness_type ( FgenPopulation *  pop,
int  t 
)

Set the selection fitness type.

Parameters:
popThe population.
tOne of FGEN_FITNESS_PROPORTIONAL, FGEN_SUBTRACT_MIN_FITNESS (the minimum fitness of the population is subtracted from all fitness values), or FGEN_SUBTRACT_FITNESS_DIV_2 (the minimum fitness divided by two is subtracted). In general FGEN_SUBTRACT_MIN_FITNESS is recommended.

Definition at line 78 of file parameters.c.

FGEN_API void fgen_set_selection_type ( FgenPopulation *  pop,
int  t 
)

Set the selection type. In general elitist SUS or elitist tournament selection is recommended.

Parameters:
popThe population.
tOne of the following:
  • FGEN_STOCHASTIC: Use simple roulette-wheel selection.
  • FGEN_SUS: Use stochastic universal sampling.
  • FGEN_RANK: Use rank-based selection.
  • FGEN_TOURNAMENT: Use tournament selection. The size is set with fgen_set_tournament_size().
  • FGEN_ELITIST_SUS, FGEN_ELITIST_RANK, FGEN_ELITIST_TOURNAMENT: One or more of the best individuals of the population are always selected. See fgen_set_number_of_elites().
  • FGEN_ELITIST_SUS_WITH_EXTINCTION, FGEN_ELITIST_TOURNAMENT_WITH_EXTINCTION: Every 200 generations, the best individual is preserved and the rest replaced with random new individuals.
  • FGEN_KILL_TOURNAMENT: In steady state evolution using fgen_run_steady_state(), the random individuals selected for replacement are selected with tournament selection of the worst individual.

Definition at line 99 of file parameters.c.

FGEN_API void fgen_set_tournament_size ( FgenPopulation *  pop,
int  n 
)

Set the tournament size for tournament selection. The default value is 3.

Definition at line 107 of file parameters.c.

FGEN_API void fgen_set_data_element_size ( FgenPopulation *  pop,
int  n 
)

Set the data element size in bits. Each individual can be divided into chromosomes (for example 32-bit values that encode an integer or real number). The "per element" crossover operators as well as macro mutation operate with data element boundaries.

Definition at line 117 of file parameters.c.

FGEN_API void fgen_set_number_of_elites ( FgenPopulation *  pop,
int  n 
)

Set the number of elite individuals (with the best fitness) that will always survive selection when elitism is enabled. The default value is the population size divided by 64.

Definition at line 126 of file parameters.c.

FGEN_API void fgen_set_permutation_size ( FgenPopulation *  pop,
int  n 
)

Set the permutation size used by the permutation mutation and crossover operators.

Definition at line 134 of file parameters.c.

FGEN_API void fgen_set_user_data ( FgenPopulation *  pop,
void *  data 
)

Set the user_data pointer field of the population. This is used by ffit. When using fgen it may be used to conveniently tie an auxilliary data structure to a population.

Definition at line 143 of file parameters.c.

FGEN_API void fgen_enable_cache ( FgenPopulation *  pop,
int  size 
)

Enable fitness cache on population. Size is the number of entries. Memory used is roughly the number of entries times the size of the bitstring of an individual. The cache imposes overhead, but for combinatorial problems with an expensive fitness function it should be beneficial. For real-valued optimization problems, it is not effective. When running an archipelago of GAs, it is safe to use a seperate cache for each population.

Definition at line 70 of file cache.c.

FGEN_API void fgen_enable_cache_on_archipelago ( int  nu_pops,
FgenPopulation **  pops,
int  size 
)

Enable one cache shared between all archipelagos. This only works with the unthreaded fgen_run_archipelago().

Definition at line 89 of file cache.c.

FGEN_API void fgen_invalidate_cache ( FgenPopulation *  pop)

Invalidate the fitness cache. Should be run when the fitness functions changes in between calls to fgen_run with the same cache enabled. When using a shared cache on an archipelago this function only needs to be called for one island.

Definition at line 112 of file cache.c.

FGEN_API void fgen_set_migration_probability ( FgenPopulation *  pop,
float  p 
)

Set the migration probability per individual during a generation when migration is allowed. This probability must be set seperately for each island.

Definition at line 152 of file parameters.c.

FGEN_API void fgen_set_migration_interval ( FgenPopulation *  pop,
int  n 
)

Set the migration interval. A value of 1 means migration happens every generation, a value of 50 means migration happens only every 50th generation. The default is 1. A value of 0 means no migration. The migration interval of the first population in an archipelago (pops[0]) is currently used for all islands.

Definition at line 163 of file parameters.c.

FGEN_API void fgen_set_generation_callback_interval ( FgenPopulation *  pop,
int  n 
)

Set the interval at which the generation callback function is called. A value of 1 means it is called every generation, a value of 50 means it is called only every 50th generation. The default is 1. Higher values are beneficial for threaded execution.

Definition at line 173 of file parameters.c.

FGEN_API void fgen_set_initialization_type ( FgenPopulation *  pop,
int  v 
)

Set the initialization type, which selects whether to continue with an existing population at the start of fgen_run() and variants, if it exists, or whether to seed a new population. FGEN_INITIALIZATION_SEED or FGEN_INITIALIZATION_CONTINUE are valid values, the default is FGEN_INITIALIZATION_SEED.

Definition at line 185 of file parameters.c.

FGEN_API void fgen_seed_random ( FgenPopulation *  pop,
unsigned char *  bitstring 
)

Seeding (initialization) operator that initializes a bitstring with random values.

Definition at line 36 of file seed.c.

FGEN_API void fgen_mutation_per_bit ( FgenPopulation *  pop,
const unsigned char *  parent,
unsigned char *  child 
)

Bit-wise mutation operator. Each bit is toggled with the preset mutation probability per bit.

Definition at line 317 of file mutation.c.

FGEN_API void fgen_mutation_per_bit_plus_macro_mutation ( FgenPopulation *  pop,
const unsigned char *  parent,
unsigned char *  child 
)

Mutation operator that performs both a bit-wise mutation and a macro mutation where a random data element is entirely replaced with a random value. The preset macro mutation probability is applied per data element.

Definition at line 346 of file mutation.c.

FGEN_API void fgen_mutation_per_bit_fast ( FgenPopulation *  pop,
const unsigned char *  parent,
unsigned char *  child 
)

Fast bit-wise mutation operator. A predefined random number of bits is mutated. There is a chance that the same bit is toggled more than once, but because the mutation rate per bit is low it is not that important.

Definition at line 333 of file mutation.c.

FGEN_API void fgen_mutation_per_bit_plus_macro_mutation_fast ( FgenPopulation *  pop,
const unsigned char *  parent,
unsigned char *  child 
)

Mutation operator that performs both a bit-wise mutation and a macro mutation where a random data element is entirely replaced with a random value. The preset macro mutation probability is applied per data element. Fast version.

Definition at line 369 of file mutation.c.

FGEN_API void fgen_crossover_one_point_per_bit ( FgenPopulation *  pop,
const unsigned char *  parent1,
const unsigned char *  parent2,
unsigned char *  child1,
unsigned char *  child2 
)

Bit-wise one-point crossover operator.

Definition at line 115 of file crossover.c.

FGEN_API void fgen_crossover_one_point_per_element ( FgenPopulation *  pop,
const unsigned char *  parent1,
const unsigned char *  parent2,
unsigned char *  child1,
unsigned char *  child2 
)

Per element one-point crossover operator that respects data element boundaries.

Definition at line 90 of file crossover.c.

FGEN_API void fgen_crossover_two_point_per_bit ( FgenPopulation *  pop,
const unsigned char *  parent1,
const unsigned char *  parent2,
unsigned char *  child1,
unsigned char *  child2 
)

Bit-wise two-point crossover operator.

Definition at line 168 of file crossover.c.

FGEN_API void fgen_crossover_two_point_per_element ( FgenPopulation *  pop,
const unsigned char *  parent1,
const unsigned char *  parent2,
unsigned char *  child1,
unsigned char *  child2 
)

Per-element two-point crossover operator that respects data element boundaries.

Definition at line 132 of file crossover.c.

FGEN_API void fgen_crossover_uniform_per_bit ( FgenPopulation *  pop,
const unsigned char *  parent1,
const unsigned char *  parent2,
unsigned char *  child1,
unsigned char *  child2 
)

Bit-wise uniform crossover operator. Each bit is randomly selected from one of the two parents.

Definition at line 193 of file crossover.c.

FGEN_API void fgen_crossover_uniform_per_element ( FgenPopulation *  pop,
const unsigned char *  parent1,
const unsigned char *  parent2,
unsigned char *  child1,
unsigned char *  child2 
)

Crossover operator that respects data element boundaries. Each data element is selected randomly from one of the two parents. This is also called discrete recombination.

Definition at line 244 of file crossover.c.

FGEN_API void fgen_seed_permutation_random ( FgenPopulation *  pop,
unsigned char *  bitstring 
)

Seeding operator that initializes an individual with a random permutation corresponding to the size set by fgen_set_permutation_size().

Definition at line 46 of file seed.c.

FGEN_API void fgen_mutation_permutation_swap ( FgenPopulation *  pop,
const unsigned char *  parent,
unsigned char *  child 
)

Simple mutation operator for permutations.

Definition at line 409 of file mutation.c.

FGEN_API void fgen_mutation_permutation_insert ( FgenPopulation *  pop,
const unsigned char *  parent,
unsigned char *  child 
)

Insert mutation operator for permutations. Takes a random element from the permutation and reinserts it at a random location in the permutation.

Definition at line 427 of file mutation.c.

FGEN_API void fgen_mutation_permutation_invert ( FgenPopulation *  pop,
const unsigned char *  parent,
unsigned char *  child 
)

Invert mutation operator for permutations that inverts a random subroute within the permutation.

Definition at line 458 of file mutation.c.

FGEN_API void fgen_crossover_permutation_order_based ( FgenPopulation *  pop,
const unsigned char *  parent1,
const unsigned char *  parent2,
unsigned char *  child1,
unsigned char *  child2 
)

Order based (OX1) crossover operator for permutations.

Definition at line 309 of file crossover.c.

FGEN_API void fgen_crossover_permutation_position_based ( FgenPopulation *  pop,
const unsigned char *  parent1,
const unsigned char *  parent2,
unsigned char *  child1,
unsigned char *  child2 
)

Position-based crossover operator for permutations. Effective for the travelling salesman problem.

Definition at line 395 of file crossover.c.

FGEN_API void fgen_crossover_noop ( FgenPopulation *  pop,
const unsigned char *  parent1,
const unsigned char *  parent2,
unsigned char *  child1,
unsigned char *  child2 
)

Crossover operator that does nothing.

Definition at line 441 of file crossover.c.

FGEN_API FgenIndividual* fgen_best_individual_of_population ( FgenPopulation *  pop)

Returns the best individual of the population (the one with the highest fitness)

Parameters:
popThe population.
Returns:
A pointer to the best individual.

Definition at line 229 of file population.c.

FGEN_API FgenIndividual* fgen_worst_individual_of_population ( FgenPopulation *  pop)

Returns the worst individual of the population (the one with the lowest fitness).

Parameters:
popThe population.
Returns:
A pointer to the worst individual.

Definition at line 249 of file population.c.

FGEN_API FgenIndividual* fgen_best_individual_of_archipelago ( int  nu_pops,
FgenPopulation **  pops 
)

Returns the best individual of an archipelago of populations (the individual with the highest fitness). This function generally only makes sense when the fitness function is the same between the different individual populations that make up the archipelago.

Parameters:
nu_popsThe number of islands in the archipelago.
popsThe archipelago (an array of populations).
Returns:
A pointer to the best individual.

Definition at line 272 of file population.c.

FGEN_API void fgen_update_population_fitness ( FgenPopulation *  pop)

Update the fitness of every individual in the population.

Definition at line 292 of file population.c.

FGEN_API void fgen_invalidate_population_fitness ( FgenPopulation *  pop)

Invalidate the fitness of every individual so that it will be recalculated (because the fitness function has changed).

Definition at line 301 of file population.c.

FGEN_API void fgen_signal_stop ( FgenPopulation *  pop)

Signal a stop to the algorithm from the generation callback function. After the callback functions returns, the algorithm will terminate.

Parameters:
popThe population.

Definition at line 287 of file ga.c.

FGEN_API int fgen_individual_size_in_bytes ( const FgenPopulation *  pop)

Returns the size of the bitstring (genetic data) of an individual in bytes.

Definition at line 313 of file population.c.

FGEN_API int fgen_get_island ( const FgenPopulation *  pop)

Return the current island, to be called from the generation callback function.

Returns:
The current island.

Definition at line 697 of file ga.c.

FGEN_API int fgen_is_cached ( const FgenPopulation *  pop)

Indicates whether the given population is cached.

Returns:
1 if cached, 0 otherwise.

Definition at line 707 of file ga.c.

FGEN_API double fgen_get_cache_hit_rate ( const FgenPopulation *  pop)

Get the cache hit-rate.

Definition at line 102 of file cache.c.

FGEN_API FgenRNG* fgen_get_rng ( const FgenPopulation *  pop)

Returns the random number generator belonging to the population.

Definition at line 715 of file ga.c.

FGEN_API int fgen_get_generation ( const FgenPopulation *  pop)

Returns the current generation. May be useful in an operator.

Definition at line 723 of file ga.c.

FGEN_API double fgen_bitstring_uint16_to_double ( const unsigned char *  bitstring,
double  domain_min,
double  domain_max 
)

Scale the 16-bit integer value stored in bitstring to a double between the given bounds.

Definition at line 35 of file decode.c.

FGEN_API double fgen_bitstring_uint32_to_double ( const unsigned char *  bitstring,
double  domain_min,
double  domain_max 
)

Scale the 32-bit integer value stored in bitstring to a double between the given bounds.

Definition at line 50 of file decode.c.

FGEN_API double fgen_bitstring_uint64_to_double ( const unsigned char *  bitstring,
double  domain_min,
double  domain_max 
)

Scale the 64-bit integer value stored in bitstring to a double between the given bounds.

Definition at line 72 of file decode.c.

FGEN_API void fgen_decode_from_gray ( const FgenPopulation *  pop,
const unsigned char *  src_bitstring,
unsigned char *  dest_bitstring 
)

Decode the source bitstring from Gray-code and store it in the destination bitstring. Each gray coded value corresponds to a data element (data element boundaries as set by fgen_create() are respected).

Definition at line 194 of file gray.c.

FGEN_API void fgen_encode_to_gray ( const FgenPopulation *  pop,
const unsigned char *  src_bitstring,
unsigned char *  dest_bitstring 
)

Encode the source bitstring to Gray-code and store it in the destination bitstring. Each gray coded value corresponds to a data element (data element boundaries as set by fgen_create() are respected).

Definition at line 227 of file gray.c.

FGEN_API void fgen_mutate_bit ( unsigned char *  bitstring,
int  bitnumber 
)

Mutate (toggle) the bit at offset bitnumber in bitstring. bitnumber must be >= 0 but may be greater than 7.

Definition at line 89 of file bitstring.c.

FGEN_API void fgen_set_random_bitstring ( FgenRNG rng,
unsigned char *  bitstring,
int  offset,
int  nu_bits 
)

Set a number of bits at position offset in bitstring each to a random value (1 or 0). offset must be >= 0 but may be greater than 7. For good performance offset and nu_bits are preferably a multiple of 8.

Definition at line 70 of file bitstring.c.

FGEN_API int fgen_get_bit ( const unsigned char *  bitstring,
int  bitnumber 
)

Returns the value (1 or 0) of the bit at offset bitnumber in bitstring. bitnumber must be >= 0.

Definition at line 97 of file bitstring.c.

FGEN_API void fgen_copy_partial_bitstring ( const unsigned char *  src,
unsigned char *  dest,
int  bitoffset,
int  number_of_bits 
)

Copy the part of the source bitstring at offset bitoffset of length number_of_bits to the same position in the destination bitstring.

Definition at line 107 of file bitstring.c.

Create a random number generator data structure and returns it. The RNG is initialized with a seed of 0. Although normally called by fgen_create(), it is possible to use this function to create a random number generator not tied to any population and use it on its own.

Definition at line 87 of file random.c.

FGEN_API void fgen_random_destroy_rng ( FgenRNG rng)

Destroy the data structure associated with an RNG.

Definition at line 102 of file random.c.

FGEN_API void fgen_random_seed_rng ( FgenRNG rng,
unsigned int  seed 
)

Seed the random number generator with an unsigned integer from 0 to 2^32 - 1.

Definition at line 110 of file random.c.

FGEN_API void fgen_random_seed_with_timer ( FgenRNG rng)

Randomize the seed of the random number generator with a value from the system timer.

Definition at line 395 of file random.c.

FGEN_API int fgen_random_2 ( FgenRNG rng)

Return a random integer value of 0 or 1.

Definition at line 141 of file random.c.

FGEN_API int fgen_random_8 ( FgenRNG rng)

Return a random integer value from 0 to 255.

Definition at line 160 of file random.c.

FGEN_API int fgen_random_16 ( FgenRNG rng)

Return a random integer value from 0 to 65535.

Definition at line 178 of file random.c.

FGEN_API unsigned int fgen_random_32 ( FgenRNG rng)

Return a random integer value from 0 to 2^32 - 1;

Definition at line 123 of file random.c.

FGEN_API int fgen_random_n ( FgenRNG rng,
int  n 
)

Return an integer from 0 to n - 1.

Definition at line 441 of file random.c.

FGEN_API float fgen_random_f ( FgenRNG rng,
float  range 
)

Return a random float from 0 to range (exclusive).

Definition at line 479 of file random.c.

FGEN_API double fgen_random_d ( FgenRNG rng,
double  range 
)

Return a random double from 0 to range (exclusive).

Definition at line 471 of file random.c.

FGEN_API double fgen_random_from_range_d ( FgenRNG rng,
double  min_bound,
double  max_bound 
)

Return a random double from min_bound to max_bound (exclusive).

Definition at line 487 of file random.c.

 All Data Structures Functions Variables