Package io.jenetics.prog.op
Class Var<T>
- java.lang.Object
-
- io.jenetics.prog.op.Var<T>
-
- All Implemented Interfaces:
Op<T>
,Serializable
,Comparable<Var<T>>
,Function<T[],T>
,Supplier<Op<T>>
public final class Var<T> extends Object implements Op<T>, Comparable<Var<T>>, Serializable
Represents the program variables. TheVar
operation is a terminal operation, which just returns the value with the defined index of the input variable array. It is essentially an orthogonal projection of the n-dimensional input space to the 1-dimensional result space.The example above shows how to define the terminal operations for a GP, which tries to optimize a 2-dimensional function.final ISeq<? extends Op<Double>> operations = ISeq.of(...); final ISeq<? extends Op<Double>> terminals = ISeq.of( Var.of("x", 0), Var.of("y", 1) );
static double error(final ProgramChromosome<Double> program) { final double x = ...; final double y = ...; final double result = program.eval(x, y); ... return ...; }
- Since:
- 3.9
- Version:
- 5.0
- See Also:
- Serialized Form
- Implementation Note:
- The
Var
object is comparable according it's name.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description T
apply(T[] variables)
int
arity()
Return the arity of the operation function.int
compareTo(Var<T> o)
boolean
equals(Object obj)
int
hashCode()
int
index()
The projection index of the variable.String
name()
Return the name of the operation.static <T> Var<T>
of(String name)
Create a new variable with the givenname
.static <T> Var<T>
of(String name, int index)
Create a new variable with the givenname
and projectionindex
.static <T> Var<T>
parse(String name)
Parses the given variable string to its name and index.static <V> void
reindex(TreeNode<Op<V>> tree)
Re-indexes the variables of the given operationtree
.static <V> void
reindex(TreeNode<Op<V>> tree, Map<Var<V>,Integer> indexes)
Re-indexes the variables of the given operationtree
.String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.jenetics.prog.op.Op
get, isTerminal
-
-
-
-
Method Detail
-
index
public int index()
The projection index of the variable.- Returns:
- the projection index of the variable
-
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.
-
of
public static <T> Var<T> of(String name, int index)
Create a new variable with the givenname
and projectionindex
.- Type Parameters:
T
- the variable type- Parameters:
name
- the variable name. Used when printing the operation tree (program)index
- the projection index- Returns:
- a new variable with the given
name
and projectionindex
- Throws:
IllegalArgumentException
- if the givenname
is not a valid Java identifierIndexOutOfBoundsException
- if the projectionindex
is smaller than zeroNullPointerException
- if the given variablename
isnull
- See Also:
parse(String)
-
of
public static <T> Var<T> of(String name)
Create a new variable with the givenname
. The projection index is set to zero. Always prefer to create new variables withof(String, int)
, especially when you define your terminal operation for your GP problem.- Type Parameters:
T
- the variable type- Parameters:
name
- the variable name. Used when printing the operation tree (program)- Returns:
- a new variable with the given
name
and projection index zero - Throws:
IllegalArgumentException
- if the givenname
is not a valid Java identifierNullPointerException
- if the given variablename
isnull
- See Also:
parse(String)
-
parse
public static <T> Var<T> parse(String name)
Parses the given variable string to its name and index. The expected format is var_name[index].x[0] y[3] my_var[4]
- Type Parameters:
T
- the operation type- Parameters:
name
- the variable name + index- Returns:
- a new variable parsed from the input string
- Throws:
IllegalArgumentException
- if the given variable couldn't be parsed and the givenname
is not a valid Java identifierNullPointerException
- if the given variablename
isnull
- See Also:
of(String, int)
-
reindex
public static <V> void reindex(TreeNode<Op<V>> tree)
Re-indexes the variables of the given operationtree
. If the operation tree is created from it's string representation, the indices of the variables (Var
), are all set to zero, since it needs the whole tree for setting the indices correctly. The mapping from the node string to theOp
object, on the other hand, is a local operation. This method gives you the possibility to fix the indices of the variables. The indices of the variables are assigned according it's natural order.The example above shows a use-case of this method. If you parse a tree string and convert it to an operation tree, you have to re-index the variables first. If not, you will get the wrong result when evaluating the tree. After the re-indexing you will get the correct result of 45.0.final TreeNode<Op<Double>> tree = TreeNode.parse( "add(mul(x,y),sub(y,x))", MathOp::toMathOp ); assert Program.eval(tree, 10.0, 5.0) == 100.0; // wrong result Var.reindex(tree); assert Program.eval(tree, 10.0, 5.0) == 45.0; // correct result
- Type Parameters:
V
- the operation value type- Parameters:
tree
- the tree where the variable indices needs to be fixed- Since:
- 5.0
- See Also:
MathOp.toMathOp(String)
,Program.eval(Tree, Object[])
-
reindex
public static <V> void reindex(TreeNode<Op<V>> tree, Map<Var<V>,Integer> indexes)
Re-indexes the variables of the given operationtree
. If the operation tree is created from it's string representation, the indices of the variables (Var
), are all set to zero, since it needs the whole tree for setting the indices correctly.The example above shows a use-case of this method. If you parse a tree string and convert it to an operation tree, you have to re-index the variables first. If not, you will get the wrong result when evaluating the tree. After the re-indexing you will get the correct result of 45.0.final TreeNode<Op<Double>> tree = TreeNode.parse( "add(mul(x,y),sub(y,x))", MathOp::toMathOp ); assert Program.eval(tree, 10.0, 5.0) == 100.0; // wrong result final Map<Var<Double>, Integer> indexes = new HashMap<>(); indexes.put(Var.of("x"), 0); indexes.put(Var.of("y"), 1); Var.reindex(tree, indexes); assert Program.eval(tree, 10.0, 5.0) == 45.0; // correct result
- Type Parameters:
V
- the operation value type- Parameters:
tree
- the tree where the variable indices needs to be fixedindexes
- the variable to index mapping- Since:
- 5.0
- See Also:
MathOp.toMathOp(String)
,BoolOp.toBoolOp(String)
,Program.eval(Tree, Object[])
-
-