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
Var
object is comparable, according its name.
-
Method Summary
Modifier and TypeMethodDescriptionint
arity()
Return the arity of the operation function.int
boolean
int
hashCode()
int
index()
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 givenname
and projectionindex
.static <T> Var<T>
Parses the given variable string to its name and index.static <V> void
Re-indexes the variables of the given operationtree
.static <V> void
Re-indexes the variables of the given operationtree
.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 Details
-
index
The projection index of the variable.- Returns:
- the projection index of the variable
-
name
Description copied from interface:Op
Return the name of the operation. -
arity
Description copied from interface:Op
Return the arity of the operation function. If the arity is zero, the operation is terminal operation. -
apply
-
compareTo
- Specified by:
compareTo
in interfaceComparable<T>
-
hashCode
-
equals
-
toString
-
of
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:
-
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
name
and projection index zero - Throws:
IllegalArgumentException
- if the givenname
is not a valid Java identifierNullPointerException
- if the given variablename
isnull
- See Also:
-
parse
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:
-
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 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 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:
-