C++ Documentation

Classes

expression: a Cartesian Genetic Program

dCGP expression

A dCGP expression

template <typename T>
class dcgp::expression

A dCGP expression.

This class represents a mathematical expression as encoded using CGP and contains algorithms to compute its value (numerical and symbolical) and its derivatives as well as to mutate the expression.

Author
Dario Izzo (dario.izzo@gmail.com)
Template Parameters
  • T: expression type. Can be double, or a gdual type.

Subclassed by dcgp::expression_weighted< T >

Public Functions

expression(unsigned int n, unsigned int m, unsigned int r, unsigned int c, unsigned int l, unsigned int arity, std::vector<kernel<T>> f, unsigned int seed)

Constructor.

Constructs a dCGP expression

Parameters
  • n: number of inputs (independent variables)
  • m: number of outputs (dependent variables)
  • r: number of rows of the dCGP
  • c: number of columns of the dCGP
  • l: number of levels-back allowed in the dCGP
  • arity: arity of the basis functions
  • f: function set. An std::vector of dcgp::kernel<expression::type>
  • seed: seed for the random number generator (initial expression and mutations depend on this)

void set(const std::vector<unsigned int> &x)

Sets the chromosome.

Sets a given chromosome as genotype for the expression and updates the active nodes and active genes information accordingly

Parameters
  • x: the new cromosome
Exceptions
  • std::invalid_argument: if the chromosome is incompatible with the expression (n.inputs, n.outputs, levels-back, etc.)

const std::vector<unsigned int> &get() const

Gets the chromosome.

Gets the chromosome encoding the current expression

Return
The chromosome

const std::vector<unsigned int> &get_lb() const

Gets the lower bounds.

Gets the lower bounds for the genes

Return
An std::vector containing the lower bound for each gene

const std::vector<unsigned int> &get_ub() const

Gets the upper bounds.

Gets the upper bounds for the genes

Return
An std::vector containing the upper bound for each gene

const std::vector<unsigned int> &get_active_genes() const

Gets the active genes.

Gets the idx of the active genes in the current chromosome (numbering is from 0)

Return
An std::vector containing the idx of the active genes in the current chromosome

const std::vector<unsigned int> &get_active_nodes() const

Gets the active nodes.

Gets the idx of the active nodes in the current chromosome. The numbering starts from 0 at the first input node to then follow PPSN tutorial from Miller

Return
An std::vector containing the idx of the active nodes

unsigned int get_n() const

Gets the number of inputs.

Gets the number of inputs of the dCGP expression

Return
the number of inputs

unsigned int get_m() const

Gets the number of outputs.

Gets the number of outputs of the dCGP expression

Return
the number of outputs

unsigned int get_rows() const

Gets the number of rows.

Gets the number of rows of the dCGP expression

Return
the number of rows

unsigned int get_cols() const

Gets the number of columns.

Gets the number of columns of the dCGP expression

Return
the number of columns

unsigned int get_levels_back() const

Gets the number of levels-back.

Gets the number of levels-back allowed for the dCGP expression

Return
the number of levels-back

unsigned int get_arity() const

Gets the arity.

Gets the arity of the basis functions of the dCGP expression

Return
the arity

const std::vector<kernel<T>> &get_f() const

Gets the function set.

Gets the set of functions used in the dCGP expression

Return
an std::vector of kernels

void mutate(unsigned int idx)

Mutates one gene.

Mutates exactly one gene within its allowed bounds.

Parameters
  • idx: index of the gene to me mutated
Exceptions
  • std::invalid_argument: if idx is too large

void mutate(std::vector<unsigned int> idxs)

Mutates multiple genes at once.

Mutates multiple genes within their allowed bounds.

Parameters
  • idxs: vector of indexes of the genes to me mutated
Exceptions
  • std::invalid_argument: if idx is too large

void mutate_random(unsigned int N)

Mutates N random genes.

Mutates a specified number of random genes within their bounds

Parameters
  • N: number of genes to be mutated

void mutate_active(unsigned int N = 1)

Mutates one of the active genes.

Mutates N active genes within their allowed bounds. The mutation can affect function genes, input genes and output genes.

Parameters
  • N: Number of active genes to be mutated

void mutate_active_fgene()

Mutates one of the active function genes.

Mutates exactly one of the active function genes within its allowed bounds.

void mutate_active_cgene()

Mutates one of the active connection genes.

Mutates exactly one of the active connection genes within its allowed bounds.

