XCSF  1.4.7
XCSF learning classifier system
gp.h File Reference

An implementation of GP trees based upon TinyGP. More...

#include "xcsf.h"
Include dependency graph for gp.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ArgsGPTree
 Parameters for initialising GP trees. More...
 
struct  GPTree
 GP tree data structure. More...
 

Functions

void tree_free (const struct GPTree *gp)
 Frees a GP tree. More...
 
void tree_rand (struct GPTree *gp, const struct ArgsGPTree *args)
 Creates a random GP tree. More...
 
void tree_copy (struct GPTree *dest, const struct GPTree *src)
 Copies a GP tree. More...
 
void tree_print (const struct GPTree *gp, const struct ArgsGPTree *args)
 Prints a GP tree. More...
 
char * tree_json_export (const struct GPTree *gp, const struct ArgsGPTree *args)
 Returns a json formatted string representation of a GP tree. More...
 
void tree_json_import (struct GPTree *gp, const struct ArgsGPTree *args, const cJSON *json)
 Creates a GP tree from a cJSON object. More...
 
double tree_eval (struct GPTree *gp, const struct ArgsGPTree *args, const double *x)
 Evaluates a GP tree. More...
 
void tree_crossover (struct GPTree *p1, struct GPTree *p2)
 Performs sub-tree crossover. More...
 
bool tree_mutate (struct GPTree *gp, const struct ArgsGPTree *args)
 Performs point mutation on a GP tree. More...
 
size_t tree_save (const struct GPTree *gp, FILE *fp)
 Writes the GP tree to a file. More...
 
size_t tree_load (struct GPTree *gp, FILE *fp)
 Reads a GP tree from a file. More...
 
void tree_args_init (struct ArgsGPTree *args)
 Sets tree GP parameters to default values. More...
 
void tree_args_free (struct ArgsGPTree *args)
 Frees memory used by GP tree parameters. More...
 
char * tree_args_json_import (struct ArgsGPTree *args, cJSON *json)
 Sets the GP tree parameters from a cJSON object. More...
 
char * tree_args_json_export (const struct ArgsGPTree *args)
 Returns a json formatted string of the GP tree parameters. More...
 
size_t tree_args_save (const struct ArgsGPTree *args, FILE *fp)
 Saves Tree GP parameters. More...
 
size_t tree_args_load (struct ArgsGPTree *args, FILE *fp)
 Loads Tree GP parameters. More...
 
void tree_args_init_constants (struct ArgsGPTree *args)
 Builds global constants used by GP trees. More...
 
void tree_param_set_max (struct ArgsGPTree *args, const double a)
 
void tree_param_set_min (struct ArgsGPTree *args, const double a)
 
void tree_param_set_n_inputs (struct ArgsGPTree *args, const int a)
 
void tree_param_set_n_constants (struct ArgsGPTree *args, const int a)
 
void tree_param_set_init_depth (struct ArgsGPTree *args, const int a)
 
void tree_param_set_max_len (struct ArgsGPTree *args, const int a)
 

Detailed Description

An implementation of GP trees based upon TinyGP.

Author
Richard Preen rpree.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om
Date
2016–2022.

Definition in file gp.h.

Function Documentation

◆ tree_args_free()

void tree_args_free ( struct ArgsGPTree args)

Frees memory used by GP tree parameters.

Parameters
[in]argsParameters for initialising and operating GP trees.

Definition at line 438 of file gp.c.

References ArgsGPTree::constants.

Referenced by cond_param_free().

Here is the caller graph for this function:

◆ tree_args_init()

void tree_args_init ( struct ArgsGPTree args)

Sets tree GP parameters to default values.

Parameters
[in]argsParameters for initialising and operating GP trees.

Definition at line 422 of file gp.c.

References ArgsGPTree::constants, ArgsGPTree::init_depth, ArgsGPTree::max, ArgsGPTree::max_len, ArgsGPTree::min, ArgsGPTree::n_constants, and ArgsGPTree::n_inputs.

Referenced by cond_gp_param_defaults().

Here is the caller graph for this function:

◆ tree_args_init_constants()

void tree_args_init_constants ( struct ArgsGPTree args)

Builds global constants used by GP trees.

Parameters
[in]argsParameters for initialising and operating GP trees.

Definition at line 539 of file gp.c.

References ArgsGPTree::constants, ArgsGPTree::max, ArgsGPTree::min, ArgsGPTree::n_constants, and rand_uniform().

Referenced by cond_gp_param_defaults(), and cond_gp_param_json_import().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tree_args_json_export()

char* tree_args_json_export ( const struct ArgsGPTree args)

