dcgpy.
expression_double
(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)¶A CGP expression
Constructs a CGP expression operating on double
Parameters: 


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 levelsback 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: 


Raises: 

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, levelsback, etc.) 
set_f_gene
(node_id, f_id)¶Sets for a valid node (i.e. not an input node) a new kernel.
Parameters: 


Raises: 

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: 


Returns:  A 
Raises: 

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 dCGP expression Visualizes the graph of the dCGP expression .. note:: This method requires the graphviz` module installed in your Python system
Parameters: 


Returns:  The 
Raises: 

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)
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: 


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 levelsback 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: 


Raises: 

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, levelsback, etc.) 
set_f_gene
(node_id, f_id)¶Sets for a valid node (i.e. not an input node) a new kernel.
Parameters: 


Raises: 

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: 


Returns:  A 
Raises: 

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 dCGP expression Visualizes the graph of the dCGP expression .. note:: This method requires the graphviz` module installed in your Python system
Parameters: 


Returns:  The 
Raises: 

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)
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: 


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 levelsback 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: 


Raises: 

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, levelsback, etc.) 
set_f_gene
(node_id, f_id)¶Sets for a valid node (i.e. not an input node) a new kernel.
Parameters: 


Raises: 

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: 


Returns:  A 
Raises: 

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 dCGP expression Visualizes the graph of the dCGP expression .. note:: This method requires the graphviz` module installed in your Python system
Parameters: 


Returns:  The 
Raises: 

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)
dcgpy.
expression_weighted_double
(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)¶Constructs a CGP expression operating on double
Parameters: 


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/ppsn2014tutorial3miller.pdf
Parameters: 


Returns:  The value of the weight (a 
Raises: 

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/ppsn2014tutorial3miller.pdf
Parameters: 


Raises: 

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) 
dcgpy.
expression_weighted_gdual_double
(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)¶Constructs a CGP expression operating on gdual_double
Parameters: 


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/ppsn2014tutorial3miller.pdf
Parameters: 


Returns:  The value of the weight (a 
Raises: 

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/ppsn2014tutorial3miller.pdf
Parameters: 


Raises: 

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) 
dcgpy.
expression_weighted_gdual_vdouble
(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)¶Constructs a CGP expression operating on gdual_vdouble
Parameters: 


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/ppsn2014tutorial3miller.pdf
Parameters: 


Returns:  The value of the weight (a 
Raises: 

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/ppsn2014tutorial3miller.pdf
Parameters: 


Raises: 

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) 
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 …
dcgpy.
expression_ann_double
(inputs, outputs, rows, columns, levels_back, arity, kernels, seed = randint)¶Constructs a CGP expression operating on double
Parameters: 


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/ppsn2014tutorial3miller.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/ppsn2014tutorial3miller.pdf
Parameters: 


Returns:  The value of the weight (a 
Raises: 

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: 


randomise_weights
(mean = 0, std = 0.1, seed = randomint)¶Randomises all the values for the weights using a normal distribution.
Parameters: 


set_bias
(node_id, bias)¶Sets a bias.
Note
Convention adopted for node numbering: http://ppsn2014.ijs.si/files/slides/ppsn2014tutorial3miller.pdf
Parameters: 


Raises: 

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/ppsn2014tutorial3miller.pdf
Parameters: 


Raises: 

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 minibatch (stochastic) gradient descent updating the weights and biases using the points and labels to decrease the loss.
Parameters: 


Returns:  this is only a proxy for the real loss on the whole data set. 
Return type:  The average error across the batches a ( 
Raises: 

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: 


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)
dcgpy.
kernel_double
(callable_f, callable_s, name)¶The function defining the generic CGP node
Constructs a kernel function from callables.
Parameters: 


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")
dcgpy.
kernel_gdual_double
(callable_f, callable_s, name)¶The function defining the generic CGP node
Constructs a kernel function from callables.
Parameters: 


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")
dcgpy.
kernel_gdual_vdouble
(callable_f, callable_s, name)¶The function defining the generic CGP node
Constructs a kernel function from callables.
Parameters: 


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")
For a list of the available kernels see kernels.
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 

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 

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 
