Util
Class GenotypeHelper

java.lang.Object
  extended by Util.GenotypeHelper

public class GenotypeHelper
extends java.lang.Object

A collection of useful functions for manipulating genotype and such

Author:
eliott bartley

Method Summary
static DerivationTree buildDerivationTree(Individual individual)
          Build a derivation tree for a given individual
static DerivationTree buildDerivationTree(Individual individual, DerivationNode node)
          Build a derivation tree rooted at the specified node for a given individual.
static DerivationTree buildInvalidTree(Individual individual)
          Build a derivation tree for a given individual
static DerivationTree buildInvalidTree(Individual individual, DerivationNode node)
          Build derivation trees for invalid individuals for repair operators.
static int calcNodeLength(DerivationNode nodes)
          Calculate how may codons are used to build a specified branch of a tree.
static boolean derivationTreesEqual(DerivationTree t1, DerivationTree t2)
          Compare the derivation trees for equality.
static Individual extendChromeAtRecursiveNode(Individual indiv, GEChromosome giver, Symbol giverSym, int mutatePoint)
           
static int extendRecursiveNode(int codonToChange, Individual indiv, Symbol childRootSym)
          This method takes the codon that's going to change and determines how it needs to change in order to map to the correct production.
static int findLastRecIndex(DerivationNode dNode, GEGrammar gram)
          This method takes a derivation node and a grammar and returns the codon index of the last recurisve node in the derivation tree.
static int findLastRecIndex(Individual indiv)
          This method takes and individual and returns the codon index of the last recurisve node in the derivation tree.
static DerivationNode findNodeFromCodonIndex(DerivationTree tree, int codonIndex)
          Given a derivation tree and a codon index, return the tree node that makes use of the codon at that index. e.g., if codonIndex was 0, the root branch decides which production is picked based on the value of the codon at index 0, so the root is returned in this case
static DerivationNode findNodeFromCodonIndex(DerivationTree tree, int codonIndex, int usedGenes)
           
static java.util.ArrayList<java.lang.Integer> getDerivationTreeIndeces(DerivationTree dT)
           
static int getMaxDTIndex(DerivationTree dT)
          Method to return the maximum codon index value in a derivation tree
static Phenotype getNodePhenotype(DerivationNode node)
           
static int[] getRecursiveNodeIndeces(DerivationTree dt, GEGrammar gram)
          Return an array with all the indeces of recursive derivation tree nodes.
static GEChromosome makeNewChromosome(Individual i1, int point, int length, Individual i2, DerivationTree tree)
          Given two individuals, whose chromosomes are split into three codon groups running from [0..point1), [point1..point1+length), and [point1+length..usedGenes), named head, body, and tail respectively, return a chromosome with a head and tail made up of individual-one's chromosome, and all of individual-two, i.e., head(i1)+all(i2)+tail(i1).
static GEChromosome makeNewChromosome(Individual i1, int point1, int length1, Individual i2, int point2, int length2)
          Given two individuals, whoses chromosomes are split into three codon groups running from [0..point1), [point1..point1+length), and [point1+length..usedGenes), named head, body, and tail respectively, return a chromosome with a head and tail made up of individual-one's chromosome, and individual-two's body, i.e., head(i1)+body(i2)+tail(i1).
static GEChromosome makeNewChromosome(Individual i1, int point1, int length1, Individual i2, int point2, int length2, DerivationTree tree)
           
static int nodesDifferentAtIndex(DerivationNode n1, DerivationNode n2)
          This method is used to compare two different derivation nodes to see at which index in the tree, they differ.
static int numRecNodes(DerivationTree tree, GEGrammar gram)
          This method takes a derivation tree and a grammar and returns the number of recursive nodes in that derivation tree.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

buildDerivationTree

public static DerivationTree buildDerivationTree(Individual individual,
                                                 DerivationNode node)
Build a derivation tree rooted at the specified node for a given individual. If node is null, the tree's root is used

Returns:
null if the tree is invalid

buildInvalidTree

public static DerivationTree buildInvalidTree(Individual individual,
                                              DerivationNode node)
Build derivation trees for invalid individuals for repair operators.

Parameters:
individual - Individual
Returns:
DerivationTree The valid or invalid derivation tree

buildInvalidTree

public static DerivationTree buildInvalidTree(Individual individual)
Build a derivation tree for a given individual

Returns:
invalid or valid derivation tree

buildDerivationTree

public static DerivationTree buildDerivationTree(Individual individual)
Build a derivation tree for a given individual

Returns:
null if the tree is invalid

findNodeFromCodonIndex

public static DerivationNode findNodeFromCodonIndex(DerivationTree tree,
                                                    int codonIndex)
Given a derivation tree and a codon index, return the tree node that makes use of the codon at that index. e.g., if codonIndex was 0, the root branch decides which production is picked based on the value of the codon at index 0, so the root is returned in this case


findNodeFromCodonIndex

public static DerivationNode findNodeFromCodonIndex(DerivationTree tree,
                                                    int codonIndex,
                                                    int usedGenes)

calcNodeLength

public static int calcNodeLength(DerivationNode nodes)
Calculate how may codons are used to build a specified branch of a tree. This works by searching the tree itself, and counting up which branches have a codon related to them. When a branch uses a codon, it gets the index of that codon attached to it by giving it a 'getCodonIndex' value other that -1


