XCSF  1.4.7
XCSF learning classifier system
dgp.c File Reference

An implementation of dynamical GP graphs with fuzzy activations. More...

#include "dgp.h"
#include "sam.h"
#include "utils.h"
Include dependency graph for dgp.c:

Go to the source code of this file.

Macros

#define FUZZY_NOT   (0)
 Fuzzy NOT function. More...
 
#define FUZZY_CFMQVS_AND   (1)
 Fuzzy AND (CFMQVS) function. More...
 
#define FUZZY_CFMQVS_OR   (2)
 Fuzzy OR (CFMQVS) function. More...
 
#define NUM_FUNC   (3)
 Number of selectable node functions. More...
 
#define N_MU   (3)
 Number of DGP graph mutation rates. More...
 
#define STRING_FUZZY_NOT   ("Fuzzy NOT\0")
 Fuzzy NOT. More...
 
#define STRING_FUZZY_CFMQVS_AND   ("Fuzzy AND\0")
 Fuzzy AND. More...
 
#define STRING_FUZZY_CFMQVS_OR   ("Fuzzy OR\0")
 Fuzzy OR. More...
 

Functions

static int random_connection (const int n_nodes, const int n_inputs)
 Returns a random connection. More...
 
static bool graph_mutate_functions (struct Graph *dgp)
 Mutates the node functions within a DGP graph. More...
 
static bool graph_mutate_connectivity (struct Graph *dgp)
 Mutates the connectivity of a DGP graph. More...
 
static bool graph_mutate_cycles (struct Graph *dgp)
 Mutates the number of update cycles performed by a DGP graph. More...
 
static double node_activate (int function, const double *inputs, const int K)
 Returns the result from applying a specified activation function. More...
 
static const char * function_string (const int function)
 Returns the name of a specified node function. More...
 
static int function_int (const char *function)
 Returns the integer value of a specified node function. More...
 
static void synchronous_update (const struct Graph *dgp, const double *inputs)
 Performs a synchronous update. More...
 
void graph_init (struct Graph *dgp, const struct ArgsDGP *args)
 Initialises a new DGP graph. More...
 
void graph_copy (struct Graph *dest, const struct Graph *src)
 Copies a DGP graph. More...
 
double graph_output (const struct Graph *dgp, const int IDX)
 Returns the current state of a specified node in the graph. More...
 
void graph_reset (const struct Graph *dgp)
 Resets the states to their initial state. More...
 
void graph_rand (struct Graph *dgp)
 Randomises a specified DGP graph. More...
 
void graph_update (const struct Graph *dgp, const double *inputs, const bool reset)
 Updates a DGP graph T cycles. More...
 
void graph_print (const struct Graph *dgp)
 Prints a DGP graph. More...
 
char * graph_json_export (const struct Graph *dgp)
 Returns a json formatted string representation of a DGP graph. More...
 
static void graph_json_import_current_state (struct Graph *dgp, const cJSON *json)
 Sets DGP current states from a cJSON object. More...
 
static void graph_json_import_initial_state (struct Graph *dgp, const cJSON *json)
 Sets DGP initial states from a cJSON object. More...
 
static void graph_json_import_functions (struct Graph *dgp, const cJSON *json)
 Sets DGP functions from a cJSON object. More...
 
static void graph_json_import_connectivity (struct Graph *dgp, const cJSON *json)
 Sets DGP mutation rates from a cJSON object. More...
 
void graph_json_import (struct Graph *dgp, const struct ArgsDGP *args, const cJSON *json)
 Creates a DGP graph from a cJSON object. More...
 
void graph_free (const struct Graph *dgp)
 Frees a DGP graph. More...
 
bool graph_mutate (struct Graph *dgp)
 Mutates a specified DGP graph. More...
 
size_t graph_save (const struct Graph *dgp, FILE *fp)
 Writes DGP graph to a file. More...
 
size_t graph_load (struct Graph *dgp, FILE *fp)
 Reads DGP graph from a file. More...
 
void graph_args_init (struct ArgsDGP *args)
 Sets DGP parameters to default values. More...
 
