- All Implemented Interfaces:
Chromosome<ProgramGene<A>>
,TreeChromosome<Op<A>,
,ProgramGene<A>> BaseSeq<ProgramGene<A>>
,Factory<Chromosome<ProgramGene<A>>>
,Verifiable
,Serializable
,Iterable<ProgramGene<A>>
,Function<A[],
,A> RandomAccess
public class ProgramChromosome<A>
extends AbstractTreeChromosome<Op<A>,ProgramGene<A>>
implements Function<A[],A>, Serializable
Holds the nodes of the operation tree.
final int depth = 6;
final ISeq<Op<Double>> operations = ISeq.of(...);
final ISeq<Op<Double>> terminals = ISeq.of(...);
final ProgramChromosome<Double> ch = ProgramChromosome.of(
depth,
// If the program has more than 200 nodes, it is marked as "invalid".
ch -> ch.length() <= 200,
operations,
terminals
);
- Since:
- 3.9
- Version:
- 4.1
- See Also:
-
Field Summary
Fields inherited from class io.jenetics.AbstractChromosome
_genes, _valid
-
Constructor Summary
ModifierConstructorDescriptionprotected
ProgramChromosome
(ISeq<ProgramGene<A>> program, Predicate<? super ProgramChromosome<A>> validator, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals) Create a new program chromosome from the given program genes. -
Method Summary
Modifier and TypeMethodDescriptionEvaluates the root node of this chromosome.final A
Evaluates the root node of this chromosome.boolean
isValid()
Check if this object is valid.Create a new instance of type T.newInstance
(ISeq<ProgramGene<A>> genes) A factory method which creates a newChromosome
of specific type and the givengenes
.static <A> ProgramChromosome
<A> Create a new program chromosome with the defined depth.static <A> ProgramChromosome
<A> of
(int depth, Predicate<? super ProgramChromosome<A>> validator, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals) Create a new program chromosome with the defined depth.static <A> ProgramChromosome
<A> of
(Tree<? extends Op<A>, ?> program, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals) Create a new chromosome from the given operation tree (program).static <A> ProgramChromosome
<A> of
(Tree<? extends Op<A>, ?> program, Predicate<? super ProgramChromosome<A>> validator, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals) Create a new chromosome from the given operation tree (program).static <A> ProgramChromosome
<A> static <A> ProgramChromosome
<A> of
(ISeq<ProgramGene<A>> genes, Predicate<? super ProgramChromosome<A>> validator, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals) Create a new program chromosome from the given (flattened) program tree.Return the allowed operations.Return the allowed terminal operations.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface io.jenetics.util.BaseSeq
forEach, get, isEmpty, iterator, length, listIterator, nonEmpty, spliterator, stream
Methods inherited from interface io.jenetics.Chromosome
as, gene
Methods inherited from interface io.jenetics.ext.TreeChromosome
root
-
Constructor Details
-
ProgramChromosome
protected ProgramChromosome(ISeq<ProgramGene<A>> program, Predicate<? super ProgramChromosome<A>> validator, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals) Create a new program chromosome from the given program genes. This constructor assumes that the givenprogram
is valid. Since the program validation is quite expensive, the validity check is skipped in this constructor.- Parameters:
program
- the program. During the program evolution, newly created program trees have the same depth than this tree.validator
- the chromosome validator. A typical validator would check the size of the tree and if the tree is too large, mark it at invalid. The validator may benull
.operations
- the allowed non-terminal operationsterminals
- the allowed terminal operations- Throws:
NullPointerException
- if one of the given arguments isnull
IllegalArgumentException
- if either theoperations
orterminals
sequence is empty
-
-
Method Details
-
operations
Return the allowed operations.- Returns:
- the allowed operations
- Since:
- 5.0
-
terminals
Return the allowed terminal operations.- Returns:
- the allowed terminal operations
- Since:
- 5.0
-
isValid
Description copied from interface:Verifiable
Check if this object is valid.- Specified by:
isValid
in interfaceChromosome<A>
- Specified by:
isValid
in interfaceVerifiable
- Overrides:
isValid
in classAbstractChromosome<ProgramGene<A>>
- Returns:
- true if this object is valid, false otherwise.
-
apply
Evaluates the root node of this chromosome.- 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
Evaluates the root node of this chromosome.- Parameters:
args
- the function arguments- Returns:
- the evaluated value
- Throws:
NullPointerException
- if the given variable array isnull
- See Also:
-
newInstance
Description copied from interface:Chromosome
A factory method which creates a newChromosome
of specific type and the givengenes
.- Specified by:
newInstance
in interfaceChromosome<A>
- Parameters:
genes
- the genes of the new chromosome. The given genes array is not copied.- Returns:
- A new
Chromosome
of the same type with the given genes.
-
newInstance
Description copied from interface:Factory
Create a new instance of type T.- Specified by:
newInstance
in interfaceFactory<A>
- Returns:
- a new instance of type T
-
of
public static <A> ProgramChromosome<A> of(Tree<? extends Op<A>, ?> program, Predicate<? super ProgramChromosome<A>> validator, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals) Create a new chromosome from the given operation tree (program).- Type Parameters:
A
- the operation type- Parameters:
program
- the operation treevalidator
- the chromosome validator. A typical validator would check the size of the tree and if the tree is too large, mark it at invalid. The validator may benull
.operations
- the allowed non-terminal operationsterminals
- the allowed terminal operations- Returns:
- a new chromosome from the given operation tree
- Throws:
NullPointerException
- if one of the given arguments isnull
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> ProgramChromosome<A> of(Tree<? extends Op<A>, ?> program, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals) Create a new chromosome from the given operation tree (program).- Type Parameters:
A
- the operation type- Parameters:
program
- the operation treeoperations
- the allowed non-terminal operationsterminals
- the allowed terminal operations- Returns:
- a new chromosome from the given operation tree
- Throws:
NullPointerException
- if one of the given arguments isnull
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> ProgramChromosome<A> of(int depth, Predicate<? super ProgramChromosome<A>> validator, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals) Create a new program chromosome with the defined depth. This method will create a full program tree.- Type Parameters:
A
- the operation type- Parameters:
depth
- the depth of the created program treevalidator
- the chromosome validator. A typical validator would check the size of the tree and if the tree is too large, mark it at invalid. The validator may benull
.operations
- the allowed non-terminal operationsterminals
- the allowed terminal operations- Returns:
- a new program chromosome from the given (flattened) program tree
- Throws:
NullPointerException
- if one of the parameters isnull
IllegalArgumentException
- if thedepth
is smaller than zero
-
of
public static <A> ProgramChromosome<A> of(int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals) Create a new program chromosome with the defined depth. This method will create a full program tree.- Type Parameters:
A
- the operation type- Parameters:
depth
- the depth of the created (full) program treeoperations
- the allowed non-terminal operationsterminals
- the allowed terminal operations- Returns:
- a new program chromosome from the given (flattened) program tree
- Throws:
NullPointerException
- if one of the parameters isnull
IllegalArgumentException
- if thedepth
is smaller than zero
-
of
public static <A> ProgramChromosome<A> of(ISeq<ProgramGene<A>> genes, Predicate<? super ProgramChromosome<A>> validator, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals) Create a new program chromosome from the given (flattened) program tree. This method doesn't make any assumption about the validity of the given operation tree. If the tree is not valid, it will repair it. This behaviour allows the safe usage of all existing alterers.final ProgramChromosome<Double> ch = ProgramChromosome.of( genes, // If the program has more that 200 nodes, it is marked as "invalid". ch -> ch.length() <= 200, operations, terminals );
- Type Parameters:
A
- the operation type- Parameters:
genes
- the program genesvalidator
- the chromosome validator to useoperations
- the allowed non-terminal operationsterminals
- the allowed terminal operations- Returns:
- a new program chromosome from the given (flattened) program tree
- Throws:
NullPointerException
- if one of the parameters isnull
-
of
public static <A> ProgramChromosome<A> of(ISeq<ProgramGene<A>> genes, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals)
-