libfgen  0.1.15
Library for optimization using a genetic algorithm or particle swarm optimization
Data Structures | Defines | Typedefs | Functions
ffit (Model fitting / function minimization) API

Data Structures

struct  Ffit

Defines

#define FFIT_MAPPING_LINEAR   0
#define FFIT_MAPPING_SQUARE   1
#define FFIT_MAPPING_CUBE   2
#define FFIT_MAPPING_LOG   3
#define FFIT_MAPPING_BINOMIAL_1_TO_5   4
#define FFIT_MAPPING_BINOMIAL_1_TO_7   5
#define FFIT_OPTIMIZATION_FGEN_ELEMENT   1
#define FFIT_OPTIMIZATION_ARCHIPELAGO_ELEMENT   2
#define FFIT_OPTIMIZATION_HILL_CLIMB_ELEMENT   4
#define FFIT_OPTIMIZATION_FPSO_ELEMENT   8
#define FFIT_OPTIMIZATION_FGEN_REAL_VALUED_ELEMENT   16
#define FFIT_OPTIMIZATION_FGEN   FFIT_OPTIMIZATION_FGEN_ELEMENT
#define FFIT_OPTIMIZATION_FGEN_ARCHIPELAGO   (FFIT_OPTIMIZATION_FGEN_ELEMENT | FFIT_OPTIMIZATION_ARCHIPELAGO_ELEMENT)
#define FFIT_OPTIMIZATION_FGEN_WITH_HILL_CLIMB   (FFIT_OPTIMIZATION_FGEN_ELEMENT | FFIT_OPTIMIZATION_HILL_CLIMB_ELEMENT)
#define FFIT_OPTIMIZATION_FGEN_ARCHIPELAGO_WITH_HILL_CLIMB
#define FFIT_OPTIMIZATION_FPSO   FFIT_OPTIMIZATION_FPSO_ELEMENT
#define FFIT_OPTIMIZATION_FGEN_REAL_VALUED
#define FFIT_OPTIMIZATION_FGEN_REAL_VALUED_ARCHIPELAGO
#define FFIT_THREADING_DISABLED   0
#define FFIT_THREADING_ENABLED   1

Typedefs

typedef double(* FfitCalculateErrorFunc )(const Ffit *fit, const double *param)
typedef void(* FfitGenerationCallbackFunc )(Ffit *fit, int generation, const double *best_param, double best_error)

Functions

FGEN_API Ffit * ffit_create (int nu_params, FfitGenerationCallbackFunc ffit_generation_callback_func, FfitCalculateErrorFunc ffit_calculate_error_func)
FGEN_API void ffit_set_parameter_range_and_mapping (Ffit *fit, int index, double range_min, double range_max, int mapping)
FGEN_API void ffit_run_fgen_with_defaults (Ffit *fit)
FGEN_API void ffit_run_fgen (Ffit *fit, int population_size, int nu_bits_per_param, int selection_type, FgenCrossoverFunc crossover, float crossover_probability_float, float mutation_per_bit_probability_float, float macro_mutation_probability_float)
FGEN_API void ffit_run_fgen_archipelago (Ffit *fit, int nu_islands, int population_size, int nu_bits_per_param, int selection_type, FgenCrossoverFunc crossover, float crossover_probability_float, float mutation_probability_float, float macro_mutation_probability_float, float migration_probability_float, int migration_interval)
FGEN_API void ffit_run_fgen_real_valued (Ffit *fit, int population_size, int selection_type, float crossover_probability_float, float mutation_per_bit_probability_float, float macro_mutation_probability_float)
FGEN_API void ffit_run_fgen_real_valued_archipelago (Ffit *fit, int nu_islands, int population_size, int selection_type, float crossover_probability_float, float mutation_probability_float, float macro_mutation_probability_float, float migration_probability_float, int migration_interval)
FGEN_API void ffit_run_fpso (Ffit *fit, int population_size, int topology, int bounding_strategy, double omega, double phi1, double phi2)
FGEN_API void ffit_signal_stop (Ffit *fit)
FGEN_API void ffit_signal_model_change (Ffit *fit)
FGEN_API void ffit_destroy (Ffit *fit)
FGEN_API int ffit_get_population_size (const Ffit *fit)
FGEN_API void ffit_get_individual_params (const Ffit *fit, int index, double *params)
FGEN_API void * ffit_get_population (const Ffit *fit)
FGEN_API void ffit_set_threading (Ffit *fit, int threading_level)
FGEN_API void ffit_set_generation_callback_interval (Ffit *fit, int interval)

