TreeMutator.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-5.1.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.ext;
021 
022 import java.util.Random;
023 
024 import io.jenetics.Chromosome;
025 import io.jenetics.Mutator;
026 import io.jenetics.MutatorResult;
027 import io.jenetics.internal.math.probability;
028 import io.jenetics.util.ISeq;
029 
030 import io.jenetics.ext.util.FlatTree;
031 import io.jenetics.ext.util.FlatTreeNode;
032 import io.jenetics.ext.util.TreeNode;
033 
034 /**
035  * Abstract class for mutating tree chromosomes.
036  *
037  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
038  @version 4.1
039  @since 4.1
040  */
041 public abstract class TreeMutator<
042     A,
043     extends TreeGene<A, G>,
044     extends Comparable<? super C>
045 >
046     extends Mutator<G, C>
047 {
048     public TreeMutator() {
049         this(DEFAULT_ALTER_PROBABILITY);
050     }
051 
052     public TreeMutator(final double probability) {
053         super(probability);
054     }
055 
056 
057     /**
058      * Mutates the given chromosome.
059      *
060      @param chromosome the chromosome to mutate
061      @param p the mutation probability for the underlying genetic objects
062      @param random the random engine used for the genotype mutation
063      @return the mutation result
064      */
065     @Override
066     protected MutatorResult<Chromosome<G>> mutate(
067         final Chromosome<G> chromosome,
068         final double p,
069         final Random random
070     ) {
071         final int P = probability.toInt(p);
072         return random.nextInt() < P
073             ? mutate(chromosome)
074             : MutatorResult.of(chromosome);
075     }
076 
077     private MutatorResult<Chromosome<G>> mutate(final Chromosome<G> chromosome) {
078         final TreeNode<A> tree = TreeNode.ofTree(chromosome.getGene());
079         mutate(tree);
080 
081         final FlatTreeNode<A> flat = FlatTreeNode.of(tree);
082         final ISeq<G> genes = flat.map(t -> gene(chromosome.getGene(), t));
083         return MutatorResult.of(chromosome.newInstance(genes)1);
084     }
085 
086     private G gene(
087         final G template,
088         final FlatTree<? extends A, ?> tree
089     ) {
090         return template.newInstance(
091             tree.getValue(),
092             tree.childOffset(),
093             tree.childCount()
094         );
095     }
096 
097     /**
098      * This method does the actual mutating, in place.
099      *
100      @param tree the mutable tree to mutate
101      */
102     protected abstract void mutate(final TreeNode<A> tree);
103 
104 }