public final class Var<T> extends Object implements Op<T>, Comparable<Var<T>>, Serializable
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 ...;
}
Var
object is comparable according it's name.Modifier and Type | Method and 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 given
name . |
static <T> Var<T> |
of(String name,
int index)
Create a new variable with the given
name and projection
index . |
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 operation
tree . |
static <V> void |
reindex(TreeNode<Op<V>> tree,
Map<Var<V>,Integer> indexes)
Re-indexes the variables of the given operation
tree . |
String |
toString() |
public int index()
public int arity()
Op
public static <T> Var<T> of(String name, int index)
name
and projection
index
.T
- the variable typename
- the variable name. Used when printing the operation tree
(program)index
- the projection indexname
and projection
index
IllegalArgumentException
- if the given name
is not a valid
Java identifierIndexOutOfBoundsException
- if the projection index
is
smaller than zeroNullPointerException
- if the given variable name
is
null
parse(String)
public static <T> Var<T> of(String name)
name
. The projection index
is set to zero. Always prefer to create new variables with
of(String, int)
, especially when you define your terminal
operation for your GP problem.T
- the variable typename
- the variable name. Used when printing the operation tree
(program)name
and projection index
zeroIllegalArgumentException
- if the given name
is not a valid
Java identifierNullPointerException
- if the given variable name
is
null
parse(String)
public static <T> Var<T> parse(String name)
x[0]
y[3]
my_var[4]
If no variable index is encoded in the name, a variable with
index 0 is created.T
- the operation typename
- the variable name + indexIllegalArgumentException
- if the given variable couldn't be parsed
and the given name
is not a valid Java identifierNullPointerException
- if the given variable name
is
null
of(String, int)
public static <V> void reindex(TreeNode<Op<V>> tree)
tree
. 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 the Op
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.
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
V
- the operation value typetree
- the tree where the variable indices needs to be fixedMathOp.toMathOp(String)
,
Program.eval(Tree, Object[])
public static <V> void reindex(TreeNode<Op<V>> tree, Map<Var<V>,Integer> indexes)
tree
. 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.
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
V
- the operation value typetree
- the tree where the variable indices needs to be fixedindexes
- the variable to index mappingMathOp.toMathOp(String)
,
BoolOp.toBoolOp(String)
,
Program.eval(Tree, Object[])
© 2007-2019 Franz Wilhelmstötter (2019-11-18 20:30)