Returns a json formatted string of the GP tree parameters.

Parameters
[in]argsParameters for initialising and operating GP trees.
Returns
String encoded in json format.

Definition at line 449 of file gp.c.

References ArgsGPTree::init_depth, ArgsGPTree::max, ArgsGPTree::max_len, ArgsGPTree::min, and ArgsGPTree::n_constants.

Referenced by cond_gp_param_json_export().

Here is the caller graph for this function:

◆ tree_args_json_import()

char* tree_args_json_import ( struct ArgsGPTree args,
cJSON *  json 
)

Sets the GP tree parameters from a cJSON object.

Parameters
[in,out]argsGP tree parameter data structure.
[in]jsoncJSON object.
Returns
NULL if successful; or the name of parameter if not found.

Definition at line 469 of file gp.c.

References tree_param_set_init_depth(), tree_param_set_max(), tree_param_set_max_len(), tree_param_set_min(), and tree_param_set_n_constants().

Referenced by cond_gp_param_json_import().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tree_args_load()

size_t tree_args_load ( struct ArgsGPTree args,
FILE *  fp 
)

Loads Tree GP parameters.

Parameters
[in]argsParameters for initialising and operating GP trees.
[in]fpPointer to the output file.
Returns
The total number of elements read.

Definition at line 521 of file gp.c.

References ArgsGPTree::constants, ArgsGPTree::init_depth, ArgsGPTree::max, ArgsGPTree::max_len, ArgsGPTree::min, ArgsGPTree::n_constants, and ArgsGPTree::n_inputs.

Referenced by cond_param_load().

Here is the caller graph for this function:

◆ tree_args_save()

size_t tree_args_save ( const struct ArgsGPTree args,
FILE *  fp 
)

Saves Tree GP parameters.

Parameters
[in]argsParameters for initialising and operating GP trees.
[in]fpPointer to the output file.
Returns
The total number of elements written.

Definition at line 501 of file gp.c.

References ArgsGPTree::constants, ArgsGPTree::init_depth, ArgsGPTree::max, ArgsGPTree::max_len, ArgsGPTree::min, ArgsGPTree::n_constants, and ArgsGPTree::n_inputs.

Referenced by cond_param_save().

Here is the caller graph for this function:

◆ tree_copy()

void tree_copy ( struct GPTree dest,
const struct GPTree src 
)

Copies a GP tree.

Parameters
[in]destThe destination GP tree.
[in]srcThe source GP tree.

Definition at line 302 of file gp.c.

References GPTree::len, GPTree::mu, N_MU, GPTree::pos, and GPTree::tree.

Referenced by cond_gp_copy().

Here is the caller graph for this function:

◆ tree_crossover()

void tree_crossover ( struct GPTree p1,
struct GPTree p2 
)

Performs sub-tree crossover.

Parameters
[in]p1The first GP tree to perform crossover.
[in]p2The second GP tree to perform crossover.

Definition at line 318 of file gp.c.

References GPTree::len, rand_uniform_int(), GPTree::tree, and tree_traverse().

Referenced by cond_gp_crossover().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tree_eval()

double tree_eval ( struct GPTree gp,
const struct ArgsGPTree args,
const double *  x 
)

Evaluates a GP tree.

Parameters
[in]gpThe GP tree to evaluate.
[in]argsTree GP parameters.
[in]xThe input state.
Returns
The result from evaluating the GP tree.

Definition at line 133 of file gp.c.

References ADD, clamp(), ArgsGPTree::constants, DIV, GP_NUM_FUNC, MUL, ArgsGPTree::n_constants, GPTree::pos, RET_MAX, RET_MIN, SUB, and GPTree::tree.

Referenced by cond_gp_match().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tree_free()

void tree_free ( const struct GPTree gp)

Frees a GP tree.

Parameters
[in]gpThe GP tree to free.

Definition at line 119 of file gp.c.

References GPTree::mu, and GPTree::tree.

Referenced by cond_gp_cover(), and cond_gp_free().

Here is the caller graph for this function:

◆ tree_json_export()

char* tree_json_export ( const struct GPTree gp,
const struct ArgsGPTree args 
)

Returns a json formatted string representation of a GP tree.

Parameters
[in]gpThe GP tree to return.
[in]argsTree GP parameters.
Returns
String encoded in json format.

Definition at line 253 of file gp.c.

References GPTree::mu, N_MU, and tree_string().

Referenced by cond_gp_json_export(), and tree_print().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tree_json_import()

void tree_json_import ( struct GPTree gp,
const struct ArgsGPTree args,
const cJSON *  json 
)

