Package io.jenetics.prog
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>>
,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 ofOp
functions. Because of the tight coupling with theProgramChromosome
, aProgramGene
can't be created directly. This reduces the the possible error space. Since theProgramGene
also is aTree
, it can be easily used as 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:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description A
apply(A[] args)
Evaluates this program gene (recursively) with the given variable values.A
eval(A... args)
Convenient method, which lets you apply the program function without explicitly create a wrapper array.ProgramGene<A>
newInstance()
ProgramGene<A>
newInstance(Op<A> op)
Create a new program gene with the given operation.ProgramGene<A>
newInstance(Op<A> op, int childOffset, int childCount)
Return a new program gene with the given operation and the local tree structure.ISeq<Op<A>>
operations()
Return the allowed operations.ISeq<Op<A>>
terminals()
Return the allowed terminal operations.TreeNode<Op<A>>
toTreeNode()
Creates a newTreeNode
from 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, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Methods 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, isLeaf, isRelated, isSibling, iterator, lastChild, lastLeaf, leafCount, level, nextLeaf, nextNode, nextSibling, path, pathElements, pathFromAncestorIterator, postorderIterator, postorderStream, preorderIterator, preorderStream, previousLeaf, previousNode, previousSibling, sharedAncestor, siblingCount, stream, toParenthesesString, toParenthesesString
-
Methods inherited from interface io.jenetics.ext.TreeGene
newInstance, value
-
Methods inherited from interface io.jenetics.util.Verifiable
isValid
-
-
-
-
Method Detail
-
apply
public A apply(A[] args)
Evaluates this program gene (recursively) with the given variable values.- Specified by:
apply
in interfaceFunction<A[],A>
- Parameters:
args
- the input variables- Returns:
- the evaluated value
- Throws:
NullPointerException
- if the given variable array isnull
- See Also:
eval(Object[])
,ProgramChromosome.eval(Object[])
-
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 isnull
- See Also:
apply(Object[])
,ProgramChromosome.eval(Object[])
-
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 newTreeNode
from this program gene.- Returns:
- a new tree node value build from this program gene
- Since:
- 5.0
-
newInstance
public ProgramGene<A> newInstance()
- Specified by:
newInstance
in interfaceFactory<A>
- Specified by:
newInstance
in interfaceGene<Op<A>,ProgramGene<A>>
-
newInstance
public ProgramGene<A> newInstance(Op<A> op)
Create a new program gene with the given operation.- Specified by:
newInstance
in 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 givenop
isnull
IllegalArgumentException
- if the arity of the given operation is different from the arity of 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 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 thechildCount
is smaller than zeroIllegalArgumentException
- if the operation arity is different from thechildCount
.NullPointerException
- if the givenop
isnull
-
-