Define Documentation

#define FFIT_OPTIMIZATION_FGEN_ARCHIPELAGO_WITH_HILL_CLIMB
Value:
(FFIT_OPTIMIZATION_FGEN_ELEMENT | \
    FFIT_OPTIMIZATION_ARCHIPELAGO_ELEMENT | FFIT_OPTIMIZATION_HILL_CLIMB_ELEMENT)

Definition at line 499 of file fgen.h.

#define FFIT_OPTIMIZATION_FGEN_REAL_VALUED
Value:
(FFIT_OPTIMIZATION_FGEN_ELEMENT | \
    FFIT_OPTIMIZATION_FGEN_REAL_VALUED_ELEMENT)

Definition at line 502 of file fgen.h.

#define FFIT_OPTIMIZATION_FGEN_REAL_VALUED_ARCHIPELAGO
Value:
(FFIT_OPTIMIZATION_FGEN_ELEMENT | \
FFIT_OPTIMIZATION_FGEN_REAL_VALUED_ELEMENT | FFIT_OPTIMIZATION_ARCHIPELAGO_ELEMENT)

Definition at line 504 of file fgen.h.


Typedef Documentation

typedef double(* FfitCalculateErrorFunc)(const Ffit *fit, const double *param)

The error calculation function evaluation function. The fit and the parameters to be evaluated are passed as arguments. See ffit_create().

Definition at line 456 of file fgen.h.

typedef void(* FfitGenerationCallbackFunc)(Ffit *fit, int generation, const double *best_param, double best_error)

The generation callback function that is called every n generations as defined by the generation callback interval. The parameters that generated the best (lowest) result in the calculate error function are passed as arguments, as is the associated error value.

Definition at line 460 of file fgen.h.


Function Documentation

FGEN_API Ffit* ffit_create ( int  nu_params,
FfitGenerationCallbackFunc  generation_callback_func,
FfitCalculateErrorFunc  calculate_error_func 
)

Create a new fit datastructure.

Parameters:
nu_paramsThe number of parameters in the solution.
generation_callback_funcThe generation callback function that is called after every n generations in the optimization algorithm, where n is the generation callback interval (see ffit_set_generation_callback_interval()).
calculate_error_funcThe error evaluation function that returns the error value of the provided parameter solution.
Returns:
The created fit.

Definition at line 65 of file ffit.c.

FGEN_API void ffit_set_parameter_range_and_mapping ( Ffit *  fit,
int  index,
double  range_min,
double  range_max,
int  mapping 
)

Set the range and mapping of a parameter. In general this function must be called, after ffit_create(), for each parameter. Counting starts with zero, so for example, if nu_params in ffit_create was 3, this function should be called three times with index equal to 0, 1 and 2 respectively.

