001/*
002 * Java Genetic Algorithm Library (jenetics-8.3.0).
003 * Copyright (c) 2007-2025 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 */
020package io.jenetics.prog.op;
021
022import static java.lang.String.format;
023import static io.jenetics.ext.internal.util.Names.isIdentifier;
024
025import java.io.Serial;
026import java.io.Serializable;
027import java.util.HashMap;
028import java.util.Map;
029import java.util.Objects;
030import java.util.SortedSet;
031import java.util.TreeSet;
032import java.util.regex.Matcher;
033import java.util.regex.Pattern;
034import java.util.stream.Collectors;
035
036import io.jenetics.ext.util.Tree;
037import io.jenetics.ext.util.TreeNode;
038
039/**
040 * Represents the program variables. The {@code Var} operation is a
041 * <em>terminal</em> operation, which just returns the value with the defined
042 * index of the input variable array. It is essentially an orthogonal projection
043 * of the <em>n</em>-dimensional input space to the <em>1</em>-dimensional
044 * result space.
045 *
046 * {@snippet lang="java":
047 * final ISeq<? extends Op<Double>> operations = ISeq.of(null); // @replace substring='null' replacement="..."
048 * final ISeq<? extends Op<Double>> terminals = ISeq.of(
049 *     Var.of("x", 0), Var.of("y", 1)
050 * );
051 * }
052 *
053 * The example above shows how to define the terminal operations for a GP, which
054 * tries to optimize a 2-dimensional function.
055 *
056 * {@snippet lang="java":
057 * static double error(final ProgramChromosome<Double> program) {
058 *     final double x = null; // @replace substring='null' replacement="..."
059 *     final double y = null; // @replace substring='null' replacement="..."
060 *     final double result = program.eval(x, y);
061 *     // ...
062 *     return null; // @replace substring='null' replacement="..."
063 * }
064 * }
065 *
066 * @implNote
067 * The {@code Var} object is comparable, according its name.
068 *
069 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
070 * @version 7.0
071 * @since 3.9
072 */
073public final class Var<T> implements Op<T>, Comparable<Var<T>>, Serializable {
074
075        @Serial
076        private static final long serialVersionUID = 1L;
077
078        private final String _name;
079        private final int _index;
080
081        /**
082         * Create a new variable with the given {@code name} and projection
083         * {@code index}.
084         *
085         * @param name the variable name. Used when printing the operation tree
086         *        (program)
087         * @param index the projection index
088         * @throws IllegalArgumentException if the given {@code name} is not a valid
089         *         Java identifier
090         * @throws IndexOutOfBoundsException if the projection {@code index} is
091         *         smaller than zero
092         * @throws NullPointerException if the given variable {@code name} is
093         *         {@code null}
094         */
095        private Var(final String name, final int index) {
096                if (!isIdentifier(name)) {
097                        throw new IllegalArgumentException(format(
098                                "'%s' is not a valid identifier.", name
099                        ));
100                }
101                if (index < 0) {
102                        throw new IndexOutOfBoundsException(
103                                "Index smaller than zero: " + index
104                        );
105                }
106
107                _name = name;
108                _index = index;
109        }
110
111        /**
112         * The projection index of the variable.
113         *
114         * @return the projection index of the variable
115         */
116        public int index() {
117                return _index;
118        }
119
120        @Override
121        public String name() {
122                return _name;
123        }
124
125        @Override
126        public int arity() {
127                return 0;
128        }
129
130        @Override
131        public T apply(final T[] variables) {
132                if (_index >= variables.length) {
133                        throw new IllegalArgumentException(format(
134                                "No value for variable '%s' given.", this
135                        ));
136                }
137                return variables[_index];
138        }
139
140        @Override
141        public int compareTo(final Var<T> o) {
142                return _name.compareTo(o._name);
143        }
144
145        @Override
146        public int hashCode() {
147                return Objects.hashCode(_name);
148        }
149
150        @Override
151        public boolean equals(final Object obj) {
152                return obj instanceof Var<?> other &&
153                        Objects.equals(other._name, _name);
154        }
155
156        @Override
157        public String toString() {
158                return _name;
159        }
160
161        /**
162         * Create a new variable with the given {@code name} and projection
163         * {@code index}.
164         *
165         * @see #parse(String)
166         *
167         * @param name the variable name. Used when printing the operation tree
168         *        (program)
169         * @param index the projection index
170         * @param <T> the variable type
171         * @return a new variable with the given {@code name} and projection
172         *         {@code index}
173         * @throws IllegalArgumentException if the given {@code name} is not a valid
174         *         Java identifier
175         * @throws IndexOutOfBoundsException if the projection {@code index} is
176         *         smaller than zero
177         * @throws NullPointerException if the given variable {@code name} is
178         *         {@code null}
179         */
180        public static <T> Var<T> of(final String name, final int index) {
181                return new Var<>(name, index);
182        }
183
184        /**
185         * Create a new variable with the given {@code name}. The projection index
186         * is set to zero. Always prefer to create new variables with
187         * {@link #of(String, int)}, especially when you define your terminal
188         * operation for your GP problem.
189         *
190         * @see #parse(String)
191         *
192         * @param name the variable name. Used when printing the operation tree
193         *        (program)
194         * @param <T> the variable type
195         * @return a new variable with the given {@code name} and projection index
196         *         zero
197         * @throws IllegalArgumentException if the given {@code name} is not a valid
198         *         Java identifier
199         * @throws NullPointerException if the given variable {@code name} is
200         *         {@code null}
201         */
202        public static <T> Var<T> of(final String name) {
203                return new Var<>(name, 0);
204        }
205
206        private static final Pattern VAR_INDEX = Pattern.compile("(.+)\\[\\s*(\\d+)\\s*]");
207
208        /**
209         * Parses the given variable string to its name and index. The expected
210         * format is <em>var_name</em>[<em>index</em>].
211         *
212         * <pre> {@code
213         * x[0]
214         * y[3]
215         * my_var[4]
216         * } </pre>
217         *
218         * If no variable <em>index</em> is encoded in the name, a variable with
219         * index 0 is created.
220         *
221         * @see #of(String, int)
222         *
223         * @param name the variable name + index
224         * @param <T> the operation type
225         * @return a new variable parsed from the input string
226         * @throws IllegalArgumentException if the given variable couldn't be parsed
227         *         and the given {@code name} is not a valid Java identifier
228         * @throws NullPointerException if the given variable {@code name} is
229         *         {@code null}
230         */
231        public static <T> Var<T> parse(final String name) {
232                final Matcher matcher = VAR_INDEX.matcher(name);
233
234                return matcher.matches()
235                        ? of(matcher.group(1), Integer.parseInt(matcher.group(2)))
236                        : of(name, 0);
237        }
238
239        /**
240         * Re-indexes the variables of the given operation {@code tree}. If the
241         * operation tree is created from its string representation, the indices
242         * of the variables ({@link Var}), are all set to zero, since it needs the
243         * whole tree for setting the indices correctly. The mapping from the node
244         * string to the {@link Op} object, on the other hand, is a <em>local</em>
245         * operation. This method gives you the possibility to fix the indices of
246         * the variables. The indices of the variables are assigned according it's
247         * <em>natural</em> order.
248         *
249         * {@snippet lang="java":
250         * final TreeNode<Op<Double>> tree = TreeNode.parse(
251         *     "add(mul(x,y),sub(y,x))",
252         *     MathOp::toMathOp
253         * );
254         *
255         * assert Program.eval(tree, 10.0, 5.0) == 100.0; // wrong result
256         * Var.reindex(tree);
257         * assert Program.eval(tree, 10.0, 5.0) == 45.0; // correct result
258         * }
259         * The example above shows a use-case of this method. If you parse a tree
260         * string and convert it to an operation tree, you have to re-index the
261         * variables first. If not, you will get the wrong result when evaluating
262         * the tree. After the re-indexing, you will get the correct result of 45.0.
263         *
264         * @since 5.0
265         *
266         * @see MathOp#toMathOp(String)
267         * @see Program#eval(Tree, Object[])
268         *
269         * @param tree the tree where the variable indices need to be fixed
270         * @param <V> the operation value type
271         */
272        public static <V> void reindex(final TreeNode<Op<V>> tree) {
273                final SortedSet<Var<V>> vars = tree.stream()
274                        .filter(node -> node.value() instanceof Var)
275                        .map(node -> (Var<V>)node.value())
276                        .collect(Collectors.toCollection(TreeSet::new));
277
278                int index = 0;
279                final Map<Var<V>, Integer> indexes = new HashMap<>();
280                for (Var<V> var : vars) {
281                        indexes.put(var, index++);
282                }
283
284                reindex(tree, indexes);
285        }
286
287        /**
288         * Re-indexes the variables of the given operation {@code tree}. If the
289         * operation tree is created from its string representation, the indices
290         * of the variables ({@link Var}), are all set to zero, since it needs the
291         * whole tree for setting the indices correctly.
292         *
293         * {@snippet lang="java":
294         * final TreeNode<Op<Double>> tree = TreeNode.parse(
295         *     "add(mul(x,y),sub(y,x))",
296         *     MathOp::toMathOp
297         * );
298         *
299         * assert Program.eval(tree, 10.0, 5.0) == 100.0; // wrong result
300         * final Map<Var<Double>, Integer> indexes = new HashMap<>();
301         * indexes.put(Var.of("x"), 0);
302         * indexes.put(Var.of("y"), 1);
303         * Var.reindex(tree, indexes);
304         * assert Program.eval(tree, 10.0, 5.0) == 45.0; // correct result
305         * }
306         * The example above shows a use-case of this method. If you parse a tree
307         * string and convert it to an operation tree, you have to re-index the
308         * variables first. If not, you will get the wrong result when evaluating
309         * the tree. After the re-indexing, you will get the correct result of 45.0.
310         *
311         * @since 5.0
312         *
313         * @see MathOp#toMathOp(String)
314         * @see BoolOp#toBoolOp(String)
315         * @see Program#eval(Tree, Object[])
316         *
317         * @param tree the tree where the variable indices need to be fixed
318         * @param indexes the variable to index mapping
319         * @param <V> the operation value type
320         */
321        public static <V> void reindex(
322                final TreeNode<Op<V>> tree,
323                final Map<Var<V>, Integer> indexes
324        ) {
325                for (TreeNode<Op<V>> node : tree) {
326                        final Op<V> op = node.value();
327                        if (op instanceof Var) {
328                                node.value(Var.of(op.name(), indexes.get(op)));
329                        }
330                }
331        }
332
333}