Python Documentation

Classes

expression

expression_double

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

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) – arity of the kernels
  • 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()

Gets the arity of the basis functions of the dCGP expression

get_cols()

Gets the number of columns of the dCGP expression

get_f()

Gets the kernel functions

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

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)

Mutates N randomly selected active genes within its allowed bounds

mutate_active_cgene()

Mutates exactly one randomly selected active connection genes within its allowed bounds

mutate_active_fgene()

Mutates exactly one randomly selected active function genes within its allowed bounds

mutate_ogene()

Mutates exactly one randomly selected output genes within its allowed bounds

mutate_random(N)

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.)
simplify(in_sym, subs_weights = False, erc = [])

Simplifies the d-CGP expressions for the outputs

Returns the simplified d-CGP 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, file_name = 'cgp_graph.png')

Visualizes the d-CGP expression

Visualizes the graph of the d-CGP expression, by generating a png image in the current directory and displaying it on Matplotlib axes

Note

This method requires matplotlib and pygraphviz modules 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
  • file_name (a str) – filename of the output image
Returns:

The matplotlib.image.AxesImage of the displayed graph

Raises:
  • ImportError – if modules matplotlib or pygraphviz are 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)
>>> img = ex.visualize(['x', 'c'], True, False, 'out_img.png')

expression_gdual_double

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

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) – arity of the kernels
  • 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()

Gets the arity of the basis functions of the dCGP expression

get_cols()

Gets the number of columns of the dCGP expression

get_f()

Gets the kernel functions

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

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)

Mutates N randomly selected active genes within its allowed bounds

mutate_active_cgene()

Mutates exactly one randomly selected active connection genes within its allowed bounds

mutate_active_fgene()

Mutates exactly one randomly selected active function genes within its allowed bounds

mutate_ogene()

Mutates exactly one randomly selected output genes within its allowed bounds

mutate_random(N)

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.)
simplify(in_sym, subs_weights = False, erc = [])

Simplifies the d-CGP expressions for the outputs

Returns the simplified d-CGP 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, file_name = 'cgp_graph.png')

Visualizes the d-CGP expression

Visualizes the graph of the d-CGP expression, by generating a png image in the current directory and displaying it on Matplotlib axes

Note

This method requires matplotlib and pygraphviz modules 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
  • file_name (a str) – filename of the output image
Returns:

The matplotlib.image.AxesImage of the displayed graph

Raises:
  • ImportError – if modules matplotlib or pygraphviz are 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)
>>> img = ex.visualize(['x', 'c'], True, False, 'out_img.png')

expression_gdual_vdouble

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

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) – arity of the kernels
  • 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()

Gets the arity of the basis functions of the dCGP expression

get_cols()

Gets the number of columns of the dCGP expression

get_f()

Gets the kernel functions

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

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)

Mutates N randomly selected active genes within its allowed bounds

mutate_active_cgene()

Mutates exactly one randomly selected active connection genes within its allowed bounds

mutate_active_fgene()

Mutates exactly one randomly selected active function genes within its allowed bounds

mutate_ogene()

Mutates exactly one randomly selected output genes within its allowed bounds

mutate_random(N)

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.)
simplify(in_sym, subs_weights = False, erc = [])

Simplifies the d-CGP expressions for the outputs

Returns the simplified d-CGP 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, file_name = 'cgp_graph.png')

Visualizes the d-CGP expression

Visualizes the graph of the d-CGP expression, by generating a png image in the current directory and displaying it on Matplotlib axes

Note

This method requires matplotlib and pygraphviz modules 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
  • file_name (a str) – filename of the output image
Returns:

The matplotlib.image.AxesImage of the displayed graph

Raises:
  • ImportError – if modules matplotlib or pygraphviz are 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)
>>> img = ex.visualize(['x', 'c'], True, False, 'out_img.png')

expression_weighted

expression_weighted_double

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

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) – arity of the kernels
  • 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 (a float) of the weight

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)

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) – arity of the kernels
  • 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 (a float) of the weight

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)

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) – arity of the kernels
  • 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 (a float) of the weight

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)

kernel

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 function 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 function 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 function name

push_back(kernel)

Adds one more kernel to the set.

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