Parameters:
fitThe fit to which the settings apply.
indexThe index (starting with 0) of the parameter.
range_minThe lower bound of the range of the parameter.
range_maxThe upper bound (exclusive) of the range of the parameter.
mappingThe mathematical mapping of the parameter range based on the behaviour of standard functions in the range [0, 1[. For example, use FFIT_MAPPING_LINEAR for a standard linear mapping. This affects the efficiency of the search algorithm only, and does not affect the range of parameters or the way parameters are presented to the callback functions.

Definition at line 94 of file ffit.c.

FGEN_API void ffit_run_fgen_with_defaults ( Ffit *  fit)

Run a fit using a genetic algorithm with default settings. Note that this function reseeds the random number generator using the system timer so that every run is different. Default settings are:

  • Population size 1024.
  • 32 bits per parameter.
  • Elitist universal stochastic sampling
  • Uniform crossover per element.
  • Crossover probability 0.9.
  • Mutation probability per bit 0.015.
  • Macro-mutation probability 0.050 per data element.
Parameters:
fitThe fit to run. Must have been created previously using ffit_create().

Definition at line 114 of file ffit.c.

FGEN_API void ffit_run_fgen ( Ffit *  fit,
int  population_size,
int  nu_bits_per_param,
int  selection_type,
FgenCrossoverFunc  crossover,
float  crossover_probability_float,
float  mutation_per_bit_probability_float,
float  macro_mutation_probability_float 
)

Run a fit using a genetic algorithm with specified settings. Note that this function reseeds the random number generator using the system timer so that every run is different. If ffit_set_threading() was called with FFIT_THREADING_ENABLED as argument prior to calling this function, the fit will be run with threaded error (fitness) calculations in each generation.

Parameters:
fitThe fit to run. Must have been created previously using ffit_create().
population_sizeThe size of GA population.
nu_bits_per_paramThe number of bits used for the parameter representation. Must be 16, 32 or 64.
selection_typeThe fgen selection type (for example, FGEN_ELITIST_SUS).
crossoverThe crossover operator function (for example, fgen_crossover_uniform_per_element).
crossover_probability_floatThe crossover rate.
mutation_per_bit_probability_floatThe mutation rate per bit.
macro_mutation_probability_floatThe macro-mutation probability per data element.

Definition at line 133 of file ffit.c.

FGEN_API void ffit_run_fgen_archipelago ( Ffit *  fit,
int  nu_islands,
int  population_size,
int  nu_bits_per_param,
int  selection_type,
FgenCrossoverFunc  crossover,
float  crossover_probability_float,
float  mutation_probability_float,
float  macro_mutation_probability_float,
float  migration_probability_float,
int  migration_interval 
)

Run a fit using an archipelago of genetic algorithms with migration. Note that this function reseeds the random number generator using the system timer so that every run is different. The populations are automatically created and the fitting process is started. If ffit_set_threading() was called with FFIT_THREADING_ENABLED as argument prior to calling this function, the fit will be run threaded.

Parameters:
fitThe fit to run.
nu_islandsThe number of islands in the archipelago.
population_sizeThe size of each GA population.
nu_bits_per_paramThe number of bits used for the parameter representation. Must be 16, 32 or 64.
selection_typeThe fgen selection type.
crossoverThe crossover operator function.
crossover_probability_floatThe crossover rate.
mutation_probability_floatThe mutation rate per bit.
macro_mutation_probability_floatThe macro-mutation probability per data element.
migration_probability_floatThe migration probability per individual.
migration_intervalThe migration interval. Must be greater than 0. A value of 1 means migrations happens every generation, a value of 50 means migrations only happens every 50th generation.

Definition at line 270 of file ffit.c.

FGEN_API void ffit_run_fgen_real_valued ( Ffit *  fit,
int  population_size,
int  selection_type,
float  crossover_probability_float,
float  mutation_per_element_probability_float,
float  macro_mutation_probability_float 
)

Run a fit using a genetic algorithm with real-valued chromosomes with specified settings. Note that ffit_run_fgen works very well with real valued parameters, despite using bitstring representation internally. This function uses real-valued parameters (double) internally in the genetic algorithm. Note that this function reseeds the random number generator using the system timer so that every run is different.

Parameters:
fitThe fit to run.
population_sizeThe size of GA population.
selection_typeThe fgen selection type.
crossover_probability_floatThe crossover rate.
mutation_per_bit_probability_floatThe mutation probability per data element.
macro_mutation_probability_floatThe macro-mutation probability per data element.

Definition at line 181 of file ffit.c.

FGEN_API void ffit_run_fgen_real_valued_archipelago ( Ffit *  fit,
int  nu_islands,
int  population_size,
int  selection_type,
float  crossover_probability_float,
float  mutation_probability_float,
float  macro_mutation_probability_float,
float  migration_probability_float,
int  migration_interval 
)

Run a fit using an archipelago of genetic algorithms with real-valued chromosomes internally, with migration. Note that this function reseeds the random number generator using the system timer so that every run is different.

Parameters:
fitThe fit to run.
nu_islandsThe number of islands in the archipelago.
population_sizeThe size of each GA population.
selection_typeThe fgen selection type.
crossover_probability_floatThe crossover probability.
mutation_probability_floatThe mutation probability per data element.
macro_mutation_probability_floatThe macro-mutation probability per data element.
migration_probability_floatThe migration probability per individual.
migration_intervalThe migration interval. Must be greater than 0. A value of 1 means migrations happens every generation, a value of 50 means migrations only happens every 50th generation.

Definition at line 328 of file ffit.c.

FGEN_API void ffit_run_fpso ( Ffit *  fit,
int  population_size,
int  topology,
int  bounding_strategy,
double  omega,
double  phi1,
double  phi2 
)

Run a fit using particle swarm optimization. Note that this function reseeds the random number generator using the system timer so that every run is different.

Parameters:
fitThe fit to run.
population_sizeThe size of the swarm population.
topologyThe fpso topology type, one of FPSO_TOPOLOGY_LBEST and FPSO_TOPOLOGY_GBEST.
bounding_strategyThe fpso bounding strategy. One of the following:
  • FPSO_BOUND_NOTHING
  • FPSO_BOUND_POSITION
  • FPSO_BOUND_VELOCITY
  • FPSO_BOUND_POSITION_AND_VELOCITY
omegaThe omega value used in the PSO. A default value is defined as FPSO_DEFAULT_OMEGA.
phi1The phi1 value used in the PSO. A default value is defined as FPSO_DEFAULT_PHI1.
phi2the phi2 value used in the PSO. A default value is defined as FPSO_DEFAULT_PHI2.

Definition at line 385 of file ffit.c.

FGEN_API void ffit_signal_stop ( Ffit *  fit)

Signal stop to the fitting algorithm. When called from the generation callback function, the algorithm will stop after the callback function returns.

Parameters:
fitThe fit.

Definition at line 541 of file ffit.c.

FGEN_API void ffit_signal_model_change ( Ffit *  fit)

Signal that the calculate error function has changed, so that fitness values in the genetic algorithm are properly invalidated and recalculated.

Definition at line 550 of file ffit.c.

FGEN_API void ffit_destroy ( Ffit *  fit)

Free all data structures associated with a fit. This includes the population (or populations in case of an archipelago), and all data structured associated with the Ffit data structure.

Parameters:
fitThe fit to destroy.

Definition at line 420 of file ffit.c.

FGEN_API int ffit_get_population_size ( const Ffit *  fit)

Return the size of the population used in a fit. Only valid after a fit has been started. If an fgen archipelago is used, this returns to total number of individuals across all islands.

Parameters:
fitThe fit.

Definition at line 446 of file ffit.c.

FGEN_API void ffit_get_individual_params ( const Ffit *  fit,
int  index,
double *  params 
)

Get the parameters of an individual from the optimization algorithm population. This can be used to enumerate the solutions present in the population.

Parameters:
fitThe fit.
indexThe index (starting at 0) of the individual in the population. The allowed range for this argument is defined by ffit_get_population_size().
paramsThe location where the fetched parameter array of the individual is stored.

Definition at line 475 of file ffit.c.

FGEN_API void* ffit_get_population ( const Ffit *  fit)

Return a pointer to the optimization algorithm population used in a fit.

Parameters:
fitThe fit.
Returns:
A pointer to the optimization algorithm population of the following type:
  • FgenPopulation * if a genetic algorithm is running.
  • FgenPopulation ** if an archipelago of genetic algorithms is running.
  • FpsoPopulation * if a particle swarm optimization is running.

Definition at line 462 of file ffit.c.

FGEN_API void ffit_set_threading ( Ffit *  fit,
int  level 
)

Set the threading level to either FFIT_THREADING_DISABLED or FFIT_THREADING_ENABLED. Threading allows threaded executing of the genetic algorithm, which is useful with archipelagos or expensive fitness functions with large populations.

Definition at line 561 of file ffit.c.

FGEN_API void ffit_set_generation_callback_interval ( Ffit *  fit,
int  interval 
)

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 when using a genetic algorithm archipelago.

Definition at line 571 of file ffit.c.

 All Data Structures Functions Variables