Python Documentation

Types of dCGPs

expression_double

class dcgpy.expression_double(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)

A CGP expression

Constructs a CGP expression operating on double

Parameters:
  • inputs (int) – number of inputs
  • outputs (int) – number of outputs
  • rows (int) – number of rows in the cartesian program
  • columns (int) – number of columns in the cartesian program
  • levels_back (int) – number of levels-back in the cartesian program
  • arity (int on list) – arity of the kernels. Assumed equal for all columns unless its specified by a list. The list must contain a number of entries equal to the number of columns.
  • kernels (List[dcgpy.kernel_double]) – kernel functions
  • seed (int) – random seed to generate mutations and chromosomes

Examples:

>>> from dcgpy import *
>>> dcgp = expression_double(1,1,1,10,11,2,kernel_set(["sum","diff","mul","div"])(), 32u)
>>> print(dcgp)
...
>>> num_out = dcgp([in])
>>> sym_out = dcgp(["x"])
get()

Gets the expression chromosome

get_active_genes()

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

get_active_nodes()

Gets the idx of the active nodes in the current chromosome

get_arity()

get_arity(node_id) Gets the arity of the basis functions of the dCGP expression. Either the whole vector or that of a single node.

get_arity() get_arity(node_id) Gets the arity of the basis functions of the dCGP expression. Either the whole vector or that of a single node.

get_cols()

Gets the number of columns of the dCGP expression

get_f()

Gets the kernel functions

get_gene_idx()

Gets a vector containing the indexes in the chromosome where each node starts to be expressed.

get_lb()

Gets the lower bounds of the chromosome

get_levels_back()

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

get_m()

Gets the number of outputs of the dCGP expression

get_n()

Gets the number of inputs of the dCGP expression

get_rows()

Gets the number of rows of the dCGP expression

get_ub()

Gets the upper bounds of the chromosome

loss(points, labels, loss_type, parallel=True)

Computes the loss of the model on the data

Parameters:
  • points (2D NumPy float array or list of lists of float) – the input data
  • labels (2D NumPy float array or list of lists of float) – the output labels (supervised signal)
  • loss_type (a str) – the loss, one of “MSE” for Mean Square Error and “CE” for Cross-Entropy.
  • parallel (a int) – sets the grain for parallelism. 0 -> no parallelism n -> divides the data into n parts and processes them in parallel threads
Raises:

ValueError – if points or labels are malformed or if loss_type is not one of the available types.

mutate(idxs)

Mutates multiple genes within their allowed bounds.

Parameters:idxs (a List[int]) – indexes of the genes to me mutated
Raises:ValueError – if the index of a gene is out of bounds
mutate_active(N = 1)

Mutates N randomly selected active genes within their allowed bounds

mutate_active_cgene(N = 1)

Mutates N randomly selected active connections within their allowed bounds

mutate_active_fgene(N = 1)

Mutates N randomly selected active function genes within their allowed bounds

mutate_ogene(N = 1)

Mutates N randomly selected output genes connection within their allowed bounds

mutate_random(N = 1)

Mutates N randomly selected genes within its allowed bounds

set(chromosome)

Sets the chromosome.

Parameters:chromosome (a List[int]) – the new chromosome
Raises:ValueError – if the the chromosome is incompatible with the expression (n.inputs, n.outputs, levels-back, etc.)
set_f_gene(node_id, f_id)

Sets for a valid node (i.e. not an input node) a new kernel.

Parameters:
  • node_id (a List[int]) – the node id
  • f_id (a List[int]) – the kernel id
Raises:

ValueError – if the node_id or f_id are incompatible with the expression.

simplify(in_sym, subs_weights = False, erc = [])

Simplifies the symbolic output of dCGP expressions

Returns the simplified dCGP expression for each output

Note

This method requires sympy and pyaudi modules installed in your Python system

Parameters:
  • in_sym (a List[str]) – input symbols (its length must match the number of inputs)
  • subs_weights (a bool) – indicates whether to substitute the weights symbols with their values
  • erc (a List[float]) – values of the ephemeral random constants (if empty their symbolic representation is used instead)
Returns:

A List[str] containing the simplified expressions

Raises:
  • ValueError – if the length of in_sym does not match the number of inputs
  • ValueError – if the length of erc is larger than the number of inputs
  • ImportError – if modules sympy or pyaudi are not installed in your Python system

Examples