void mutate_ogene()

Mutates one of the active output genes.

Mutates exactly one of the output genes within its allowed bounds.

template <typename U, functor_enabler< U > = 0>
std::vector<U> operator()(const std::vector<U> &in) const

Evaluates the dCGP expression.

This evaluates the dCGP expression. According to the template parameter it will compute the value (double) the Taylor expansion (gdual) or a symbolic representation (std::string). Any other type will result in a compilation-time error (SFINAE).

Return
The value of the function (an std::vector)
Parameters
  • in: an std::vector containing the values where the dCGP expression has to be computed (doubles, gduals or strings)

template <typename U, functor_enabler< U > = 0>
std::vector<U> operator()(const std::initializer_list<U> &in) const

Evaluates the dCGP expression.

This evaluates the dCGP expression. According to the template parameter it will compute the value (double) the Taylor expansion (gdual) or a symbolic representation (std::string). Any other type will result in a compilation-time error (SFINAE). This is identical to the other overload and is provided only for convenience

Return
The value of the function (an std::vector)
Parameters
  • in: an initializer list containing the values where the dCGP expression has to be computed (doubles, gduals or strings)

Friends

std::ostream &operator<<(std::ostream &os, const expression &d)

Overloaded stream operator.

Will return a formatted string containing a human readable representation of the class

Return
std::string containing a human-readable representation of the problem.


expression_weighted: a weighted Cartesian Genetic Program

weighted dCGP expression

A weighted dCGP expression

template <typename T>
class dcgp::expression_weighted

A weighted dCGP expression.

This class represents a mathematical expression as encoded using CGP with the addition of weights on the connections. It contains algorithms to compute the value (numerical and symbolical) of the expression and its derivatives, as well as to mutate the expression.

Author
Dario Izzo (dario.izzo@gmail.com)
Template Parameters
  • T: expression type. Can be double, or a gdual type.

Inherits from dcgp::expression< T >

Public Functions

expression_weighted(unsigned int n, unsigned int m, unsigned int r, unsigned int c, unsigned int l, unsigned int arity, std::vector<kernel<T>> f, unsigned int seed)

Constructor.

Constructs a dCGP expression

Parameters
  • n: number of inputs (independent variables)
  • m: number of outputs (dependent variables)
  • r: number of rows of the dCGP
  • c: number of columns of the dCGP
  • l: number of levels-back allowed for the dCGP
  • arity: arity of the basis functions
  • f: function set. An std::vector of dcgp::kernel<expression::type>
  • seed: seed for the random number generator (initial expression and mutations depend on this)

template <typename U, functor_enabler< U > = 0>
std::vector<U> operator()(const std::vector<U> &in) const

Evaluates the dCGP expression.

This evaluates the dCGP expression. According to the template parameter it will compute the value (double) the Taylor expansion (gdual) or a symbolic representation (std::string). Any other type will result in a compilation-time error (SFINAE).

Return
The value of the function (an std::vector)
Parameters
  • in: an std::vector containing the values where the dCGP expression has to be computed (doubles, gduals or strings)

template <typename U, functor_enabler< U > = 0>
std::vector<U> operator()(const std::initializer_list<U> &in) const

Evaluates the dCGP expression.

This evaluates the dCGP expression. According to the template parameter it will compute the value (double) the Taylor expansion (gdual) or a symbolic representation (std::string). Any other type will result in a compilation-time error (SFINAE). This is identical to the other overload and is provided only for convenience

Return
The value of the function (an std::vector)
Parameters
  • in: an initializer list containing the values where the dCGP expression has to be computed (doubles, gduals or strings)

void set_weight(typename std::vector<T>::size_type node_id, typename std::vector<T>::size_type input_id, const T &w)

Sets a weight.

Sets a connection weight to a new value

Parameters
Exceptions
  • std::invalid_argument: if the node_id or input_id are not valid

T get_weight(typename std::vector<T>::size_type node_id, typename std::vector<T>::size_type input_id)

Gets a weight.

Gets the value of a connection weight

Return
the value of the weight
Parameters
Exceptions
  • std::invalid_argument: if the node_id or input_id are not valid

void set_weights(const std::vector<T> &ws)

Sets all weights.

Sets all the connection weights at once

Parameters
  • ws: an std::vector containing all the weights to set
Exceptions
  • std::invalid_argument: if the input vector dimension is not valid (r**arity)