char * graph_args_json_export (const struct ArgsDGP *args)
 Returns a json formatted string of the DGP parameters. More...
 
char * graph_args_json_import (struct ArgsDGP *args, cJSON *json)
 Sets the DGP graph parameters from a cJSON object. More...
 
size_t graph_args_save (const struct ArgsDGP *args, FILE *fp)
 Saves DGP parameters. More...
 
size_t graph_args_load (struct ArgsDGP *args, FILE *fp)
 Loads DGP parameters. More...
 
void graph_param_set_max_k (struct ArgsDGP *args, const int a)
 
void graph_param_set_max_t (struct ArgsDGP *args, const int a)
 
void graph_param_set_n (struct ArgsDGP *args, const int a)
 
void graph_param_set_n_inputs (struct ArgsDGP *args, const int a)
 
void graph_param_set_evolve_cycles (struct ArgsDGP *args, const bool a)
 

Variables

static const int MU_TYPE [(3)]
 Self-adaptation method for mutating DGP graphs. More...
 

Detailed Description

An implementation of dynamical GP graphs with fuzzy activations.

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

Definition in file dgp.c.

Macro Definition Documentation

◆ FUZZY_CFMQVS_AND

#define FUZZY_CFMQVS_AND   (1)

Fuzzy AND (CFMQVS) function.

Definition at line 29 of file dgp.c.

◆ FUZZY_CFMQVS_OR

#define FUZZY_CFMQVS_OR   (2)

Fuzzy OR (CFMQVS) function.

Definition at line 30 of file dgp.c.

◆ FUZZY_NOT

#define FUZZY_NOT   (0)

Fuzzy NOT function.

Definition at line 28 of file dgp.c.

◆ N_MU

#define N_MU   (3)

Number of DGP graph mutation rates.

Definition at line 32 of file dgp.c.

◆ NUM_FUNC

#define NUM_FUNC   (3)

Number of selectable node functions.

Definition at line 31 of file dgp.c.

◆ STRING_FUZZY_CFMQVS_AND

#define STRING_FUZZY_CFMQVS_AND   ("Fuzzy AND\0")

Fuzzy AND.

Definition at line 35 of file dgp.c.

◆ STRING_FUZZY_CFMQVS_OR

#define STRING_FUZZY_CFMQVS_OR   ("Fuzzy OR\0")

Fuzzy OR.

Definition at line 36 of file dgp.c.

◆ STRING_FUZZY_NOT

#define STRING_FUZZY_NOT   ("Fuzzy NOT\0")

Fuzzy NOT.

Definition at line 34 of file dgp.c.

Function Documentation

◆ function_int()

static int function_int ( const char *  function)
static

Returns the integer value of a specified node function.

Parameters
[in]functionThe node function.
Returns
An integer representing a node function.

Definition at line 184 of file dgp.c.

References FUZZY_CFMQVS_AND, FUZZY_CFMQVS_OR, FUZZY_NOT, STRING_FUZZY_CFMQVS_AND, STRING_FUZZY_CFMQVS_OR, and STRING_FUZZY_NOT.

Referenced by graph_json_import_functions().

Here is the caller graph for this function:

◆ function_string()

static const char* function_string ( const int  function)
static

Returns the name of a specified node function.

Parameters
[in]functionThe node function.
Returns
The name of the node function.

Definition at line 162 of file dgp.c.

References FUZZY_CFMQVS_AND, FUZZY_CFMQVS_OR, FUZZY_NOT, STRING_FUZZY_CFMQVS_AND, STRING_FUZZY_CFMQVS_OR, and STRING_FUZZY_NOT.

Referenced by graph_json_export().

Here is the caller graph for this function:

◆ graph_args_init()

void graph_args_init ( struct ArgsDGP args)

Sets DGP parameters to default values.

Parameters
[in]argsParameters for initialising and operating DGP graphs.

Definition at line 624 of file dgp.c.

References ArgsDGP::evolve_cycles, ArgsDGP::max_k, ArgsDGP::max_t, ArgsDGP::n, and ArgsDGP::n_inputs.