>>> ex = dcgpy.expression_weighted_gdual_double(3,2,3,3,2,2,dcgpy.kernel_set_gdual_double(["sum","diff"])(),0)
>>> print(ex.simplify(['x','c0','c1'],True,[1,2])[0])
x + 6
visualize(in_sym = [], draw_inactive = True, draw_weights = False)

Visualizes the d-CGP expression Visualizes the graph of the d-CGP expression .. note:: This method requires the graphviz` module installed in your Python system

Parameters:
  • in_sym (a List[str]) – input symbols. Its length must either match the number of inputs or be zero (to visualize them as x_i)
  • draw_inactive (a bool) – indicates whether to draw inactive nodes
  • draw_weights (a bool) – indicates whether to draw connection weights symbols
Returns:

The graphviz.Digraph for the given expression

Raises:
  • ImportError – if module pygraphviz is not installed in your Python system
  • ValueError – if in_sym is nonempty but its length does not match the number of inputs

Examples

>>> ex = dcgpy.expression_double(2,1,3,3,2,2,dcgpy.kernel_set_double(["sum","diff"])(),0)
>>> graph = ex.visualize(['x', 'c'], True, False)

expression_gdual_double

class dcgpy.expression_gdual_double(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)

A CGP expression

Constructs a CGP expression operating on gdual_double

Parameters:
  • inputs (int) – number of inputs
  • outputs (int) – number of outputs
  • rows (int) – number of rows in the cartesian program
  • columns (int) – number of columns in the cartesian program
  • levels_back (int) – number of levels-back in the cartesian program
  • arity (int on list) – arity of the kernels. Assumed equal for all columns unless its specified by a list. The list must contain a number of entries equal to the number of columns.
  • kernels (List[dcgpy.kernel_gdual_double]) – kernel functions
  • seed (int) – random seed to generate mutations and chromosomes

Examples:

>>> from dcgpy import *
>>> dcgp = expression_gdual_double(1,1,1,10,11,2,kernel_set(["sum","diff","mul","div"])(), 32u)
>>> print(dcgp)
...
>>> num_out = dcgp([in])
>>> sym_out = dcgp(["x"])
get()

Gets the expression chromosome

get_active_genes()

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

get_active_nodes()

Gets the idx of the active nodes in the current chromosome

get_arity()

get_arity(node_id) Gets the arity of the basis functions of the dCGP expression. Either the whole vector or that of a single node.

get_arity() get_arity(node_id) Gets the arity of the basis functions of the dCGP expression. Either the whole vector or that of a single node.

get_cols()

Gets the number of columns of the dCGP expression

get_f()

Gets the kernel functions

get_gene_idx()

Gets a vector containing the indexes in the chromosome where each node starts to be expressed.

get_lb()

Gets the lower bounds of the chromosome

get_levels_back()

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

get_m()

Gets the number of outputs of the dCGP expression

get_n()

Gets the number of inputs of the dCGP expression

get_rows()

Gets the number of rows of the dCGP expression

get_ub()

Gets the upper bounds of the chromosome

loss(points, labels, loss_type, parallel=True)

Computes the loss of the model on the data

Parameters:
  • points (2D NumPy float array or list of lists of float) – the input data
  • labels (2D NumPy float array or list of lists of float) – the output labels (supervised signal)
  • loss_type (a str) – the loss, one of “MSE” for Mean Square Error and “CE” for Cross-Entropy.
  • parallel (a int) – sets the grain for parallelism. 0 -> no parallelism n -> divides the data into n parts and processes them in parallel threads
Raises:

ValueError – if points or labels are malformed or if loss_type is not one of the available types.

mutate(idxs)

Mutates multiple genes within their allowed bounds.

Parameters:idxs (a List[int]) – indexes of the genes to me mutated
Raises:ValueError – if the index of a gene is out of bounds
mutate_active(N = 1)

Mutates N randomly selected active genes within their allowed bounds

mutate_active_cgene(N = 1)

Mutates N randomly selected active connections within their allowed bounds

mutate_active_fgene(N = 1)

Mutates N randomly selected active function genes within their allowed bounds

mutate_ogene(N = 1)

Mutates N randomly selected output genes connection within their allowed bounds

mutate_random(N = 1)

Mutates N randomly selected genes within its allowed bounds

set(chromosome)

Sets the chromosome.

Parameters:chromosome (a List[int]) – the new chromosome
Raises:ValueError – if the the chromosome is incompatible with the expression (n.inputs, n.outputs, levels-back, etc.)
set_f_gene(node_id, f_id)

Sets for a valid node (i.e. not an input node) a new kernel.

Parameters:
  • node_id (a List[int]) – the node id
  • f_id (a List[int]) – the kernel id
Raises:

ValueError – if the node_id or f_id are incompatible with the expression.

simplify(in_sym, subs_weights = False, erc = [])

Simplifies the symbolic output of dCGP expressions

Returns the simplified dCGP expression for each output

Note

This method requires sympy and pyaudi modules installed in your Python system

Parameters:
  • in_sym (a List[str]) – input symbols (its length must match the number of inputs)
  • subs_weights (a bool) – indicates whether to substitute the weights symbols with their values
  • erc (a List[float]) – values of the ephemeral random constants (if empty their symbolic representation is used instead)
Returns:

A List[str] containing the simplified expressions

Raises:
  • ValueError – if the length of in_sym does not match the number of inputs
  • ValueError – if the length of erc is larger than the number of inputs
  • ImportError – if modules sympy or pyaudi are not installed in your Python system

Examples

>>> ex = dcgpy.expression_weighted_gdual_double(3,2,3,3,2,2,dcgpy.kernel_set_gdual_double(["sum","diff"])(),0)
>>> print(ex.simplify(['x','c0','c1'],True,[1,2])[0])
x + 6
visualize(in_sym = [], draw_inactive = True, draw_weights = False)

Visualizes the d-CGP expression Visualizes the graph of the d-CGP expression .. note:: This method requires the graphviz` module installed in your Python system

Parameters:
  • in_sym (a List[str]) – input symbols. Its length must either match the number of inputs or be zero (to visualize them as x_i)
  • draw_inactive (a bool) – indicates whether to draw inactive nodes
  • draw_weights (a bool) – indicates whether to draw connection weights symbols
Returns:

The graphviz.Digraph for the given expression

Raises:
  • ImportError – if module pygraphviz is not installed in your Python system
  • ValueError – if in_sym is nonempty but its length does not match the number of inputs

Examples

>>> ex = dcgpy.expression_double(2,1,3,3,2,2,dcgpy.kernel_set_double(["sum","diff"])(),0)
>>> graph = ex.visualize(['x', 'c'], True, False)

expression_gdual_vdouble

class dcgpy.expression_gdual_vdouble(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)

A CGP expression

Constructs a CGP expression operating on gdual_vdouble

Parameters:
  • inputs (int) – number of inputs
  • outputs (int) – number of outputs
  • rows (int) – number of rows in the cartesian program
  • columns (int) – number of columns in the cartesian program
  • levels_back (int) – number of levels-back in the cartesian program
  • arity (int on list) – arity of the kernels. Assumed equal for all columns unless its specified by a list. The list must contain a number of entries equal to the number of columns.
  • kernels (List[dcgpy.kernel_gdual_vdouble]) – kernel functions
  • seed (int) – random seed to generate mutations and chromosomes

Examples:

>>> from dcgpy import *
>>> dcgp = expression_gdual_vdouble(1,1,1,10,11,2,kernel_set(["sum","diff","mul","div"])(), 32u)
>>> print(dcgp)
...
>>> num_out = dcgp([in])
>>> sym_out = dcgp(["x"])
get()

Gets the expression chromosome

get_active_genes()

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

get_active_nodes()

Gets the idx of the active nodes in the current chromosome

get_arity()

get_arity(node_id) Gets the arity of the basis functions of the dCGP expression. Either the whole vector or that of a single node.

get_arity() get_arity(node_id) Gets the arity of the basis functions of the dCGP expression. Either the whole vector or that of a single node.

get_cols()

Gets the number of columns of the dCGP expression

get_f()

Gets the kernel functions

get_gene_idx()

Gets a vector containing the indexes in the chromosome where each node starts to be expressed.

get_lb()

Gets the lower bounds of the chromosome

get_levels_back()

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

get_m()

Gets the number of outputs of the dCGP expression

get_n()

Gets the number of inputs of the dCGP expression

get_rows()

Gets the number of rows of the dCGP expression

get_ub()

Gets the upper bounds of the chromosome

loss(points, labels, loss_type, parallel=True)

Computes the loss of the model on the data

