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