makeNewChromosome

public static GEChromosome makeNewChromosome(Individual i1,
                                             int point1,
                                             int length1,
                                             Individual i2,
                                             int point2,
                                             int length2)
Given two individuals, whoses chromosomes are split into three codon groups running from [0..point1), [point1..point1+length), and [point1+length..usedGenes), named head, body, and tail respectively, return a chromosome with a head and tail made up of individual-one's chromosome, and individual-two's body, i.e., head(i1)+body(i2)+tail(i1). The returned chromosome will be unwrapped and therefor may be larger than either or both the two individuals.

Parameters:
i1 - The individual whose genotype will be the head and tail of the new chromosome
point1 - The index of the body (excluded from new chromosome)
length1 - The length of the body (excluded from new chromosome)
i2 - The individual whose genotype will be the body of the new chromosome
point2 - The index of the body (included from new chromosome)
length2 - The length of the body (included from new chromosome)
Returns:
A new chromosome made up of i1's head, i2's body, and i1's tail

makeNewChromosome

public static GEChromosome makeNewChromosome(Individual i1,
                                             int point1,
                                             int length1,
                                             Individual i2,
                                             int point2,
                                             int length2,
                                             DerivationTree tree)

makeNewChromosome

public static GEChromosome makeNewChromosome(Individual i1,
                                             int point,
                                             int length,
                                             Individual i2,
                                             DerivationTree tree)
Given two individuals, whose chromosomes are split into three codon groups running from [0..point1), [point1..point1+length), and [point1+length..usedGenes), named head, body, and tail respectively, return a chromosome with a head and tail made up of individual-one's chromosome, and all of individual-two, i.e., head(i1)+all(i2)+tail(i1). The returned chromosome will be unwrapped and therefor may be larger than either or both the two individuals.

Parameters:
i1 - The individual whose genotype will be the head and tail of the new chromosome
point - The index of the body (excluded from new chromosome)
length - The length of the body (excluded from new chromosome)
i2 - The individual whose genotype will be the body of the new chromosome
Returns:
A new chromosome made up of i1's head, all of i2, and i1's tail

getMaxDTIndex

public static int getMaxDTIndex(DerivationTree dT)
Method to return the maximum codon index value in a derivation tree


derivationTreesEqual

public static boolean derivationTreesEqual(DerivationTree t1,
                                           DerivationTree t2)
Compare the derivation trees for equality. I couldn't find a method for this anywhere so I wrote on here. This is also probably not the most efficient way of doing this. Maybe there should be a .equals() method in DerivationNode or DerivationTree?

Returns:
boolean Are the trees equal?

getRecursiveNodeIndeces

public static int[] getRecursiveNodeIndeces(DerivationTree dt,
                                            GEGrammar gram)
Return an array with all the indeces of recursive derivation tree nodes.

Returns:
int[] The indeces of the recursive nodes

extendChromeAtRecursiveNode

public static Individual extendChromeAtRecursiveNode(Individual indiv,
                                                     GEChromosome giver,
                                                     Symbol giverSym,
                                                     int mutatePoint)
Parameters:
indiv - Individual to extend
giver - GEChromosome used to extend the indiv
giverSym - Symbol of the node in the derivation tree that marks the beginning of the chromosome being given
mutatePoint - Point on indiv's chromosome that is getting the new information
Returns:
Individual Updated individual

findLastRecIndex

public static int findLastRecIndex(Individual indiv)
This method takes and individual and returns the codon index of the last recurisve node in the derivation tree.

Parameters:
indiv - The individual to search
Returns:
int Index of the last recursive derivation tree node

findLastRecIndex

public static int findLastRecIndex(DerivationNode dNode,
                                   GEGrammar gram)
This method takes a derivation node and a grammar and returns the codon index of the last recurisve node in the derivation tree. There may be recursive nodes after the returned index, but they will be unmapped and have no index to return since they didn't use a codon.

Parameters:
dNode - The derivation node to search
gram - The grammar to use for information about the rules
Returns:
int Index of the last recursive derivation tree node

extendRecursiveNode

public static int extendRecursiveNode(int codonToChange,
                                      Individual indiv,
                                      Symbol childRootSym)
This method takes the codon that's going to change and determines how it needs to change in order to map to the correct production.

Parameters:
codonToChange - The original codon value
indiv - The parent individual that will be the basis for remapping
childRootSym - The root symbol of the child that needs to be picked up
Returns:
int The new value of the codon so it will map to a recursive production

numRecNodes

public static int numRecNodes(DerivationTree tree,
                              GEGrammar gram)
This method takes a derivation tree and a grammar and returns the number of recursive nodes in that derivation tree.

Parameters:
tree - Derivation tree to search
gram - Grammar corresponding to the derivation tree
Returns:
int Number of recursive nodes in the derivation tree

nodesDifferentAtIndex

public static int nodesDifferentAtIndex(DerivationNode n1,
                                        DerivationNode n2)
This method is used to compare two different derivation nodes to see at which index in the tree, they differ. Returns the index of the derivation node in n1 where the nodes are different and -1 if the nodes are equal.


getDerivationTreeIndeces

public static java.util.ArrayList<java.lang.Integer> getDerivationTreeIndeces(DerivationTree dT)

getNodePhenotype

public static Phenotype getNodePhenotype(DerivationNode node)