Var.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.9.0).
003  * Copyright (c) 2007-2017 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@gmx.at)
019  */
020 package org.jenetics.prog.op;
021 
022 import static java.util.Objects.requireNonNull;
023 
024 /**
025  * Represents the program variables. The {@code Var} operation is a termination
026  * operation, which just returns the value with the defined index of the input
027  * variable array. It is essentially an orthogonal projection of the
028  <em>n</em>-dimensional input space to the <em>1</em>-dimensional result space.
029  *
030  <pre>{@code
031  * final ISeq<? extends Op<Double>> operations = ISeq.of(...);
032  * final ISeq<? extends Op<Double>> terminals = ISeq.of(
033  *     Var.of("x", 0), Var.of("y", 1)
034  * );
035  * }</pre>
036  *
037  * The example above shows how to define the terminal operations for a GP, which
038  * tries to optimize a 2-dimensional function.
039  *
040  <pre>{@code
041  * static double error(final ProgramChromosome<Double> program) {
042  *     final double x = ...;
043  *     final double y = ...;
044  *     final double result = program.apply(x, y);
045  *     ...
046  *
047  *     return ...;
048  * }
049  * }</pre>
050  *
051  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
052  @version 3.9
053  @since 3.9
054  */
055 public final class Var<T> implements Op<T> {
056 
057     private final String _name;
058     private final int _index;
059 
060     /**
061      * Create a new variable with the given {@code name} and projection
062      * {@code index}.
063      *
064      @param name the variable name. Used when printing the operation tree
065      *        (program)
066      @param index the projection index
067      @throws IllegalArgumentException if the projection {@code index} is
068      *         smaller than zero
069      @throws NullPointerException if the given variable {@code name} is
070      *         {@code null}
071      */
072     private Var(final String name, final int index) {
073         _name = requireNonNull(name);
074         if (index < 0) {
075             throw new IndexOutOfBoundsException(
076                 "Index smaller than zero: " + index
077             );
078         }
079         _index = index;
080     }
081 
082     /**
083      * The projection index of the variable.
084      *
085      @return the projection index of the variable
086      */
087     public int index() {
088         return _index;
089     }
090 
091     @Override
092     public String name() {
093         return _name;
094     }
095 
096     @Override
097     public int arity() {
098         return 0;
099     }
100 
101     @Override
102     public T apply(final T[] variables) {
103         return variables[_index];
104     }
105 
106     @Override
107     public int hashCode() {
108         int hash = 17;
109         hash += 31*_name.hashCode() 37;
110         hash += 31*_index + 37;
111         return hash;
112     }
113 
114     @Override
115     public boolean equals(final Object obj) {
116         return obj instanceof Var<?> &&
117             ((Var)obj)._name.equals(_name&&
118             ((Var)obj)._index == _index;
119     }
120 
121     @Override
122     public String toString() {
123         return _name;
124     }
125 
126     /**
127      * Create a new variable with the given {@code name} and projection
128      * {@code index}.
129      *
130      @param name the variable name. Used when printing the operation tree
131      *        (program)
132      @param index the projection index
133      @param <T> the variable type
134      @return a new variable with the given {@code name} and projection
135      *         {@code index}
136      */
137     public static <T> Var<T> of(final String name, final int index) {
138         return new Var<>(name, index);
139     }
140 
141 }