Parameters:
  • points (2D NumPy float array or list of lists of float) – the input data
  • labels (2D NumPy float array or list of lists of float) – the output labels (supervised signal)
  • loss_type (a str) – the loss, one of “MSE” for Mean Square Error and “CE” for Cross-Entropy.
  • parallel (a int) – sets the grain for parallelism. 0 -> no parallelism n -> divides the data into n parts and processes them in parallel threads
Raises:

ValueError – if points or labels are malformed or if loss_type is not one of the available types.

mutate(idxs)

Mutates multiple genes within their allowed bounds.

Parameters:idxs (a List[int]) – indexes of the genes to me mutated
Raises:ValueError – if the index of a gene is out of bounds
mutate_active(N = 1)

Mutates N randomly selected active genes within their allowed bounds

mutate_active_cgene(N = 1)

Mutates N randomly selected active connections within their allowed bounds

mutate_active_fgene(N = 1)

Mutates N randomly selected active function genes within their allowed bounds

mutate_ogene(N = 1)

Mutates N randomly selected output genes connection within their allowed bounds

mutate_random(N = 1)

Mutates N randomly selected genes within its allowed bounds

set(chromosome)

Sets the chromosome.

Parameters:chromosome (a List[int]) – the new chromosome
Raises:ValueError – if the the chromosome is incompatible with the expression (n.inputs, n.outputs, levels-back, etc.)
set_f_gene(node_id, f_id)

Sets for a valid node (i.e. not an input node) a new kernel.

Parameters:
  • node_id (a List[int]) – the node id
  • f_id (a List[int]) – the kernel id
Raises:

ValueError – if the node_id or f_id are incompatible with the expression.

simplify(in_sym, subs_weights = False, erc = [])

Simplifies the symbolic output of dCGP expressions

Returns the simplified dCGP expression for each output

Note

This method requires sympy and pyaudi modules installed in your Python system

Parameters:
  • in_sym (a List[str]) – input symbols (its length must match the number of inputs)
  • subs_weights (a bool) – indicates whether to substitute the weights symbols with their values
  • erc (a List[float]) – values of the ephemeral random constants (if empty their symbolic representation is used instead)
Returns:

A List[str] containing the simplified expressions

Raises:
  • ValueError – if the length of in_sym does not match the number of inputs
  • ValueError – if the length of erc is larger than the number of inputs
  • ImportError – if modules sympy or pyaudi are not installed in your Python system

Examples

>>> ex = dcgpy.expression_weighted_gdual_double(3,2,3,3,2,2,dcgpy.kernel_set_gdual_double(["sum","diff"])(),0)
>>> print(ex.simplify(['x','c0','c1'],True,[1,2])[0])
x + 6
visualize(in_sym = [], draw_inactive = True, draw_weights = False)

