GTree
and GTreeGP – the tree chromosomes¶
This is the rooted tree representation, this chromosome representation can carry any data-type.
Default Parameters¶
Initializator
Initializators.GTreeInitializatorInteger()
The Integer Initializator for GTree
Mutator
Mutators.GTreeMutatorIntegerRange()
The Integer Range mutator for GTree
Crossover
Crossovers.GTreeCrossoverSinglePointStrict()
The Strict Single Point crossover for GTree
New in version 0.6: The GTree module.
Classes¶
-
class
GTree.
GTree
(root_node=None)¶ The GTree class - The tree chromosome representation
Inheritance diagram for
GTree.GTree
:Parameters: root_node – the root node of the tree -
clone
()¶ Return a new instance of the genome
Return type: new GTree instance
-
copy
(g)¶ Copy the contents to the destination g
Parameters: g – the GTree genome destination
-
crossover
= None¶ This is the reproduction function slot, the crossover. You can change the default crossover method using:
genome.crossover.set(Crossovers.G1DListCrossoverUniform)
-
evaluate
(**args)¶ Called to evaluate genome
Parameters: args – this parameters will be passes to the evaluator
-
evaluator
= None¶ This is the evaluation function slot, you can add a function with the set method:
genome.evaluator.set(eval_func)
-
getAllNodes
()¶ Return a new list with all nodes
Return type: the list with all nodes
-
getFitnessScore
()¶ Get the Fitness Score of the genome
Return type: genome fitness score
-
getHeight
()¶ Return the tree height
Return type: the tree height
-
getNodeDepth
(node)¶ Returns the depth of a node
Return type: the depth of the node, the depth of root node is 0
-
getNodeHeight
(node)¶ Returns the height of a node
Note
If the node has no childs, the height will be 0.
Return type: the height of the node
-
getNodesCount
(start_node=None)¶ Return the number of the nodes on the tree starting at the start_node, if start_node is None, then the method will count all the tree nodes.
Return type: the number of nodes
-
getParam
(key, nvl=None)¶ Gets an internal parameter
- Example:
>>> genome.getParam("rangemax") 100
Note
All the individuals of the population shares this parameters and uses the same instance of this dict.
Parameters: - key – the key of param
- nvl – if the key doesn’t exist, the nvl will be returned
-
getRandomNode
(node_type=0)¶ Returns a random node from the Tree
Parameters: node_type – 0 = Any, 1 = Leaf, 2 = Branch Return type: random node
-
getRawScore
()¶ Get the Raw Score of the genome
Return type: genome raw score
-
getRoot
()¶ Return the tree root node
Return type: the tree root node
-
getTraversalString
(start_node=None, spc=0)¶ Returns a tree-formated string of the tree. This method is used by the __repr__ method of the tree
Return type: a string representing the tree
-
initializator
= None¶ This is the initialization function of the genome, you can change the default initializator using the function slot:
genome.initializator.set(Initializators.G1DListInitializatorAllele)
In this example, the initializator
Initializators.G1DListInitializatorAllele()
will be used to create the initial population.
-
initialize
(**args)¶ Called to initialize genome
Parameters: args – this parameters will be passed to the initializator
-
mutate
(**args)¶ Called to mutate the genome
Parameters: args – this parameters will be passed to the mutator Return type: the number of mutations returned by mutation operator
-
mutator
= None¶ This is the mutator function slot, you can change the default mutator using the slot set function:
genome.mutator.set(Mutators.G1DListMutatorSwap)
-
processNodes
(cloning=False)¶ Creates a cache on the tree, this method must be called every time you change the shape of the tree. It updates the internal nodes list and the internal nodes properties such as depth and height.
-
resetStats
()¶ Clear score and fitness of genome
-
setParams
(**args)¶ Set the internal params
- Example:
>>> genome.setParams(rangemin=0, rangemax=100, gauss_mu=0, gauss_sigma=1)
Note
All the individuals of the population shares this parameters and uses the same instance of this dict.
Parameters: args – this params will saved in every chromosome for genetic op. use
-
setRoot
(root)¶ Sets the root of the tree
Parameters: root – the tree root node
-
traversal
(callback, start_node=None)¶ Traversal the tree, this method will call the user-defined callback function for each node on the tree
Parameters: - callback – a function
- start_node – the start node to begin the traversal
-
-
class
GTree.
GTreeGP
(root_node=None, cloning=False)¶ The GTreeGP Class - The Genetic Programming Tree representation
Inheritance diagram for
GTree.GTreeGP
:Parameters: root_node – the Root node of the GP Tree -
clone
()¶ Return a new instance of the genome
Return type: the new GTreeGP instance
-
compare
(other)¶ This method will compare the currently tree with another one
Parameters: other – the other GTreeGP to compare
-
copy
(g)¶ Copy the contents to the destination g
Parameters: g – the GTreeGP genome destination
-
evaluate
(**args)¶ Called to evaluate genome
Parameters: args – this parameters will be passes to the evaluator
-
getAllNodes
()¶ Return a new list with all nodes
Return type: the list with all nodes
-
getCompiledCode
()¶ Get the compiled code for the Tree expression After getting the compiled code object, you just need to evaluate it using the
eval()
native Python method.Return type: compiled python code
-
getFitnessScore
()¶ Get the Fitness Score of the genome
Return type: genome fitness score
-
getHeight
()¶ Return the tree height
Return type: the tree height
-
getNodeDepth
(node)¶ Returns the depth of a node
Return type: the depth of the node, the depth of root node is 0
-
getNodeHeight
(node)¶ Returns the height of a node
Note
If the node has no childs, the height will be 0.
Return type: the height of the node
-
getNodesCount
(start_node=None)¶ Return the number of the nodes on the tree starting at the start_node, if start_node is None, then the method will count all the tree nodes.
Return type: the number of nodes
-
getParam
(key, nvl=None)¶ Gets an internal parameter
- Example:
>>> genome.getParam("rangemax") 100
Note
All the individuals of the population shares this parameters and uses the same instance of this dict.
Parameters: - key – the key of param
- nvl – if the key doesn’t exist, the nvl will be returned
-
getPreOrderExpression
(start_node=None)¶ Return the pre order expression string of the Tree, used to python eval.
Return type: the expression string
-
getRandomNode
(node_type=0)¶ Returns a random node from the Tree
Parameters: node_type – 0 = Any, 1 = Leaf, 2 = Branch Return type: random node
-
getRawScore
()¶ Get the Raw Score of the genome
Return type: genome raw score
-
getRoot
()¶ Return the tree root node
Return type: the tree root node
-
getSExpression
(start_node=None)¶ Returns a tree-formated string (s-expression) of the tree.
Return type: a S-Expression representing the tree
-
getTraversalString
(start_node=None, spc=0)¶ Returns a tree-formated string of the tree. This method is used by the __repr__ method of the tree
Return type: a string representing the tree
-
initialize
(**args)¶ Called to initialize genome
Parameters: args – this parameters will be passed to the initializator
-
mutate
(**args)¶ Called to mutate the genome
Parameters: args – this parameters will be passed to the mutator Return type: the number of mutations returned by mutation operator
-
processNodes
(cloning=False)¶ Creates a cache on the tree, this method must be called every time you change the shape of the tree. It updates the internal nodes list and the internal nodes properties such as depth and height.
-
resetStats
()¶ Clear score and fitness of genome
-
setParams
(**args)¶ Set the internal params
- Example:
>>> genome.setParams(rangemin=0, rangemax=100, gauss_mu=0, gauss_sigma=1)
Note
All the individuals of the population shares this parameters and uses the same instance of this dict.
Parameters: args – this params will saved in every chromosome for genetic op. use
-
setRoot
(root)¶ Sets the root of the tree
Parameters: root – the tree root node
-
traversal
(callback, start_node=None)¶ Traversal the tree, this method will call the user-defined callback function for each node on the tree
Parameters: - callback – a function
- start_node – the start node to begin the traversal
-
writeDotGraph
(graph, startNode=0)¶ Write a graph to the pydot Graph instance
Parameters: - graph – the pydot Graph instance
- startNode – used to plot more than one individual
-
writeDotImage
(filename)¶ Writes a image representation of the individual
Parameters: filename – the output file image
-
writeDotRaw
(filename)¶ Writes the raw dot file (text-file used by dot/neato) with the representation of the individual
Parameters: filename – the output file, ex: individual.dot
-
static
writePopulationDot
(ga_engine, filename, format='jpeg', start=0, end=0)¶ Writes to a graphical file using pydot, the population of trees
- Example:
>>> GTreeGP.writePopulationDot(ga_engine, "pop.jpg", "jpeg", 0, 10)
This example will draw the first ten individuals of the population into the file called “pop.jpg”.
Parameters: - ga_engine – the GA Engine
- filename – the filename, ie. population.jpg
- start – the start index of individuals
- end – the end index of individuals
-
static
writePopulationDotRaw
(ga_engine, filename, start=0, end=0)¶ Writes to a raw dot file using pydot, the population of trees
- Example:
>>> GTreeGP.writePopulationDotRaw(ga_engine, "pop.dot", 0, 10)
This example will draw the first ten individuals of the population into the file called “pop.dot”.
Parameters: - ga_engine – the GA Engine
- filename – the filename, ie. population.dot
- start – the start index of individuals
- end – the end index of individuals
-
-
class
GTree.
GTreeNode
(data, parent=None)¶ The GTreeNode class - The node representation
Inheritance diagram for
GTree.GTreeNode
:Parameters: - data – the root node of the tree
- parent – the parent node, if root, this must be None
-
addChild
(child)¶ Adds a child to the node
Parameters: child – the node to be added
-
clone
()¶ Return a new instance of the genome
Return type: new GTree instance
-
copy
(g)¶ Copy the contents to the destination g
Parameters: g – the GTreeNode genome destination
-
getChild
(index)¶ Returns the index-child of the node
Return type: child node
-
getChilds
()¶ Return the childs of the node
Warning
use .getChilds()[:] if you’ll change the list itself, like using childs.reverse(), otherwise the original genome child order will be changed.
Return type: a list of nodes
-
getData
()¶ Return the data of the node
Return type: the data of the node
-
getParent
()¶ Get the parent node of the node
Return type: the parent node
-
isLeaf
()¶ Return True if the node is a leaf
Return type: True or False
-
newNode
(data)¶ Created a new child node
Parameters: data – the data of the new created node
-
replaceChild
(older, newer)¶ Replaces a child of the node
Parameters: - older – the child to be replaces
- newer – the new child which replaces the older
-
setData
(data)¶ Sets the data of the node
Parameters: data – the data of the node
-
setParent
(parent)¶ Sets the parent of the node
Parameters: parent – the parent node
-
swapNodeData
(node)¶ Swaps the node data with another node
Parameters: node – the node to do the data swap
-
class
GTree.
GTreeNodeGP
(data, node_type=0, parent=None)¶ The GTreeNodeGP Class - The Genetic Programming Node representation
Inheritance diagram for
GTree.GTreeNodeGP
:Parameters: - data – the node data
- type – the node type
- parent – the node parent
-
addChild
(child)¶ Adds a child to the node
Parameters: child – the node to be added
-
clone
()¶ Return a new copy of the node
Return type: the new GTreeNodeGP instance
-
compare
(other)¶ Compare this node with other
Parameters: other – the other GTreeNodeGP
-
copy
(g)¶ Copy the contents to the destination g
Parameters: g – the GTreeNodeGP genome destination
-
getChild
(index)¶ Returns the index-child of the node
Return type: child node
-
getChilds
()¶ Return the childs of the node
Warning
use .getChilds()[:] if you’ll change the list itself, like using childs.reverse(), otherwise the original genome child order will be changed.
Return type: a list of nodes
-
getData
()¶ Gets the node internal data
Return type: the internal data
-
getParent
()¶ Get the parent node of the node
Return type: the parent node
-
getType
()¶ Get the node type
Return type: the node type is type of Consts.nodeType
-
isLeaf
()¶ Return True if the node is a leaf
Return type: True or False
-
newNode
(data)¶ Creates a new node and adds this node as children of current node
Parameters: data – the internal node data
-
replaceChild
(older, newer)¶ Replaces a child of the node
Parameters: - older – the child to be replaces
- newer – the new child which replaces the older
-
setData
(data)¶ Sets the node internal data
Parameters: data – the internal data
-
setParent
(parent)¶ Sets the parent of the node
Parameters: parent – the parent node
-
setType
(node_type)¶ Sets the node type
Parameters: node_type – the node type is type of Consts.nodeType
-
swapNodeData
(node)¶ Swaps the node data and type with another node
Parameters: node – the node
-
GTree.
buildGTreeFull
(depth, value_callback, max_siblings, max_depth)¶ Random generates a Tree structure using the value_callback for data generation and the method “Full”
Parameters: - depth – the initial depth, zero
- value_callback – the function which generates the random values for nodes
- max_siblings – the maximum number of sisters of a node
- max_depth – the maximum depth of the tree
Return type: the root node of created tree
-
GTree.
buildGTreeGPFull
(ga_engine, depth, max_depth)¶ Creates a new random GTreeGP root node with subtrees using the “Full” method.
Parameters: - ga_engine – the GA Core
- depth – the initial depth
Max_depth: the maximum depth of the tree
Return type: the root node
-
GTree.
buildGTreeGPGrow
(ga_engine, depth, max_depth)¶ Creates a new random GTreeGP root node with subtrees using the “Grow” method.
Parameters: - ga_engine – the GA Core
- depth – the initial depth
Max_depth: the maximum depth of the tree
Return type: the root node
-
GTree.
buildGTreeGrow
(depth, value_callback, max_siblings, max_depth)¶ Random generates a Tree structure using the value_callback for data generation and the method “Grow”
Parameters: - depth – the initial depth, zero
- value_callback – the function which generates the random values for nodes
- max_siblings – the maximum number of sisters of a node
- max_depth – the maximum depth of the tree
Return type: the root node of created tree
-
GTree.
checkTerminal
(terminal)¶ Do some check on the terminal, to evaluate ephemeral constants
Parameters: terminal – the terminal string
-
GTree.
gpdec
(**kwds)¶ This is a decorator to use with genetic programming non-terminals
It currently accepts the attributes: shape, color and representation.