Referenced by cond_dgp_param_defaults().

Here is the caller graph for this function:

◆ graph_args_json_export()

char* graph_args_json_export ( const struct ArgsDGP args)

Returns a json formatted string of the DGP parameters.

Parameters
[in]argsParameters for initialising and operating DGP graphs.
Returns
String encoded in json format.

Definition at line 639 of file dgp.c.

References ArgsDGP::evolve_cycles, ArgsDGP::max_k, ArgsDGP::max_t, and ArgsDGP::n.

Referenced by cond_dgp_param_json_export().

Here is the caller graph for this function:

◆ graph_args_json_import()

char* graph_args_json_import ( struct ArgsDGP args,
cJSON *  json 
)

Sets the DGP graph parameters from a cJSON object.

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

Definition at line 658 of file dgp.c.

References graph_param_set_evolve_cycles(), graph_param_set_max_k(), graph_param_set_max_t(), and graph_param_set_n().

Referenced by cond_dgp_param_json_import().

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

◆ graph_args_load()

size_t graph_args_load ( struct ArgsDGP args,
FILE *  fp 
)

Loads DGP parameters.

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

Definition at line 705 of file dgp.c.

References ArgsDGP::evolve_cycles, ArgsDGP::max_k, ArgsDGP::max_t, ArgsDGP::n, and ArgsDGP::n_inputs.

Referenced by cond_param_load().

Here is the caller graph for this function:

◆ graph_args_save()

size_t graph_args_save ( const struct ArgsDGP args,
FILE *  fp 
)

Saves DGP parameters.

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

Definition at line 687 of file dgp.c.

References ArgsDGP::evolve_cycles, ArgsDGP::max_k, ArgsDGP::max_t, ArgsDGP::n, and ArgsDGP::n_inputs.

Referenced by cond_param_save().

Here is the caller graph for this function:

◆ graph_copy()

void graph_copy ( struct Graph dest,
const struct Graph src 
)

Copies a DGP graph.

Parameters
[in]destThe destination DGP graph.
[in]srcThe source DGP graph.

Definition at line 251 of file dgp.c.

References Graph::connectivity, Graph::evolve_cycles, Graph::function, Graph::initial_state, Graph::klen, Graph::max_k, Graph::max_t, Graph::mu, Graph::n, Graph::n_inputs, N_MU, Graph::state, and Graph::t.

Referenced by cond_dgp_copy(), and rule_dgp_cond_copy().

Here is the caller graph for this function:

◆ graph_free()

void graph_free ( const struct Graph dgp)

Frees a DGP graph.

Parameters
[in]dgpThe DGP graph to be freed.

Definition at line 523 of file dgp.c.

References Graph::connectivity, Graph::function, Graph::initial_state, Graph::mu, Graph::state, Graph::tmp_input, and Graph::tmp_state.

Referenced by cond_dgp_free(), cond_dgp_json_import(), rule_dgp_cond_free(), and rule_dgp_cond_json_import().

Here is the caller graph for this function:

◆ graph_init()

void graph_init ( struct Graph dgp,
const struct ArgsDGP args 
)

Initialises a new DGP graph.

Parameters
[in]dgpThe DGP graph to initialise.
[in]argsParameters for initialising a DGP graph.

Definition at line 226 of file dgp.c.

References Graph::connectivity, ArgsDGP::evolve_cycles, Graph::evolve_cycles, Graph::function, Graph::initial_state, Graph::klen, ArgsDGP::max_k, Graph::max_k, ArgsDGP::max_t, Graph::max_t, Graph::mu, MU_TYPE, ArgsDGP::n, Graph::n, ArgsDGP::n_inputs, Graph::n_inputs, N_MU, sam_init(), Graph::state, Graph::t, Graph::tmp_input, and Graph::tmp_state.

Referenced by cond_dgp_copy(), cond_dgp_init(), rule_dgp_cond_copy(), and rule_dgp_cond_init().

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

◆ graph_json_export()