const std::vector<T> &get_weights() const

Gets the weights.

Gets the values of all the weights

Return
an std::vector containing all the weights

Friends

std::ostream &operator<<(std::ostream &os, const expression_weighted &d)

Overloaded stream operator.

Will return a formatted string containing a human readable representation of the class

Return
std::string containing a human-readable representation of the problem.


kernel: a function defining the generic CGP node

template <typename T>
class dcgp::kernel

Basis function.

This class represents the function defining the generic CGP node. To be constructed it accepts two functions having prototype T(const std::vector<T>&) and std::string(const std::vector<std::string>&) computing, respectively, the function value on generic inputs and the textual representation of the operation.

The intended use would then be something like:

kernel<double> f(my_sum<double>, print_my_sum, "sum");
kernel<double> f(my_sum<gdual_d>, print_my_sum, "sum");

Template Parameters
  • T: The type of the function output (and inputs)

Public Types

using >
using dcgp::kernel<T>::my_fun_type = std::function<T(const std::vector<T>&)>

Basic prototype of a kernel function returning its evaluation.

using >
using dcgp::kernel<T>::my_print_fun_type = std::function<std::string(const std::vector<std::string>&)>

Basic prototype of a kernel function returning its symbolic representation.

Public Functions

template <typename U, typename V>
kernel(U &&f, V &&pf, std::string name)

Constructor.

Constructs a kernel that can be used as kernel in a dCGP expression

Parameters
  • f: any callable with prototype T(const std::vector<T>&)
  • pf: any callable with prototype std::string(const std::vector<std::string>&)
  • name: string containing the function name (ex. “sum”)

T operator()(const std::vector<T> &in) const

Parenthesis operator.

Evaluates the kernel in the point in

Return
the function value
Parameters
  • in: the evaluation point as an std::vector<T>

T operator()(const std::initializer_list<T> &in) const

Parenthesis operator.

Evaluates the kernel in the point in

Return
the function value
Parameters
  • in: the evaluation point as an std::initializer_list<T>

std::string operator()(const std::vector<std::string> &in) const

Parenthesis operator.

Returns a symbolic representation of the operation made by \(f\)

Return
the string representation of the operation (ex. “ln(s1+s2)”)
Parameters
  • in: std::vector<std::string> with the symbolic names to be used

Friends

std::ostream &operator<<(std::ostream &os, const kernel<T> &d)

Overloaded stream operator.

Will stream the function name

Return
reference to os.
Parameters
  • os: target stream.
  • d: kernel argument.


kernel_set: a set of kernels

Kernel name Function
“sum” addition
“diff” subtraction
“mul” multiplication
“div” division
“pdiv” protected division
“sig” sigmoid
“sin” sine
“cos” cosine
“log” logarithm
“exp” exponential
template <typename T>
class dcgp::kernel_set

Function set.

This class is provided as an helper to construct the std::vector<kernel<T>> that is requested to form a dcgp::expression<T>. Once constructed, a call to its parenthesis operator will return the std::vector containing the requested kernels

Template Parameters
  • T: The type of the functions output (and inputs)

Public Functions

kernel_set()

Constructor.

Default constructor

kernel_set(const std::vector<std::string> &list)

Constructor.

Constructs a kernel set that can be used in a dCGP expression

Parameters
  • list: an std::vector of strings containing the function names (e.g., “sum”)

void push_back(std::string kernel_name)

Adds a kernel to the set.

Constructs a kernel<T> given a string containing the function name, and inserts it into the std::vector

Parameters
  • kernel_name: a string containing the function name
Exceptions
  • std::invalid_argument: if the function is not implemented

void push_back(const dcgp::kernel<T> &kernel)

Adds a kernel to the set.

Inserts the given kernel<T> into the std::vector

Parameters
  • kernel: the dcgp::kernel<T> to add

void clear()

Clears the kernel set.

Removes all the elements from the std::vector containing the kernels

std::vector<dcgp::kernel<T>> operator()() const

Overloaded function call operator.

Returns the std::vector containing the kernels

dcgp::kernel<T> operator[](const typename std::vector<dcgp::kernel<T>>::size_type idx) const

Returns the kernel at a specified index in the std::vector

Return
the kernel at the specified index
Parameters
  • idx: index of the required kernel

Friends

std::ostream &operator<<(std::ostream &os, const kernel_set<T> &d)

Overloaded stream operator.

Will stream the function names

Return
reference to os
Parameters