Package org.moeaframework.util.tree
Class Rules
java.lang.Object
org.moeaframework.util.tree.Rules
- All Implemented Interfaces:
Serializable
The rules defining the program syntax. At a minimum, the rules must define the program return type and the set of
nodes which can appear in the program.
It is also possible to define program scaffolding, which defines the fixed initial structure of the program that is
not modified by any variation operators. For example, this can be used to define function prototypes (similar to
automatically defined functions), so that the function appears in all programs. Any undefined arguments (i.e.,
null
) to a node will be filled by the genetic programming algorithm.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a node that can appear in programs produced using these rules.buildTreeFull
(Class<?> type, int depth) Generates an expression tree with the given return type using the full initialization method.buildTreeFull
(Node node, int depth) Generates an expression tree with the given scaffolding using the full initialization method.buildTreeGrow
(Class<?> type, int depth) Generates an expression tree with the given return type using the grow initialization method.buildTreeGrow
(Node node, int depth) Generates an expression tree with the given scaffolding using the grow initialization method.Returns the list of all nodes which can appear in programs produced using these rules.double
Returns the probability of crossover being applied to a function (non-terminal) node.int
Returns the maximum depth of the expression trees produced by any initialization routine.int
Returns the maximum depth of the expression trees produced by any variation operator.Class<?>
Returns the return type of all programs produced using these rules.Returns the program scaffolding; ornull
if the program has no defined scaffolding.protected boolean
isMutationCompatible
(Node original, Node mutation) Returnstrue
if the original node can be replaced via point mutation with the given mutation node and satisfy type safety;false
otherwise.listAvailableCrossoverNodes
(Node node, Class<?> type) Returns the list of all nodes in the tree rooted at the specified node with the given return type.listAvailableFunctions
(Class<?> type) Returns the list of all available function (non-terminal) nodes with the given return type.listAvailableMutations
(Node node) Returns the list of all available mutations to the given node.listAvailableNodes
(Class<?> type) Returns the list of all available nodes with the given return type.listAvailableTerminals
(Class<?> type) Returns the list of all available terminal nodes with the given return type.void
Allows the default arithmetic nodes to appear in programs produced using these rules.void
Allows the default constant nodes to appear in programs produced using these rules.void
Allows the default control nodes to appear in programs produced using these rules.void
Allows all default nodes to appear in programs.void
Allows the default logic nodes to appear in programs produced using these rules.void
Allows the default trigonometric nodes to appear in programs produced using these rules.void
setFunctionCrossoverProbability
(double functionCrossoverProbability) Sets the probability of crossover being applied to a function (non-terminal) node.void
setMaxInitializationDepth
(int maxInitializationDepth) Sets the maximum depth of the expression trees produced by any initialization routine.void
setMaxVariationDepth
(int maxVariationDepth) Sets the maximum depth of the expression trees produced by any variation operator.void
setReturnType
(Class<?> returnType) Sets the return type of all programs produced using these rules.void
setScaffolding
(Node scaffolding) Sets the program scaffolding.
-
Constructor Details
-
Rules
public Rules()Constructs a new set of rules for defining program syntax.
-
-
Method Details
-
getReturnType
Returns the return type of all programs produced using these rules.- Returns:
- the return type of all programs produced using these rules
-
setReturnType
Sets the return type of all programs produced using these rules.- Parameters:
returnType
- the return type of all programs produced using these rules
-
getScaffolding
Returns the program scaffolding; ornull
if the program has no defined scaffolding.- Returns:
- the program scaffolding; or
null
if the program has no defined scaffolding
-
setScaffolding
Sets the program scaffolding. Setting the program scaffolding automatically sets the return type.- Parameters:
scaffolding
- the program scaffolding
-
getMaxInitializationDepth
public int getMaxInitializationDepth()Returns the maximum depth of the expression trees produced by any initialization routine.- Returns:
- the maximum depth of the expression trees produced by any initialization routine
-
setMaxInitializationDepth
public void setMaxInitializationDepth(int maxInitializationDepth) Sets the maximum depth of the expression trees produced by any initialization routine.- Parameters:
maxInitializationDepth
- the maximum depth of the expression trees produced by any initialization routine
-
getMaxVariationDepth
public int getMaxVariationDepth()Returns the maximum depth of the expression trees produced by any variation operator.- Returns:
- the maximum depth of the expression trees produced by any variation operator
-
setMaxVariationDepth
public void setMaxVariationDepth(int maxVariationDepth) Sets the maximum depth of the expression trees produced by any variation operator.- Parameters:
maxVariationDepth
- the maximum depth of the expression trees produced by any variation operator
-
getFunctionCrossoverProbability
public double getFunctionCrossoverProbability()Returns the probability of crossover being applied to a function (non-terminal) node.- Returns:
- the probability of crossover being applied to a function (non-terminal) node
-
setFunctionCrossoverProbability
public void setFunctionCrossoverProbability(double functionCrossoverProbability) Sets the probability of crossover being applied to a function (non-terminal) node. To set an equal probability of crossing all nodes, set the function crossover probability to(No. of Functions) / (No. of Functions + No. of Terminals)
.- Parameters:
functionCrossoverProbability
- the probability of crossover being applied to a function (non-terminal) node
-
add
Adds a node that can appear in programs produced using these rules.- Parameters:
node
- the node that can appear in programs produced using these rules
-
populateWithLogic
public void populateWithLogic()Allows the default logic nodes to appear in programs produced using these rules. This includesAnd
,Or
,Not
,Equals
,GreaterThan
,LessThan
,GreaterThanOrEqual
,LessThanOrEqual
, and constantstrue
andfalse
. -
populateWithArithmetic
public void populateWithArithmetic() -
populateWithTrig
public void populateWithTrig() -
populateWithControl
public void populateWithControl()Allows the default control nodes to appear in programs produced using these rules. This includesIfElse
,Sequence
, andNOP
. Several control nodes are not included in the defaults, such asFor
andWhile
. These are not included as they can easily result in infinite loops. These other control nodes can be manually added if needed. -
populateWithConstants
public void populateWithConstants()Allows the default constant nodes to appear in programs produced using these rules. This includes the constants0
,1
,2
,10
,-1
,Math.E
, andMath.PI
. -
populateWithDefaults
public void populateWithDefaults()Allows all default nodes to appear in programs. SeepopulateWithLogic()
,populateWithArithmetic()
,populateWithTrig()
,populateWithControl()
, andpopulateWithConstants()
for details. -
getAvailableNodes
Returns the list of all nodes which can appear in programs produced using these rules.- Returns:
- the list of all nodes which can appear in programs produced using these rules
-
listAvailableCrossoverNodes
Returns the list of all nodes in the tree rooted at the specified node with the given return type. This method ensures the crossover remains strongly-typed.- Parameters:
node
- the root of the treetype
- the required return type- Returns:
- the list of all nodes in the tree rooted at the specified node with the given return type
-
listAvailableMutations
Returns the list of all available mutations to the given node. This method ensures the mutation remains strongly-typed.- Parameters:
node
- the node to be mutated- Returns:
- the list of all available mutations to the given node
-
isMutationCompatible
Returnstrue
if the original node can be replaced via point mutation with the given mutation node and satisfy type safety;false
otherwise.- Parameters:
original
- the original nodemutation
- the mutation node- Returns:
true
if the original node can be replaced via point mutation with the given mutation node and satisfy type safety;false
otherwise
-
listAvailableNodes
Returns the list of all available nodes with the given return type.- Parameters:
type
- the required return type- Returns:
- the list of all available nodes with the given return type
-
listAvailableTerminals
Returns the list of all available terminal nodes with the given return type.- Parameters:
type
- the required return type- Returns:
- the list of all available terminal nodes with the given return type
-
listAvailableFunctions
Returns the list of all available function (non-terminal) nodes with the given return type.- Parameters:
type
- the required return type- Returns:
- the list of all available function (non-terminal) nodes with the given return type
-
buildTreeFull
Generates an expression tree with the given return type using the full initialization method. This method builds the tree so every leaf node is at the specified depth.- Parameters:
type
- the required return typedepth
- the required depth of each leaf node in the tree- Returns:
- an expression tree with the given return type using the full initialization method
-
buildTreeGrow
Generates an expression tree with the given return type using the grow initialization method. This method builds the tree such that the depth of each leaf is at most the specified maximum depth.- Parameters:
type
- the required return typedepth
- the maximum depth of each leaf node in the tree- Returns:
- an expression tree with the given return type using the grow initialization method
-
buildTreeFull
Generates an expression tree with the given scaffolding using the full initialization method. This method builds the tree so every leaf node is at the specified depth.- Parameters:
node
- the initial scaffolding for the treedepth
- the required depth of each leaf node in the tree- Returns:
- an expression tree with the given scaffolding using the full initialization method
-
buildTreeGrow
Generates an expression tree with the given scaffolding using the grow initialization method. This method builds the tree such that the depth of each leaf is at most the specified maximum depth.- Parameters:
node
- the initial scaffolding for the treedepth
- the maximum depth of each leaf node in the tree- Returns:
- an expression tree with the given scaffolding using the grow initialization method
-