char* graph_json_export ( const struct Graph dgp)

Returns a json formatted string representation of a DGP graph.

Parameters
[in]dgpThe DGP graph to return.
Returns
String encoded in json format.

Definition at line 346 of file dgp.c.

References Graph::connectivity, Graph::function, function_string(), Graph::initial_state, Graph::klen, Graph::mu, Graph::n, Graph::n_inputs, N_MU, Graph::state, and Graph::t.

Referenced by cond_dgp_json_export(), graph_print(), and rule_dgp_cond_json_export().

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

◆ graph_json_import()

void graph_json_import ( struct Graph dgp,
const struct ArgsDGP args,
const cJSON *  json 
)

Creates a DGP graph from a cJSON object.

Parameters
[in,out]dgpThe DGP to initialise.
[in]argsParameters for initialising a DGP graph.
[in]jsoncJSON object.

Definition at line 477 of file dgp.c.

References Graph::connectivity, ArgsDGP::evolve_cycles, Graph::evolve_cycles, Graph::function, graph_json_import_connectivity(), graph_json_import_current_state(), graph_json_import_functions(), graph_json_import_initial_state(), graph_rand(), Graph::initial_state, Graph::klen, ArgsDGP::max_k, Graph::max_k, ArgsDGP::max_t, Graph::max_t, Graph::mu, ArgsDGP::n, Graph::n, ArgsDGP::n_inputs, Graph::n_inputs, N_MU, sam_json_import(), Graph::state, Graph::t, Graph::tmp_input, and Graph::tmp_state.

Referenced by cond_dgp_json_import(), and rule_dgp_cond_json_import().

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

◆ graph_json_import_connectivity()

static void graph_json_import_connectivity ( struct Graph dgp,
const cJSON *  json 
)
static

Sets DGP mutation rates from a cJSON object.

Parameters
[in,out]dgpThe DGP to initialise.
[in]jsoncJSON object.

Definition at line 450 of file dgp.c.

References Graph::connectivity, Graph::klen, Graph::n, and Graph::n_inputs.

Referenced by graph_json_import().

Here is the caller graph for this function:

◆ graph_json_import_current_state()

static void graph_json_import_current_state ( struct Graph dgp,
const cJSON *  json 
)
static

Sets DGP current states from a cJSON object.

Parameters
[in,out]dgpThe DGP to initialise.
[in]jsoncJSON object.

Definition at line 377 of file dgp.c.

References Graph::n, and Graph::state.

Referenced by graph_json_import().

Here is the caller graph for this function:

◆ graph_json_import_functions()

static void graph_json_import_functions ( struct Graph dgp,
const cJSON *  json 
)
static

Sets DGP functions from a cJSON object.

Parameters
[in,out]dgpThe DGP to initialise.
[in]jsoncJSON object.

Definition at line 427 of file dgp.c.

References Graph::function, function_int(), and Graph::n.

Referenced by graph_json_import().

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

◆ graph_json_import_initial_state()

static void graph_json_import_initial_state ( struct Graph dgp,
const cJSON *  json 
)
static

Sets DGP initial states from a cJSON object.

Parameters
[in,out]dgpThe DGP to initialise.
[in]jsoncJSON object.

Definition at line 402 of file dgp.c.

References Graph::initial_state, and Graph::n.

Referenced by graph_json_import().

Here is the caller graph for this function:

◆ graph_load()

size_t graph_load ( struct Graph dgp,
FILE *  fp 
)

Reads DGP graph from a file.

Parameters
[in]dgpThe DGP graph to load.
[in]fpPointer to the file to be written.
Returns
The number of elements written.

Definition at line 588 of file dgp.c.

References Graph::connectivity, Graph::evolve_cycles, Graph::function, Graph::initial_state, Graph::klen, Graph::max_k, Graph::max_t, Graph::mu, Graph::n, Graph::n_inputs, N_MU, Graph::state, Graph::t, Graph::tmp_input, and Graph::tmp_state.

Referenced by cond_dgp_load(), and rule_dgp_cond_load().

