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