Visualizes the d-CGP expression Visualizes the graph of the d-CGP expression .. note:: This method requires the graphviz` module installed in your Python system

Parameters:
  • in_sym (a List[str]) – input symbols. Its length must either match the number of inputs or be zero (to visualize them as x_i)
  • draw_inactive (a bool) – indicates whether to draw inactive nodes
  • draw_weights (a bool) – indicates whether to draw connection weights symbols
Returns:

The graphviz.Digraph for the given expression

Raises:
  • ImportError – if module pygraphviz is not installed in your Python system
  • ValueError – if in_sym is nonempty but its length does not match the number of inputs

Examples

>>> ex = dcgpy.expression_double(2,1,3,3,2,2,dcgpy.kernel_set_double(["sum","diff"])(),0)
>>> graph = ex.visualize(['x', 'c'], True, False)

expression_weighted_double

class dcgpy.expression_weighted_double(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)

Constructs a CGP expression operating on double

Parameters:
  • inputs (int) – number of inputs
  • outputs (int) – number of outputs
  • rows (int) – number of rows in the cartesian program
  • columns (int) – number of columns in the cartesian program
  • levels_back (int) – number of levels-back in the cartesian program
  • arity (int on list) – arity of the kernels. Assumed equal for all columns unless its specified by a list. The list must contain a number of entries equal to the number of columns.
  • kernels (List[dcgpy.kernel_double]) – kernel functions
  • seed (int) – random seed to generate mutations and chromosomes

Examples:

>>> from dcgpy import *
>>> dcgp = expression_double(1,1,1,10,11,2,kernel_set(["sum","diff","mul","div"])(), 32u)
>>> print(dcgp)
...
>>> num_out = dcgp([in])
>>> sym_out = dcgp(["x"])
get_weight(node_id, input_id)

Gets a weight.

Note

Convention adopted for node numbering: http://ppsn2014.ijs.si/files/slides/ppsn2014-tutorial3-miller.pdf

Parameters:
  • node_id (an int) – the id of the node
  • input_id (an int) – the id of the node input (0 for the first one up to arity-1)
Returns:

The value of the weight (a float)

Raises:

ValueError – if node_id or input_id are not valid

get_weights()

Gets all weights

set_weight(node_id, input_id, weight)

Sets a weight.

Note

Convention adopted for node numbering: http://ppsn2014.ijs.si/files/slides/ppsn2014-tutorial3-miller.pdf

Parameters:
  • node_id (an int) – the id of the node whose weight is being set
  • input_id (an int) – the id of the node input (0 for the first one up to arity-1)
  • weight (a float) – the new value of the weight
Raises:

ValueError – if node_id or input_id are not valid

set_weights(weights)

Sets all weights.

Parameters:weights (a List[float]) – the new values of the weights
Raises:ValueError – if the input vector dimension is not valid (r*c*arity)

expression_weighted_gdual_double

class dcgpy.expression_weighted_gdual_double(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)

Constructs a CGP expression operating on gdual_double

Parameters:
  • inputs (int) – number of inputs
  • outputs (int) – number of outputs
  • rows (int) – number of rows in the cartesian program
  • columns (int) – number of columns in the cartesian program
  • levels_back (int) – number of levels-back in the cartesian program
  • arity (int on list) – arity of the kernels. Assumed equal for all columns unless its specified by a list. The list must contain a number of entries equal to the number of columns.
  • kernels (List[dcgpy.kernel_gdual_double]) – kernel functions
  • seed (int) – random seed to generate mutations and chromosomes

Examples:

>>> from dcgpy import *
>>> dcgp = expression_gdual_double(1,1,1,10,11,2,kernel_set(["sum","diff","mul","div"])(), 32u)
>>> print(dcgp)
...
>>> num_out = dcgp([in])
>>> sym_out = dcgp(["x"])
get_weight(node_id, input_id)

Gets a weight.

Note

Convention adopted for node numbering: http://ppsn2014.ijs.si/files/slides/ppsn2014-tutorial3-miller.pdf

Parameters:
  • node_id (an int) – the id of the node
  • input_id (an int) – the id of the node input (0 for the first one up to arity-1)
Returns:

The value of the weight (a float)

Raises:

ValueError – if node_id or input_id are not valid

get_weights()

Gets all weights

set_weight(node_id, input_id, weight)

Sets a weight.

Note

Convention adopted for node numbering: http://ppsn2014.ijs.si/files/slides/ppsn2014-tutorial3-miller.pdf

Parameters:
  • node_id (an int) – the id of the node whose weight is being set
  • input_id (an int) – the id of the node input (0 for the first one up to arity-1)
  • weight (a float) – the new value of the weight
Raises:

ValueError – if node_id or input_id are not valid

set_weights(weights)

Sets all weights.

Parameters:weights (a List[float]) – the new values of the weights
Raises:ValueError – if the input vector dimension is not valid (r*c*arity)

expression_weighted_gdual_vdouble

class dcgpy.expression_weighted_gdual_vdouble(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)

Constructs a CGP expression operating on gdual_vdouble

Parameters:
  • inputs (int) – number of inputs
  • outputs (int) – number of outputs
  • rows (int) – number of rows in the cartesian program
  • columns (int) – number of columns in the cartesian program
  • levels_back (int) – number of levels-back in the cartesian program
  • arity (int on list) – arity of the kernels. Assumed equal for all columns unless its specified by a list. The list must contain a number of entries equal to the number of columns.
  • kernels (List[dcgpy.kernel_gdual_vdouble]) – kernel functions
  • seed (int) – random seed to generate mutations and chromosomes

Examples:

>>> from dcgpy import *
>>> dcgp = expression_gdual_vdouble(1,1,1,10,11,2,kernel_set(["sum","diff","mul","div"])(), 32u)
>>> print(dcgp)
...
>>> num_out = dcgp([in])
>>> sym_out = dcgp(["x"])
get_weight(node_id, input_id)

Gets a weight.

Note

Convention adopted for node numbering: http://ppsn2014.ijs.si/files/slides/ppsn2014-tutorial3-miller.pdf

Parameters:
  • node_id (an int) – the id of the node
  • input_id (an int) – the id of the node input (0 for the first one up to arity-1)
Returns:

The value of the weight (a float)

Raises:

ValueError – if node_id or input_id are not valid

get_weights()

Gets all weights

set_weight(node_id, input_id, weight)

Sets a weight.

Note

Convention adopted for node numbering: http://ppsn2014.ijs.si/files/slides/ppsn2014-tutorial3-miller.pdf

Parameters:
  • node_id (an int) – the id of the node whose weight is being set
  • input_id (an int) – the id of the node input (0 for the first one up to arity-1)
  • weight (a float) – the new value of the weight
Raises:

ValueError – if node_id or input_id are not valid

set_weights(weights)

Sets all weights.

Parameters:weights (a List[float]) – the new values of the weights
Raises:ValueError – if the input vector dimension is not valid (r*c*arity)

expression_ann_double

Important

This Cartesian Genetic Program is able to encode an Artificial Neural Network. Extra methods allow to perform backpropagation, to visualize the network and more …

class dcgpy.expression_ann_double(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)

Constructs a CGP expression operating on double

Parameters:
  • inputs (int) – number of inputs
  • outputs (int) – number of outputs
  • rows (int) – number of rows in the cartesian program
  • columns (int) – number of columns in the cartesian program
  • levels_back (int) – number of levels-back in the cartesian program
  • arity (int on list) – arity of the kernels. Assumed equal for all columns unless its specified by a list. The list must contain a number of entries equal to the number of columns.
  • kernels (List[dcgpy.kernel_double]) – kernel functions
  • seed (int) – random seed to generate mutations and chromosomes

Examples:

>>> from dcgpy import *
>>> dcgp = expression_double(1,1,1,10,11,2,kernel_set(["sum","diff","mul","div"])(), 32u)
>>> print(dcgp)
...
>>> num_out = dcgp([in])
>>> sym_out = dcgp(["x"])
get_bias(node_id)

Gets a bias.

Note

Convention adopted for node numbering: http://ppsn2014.ijs.si/files/slides/ppsn2014-tutorial3-miller.pdf

Parameters:node_id (an int) – the id of the node
Returns:The value of the bias (a float)
Raises:ValueError – if node_id is not valid
get_biases()

Gets all biases

get_weight(node_id, input_id)

get_weight(idx)

Gets a weight. Two overloads are available. You can get the weight specifying the node and the input id (that needs to be less than the arity), or directly specifying its position in the weight vector.

Note

Convention adopted for node numbering: http://ppsn2014.ijs.si/files/slides/ppsn2014-tutorial3-miller.pdf

Parameters:
  • node_id (an int) – the id of the node
  • input_id (an int) – the id of the node input (0 for the first one up to arity-1)
Returns:

The value of the weight (a float)

Raises:

ValueError – if node_id or input_id or idx are not valid

get_weights()

Gets all weights

n_active_weights(unique = False)

Computes the number of weights influencing the result. This will also be the number of weights that are updated when calling sgd. The number of active weights, as well as the number of active nodes, define the complexity of the expression expressed by the chromosome.

Parameters:unique (a bool) – when True weights are counted only once if connecting the same two nodes.
randomise_biases(mean = 0, std = 0.1, seed = randomint)

Randomises all the values for the biases using a normal distribution.

Parameters:
  • mean (a float) – the mean of the normal distribution.
  • std (a float) – the standard deviation of the normal distribution.
  • seed (an int) – the random seed to use.
randomise_weights(mean = 0, std = 0.1, seed = randomint)

Randomises all the values for the weights using a normal distribution.

Parameters:
  • mean (a float) – the mean of the normal distribution.
  • std (a float) – the standard deviation of the normal distribution.
  • seed (an int) – the random seed to use.
set_bias(node_id, bias)

Sets a bias.

Note

Convention adopted for node numbering: http://ppsn2014.ijs.si/files/slides/ppsn2014-tutorial3-miller.pdf

Parameters:
  • node_id (an int) – the id of the node whose weight is being set
  • weight (a float) – the new value of the weight
Raises:

ValueError – if node_id is not valid

set_biases(biases)

Sets all biases.

Parameters:biases (a List[float]) – the new values of the biases
Raises:ValueError – if the input vector dimension is not valid (r*c)
set_output_f(name)

Sets the nonlinearities of all nodes connected to the output nodes. This is useful when, for example, the dCGPANN is used for a regression task where output values are expected in [-1 1] and hence the output layer should have some sigmoid or tanh nonlinearity, or in a classification task when one wants to have a softmax layer by having a sum in all output neurons.

Parameters:name (a string) – the kernel name
Raises:ValueError – if name is not one of the kernels in the expression.
set_weight(node_id, input_id, weight)

set_weight(idx, weight)

Sets a weight. Two overloads are available. You can set the weight specifying the node and the input id (that needs to be less than the arity), or directly specifying its position in the weight vector.

Note

Convention adopted for node numbering: http://ppsn2014.ijs.si/files/slides/ppsn2014-tutorial3-miller.pdf

Parameters:
  • node_id (an int) – the id of the node whose weight is being set
  • input_id (an int) – the id of the node input (0 for the first one up to arity-1)
  • weight (a float) – the new value of the weight
  • idx (an int) – the idx of weight to be set
Raises:

ValueError – if node_id or input_id or idx are not valid

set_weights(weights)

Sets all weights.

Parameters:weights (a List[float]) – the new values of the weights
Raises:ValueError – if the input vector dimension is not valid (r*c*arity)
sgd(points, labels, lr, batch_size, loss_type, parallel = 0, shuffle = True)

Performs one epoch of mini-batch (stochastic) gradient descent updating the weights and biases using the points and labels to decrease the loss.

Parameters:
  • points (2D NumPy float array or list of lists of float) – the input data
  • labels (2D NumPy float array or list of lists of float) – the output labels (supervised signal)
  • lr (a float) – the learning generate
  • batch_size (an int) – the batch size
  • loss_type (a str) – the loss, one of “MSE” for Mean Square Error and “CE” for Cross-Entropy.
  • parallel (a int) – sets the grain for parallelism. 0 -> no parallelism n -> divides the data into n parts and processes them in parallel threads
  • shuffle (a bool) – when True it shuffles the points and labels before performing one epoch of training.
Returns:

this is only a proxy for the real loss on the whole data set.

Return type:

The average error across the batches a (float). Note

Raises:

ValueError – if points or labels are malformed or if loss_type is not one of the available types.

visualize(show_connections = True, fill_color = 'w', show_nonlinearities = False, active_connection_alpha = 0.1, inactive_connection_alpha = 0.01, legend = True)

Visualizes the dCGPANN expression

Parameters:
  • show_connections (bool) – shows active connections between nodes
  • show_inactive (`bool`) – shows also inactive connections between nodes
  • active_connection_alpha (bool) – the alpha used to show active connections
  • inactive_connection_alpha (bool) – the alpha used to show inactive connections
  • fill_color (str or `RGB values`) – the fill color of all nodes
  • show_nonlinearities (bool) – color codes the nodes with the contained nonlinearity
  • legend (bool) – shows a legend (only when show_nonlinearities is also True)

Examples:

>>> from dcgpy import *
>>> nonlinearities = dcgpy.kernel_set_double(["sig", "ReLu", "tanh"])
>>> dcgpann = dcgpy.expression_ann_double(inputs=3, outputs=4, rows=15, cols=8,
                                  levels_back=2, arity=4, kernels=nonlinearities(), seed=32)
>>> dcgpann.randomise_weights()
>>> dcgpann.visualize(show_nonlinearities=False)

Non linearities

kernel_double

class dcgpy.kernel_double(callable_f, callable_s, name)

The function defining the generic CGP node

Constructs a kernel function from callables.

Parameters:
  • callable_f (callable - List[double] -> double) – a callable taking a list of double as inputs and returning a double (the value of the kernel function evaluated on the inputs)
  • callable_s (callable - List[string] -> string) – a callable taking a list of string as inputs and returning a string (the symbolic representation of the kernel function evaluated on the input symbols)
  • name (string) – name of the kernel

Examples:

>>> from dcgpy import *
>>> def my_sum(x):
...     return sum(x)
>>> def print_my_sum(x)
...     s = "+"
...     return "(" + s.join(x) + ") "
>>> my_kernel = kernel_double(my_sum, print_my_sum, "my_sum")

kernel_gdual_double

class dcgpy.kernel_gdual_double(callable_f, callable_s, name)

The function defining the generic CGP node

Constructs a kernel function from callables.

Parameters:
  • callable_f (callable - List[gdual_double] -> gdual_double) – a callable taking a list of gdual_double as inputs and returning a gdual_double (the value of the kernel function evaluated on the inputs)
  • callable_s (callable - List[string] -> string) – a callable taking a list of string as inputs and returning a string (the symbolic representation of the kernel function evaluated on the input symbols)
  • name (string) – name of the kernel

Examples:

>>> from dcgpy import *
>>> def my_sum(x):
...     return sum(x)
>>> def print_my_sum(x)
...     s = "+"
...     return "(" + s.join(x) + ") "
>>> my_kernel = kernel_gdual_double(my_sum, print_my_sum, "my_sum")

kernel_gdual_vdouble

class dcgpy.kernel_gdual_vdouble(callable_f, callable_s, name)

The function defining the generic CGP node

Constructs a kernel function from callables.

Parameters:
  • callable_f (callable - List[gdual_vdouble] -> gdual_vdouble) – a callable taking a list of gdual_vdouble as inputs and returning a gdual_vdouble (the value of the kernel function evaluated on the inputs)
  • callable_s (callable - List[string] -> string) – a callable taking a list of string as inputs and returning a string (the symbolic representation of the kernel function evaluated on the input symbols)
  • name (string) – name of the kernel

Examples:

>>> from dcgpy import *
>>> def my_sum(x):
...     return sum(x)
>>> def print_my_sum(x)
...     s = "+"
...     return "(" + s.join(x) + ") "
>>> my_kernel = kernel_gdual_vdouble(my_sum, print_my_sum, "my_sum")

kernel_set

For a list of the available kernels see kernels.

kernel_set_double

class dcgpy.kernel_set_double(kernels)

Helper to construct a set of kernel functions from their common name

Constructs a set of common kernel functions from their common name. The kernel functions can be then retrieved via the call operator.

Parameters:kernels (List[string]) – a list of strings indicating names of kernels to use. The following are available: “sum”, “diff”, “mul”, “div”, “sig”, “sin”, “log”, “exp”

Examples:

>>> from dcgpy import *
>>> kernels = kernel_set_double(["sum", "diff", "mul", "div"])
>>> kernels()[0](["x", "y"])
push_back()

push_back(kernel_name)

Adds one more kernel to the set by common name.

Parameters:kernel_name (string) – a string containing the kernel name

push_back(kernel)

Adds one more kernel to the set.

Parameters:kernel (dcgpy.kernel_double) – the kernel to add

kernel_set_gdual_double

class dcgpy.kernel_set_gdual_double(kernels)

Helper to construct a set of kernel functions from their common name

Constructs a set of common kernel functions from their common name. The kernel functions can be then retrieved via the call operator.

Parameters:kernels (List[string]) – a list of strings indicating names of kernels to use. The following are available: “sum”, “diff”, “mul”, “div”, “sig”, “sin”, “log”, “exp”

Examples:

>>> from dcgpy import *
>>> kernels = kernel_set_gdual_double(["sum", "diff", "mul", "div"])
>>> kernels()[0](["x", "y"])
push_back()

push_back(kernel_name)

Adds one more kernel to the set by common name.

Parameters:kernel_name (string) – a string containing the kernel name

push_back(kernel)

Adds one more kernel to the set.

Parameters:kernel (dcgpy.kernel_gdual_double) – the kernel to add

kernel_set_gdual_vdouble

class dcgpy.kernel_set_gdual_vdouble(kernels)

Helper to construct a set of kernel functions from their common name

Constructs a set of common kernel functions from their common name. The kernel functions can be then retrieved via the call operator.

Parameters:kernels (List[string]) – a list of strings indicating names of kernels to use. The following are available: “sum”, “diff”, “mul”, “div”, “sig”, “sin”, “log”, “exp”

Examples:

>>> from dcgpy import *
>>> kernels = kernel_set_gdual_vdouble(["sum", "diff", "mul", "div"])
>>> kernels()[0](["x", "y"])
push_back()

push_back(kernel_name)

Adds one more kernel to the set by common name.

Parameters:kernel_name (string) – a string containing the kernel name

push_back(kernel)

Adds one more kernel to the set.

Parameters:kernel (dcgpy.kernel_gdual_vdouble) – the kernel to add