Var.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.3.0).
003  * Copyright (c) 2007-2018 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019  */
020 package io.jenetics.prog.op;
021 
022 import static java.util.Objects.requireNonNull;
023 import static io.jenetics.internal.util.Hashes.hash;
024 
025 import java.io.Serializable;
026 import java.util.Objects;
027 
028 /**
029  * Represents the program variables. The {@code Var} operation is a termination
030  * operation, which just returns the value with the defined index of the input
031  * variable array. It is essentially an orthogonal projection of the
032  <em>n</em>-dimensional input space to the <em>1</em>-dimensional result space.
033  *
034  <pre>{@code
035  * final ISeq<? extends Op<Double>> operations = ISeq.of(...);
036  * final ISeq<? extends Op<Double>> terminals = ISeq.of(
037  *     Var.of("x", 0), Var.of("y", 1)
038  * );
039  * }</pre>
040  *
041  * The example above shows how to define the terminal operations for a GP, which
042  * tries to optimize a 2-dimensional function.
043  *
044  <pre>{@code
045  * static double error(final ProgramChromosome<Double> program) {
046  *     final double x = ...;
047  *     final double y = ...;
048  *     final double result = program.apply(x, y);
049  *     ...
050  *
051  *     return ...;
052  * }
053  * }</pre>
054  *
055  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
056  @version 4.1
057  @since 3.9
058  */
059 public final class Var<T> implements Op<T>, Serializable {
060 
061     private static final long serialVersionUID = 1L;
062 
063     private final String _name;
064     private final int _index;
065 
066     /**
067      * Create a new variable with the given {@code name} and projection
068      * {@code index}.
069      *
070      @param name the variable name. Used when printing the operation tree
071      *        (program)
072      @param index the projection index
073      @throws IllegalArgumentException if the projection {@code index} is
074      *         smaller than zero
075      @throws NullPointerException if the given variable {@code name} is
076      *         {@code null}
077      */
078     private Var(final String name, final int index) {
079         _name = requireNonNull(name);
080         if (index < 0) {
081             throw new IndexOutOfBoundsException(
082                 "Index smaller than zero: " + index
083             );
084         }
085         _index = index;
086     }
087 
088     /**
089      * The projection index of the variable.
090      *
091      @return the projection index of the variable
092      */
093     public int index() {
094         return _index;
095     }
096 
097     @Override
098     public String name() {
099         return _name;
100     }
101 
102     @Override
103     public int arity() {
104         return 0;
105     }
106 
107     @Override
108     public T apply(final T[] variables) {
109         return variables[_index];
110     }
111 
112     @Override
113     public int hashCode() {
114         return hash(_name, hash(_index));
115     }
116 
117     @Override
118     public boolean equals(final Object obj) {
119         return obj == this ||
120             obj instanceof Var &&
121             Objects.equals(((Var)obj)._name, _name&&
122             ((Var)obj)._index == _index;
123     }
124 
125     @Override
126     public String toString() {
127         return _name;
128     }
129 
130     /**
131      * Create a new variable with the given {@code name} and projection
132      * {@code index}.
133      *
134      @param name the variable name. Used when printing the operation tree
135      *        (program)
136      @param index the projection index
137      @param <T> the variable type
138      @return a new variable with the given {@code name} and projection
139      *         {@code index}
140      */
141     public static <T> Var<T> of(final String name, final int index) {
142         return new Var<>(name, index);
143     }
144 
145 }