001/*
002 * Java Genetic Algorithm Library (jenetics-8.1.0).
003 * Copyright (c) 2007-2024 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;
021
022import io.jenetics.ext.TreeGene;
023import io.jenetics.ext.TreeRewriteAlterer;
024import io.jenetics.ext.rewriting.TreeRewriter;
025
026import io.jenetics.prog.op.MathExpr;
027import io.jenetics.prog.op.Op;
028
029/**
030 * Prunes a given mathematical tree with the given alterer probability.
031 *
032 * @see TreeRewriteAlterer
033 *
034 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
035 * @version 5.0
036 * @since 4.1
037 */
038public class MathRewriteAlterer<
039        G extends TreeGene<Op<Double>, G>,
040        C extends Comparable<? super C>
041>
042        extends TreeRewriteAlterer<Op<Double>, G, C>
043{
044
045        /**
046         * Create a new alterer with the given {@code rewriter} and given rewrite
047         * {@code limit}.
048         *
049         * @param rewriter the tree rewriter
050         * @param limit the rewriting limit
051         * @param probability the altering probability
052         * @throws NullPointerException if the {@code rewriter} is {@code null}
053         */
054        public MathRewriteAlterer(
055                final TreeRewriter<Op<Double>> rewriter,
056                final int limit,
057                final double probability
058        ) {
059                super(rewriter, limit, probability);
060        }
061
062        /**
063         * Create a new alterer with the given {@code rewriter} and given rewrite
064         * {@code limit}.
065         *
066         * @param rewriter the tree rewriter
067         * @param probability the altering probability
068         * @throws NullPointerException if the {@code rewriter} is {@code null}
069         */
070        public MathRewriteAlterer(
071                final TreeRewriter<Op<Double>> rewriter,
072                final double probability
073        ) {
074                this(rewriter, Integer.MAX_VALUE, probability);
075        }
076
077        /**
078         * Create a new alterer with the given {@code rewriter}.
079         *
080         * @param rewriter the tree rewriter
081         * @throws NullPointerException if the {@code rewriter} is {@code null}
082         */
083        public MathRewriteAlterer(final TreeRewriter<Op<Double>> rewriter) {
084                this(rewriter, Integer.MAX_VALUE, DEFAULT_ALTER_PROBABILITY);
085        }
086
087        /**
088         * Create a new alterer with the default math rewriter
089         * {@link MathExpr#REWRITER} and given rewrite.
090         *
091         * @param probability the altering probability
092         */
093        public MathRewriteAlterer(final double probability) {
094                this(MathExpr.REWRITER, Integer.MAX_VALUE, probability);
095        }
096
097        /**
098         * Create a new alterer with the default math rewriter
099         * {@link MathExpr#REWRITER} and given rewrite.
100         */
101        public MathRewriteAlterer() {
102                this(MathExpr.REWRITER, Integer.MAX_VALUE, DEFAULT_ALTER_PROBABILITY);
103        }
104
105}