java.lang.Object
io.jenetics.prog.op.Var<T>
- All Implemented Interfaces:
Op<T>,Serializable,Comparable<Var<T>>,Function<T[],,T> Supplier<Op<T>>
Represents the program variables. The
The example above shows how to define the terminal operations for a GP, which
tries to optimize a 2-dimensional function.
Var 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.
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:
- 7.0
- See Also:
- Implementation Note:
- The
Varobject is comparable, according its name.
-
Method Summary
Modifier and TypeMethodDescriptionintarity()Return the arity of the operation function.intbooleaninthashCode()intindex()The projection index of the variable.name()Return the name of the operation.static <T> Var<T> Create a new variable with the givenname.static <T> Var<T> Create a new variable with the givennameand projectionindex.static <T> Var<T> Parses the given variable string to its name and index.static <V> voidRe-indexes the variables of the given operationtree.static <V> voidRe-indexes the variables of the given operationtree.toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface io.jenetics.prog.op.Op
get, isTerminal
-
Method Details
-
index
The projection index of the variable.- Returns:
- the projection index of the variable
-
name
Description copied from interface:OpReturn the name of the operation. -
arity
Description copied from interface:OpReturn the arity of the operation function. If the arity is zero, the operation is terminal operation. -
apply
-
compareTo
- Specified by:
compareToin interfaceComparable<T>
-
hashCode
-
equals
-
toString
-
of
Create a new variable with the givennameand 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
nameand projectionindex - Throws:
IllegalArgumentException- if the givennameis not a valid Java identifierIndexOutOfBoundsException- if the projectionindexis smaller than zeroNullPointerException- if the given variablenameisnull- See Also:
-
of
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
nameand projection index zero - Throws:
IllegalArgumentException- if the givennameis not a valid Java identifierNullPointerException- if the given variablenameisnull- See Also:
-
parse
Parses the given variable string to its name and index. The expected format is var_name[index].
If no variable index is encoded in the name, a variable with index 0 is created.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 givennameis not a valid Java identifierNullPointerException- if the given variablenameisnull- See Also:
-
reindex
Re-indexes the variables of the given operationtree. If the operation tree is created from its 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 theOpobject, 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 need to be fixed- Since:
- 5.0
- See Also:
-
reindex
Re-indexes the variables of the given operationtree. If the operation tree is created from its 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 need to be fixedindexes- the variable to index mapping- Since:
- 5.0
- See Also:
-