XCSF  1.4.7
XCSF learning classifier system
gp.h
Go to the documentation of this file.
1 /*
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
24 #pragma once
25 
26 #include "xcsf.h"
27 
31 struct ArgsGPTree {
32  double max;
33  double min;
34  int n_inputs;
36  int init_depth;
37  int max_len;
38  double *constants;
39 };
40 
44 struct GPTree {
45  int *tree;
46  int len;
47  int pos;
48  double *mu;
49 };
50 
51 void
52 tree_free(const struct GPTree *gp);
53 
54 void
55 tree_rand(struct GPTree *gp, const struct ArgsGPTree *args);
56 
57 void
58 tree_copy(struct GPTree *dest, const struct GPTree *src);
59 
60 void
61 tree_print(const struct GPTree *gp, const struct ArgsGPTree *args);
62 
63 char *
64 tree_json_export(const struct GPTree *gp, const struct ArgsGPTree *args);
65 
66 void
67 tree_json_import(struct GPTree *gp, const struct ArgsGPTree *args,
68  const cJSON *json);
69 
70 double
71 tree_eval(struct GPTree *gp, const struct ArgsGPTree *args, const double *x);
72 
73 void
74 tree_crossover(struct GPTree *p1, struct GPTree *p2);
75 
76 bool
77 tree_mutate(struct GPTree *gp, const struct ArgsGPTree *args);
78 
79 size_t
80 tree_save(const struct GPTree *gp, FILE *fp);
81 
82 size_t
83 tree_load(struct GPTree *gp, FILE *fp);
84 
85 void
86 tree_args_init(struct ArgsGPTree *args);
87 
88 void
89 tree_args_free(struct ArgsGPTree *args);
90 
91 char *
92 tree_args_json_import(struct ArgsGPTree *args, cJSON *json);
93 
94 char *
95 tree_args_json_export(const struct ArgsGPTree *args);
96 
97 size_t
98 tree_args_save(const struct ArgsGPTree *args, FILE *fp);
99 
100 size_t
101 tree_args_load(struct ArgsGPTree *args, FILE *fp);
102 
103 void
105 
106 /* parameter setters */
107 
108 void
109 tree_param_set_max(struct ArgsGPTree *args, const double a);
110 
111 void
112 tree_param_set_min(struct ArgsGPTree *args, const double a);
113 
114 void
115 tree_param_set_n_inputs(struct ArgsGPTree *args, const int a);
116 
117 void
118 tree_param_set_n_constants(struct ArgsGPTree *args, const int a);
119 
120 void
121 tree_param_set_init_depth(struct ArgsGPTree *args, const int a);
122 
123 void
124 tree_param_set_max_len(struct ArgsGPTree *args, const int a);
void tree_json_import(struct GPTree *gp, const struct ArgsGPTree *args, const cJSON *json)
Creates a GP tree from a cJSON object.
Definition: gp.c:273
void tree_args_free(struct ArgsGPTree *args)
Frees memory used by GP tree parameters.
Definition: gp.c:438
size_t tree_load(struct GPTree *gp, FILE *fp)
Reads a GP tree from a file.
Definition: gp.c:400
void tree_param_set_max_len(struct ArgsGPTree *args, const int a)
Definition: gp.c:598
void tree_crossover(struct GPTree *p1, struct GPTree *p2)
Performs sub-tree crossover.
Definition: gp.c:318
size_t tree_save(const struct GPTree *gp, FILE *fp)
Writes the GP tree to a file.
Definition: gp.c:383
void tree_print(const struct GPTree *gp, const struct ArgsGPTree *args)
Prints a GP tree.
Definition: gp.c:289
void tree_param_set_max(struct ArgsGPTree *args, const double a)
Definition: gp.c:553
void tree_param_set_n_inputs(struct ArgsGPTree *args, const int a)
Definition: gp.c:565
char * tree_args_json_import(struct ArgsGPTree *args, cJSON *json)
Sets the GP tree parameters from a cJSON object.
Definition: gp.c:469
double tree_eval(struct GPTree *gp, const struct ArgsGPTree *args, const double *x)
Evaluates a GP tree.
Definition: gp.c:133
void tree_param_set_n_constants(struct ArgsGPTree *args, const int a)
Definition: gp.c:576
void tree_args_init_constants(struct ArgsGPTree *args)
Builds global constants used by GP trees.
Definition: gp.c:539
void tree_param_set_init_depth(struct ArgsGPTree *args, const int a)
Definition: gp.c:587
bool tree_mutate(struct GPTree *gp, const struct ArgsGPTree *args)
Performs point mutation on a GP tree.
Definition: gp.c:355
void tree_args_init(struct ArgsGPTree *args)
Sets tree GP parameters to default values.
Definition: gp.c:422
size_t tree_args_load(struct ArgsGPTree *args, FILE *fp)
Loads Tree GP parameters.
Definition: gp.c:521
void tree_param_set_min(struct ArgsGPTree *args, const double a)
Definition: gp.c:559
char * tree_args_json_export(const struct ArgsGPTree *args)
Returns a json formatted string of the GP tree parameters.
Definition: gp.c:449
char * tree_json_export(const struct GPTree *gp, const struct ArgsGPTree *args)
Returns a json formatted string representation of a GP tree.
Definition: gp.c:253
void tree_copy(struct GPTree *dest, const struct GPTree *src)
Copies a GP tree.
Definition: gp.c:302
void tree_free(const struct GPTree *gp)
Frees a GP tree.
Definition: gp.c:119
size_t tree_args_save(const struct ArgsGPTree *args, FILE *fp)
Saves Tree GP parameters.
Definition: gp.c:501
void tree_rand(struct GPTree *gp, const struct ArgsGPTree *args)
Creates a random GP tree.
Definition: gp.c:102
Parameters for initialising GP trees.
Definition: gp.h:31
double * constants
Constants available for GP trees.
Definition: gp.h:38
int n_constants
Number of constants available.
Definition: gp.h:35
int init_depth
Initial depth.
Definition: gp.h:36
int max_len
Maximum initial length.
Definition: gp.h:37
double min
Minimum value of a constant.
Definition: gp.h:33
int n_inputs
Number of inputs.
Definition: gp.h:34
double max
Maximum value of a constant.
Definition: gp.h:32
GP tree data structure.
Definition: gp.h:44
int * tree
Flattened tree representation of functions and terminals.
Definition: gp.h:45
int pos
Current position in the tree.
Definition: gp.h:47
double * mu
Mutation rates.
Definition: gp.h:48
int len
Size of the tree.
Definition: gp.h:46
XCSF data structures.