001 /*
002 * Java Genetic Algorithm Library (jenetics-6.0.0).
003 * Copyright (c) 2007-2020 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 static java.lang.Math.min;
023
024 import java.util.Random;
025
026 import io.jenetics.Chromosome;
027 import io.jenetics.Genotype;
028 import io.jenetics.Phenotype;
029 import io.jenetics.Recombinator;
030 import io.jenetics.util.MSeq;
031 import io.jenetics.util.RandomRegistry;
032
033 import io.jenetics.ext.util.FlatTree;
034 import io.jenetics.ext.util.FlatTreeNode;
035 import io.jenetics.ext.util.TreeNode;
036
037 /**
038 * Abstract implementation of tree base crossover recombinator. This class
039 * simplifies the implementation of tree base crossover implementation, by doing
040 * the transformation of the flattened tree genes to actual trees and vice versa.
041 * Only the {@link #crossover(TreeNode, TreeNode)} method must be implemented.
042 *
043 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
044 * @version 3.9
045 * @since 3.9
046 */
047 public abstract class TreeCrossover<
048 G extends TreeGene<?, G>,
049 C extends Comparable<? super C>
050 >
051 extends Recombinator<G, C>
052 {
053
054 /**
055 * Constructs an tree crossover with a given recombination probability.
056 *
057 * @param probability the recombination probability
058 * @throws IllegalArgumentException if the {@code probability} is not in the
059 * valid range of {@code [0, 1]}
060 */
061 protected TreeCrossover(final double probability) {
062 super(probability, 2);
063 }
064
065 @Override
066 protected int recombine(
067 final MSeq<Phenotype<G, C>> population,
068 final int[] individuals,
069 final long generation
070 ) {
071 assert individuals.length == 2 : "Required order of 2";
072 final Random random = RandomRegistry.random();
073
074 final Phenotype<G, C> pt1 = population.get(individuals[0]);
075 final Phenotype<G, C> pt2 = population.get(individuals[1]);
076 final Genotype<G> gt1 = pt1.genotype();
077 final Genotype<G> gt2 = pt2.genotype();
078
079 //Choosing the Chromosome index for crossover.
080 final int chIndex = random.nextInt(min(gt1.length(), gt2.length()));
081
082 final MSeq<Chromosome<G>> c1 = MSeq.of(gt1);
083 final MSeq<Chromosome<G>> c2 = MSeq.of(gt2);
084
085 crossover(c1, c2, chIndex);
086
087 //Creating two new Phenotypes and exchanging it with the old.
088 population.set(
089 individuals[0],
090 Phenotype.of(Genotype.of(c1.toISeq()), generation)
091 );
092 population.set(
093 individuals[1],
094 Phenotype.of(Genotype.of(c2.toISeq()), generation)
095 );
096
097 return order();
098 }
099
100 // Since the allele type "A" is not part of the type signature, we have to
101 // do some unchecked casts to make it "visible" again. The implementor of
102 // the abstract "crossover" method usually don't have to do additional casts.
103 private <A> void crossover(
104 final MSeq<Chromosome<G>> c1,
105 final MSeq<Chromosome<G>> c2,
106 final int index
107 ) {
108 @SuppressWarnings("unchecked")
109 final TreeNode<A> tree1 = (TreeNode<A>)TreeNode.ofTree(c1.get(index).gene());
110 @SuppressWarnings("unchecked")
111 final TreeNode<A> tree2 = (TreeNode<A>)TreeNode.ofTree(c2.get(index).gene());
112
113 crossover(tree1, tree2);
114
115 final var flat1 = FlatTreeNode.of(tree1);
116 final var flat2 = FlatTreeNode.of(tree2);
117
118 @SuppressWarnings("unchecked")
119 final var template = (TreeGene<A, ?>)c1.get(0).gene();
120
121 final var genes1 = flat1.map(tree -> gene(template, tree));
122 final var genes2 = flat2.map(tree -> gene(template, tree));
123
124 c1.set(index, c1.get(index).newInstance(genes1));
125 c2.set(index, c2.get(index).newInstance(genes2));
126 }
127
128 @SuppressWarnings("unchecked")
129 private <A> G gene(
130 final TreeGene<A, ?> template,
131 final FlatTree<? extends A, ?> tree
132 ) {
133 return (G)template.newInstance(
134 tree.value(),
135 tree.childOffset(),
136 tree.childCount()
137 );
138 }
139
140 /**
141 * Template method which performs the crossover. The arguments given are
142 * mutable non null trees.
143 *
144 * @param <A> the <em>existential</em> allele type
145 * @param that the first (chromosome) tree
146 * @param other he second (chromosome) tree
147 * @return the number of altered genes
148 */
149 protected abstract <A> int crossover(
150 final TreeNode<A> that,
151 final TreeNode<A> other
152 );
153
154 }
|