Here is the caller graph for this function:

◆ graph_mutate()

bool graph_mutate ( struct Graph dgp)

Mutates a specified DGP graph.

Parameters
[in]dgpThe DGP graph to be mutated.
Returns
Whether any alterations were made.

Definition at line 540 of file dgp.c.

References Graph::evolve_cycles, graph_mutate_connectivity(), graph_mutate_cycles(), graph_mutate_functions(), Graph::mu, MU_TYPE, N_MU, and sam_adapt().

Referenced by cond_dgp_mutate(), and rule_dgp_cond_mutate().

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

◆ graph_mutate_connectivity()

static bool graph_mutate_connectivity ( struct Graph dgp)
static

Mutates the connectivity of a DGP graph.

Parameters
[in]dgpThe DGP graph to be mutated.
Returns
Whether any alterations were made.

Definition at line 90 of file dgp.c.

References Graph::connectivity, Graph::klen, Graph::mu, Graph::n, Graph::n_inputs, rand_uniform(), and random_connection().

Referenced by graph_mutate().

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

◆ graph_mutate_cycles()

static bool graph_mutate_cycles ( struct Graph dgp)
static

Mutates the number of update cycles performed by a DGP graph.

Parameters
[in]dgpThe DGP graph to be mutated.
Returns
Whether any alterations were made.

Definition at line 111 of file dgp.c.

References Graph::max_t, Graph::mu, and Graph::t.

Referenced by graph_mutate().

Here is the caller graph for this function:

◆ graph_mutate_functions()

static bool graph_mutate_functions ( struct Graph dgp)
static

Mutates the node functions within a DGP graph.

Parameters
[in]dgpThe DGP graph to be mutated.
Returns
Whether any alterations were made.

Definition at line 69 of file dgp.c.

References Graph::function, Graph::mu, Graph::n, NUM_FUNC, rand_uniform(), and rand_uniform_int().

Referenced by graph_mutate().

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

◆ graph_output()

double graph_output ( const struct Graph dgp,
const int  IDX 
)

Returns the current state of a specified node in the graph.

Parameters
[in]dgpThe DGP graph to output.
[in]IDXWhich node within the graph to output.
Returns
The current state of the specified node.

Definition at line 274 of file dgp.c.

References Graph::state.

Referenced by cond_dgp_match(), rule_dgp_act_compute(), and rule_dgp_cond_match().

Here is the caller graph for this function:

◆ graph_param_set_evolve_cycles()

void graph_param_set_evolve_cycles ( struct ArgsDGP args,
const bool  a 
)

Definition at line 763 of file dgp.c.

References ArgsDGP::evolve_cycles.

Referenced by cond_dgp_param_defaults(), and graph_args_json_import().

Here is the caller graph for this function:

◆ graph_param_set_max_k()

void graph_param_set_max_k ( struct ArgsDGP args,
const int  a 
)

Definition at line 719 of file dgp.c.

References ArgsDGP::max_k.

Referenced by cond_dgp_param_defaults(), and graph_args_json_import().

Here is the caller graph for this function:

◆ graph_param_set_max_t()

void graph_param_set_max_t ( struct ArgsDGP args,
const int  a 
)

Definition at line 730 of file dgp.c.

References ArgsDGP::max_t.

Referenced by cond_dgp_param_defaults(), and graph_args_json_import().

Here is the caller graph for this function:

◆ graph_param_set_n()

void graph_param_set_n ( struct ArgsDGP args,
const int  a 
)

Definition at line 741 of file dgp.c.

References ArgsDGP::n.

Referenced by cond_dgp_param_defaults(), and graph_args_json_import().

Here is the caller graph for this function:

◆ graph_param_set_n_inputs()

void graph_param_set_n_inputs ( struct ArgsDGP args,
const int  a 
)

Definition at line 752 of file dgp.c.

References ArgsDGP::n_inputs.

Referenced by cond_dgp_param_defaults().

Here is the caller graph for this function:

◆ graph_print()

void graph_print ( const struct Graph dgp)

Prints a DGP graph.

Parameters
[in]dgpThe DGP graph to print.

Definition at line 333 of file dgp.c.

References graph_json_export().

Here is the call graph for this function:

◆ graph_rand()

void graph_rand ( struct Graph dgp)

Randomises a specified DGP graph.

Parameters
[in]dgpThe DGP graph to randomise.

Definition at line 296 of file dgp.c.

References Graph::connectivity, Graph::evolve_cycles, Graph::function, Graph::initial_state, Graph::klen, Graph::max_t, Graph::n, Graph::n_inputs, NUM_FUNC, rand_uniform(), rand_uniform_int(), random_connection(), Graph::state, and Graph::t.

Referenced by cond_dgp_cover(), cond_dgp_init(), graph_json_import(), rule_dgp_act_cover(), and rule_dgp_cond_init().

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

◆ graph_reset()

void graph_reset ( const struct Graph dgp)

Resets the states to their initial state.

Parameters
[in]dgpThe DGP graph to reset.

Definition at line 284 of file dgp.c.

References Graph::initial_state, Graph::n, and Graph::state.

Referenced by graph_update().

Here is the caller graph for this function:

◆ graph_save()

size_t graph_save ( const struct Graph dgp,
FILE *  fp 
)

Writes DGP graph to a file.

Parameters
[in]dgpThe DGP graph to save.
[in]fpPointer to the file to be written.
Returns
The number of elements written.

Definition at line 563 of file dgp.c.

References Graph::connectivity, Graph::evolve_cycles, Graph::function, Graph::initial_state, Graph::klen, Graph::max_k, Graph::max_t, Graph::mu, Graph::n, Graph::n_inputs, N_MU, Graph::state, and Graph::t.

Referenced by cond_dgp_save(), and rule_dgp_cond_save().

Here is the caller graph for this function:

◆ graph_update()

void graph_update ( const struct Graph dgp,
const double *  inputs,
const bool  reset 
)

Updates a DGP graph T cycles.

Parameters
[in]dgpThe DGP graph to update.
[in]inputsThe inputs to the graph.
[in]resetWhether to reset states to initial values.

Definition at line 318 of file dgp.c.

References graph_reset(), synchronous_update(), and Graph::t.

Referenced by cond_dgp_match(), and rule_dgp_cond_match().

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

◆ node_activate()

static double node_activate ( int  function,
const double *  inputs,
const int  K 
)
static

Returns the result from applying a specified activation function.

Parameters
[in]functionThe activation function to apply.
[in]inputsThe input to the activation function.
[in]KThe number of inputs to the activation function.
Returns
The result from applying the activation function.

Definition at line 129 of file dgp.c.

References clamp(), FUZZY_CFMQVS_AND, FUZZY_CFMQVS_OR, and FUZZY_NOT.

Referenced by synchronous_update().

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

◆ random_connection()

static int random_connection ( const int  n_nodes,
const int  n_inputs 
)
static

Returns a random connection.

Parameters
[in]n_nodesThe number of nodes in the graph.
[in]n_inputsThe number of external inputs to the graph.

Definition at line 53 of file dgp.c.

References rand_uniform(), and rand_uniform_int().

Referenced by graph_mutate_connectivity(), and graph_rand().

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

◆ synchronous_update()

static void synchronous_update ( const struct Graph dgp,
const double *  inputs 
)
static

Performs a synchronous update.

Parameters
[in]dgpThe DGP graph to update.
[in]inputsThe inputs to the graph.

Definition at line 203 of file dgp.c.

References Graph::connectivity, Graph::function, Graph::max_k, Graph::n, Graph::n_inputs, node_activate(), Graph::state, Graph::tmp_input, and Graph::tmp_state.

Referenced by graph_update().

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

Variable Documentation

◆ MU_TYPE

const int MU_TYPE[(3)]
static
Initial value:
= {
(0) ,
(0) ,
(2)
}

Self-adaptation method for mutating DGP graphs.

Definition at line 41 of file dgp.c.

Referenced by graph_init(), and graph_mutate().