Creates a GP tree from a cJSON object.

Parameters
[in,out]gpThe GP tree to initialise.
[in]argsParameters for initialising a GP tree.
[in]jsoncJSON object.

Definition at line 273 of file gp.c.

Referenced by cond_gp_json_import().

Here is the caller graph for this function:

◆ tree_load()

size_t tree_load ( struct GPTree gp,
FILE *  fp 
)

Reads a GP tree from a file.

Parameters
[in]gpThe GP tree to load.
[in]fpPointer to the file to be read.
Returns
The number of elements read.

Definition at line 400 of file gp.c.

References GPTree::len, GPTree::mu, N_MU, GPTree::pos, and GPTree::tree.

Referenced by cond_gp_load().

Here is the caller graph for this function:

◆ tree_mutate()

bool tree_mutate ( struct GPTree gp,
const struct ArgsGPTree args 
)

Performs point mutation on a GP tree.

Terminals are randomly replaced with other terminals and functions are randomly replaced with other functions.

Parameters
[in]gpThe GP tree to be mutated.
[in]argsTree GP parameters.
Returns
Whether any alterations were made.

Definition at line 355 of file gp.c.

References GP_NUM_FUNC, GPTree::len, GPTree::mu, MU_TYPE, ArgsGPTree::n_constants, ArgsGPTree::n_inputs, N_MU, rand_uniform(), rand_uniform_int(), sam_adapt(), and GPTree::tree.

Referenced by cond_gp_mutate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tree_param_set_init_depth()

void tree_param_set_init_depth ( struct ArgsGPTree args,
const int  a 
)

Definition at line 587 of file gp.c.

References ArgsGPTree::init_depth.

Referenced by cond_gp_param_defaults(), and tree_args_json_import().

Here is the caller graph for this function:

◆ tree_param_set_max()

void tree_param_set_max ( struct ArgsGPTree args,
const double  a 
)

Definition at line 553 of file gp.c.

References ArgsGPTree::max.

Referenced by cond_gp_param_defaults(), and tree_args_json_import().

Here is the caller graph for this function:

◆ tree_param_set_max_len()

void tree_param_set_max_len ( struct ArgsGPTree args,
const int  a 
)

Definition at line 598 of file gp.c.

References ArgsGPTree::max_len.

Referenced by cond_gp_param_defaults(), and tree_args_json_import().

Here is the caller graph for this function:

◆ tree_param_set_min()

void tree_param_set_min ( struct ArgsGPTree args,
const double  a 
)

Definition at line 559 of file gp.c.

References ArgsGPTree::min.

Referenced by cond_gp_param_defaults(), and tree_args_json_import().

Here is the caller graph for this function:

◆ tree_param_set_n_constants()

void tree_param_set_n_constants ( struct ArgsGPTree args,
const int  a 
)

Definition at line 576 of file gp.c.

References ArgsGPTree::n_constants.

Referenced by cond_gp_param_defaults(), and tree_args_json_import().

Here is the caller graph for this function:

◆ tree_param_set_n_inputs()

void tree_param_set_n_inputs ( struct ArgsGPTree args,
const int  a 
)

Definition at line 565 of file gp.c.

References ArgsGPTree::n_inputs.

Referenced by cond_gp_param_defaults().

Here is the caller graph for this function:

◆ tree_print()

void tree_print ( const struct GPTree gp,
const struct ArgsGPTree args 
)

Prints a GP tree.

Parameters
[in]gpThe GP tree to print.
[in]argsTree GP parameters.

Definition at line 289 of file gp.c.

References tree_json_export().

Here is the call graph for this function:

◆ tree_rand()

void tree_rand ( struct GPTree gp,
const struct ArgsGPTree args 
)

Creates a random GP tree.

Parameters
[in]gpThe GP tree being randomised.
[in]argsTree GP parameters.

Definition at line 102 of file gp.c.

References ArgsGPTree::init_depth, GPTree::len, ArgsGPTree::max_len, GPTree::mu, MU_TYPE, N_MU, sam_init(), GPTree::tree, and tree_grow().

Referenced by cond_gp_cover(), and cond_gp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tree_save()

size_t tree_save ( const struct GPTree gp,
FILE *  fp 
)

Writes the GP tree to a file.

Parameters
[in]gpThe GP tree to save.
[in]fpPointer to the file to be written.
Returns
The number of elements written.

Definition at line 383 of file gp.c.

References GPTree::len, GPTree::mu, N_MU, GPTree::pos, and GPTree::tree.

Referenced by cond_gp_save().

Here is the caller graph for this function: