- 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:
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionEvaluates this program gene (recursively) with the given variable values.final AConvenient method, which lets you apply the program function without explicitly create a wrapper array.Return a new, random gene with the same type and with the same constraints as this gene.newInstance(Op<A> op) Create a new program gene with the given operation.newInstance(Op<A> op, int childOffset, int childCount) Return a new program gene with the given operation and the local tree structure.Return the allowed operations.Return the allowed terminal operations.Creates a newTreeNodefrom this program gene.Methods inherited from class io.jenetics.ext.AbstractTreeGene
allele, bind, checkTreeState, childAt, childCount, childOffset, equals, flattenedNodes, hashCode, isRoot, isValid, parent, root, size, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface io.jenetics.ext.util.Tree
breadthFirstIterator, breadthFirstStream, childAfter, childAtPath, childAtPath, childBefore, childIterator, childPath, childStream, depth, depthFirstIterator, depthFirstStream, firstChild, firstLeaf, identical, indexOf, isAncestor, isChild, isDescendant, isEmpty, isLeaf, isRelated, isSibling, iterator, lastChild, lastLeaf, leafCount, leaves, level, nextLeaf, nextNode, nextSibling, path, pathElements, pathFromAncestorIterator, postorderIterator, postorderStream, preorderIterator, preorderStream, previousLeaf, previousNode, previousSibling, reduce, sharedAncestor, siblingCount, stream, toParenthesesString, toParenthesesStringMethods inherited from interface io.jenetics.ext.TreeGene
newInstance, valueMethods inherited from interface io.jenetics.util.Verifiable
isValid
-
Method Details
-
apply
Evaluates this program gene (recursively) with the given variable values.- Specified by:
applyin interfaceFunction<A[],A> - Parameters:
args- the input variables- Returns:
- the evaluated value
- Throws:
NullPointerException- if the given variable array isnull- See Also:
-
eval
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 isnull- See Also:
-
operations
Return the allowed operations.- Returns:
- the allowed operations
-
terminals
Return the allowed terminal operations.- Returns:
- the allowed terminal operations
-
toTreeNode
Creates a newTreeNodefrom this program gene.- Returns:
- a new tree node value build from this program gene
- Since:
- 5.0
-
newInstance
Description copied from interface:GeneReturn a new, random gene with the same type and with the same constraints as this gene. For all genes returned by this method holdsgene.getClass() == gene.newInstance().getClass(). Implementations of this method have to use theRandomGeneratorobject which can be fetched from theRandomRegistry.- Specified by:
newInstancein interfaceFactory<A>- Specified by:
newInstancein interfaceGene<Op<A>,ProgramGene<A>> - Returns:
- a new instance of type T
-
newInstance
Create a new program gene with the given operation.- Specified by:
newInstancein interfaceGene<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 givenopisnullIllegalArgumentException- 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
Return a new program gene with the given operation and the local tree structure.- Specified by:
newInstancein interfaceTreeGene<Op<A>,ProgramGene<A>> - Parameters:
op- the new operationchildOffset- the offset of the first node child within the chromosomechildCount- the number of children of the new tree gene- Returns:
- a new tree gene with the given parameters
- Throws:
IllegalArgumentException- if thechildCountis smaller than zeroIllegalArgumentException- if the operation arity is different from thechildCount.NullPointerException- if the givenopisnull
-