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. The 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) );
The example above shows how to define the terminal operations for a GP, which tries to optimize a 2-dimensional function.
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 Details

    • index

      public int index()
      The projection index of the variable.
      Returns:
      the projection index of the variable
    • name

      public String name()
      Description copied from interface: Op
      Return the name of the operation.
      Specified by:
      name in interface Op<T>
      Returns:
      the name of the operation
    • 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.
      Specified by:
      arity in interface Op<T>
      Returns:
      the arity of the operation
    • apply

      public T apply(T[] variables)
      Specified by:
      apply in interface Function<T[],T>
    • compareTo

      public int compareTo(Var<T> o)
      Specified by:
      compareTo in interface Comparable<T>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • of

      public static <T> Var<T> of(String name, int index)
      Create a new variable with the given name and projection index.
      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 projection index
      Throws:
      IllegalArgumentException - if the given name is not a valid Java identifier
      IndexOutOfBoundsException - if the projection index is smaller than zero
      NullPointerException - if the given variable name is null
      See Also:
    • of

      public static <T> Var<T> of(String name)
      Create a new variable with the given 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.
      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 given name is not a valid Java identifier
      NullPointerException - if the given variable name is null
      See Also:
    • 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]
       
      If no variable index is encoded in the name, a variable with index 0 is created.
      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 given name is not a valid Java identifier
      NullPointerException - if the given variable name is null
      See Also:
    • reindex

      public static <V> void reindex(TreeNode<Op<V>> tree)
      Re-indexes the variables of the given operation tree. 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 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
      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.
      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

      public static <V> void reindex(TreeNode<Op<V>> tree, Map<Var<V>,Integer> indexes)
      Re-indexes the variables of the given operation tree. 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.
      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
      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.
      Type Parameters:
      V - the operation value type
      Parameters:
      tree - the tree where the variable indices need to be fixed
      indexes - the variable to index mapping
      Since:
      5.0
      See Also: