This is the rooted tree representation, this chromosome representation can carry any data-type.
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.
The GTree class - The tree chromosome representation
Inheritance diagram for GTree.GTree:
| Parameters: | root_node – the root node of the tree |
|---|
Return a new instance of the genome
| Return type: | new GTree instance |
|---|
Copy the contents to the destination g
| Parameters: | g – the GTree genome destination |
|---|
This is the reproduction function slot, the crossover. You can change the default crossover method using:
genome.crossover.set(Crossovers.G1DListCrossoverUniform)
Called to evaluate genome
| Parameters: | args – this parameters will be passes to the evaluator |
|---|
This is the evaluation function slot, you can add a function with the set method:
genome.evaluator.set(eval_func)
Return a new list with all nodes
| Return type: | the list with all nodes |
|---|
Get the Fitness Score of the genome
| Return type: | genome fitness score |
|---|
Return the tree height
| Return type: | the tree height |
|---|
Returns the depth of a node
| Return type: | the depth of the node, the depth of root node is 0 |
|---|
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 |
|---|
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 |
|---|
Gets an internal parameter
>>> genome.getParam("rangemax")
100
Note
All the individuals of the population shares this parameters and uses the same instance of this dict.
| Parameters: |
|
|---|
Returns a random node from the Tree
| Parameters: | node_type – 0 = Any, 1 = Leaf, 2 = Branch |
|---|---|
| Return type: | random node |
Get the Raw Score of the genome
| Return type: | genome raw score |
|---|
Return the tree root node
| Return type: | the tree root node |
|---|
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 |
|---|
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.
Called to initialize genome
| Parameters: | args – this parameters will be passed to the initializator |
|---|
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 |
This is the mutator function slot, you can change the default mutator using the slot set function:
genome.mutator.set(Mutators.G1DListMutatorSwap)
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.
Clear score and fitness of genome
Set the internal params
>>> 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 |
|---|
Sets the root of the tree
| Parameters: | root – the tree root node |
|---|
Traversal the tree, this method will call the user-defined callback function for each node on the tree
| Parameters: |
|
|---|
The GTreeGP Class - The Genetic Programming Tree representation
Inheritance diagram for GTree.GTreeGP:
| Parameters: | root_node – the Root node of the GP Tree |
|---|
Return a new instance of the genome
| Return type: | the new GTreeGP instance |
|---|
This method will compare the currently tree with another one
| Parameters: | other – the other GTreeGP to compare |
|---|
Copy the contents to the destination g
| Parameters: | g – the GTreeGP genome destination |
|---|
Called to evaluate genome
| Parameters: | args – this parameters will be passes to the evaluator |
|---|
Return a new list with all nodes
| Return type: | the list with all nodes |
|---|
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 |
|---|
Get the Fitness Score of the genome
| Return type: | genome fitness score |
|---|
Return the tree height
| Return type: | the tree height |
|---|
Returns the depth of a node
| Return type: | the depth of the node, the depth of root node is 0 |
|---|
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 |
|---|
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 |
|---|
Gets an internal parameter
>>> genome.getParam("rangemax")
100
Note
All the individuals of the population shares this parameters and uses the same instance of this dict.
| Parameters: |
|
|---|
Return the pre order expression string of the Tree, used to python eval.
| Return type: | the expression string |
|---|
Returns a random node from the Tree
| Parameters: | node_type – 0 = Any, 1 = Leaf, 2 = Branch |
|---|---|
| Return type: | random node |
Get the Raw Score of the genome
| Return type: | genome raw score |
|---|
Return the tree root node
| Return type: | the tree root node |
|---|
Returns a tree-formated string (s-expression) of the tree.
| Return type: | a S-Expression representing the tree |
|---|
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 |
|---|
Called to initialize genome
| Parameters: | args – this parameters will be passed to the initializator |
|---|
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 |
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.
Clear score and fitness of genome
Set the internal params
>>> 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 |
|---|
Sets the root of the tree
| Parameters: | root – the tree root node |
|---|
Traversal the tree, this method will call the user-defined callback function for each node on the tree
| Parameters: |
|
|---|
Write a graph to the pydot Graph instance
| Parameters: |
|
|---|
Writes a image representation of the individual
| Parameters: | filename – the output file image |
|---|
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 |
|---|
Writes to a graphical file using pydot, the population of trees
>>> 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: |
|
|---|
Writes to a raw dot file using pydot, the population of trees
>>> 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: |
|
|---|
The GTreeNode class - The node representation
Inheritance diagram for GTree.GTreeNode:
| Parameters: |
|
|---|
Adds a child to the node
| Parameters: | child – the node to be added |
|---|
Return a new instance of the genome
| Return type: | new GTree instance |
|---|
Copy the contents to the destination g
| Parameters: | g – the GTreeNode genome destination |
|---|
Returns the index-child of the node
| Return type: | child node |
|---|
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 |
|---|
Return the data of the node
| Return type: | the data of the node |
|---|
Get the parent node of the node
| Return type: | the parent node |
|---|
Return True if the node is a leaf
| Return type: | True or False |
|---|
Created a new child node
| Parameters: | data – the data of the new created node |
|---|
Replaces a child of the node
| Parameters: |
|
|---|
Sets the data of the node
| Parameters: | data – the data of the node |
|---|
Sets the parent of the node
| Parameters: | parent – the parent node |
|---|
Swaps the node data with another node
| Parameters: | node – the node to do the data swap |
|---|
The GTreeNodeGP Class - The Genetic Programming Node representation
Inheritance diagram for GTree.GTreeNodeGP:
| Parameters: |
|
|---|
Adds a child to the node
| Parameters: | child – the node to be added |
|---|
Return a new copy of the node
| Return type: | the new GTreeNodeGP instance |
|---|
Compare this node with other
| Parameters: | other – the other GTreeNodeGP |
|---|
Copy the contents to the destination g
| Parameters: | g – the GTreeNodeGP genome destination |
|---|
Returns the index-child of the node
| Return type: | child node |
|---|
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 |
|---|
Gets the node internal data
| Return type: | the internal data |
|---|
Get the parent node of the node
| Return type: | the parent node |
|---|
Get the node type
| Return type: | the node type is type of Consts.nodeType |
|---|
Return True if the node is a leaf
| Return type: | True or False |
|---|
Creates a new node and adds this node as children of current node
| Parameters: | data – the internal node data |
|---|
Replaces a child of the node
| Parameters: |
|
|---|
Sets the node internal data
| Parameters: | data – the internal data |
|---|
Sets the parent of the node
| Parameters: | parent – the parent node |
|---|
Sets the node type
| Parameters: | node_type – the node type is type of Consts.nodeType |
|---|
Swaps the node data and type with another node
| Parameters: | node – the node |
|---|
Random generates a Tree structure using the value_callback for data generation and the method “Full”
| Parameters: |
|
|---|---|
| Return type: | the root node of created tree |
Creates a new random GTreeGP root node with subtrees using the “Full” method.
| Parameters: |
|
|---|---|
| Max_depth : | the maximum depth of the tree |
| Return type: | the root node |
Creates a new random GTreeGP root node with subtrees using the “Grow” method.
| Parameters: |
|
|---|---|
| Max_depth : | the maximum depth of the tree |
| Return type: | the root node |
Random generates a Tree structure using the value_callback for data generation and the method “Grow”
| Parameters: |
|
|---|---|
| Return type: | the root node of created tree |
Do some check on the terminal, to evaluate ephemeral constants
| Parameters: | terminal – the terminal string |
|---|
This is a decorator to use with genetic programming non-terminals
It currently accepts the attributes: shape, color and representation.