Class Program<T>

java.lang.Object
io.jenetics.prog.op.Program<T>
Type Parameters:
T - the argument type of the operation
All Implemented Interfaces:
Op<T>, Serializable, Function<T[],T>, Supplier<Op<T>>

public class Program<T> extends Object implements Op<T>, Serializable
This class composes a given operation tree to a new operation, which can serve as a sub program in another operation tree.
Since:
3.9
Version:
4.1
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Program(String name, Tree<? extends Op<T>,?> tree)
    Create a new program with the given name and the given operation tree.
  • Method Summary

    Modifier and Type
    Method
    Description
    apply(T[] args)
     
    int
    Return the arity of the operation function.
    static void
    check(Tree<? extends Op<?>,?> program)
    Validates the given program tree.
    boolean
     
    static <T> T
    eval(Tree<? extends Op<T>,?> tree, T... variables)
    Evaluates the given operation tree with the given variables.
    final T
    eval(T... args)
    Convenient method, which lets you apply the program function without explicitly create a wrapper array.
    int
     
    Return the name of the operation.
    static <A> TreeNode<Op<A>>
    of(int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals)
    Create a new, random program tree from the given (non) terminal operations with the desired depth.
    static <A> TreeNode<Op<A>>
    of(int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals, RandomGenerator random)
    Create a new, random program tree from the given (non) terminal operations with the desired depth.
    static <A> Program<A>
    of(String name, int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals)
    Create a new, random program from the given (non) terminal operations with the desired depth.
    static <A> Program<A>
    of(String name, int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals, RandomGenerator random)
    Create a new, random program from the given (non) terminal operations with the desired depth.
     
    static <A> TreeNode<Op<A>>
    toTree(ISeq<? extends FlatTree<? extends Op<A>,?>> nodes, ISeq<? extends Op<A>> terminals)
    Creates a valid program tree from the given flattened sequence of op nodes.
    Tree<Op<T>,?>
    Return the underlying expression tree.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.util.function.Function

    andThen, compose

    Methods inherited from interface io.jenetics.prog.op.Op

    get, isTerminal
  • Constructor Details

    • Program

      public Program(String name, Tree<? extends Op<T>,?> tree)
      Create a new program with the given name and the given operation tree. The arity of the program is calculated from the given operation tree and set to the maximal arity of the operations of the tree.
      Parameters:
      name - the program name
      tree - the operation tree
      Throws:
      NullPointerException - if one of the given arguments is null
      IllegalArgumentException - if the given operation tree is invalid, which means there is at least one node where the operation arity and the node child count differ.
  • Method Details

    • name

      public String name()
      Description copied from interface: Op
      Return the name of the operation.
      Specified by:
      name in interface Op<T>
      Returns:
      the name of the operation
    • arity

      public int arity()
      Description copied from interface: Op
      Return the arity of the operation function. If the arity is zero, the operation is terminal operation.
      Specified by:
      arity in interface Op<T>
      Returns:
      the arity of the operation
    • tree

      public Tree<Op<T>,?> tree()
      Return the underlying expression tree.
      Returns:
      the underlying expression tree
      Since:
      4.1
    • apply

      public T apply(T[] args)
      Specified by:
      apply in interface Function<T[],T>
    • eval

      @SafeVarargs public final T eval(T... args)
      Convenient method, which lets you apply the program function without explicitly create a wrapper array.
      Parameters:
      args - the function arguments
      Returns:
      the evaluated value
      Throws:
      NullPointerException - if the given variable array is null
      IllegalArgumentException - if the length of the argument array is smaller than the program arity
      See Also:
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • eval

      @SafeVarargs public static <T> T eval(Tree<? extends Op<T>,?> tree, T... variables)
      Evaluates the given operation tree with the given variables. This method is equivalent to
      final T result = tree.reduce(variables, Op::apply);
      
      but handles the variable sized variables array more conveniently.
      Type Parameters:
      T - the argument type
      Parameters:
      tree - the operation tree
      variables - the input variables
      Returns:
      the result of the operation tree evaluation
      Throws:
      NullPointerException - if one of the arguments is null
      IllegalArgumentException - if the length of the variable array is smaller than the program arity
      See Also:
    • check

      public static void check(Tree<? extends Op<?>,?> program)
      Validates the given program tree.
      Parameters:
      program - the program to validate
      Throws:
      NullPointerException - if the given program is null
      IllegalArgumentException - if the given operation tree is invalid, which means there is at least one node where the operation arity and the node child count differ.
    • of

      public static <A> Program<A> of(String name, int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals)
      Create a new, random program from the given (non) terminal operations with the desired depth. The created program tree is a full tree.
      Type Parameters:
      A - the operational type
      Parameters:
      name - the program name
      depth - the desired depth of the program tree
      operations - the list of non-terminal operations
      terminals - the list of terminal operations
      Returns:
      a new program
      Throws:
      NullPointerException - if one of the given operations is null
      IllegalArgumentException - if the given tree depth is smaller than zero
      Since:
      4.1
    • of

      public static <A> Program<A> of(String name, int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals, RandomGenerator random)
      Create a new, random program from the given (non) terminal operations with the desired depth. The created program tree is a full tree.
      Type Parameters:
      A - the operational type
      Parameters:
      name - the program name
      depth - the desired depth of the program tree
      operations - the list of non-terminal operations
      terminals - the list of terminal operations
      random - the random engine used for creating the program
      Returns:
      a new program
      Throws:
      NullPointerException - if one of the given operations is null
      IllegalArgumentException - if the given tree depth is smaller than zero
      Since:
      4.1
    • of

      public static <A> TreeNode<Op<A>> of(int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals)
      Create a new, random program tree from the given (non) terminal operations with the desired depth. The created program tree is a full tree.
      Type Parameters:
      A - the operational type
      Parameters:
      depth - the desired depth of the program tree
      operations - the list of non-terminal operations
      terminals - the list of terminal operations
      Returns:
      a new program tree
      Throws:
      NullPointerException - if one of the given operations is null
      IllegalArgumentException - if the given tree depth is smaller than zero
    • of

      public static <A> TreeNode<Op<A>> of(int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals, RandomGenerator random)
      Create a new, random program tree from the given (non) terminal operations with the desired depth. The created program tree is a full tree.
      Type Parameters:
      A - the operational type
      Parameters:
      depth - the desired depth of the program tree
      operations - the list of non-terminal operations
      terminals - the list of terminal operations
      random - the random engine used for creating the program
      Returns:
      a new program tree
      Throws:
      NullPointerException - if one of the given operations is null
      IllegalArgumentException - if the given tree depth is smaller than zero
      Since:
      4.1
    • toTree

      public static <A> TreeNode<Op<A>> toTree(ISeq<? extends FlatTree<? extends Op<A>,?>> nodes, ISeq<? extends Op<A>> terminals)
      Creates a valid program tree from the given flattened sequence of op nodes. The given operations and termination nodes are used for repairing the program tree, if necessary.
      Type Parameters:
      A - the operation argument type
      Parameters:
      nodes - the flattened, possible corrupt, program tree
      terminals - the usable non-terminal operation nodes to use for reparation
      Returns:
      a new valid program tree build from the flattened program tree
      Throws:
      NullPointerException - if one of the arguments is null
      IllegalArgumentException - if the nodes sequence is empty