com.epochx.representation
Class CandidateProgram<TYPE>

java.lang.Object
  extended by com.epochx.representation.CandidateProgram<TYPE>
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable<CandidateProgram<TYPE>>

public class CandidateProgram<TYPE>
extends java.lang.Object
implements java.lang.Cloneable, java.lang.Comparable<CandidateProgram<TYPE>>

A CandidateProgram encapsulates an individual program within a generation of a GP run.

Instances of CandidateProgram can be requested to evaluate themselves, which will trigger an evaluation of each Node and their child nodes recursively down the tree. As well as the program tree itself, each CandidateProgram allows the retrieval of meta-data about the program.


Constructor Summary
CandidateProgram(Node<TYPE> rootNode, GPModel<TYPE> model)
          Constructs a new program individual where rootNode is the top most node in the program, and which may have 0 or more child nodes.
 
Method Summary
 java.lang.Object clone()
          Creates and returns a copy of this program.
 int compareTo(CandidateProgram<TYPE> o)
          Compares this program to another based upon fitness.
 boolean equals(java.lang.Object obj)
          This equals method compares the given object to this CandidateProgram to determine if they are equal.
 TYPE evaluate()
          Evaluates the candidate program by triggering a recursive evaluation of the node tree from the root.
 double getFitness()
          Requests the controlling model to calculate the fitness of this CandidateProgram.
 java.util.List<Node<TYPE>> getNodesAtDepth(int depth)
          Retrieves all the nodes in the program tree at a specified depth.
 int getNoDistinctFunctions()
          Returns a count of how many unique function nodes are in the program tree.
 int getNoDistinctTerminals()
          Returns a count of how many unique terminal nodes are in the program tree.
 int getNoFunctions()
          Returns a count of how many function nodes are in the program tree.
 int getNoTerminals()
          Returns a count of how many terminal nodes are in the program tree.
 Node<TYPE> getNthNode(int n)
          Returns the nth node in the CandidateProgram.
 int getProgramDepth()
          Returns the depth of deepest node in the program tree.
 int getProgramLength()
          Returns the number of nodes in the program tree.
 Node<TYPE> getRootNode()
          Returns the root node of the node tree held by the candidate program.
 void setNthNode(int n, Node<TYPE> newNode)
          Replaces the node at the specified position in the CandidateProgram with the specified node.
 java.lang.String toString()
          Return a string representation of the program node tree.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CandidateProgram

public CandidateProgram(Node<TYPE> rootNode,
                        GPModel<TYPE> model)
Constructs a new program individual where rootNode is the top most node in the program, and which may have 0 or more child nodes.

Parameters:
rootNode - the Node of the program tree which is the parent to all other nodes. It may be either a FunctionNode or a TerminalNode
model - the controlling model which provides the configuration parameters for the run.
Method Detail

evaluate

public TYPE evaluate()
Evaluates the candidate program by triggering a recursive evaluation of the node tree from the root.

Returns:
The result of the evaluation of the program.

getRootNode

public Node<TYPE> getRootNode()
Returns the root node of the node tree held by the candidate program.

Returns:
The root node of the node tree.

getNthNode

public Node<TYPE> getNthNode(int n)
Returns the nth node in the CandidateProgram. The program tree is traversed in pre-order (depth-first), indexed from 0 so that the root node is at node 0.

Parameters:
n - The index of the node required. Indexes are from zero.
Returns:
The node at the specified index.

setNthNode

public void setNthNode(int n,
                       Node<TYPE> newNode)
Replaces the node at the specified position in the CandidateProgram with the specified node.

Parameters:
n - index of the node to replace.
newNode - node to be set at the specified position.

getNodesAtDepth

public java.util.List<Node<TYPE>> getNodesAtDepth(int depth)
Retrieves all the nodes in the program tree at a specified depth.

Parameters:
depth - the specified depth of the nodes.
Returns:
a List of all the nodes at the specified depth.

getFitness

public double getFitness()
Requests the controlling model to calculate the fitness of this CandidateProgram.

Returns:
the fitness of this candidate program according to the model.

getNoTerminals

public int getNoTerminals()
Returns a count of how many terminal nodes are in the program tree.

Returns:
the number of terminal nodes in this program.

getNoDistinctTerminals

public int getNoDistinctTerminals()
Returns a count of how many unique terminal nodes are in the program tree.

Returns:
the number of unique terminal nodes in this program.

getNoFunctions

public int getNoFunctions()
Returns a count of how many function nodes are in the program tree.

Returns:
the number of function nodes in this program.

getNoDistinctFunctions

public int getNoDistinctFunctions()
Returns a count of how many unique function nodes are in the program tree.

Returns:
the number of unique function nodes in this program.

getProgramDepth

public int getProgramDepth()
Returns the depth of deepest node in the program tree.

Returns:
the depth of the program.

getProgramLength

public int getProgramLength()
Returns the number of nodes in the program tree.

Returns:
the number of nodes in the program tree.

compareTo

public int compareTo(CandidateProgram<TYPE> o)
Compares this program to another based upon fitness. Returns a negative integer if this program has a larger (worse) fitness value, zero if they have equal fitnesses and a positive integer if this program has a smaller (better) fitness value. This is super expensive if using to sort a list. Might be possible to improve performance if we can implement caching of fitness within a CandidateProgram.

Specified by:
compareTo in interface java.lang.Comparable<CandidateProgram<TYPE>>
Parameters:
o - the CandidateProgram to be compared.
Returns:
a negative integer, zero, or a positive integer if this program has a worse, equal or better fitness respectively.

clone

public java.lang.Object clone()
Creates and returns a copy of this program. The clone includes a deep copy of all the program nodes, so after calling this method none of the clones nodes will refer to the same instance.

Overrides:
clone in class java.lang.Object
Returns:
a clone of this CandidateProgram instance.

toString

public java.lang.String toString()
Return a string representation of the program node tree.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this program instance.

equals

public boolean equals(java.lang.Object obj)
This equals method compares the given object to this CandidateProgram to determine if they are equal. Equivalence is defined by their both being instances of CandidateProgram and them having equal program node trees according to the equals methods of the root node.

Overrides:
equals in class java.lang.Object
Parameters:
obj - an object to be compared for equivalence.
Returns:
true if this CandidateProgram is the same as the obj argument; false otherwise.