Class Rules

java.lang.Object
org.moeaframework.util.tree.Rules
All Implemented Interfaces:
Serializable

public class Rules extends Object implements 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

    Constructors
    Constructor
    Description
    Constructs a new set of rules for defining program syntax.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(Node node)
    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.
    Returns the return type of all programs produced using these rules.
    Returns the program scaffolding; or null if the program has no defined scaffolding.
    protected boolean
    isMutationCompatible(Node original, Node mutation)
    Returns true if the original node can be replaced via point mutation with the given mutation node and satisfy type safety; false otherwise.
    Returns the list of all nodes in the tree rooted at the specified node with the given return type.
    Returns the list of all available function (non-terminal) nodes with the given return type.
    Returns the list of all available mutations to the given node.
    Returns the list of all available nodes with the given return 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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Rules

      public Rules()
      Constructs a new set of rules for defining program syntax.
  • Method Details

    • getReturnType

      public Class<?> getReturnType()
      Returns the return type of all programs produced using these rules.
      Returns:
      the return type of all programs produced using these rules
    • setReturnType

      public void setReturnType(Class<?> returnType)
      Sets the return type of all programs produced using these rules.
      Parameters:
      returnType - the return type of all programs produced using these rules
    • getScaffolding

      public Node getScaffolding()
      Returns the program scaffolding; or null if the program has no defined scaffolding.
      Returns:
      the program scaffolding; or null if the program has no defined scaffolding
    • setScaffolding

      public void setScaffolding(Node scaffolding)
      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

      public void add(Node node)
      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 includes And, Or, Not, Equals, GreaterThan, LessThan, GreaterThanOrEqual, LessThanOrEqual, and constants true and false.
    • populateWithArithmetic

      public void populateWithArithmetic()
      Allows the default arithmetic nodes to appear in programs produced using these rules. This includes Add, Subtract, Multiply, Divide, Modulus, Floor, Ceil, Round, Max, Min, Power, Square, SquareRoot, Abs, Log, Log10, Exp, and Sign.
    • populateWithTrig

      public void populateWithTrig()
      Allows the default trigonometric nodes to appear in programs produced using these rules. This includes Sin, Cos, Tan, Asin, Acos, Atan, Sinh, Cosh, Tanh, Asinh, Acosh, and Atanh.
    • populateWithControl

      public void populateWithControl()
      Allows the default control nodes to appear in programs produced using these rules. This includes IfElse, Sequence, and NOP. Several control nodes are not included in the defaults, such as For and While. 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 constants 0, 1, 2, 10, -1, Math.E, and Math.PI.
    • populateWithDefaults

      public void populateWithDefaults()
      Allows all default nodes to appear in programs. See populateWithLogic(), populateWithArithmetic(), populateWithTrig(), populateWithControl(), and populateWithConstants() for details.
    • getAvailableNodes

      public List<Node> 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

      public List<Node> listAvailableCrossoverNodes(Node node, Class<?> type)
      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 tree
      type - the required return type
      Returns:
      the list of all nodes in the tree rooted at the specified node with the given return type
    • listAvailableMutations

      public List<Node> listAvailableMutations(Node node)
      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

      protected boolean isMutationCompatible(Node original, Node mutation)
      Returns true 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 node
      mutation - 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

      public List<Node> listAvailableNodes(Class<?> type)
      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

      public List<Node> listAvailableTerminals(Class<?> type)
      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

      public List<Node> listAvailableFunctions(Class<?> type)
      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

      public Node buildTreeFull(Class<?> type, int depth)
      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 type
      depth - 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

      public Node buildTreeGrow(Class<?> type, int depth)
      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 type
      depth - 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

      public Node buildTreeFull(Node node, int depth)
      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 tree
      depth - the required depth of each leaf node in the tree
      Returns:
      an expression tree with the given scaffolding using the full initialization method
    • buildTreeGrow

      public Node buildTreeGrow(Node node, int depth)
      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 tree
      depth - the maximum depth of each leaf node in the tree
      Returns:
      an expression tree with the given scaffolding using the grow initialization method