Class ProgramGene<A>

java.lang.Object
io.jenetics.ext.AbstractTreeGene<Op<A>,ProgramGene<A>>
io.jenetics.prog.ProgramGene<A>
All Implemented Interfaces:
TreeGene<Op<A>,ProgramGene<A>>, FlatTree<Op<A>,ProgramGene<A>>, Tree<Op<A>,ProgramGene<A>>, Gene<Op<A>,ProgramGene<A>>, Factory<ProgramGene<A>>, Self<ProgramGene<A>>, Verifiable, Serializable, Iterable<ProgramGene<A>>, Function<A[],A>

public final class ProgramGene<A> extends AbstractTreeGene<Op<A>,ProgramGene<A>> implements Gene<Op<A>,ProgramGene<A>>, Function<A[],A>
This gene represents a program, build upon an AST of Op functions. Because of the tight coupling with the ProgramChromosome, a ProgramGene can't be created directly. This reduces the possible error space. Since the ProgramGene also is a Tree, it can be easily used as a result.
final ProgramGene<Double> program = engine.stream()
    .limit(300)
    .collect(EvolutionResult.toBestGenotype())
    .getGene();

final double result = program.eval(3.4);
Since:
3.9
Version:
5.2
See Also:
  • Method Details

    • apply

      public A apply(A[] args)
      Evaluates this program gene (recursively) with the given variable values.
      Specified by:
      apply in interface Function<A[],A>
      Parameters:
      args - the input variables
      Returns:
      the evaluated value
      Throws:
      NullPointerException - if the given variable array is null
      See Also:
    • eval

      @SafeVarargs public final A eval(A... 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
      See Also:
    • operations

      public ISeq<Op<A>> operations()
      Return the allowed operations.
      Returns:
      the allowed operations
    • terminals

      public ISeq<Op<A>> terminals()
      Return the allowed terminal operations.
      Returns:
      the allowed terminal operations
    • toTreeNode

      public TreeNode<Op<A>> toTreeNode()
      Creates a new TreeNode from this program gene.
      Returns:
      a new tree node value build from this program gene
      Since:
      5.0
    • newInstance

      Description copied from interface: Gene
      Return a new, random gene with the same type and with the same constraints as this gene. For all genes returned by this method holds gene.getClass() == gene.newInstance().getClass(). Implementations of this method have to use the RandomGenerator object which can be fetched from the RandomRegistry.
      Specified by:
      newInstance in interface Factory<A>
      Specified by:
      newInstance in interface Gene<Op<A>,ProgramGene<A>>
      Returns:
      a new instance of type T
    • newInstance

      public ProgramGene<A> newInstance(Op<A> op)
      Create a new program gene with the given operation.
      Specified by:
      newInstance in interface Gene<Op<A>,ProgramGene<A>>
      Parameters:
      op - the operation of the new program gene
      Returns:
      a new program gene with the given operation
      Throws:
      NullPointerException - if the given op is null
      IllegalArgumentException - if the arity of the given operation is different from the arity of the current operation. This restriction ensures that only valid program genes are created by this method.
    • newInstance

      public ProgramGene<A> newInstance(Op<A> op, int childOffset, int childCount)
      Return a new program gene with the given operation and the local tree structure.
      Specified by:
      newInstance in interface TreeGene<Op<A>,ProgramGene<A>>
      Parameters:
      op - the new operation
      childOffset - the offset of the first node child within the chromosome
      childCount - the number of children of the new tree gene
      Returns:
      a new tree gene with the given parameters
      Throws:
      IllegalArgumentException - if the childCount is smaller than zero
      IllegalArgumentException - if the operation arity is different from the childCount.
      NullPointerException - if the given op is null