io.jenetics.ext.TreeCrossover Maven / Gradle / Ivy
The newest version!
/*
* Java Genetic Algorithm Library (jenetics-8.1.0).
* Copyright (c) 2007-2024 Franz Wilhelmstötter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author:
* Franz Wilhelmstötter ([email protected])
*/
package io.jenetics.ext;
import static java.lang.Math.min;
import io.jenetics.Chromosome;
import io.jenetics.Genotype;
import io.jenetics.Phenotype;
import io.jenetics.Recombinator;
import io.jenetics.util.MSeq;
import io.jenetics.util.RandomRegistry;
import io.jenetics.ext.util.FlatTree;
import io.jenetics.ext.util.FlatTreeNode;
import io.jenetics.ext.util.TreeNode;
/**
* Abstract implementation of tree base crossover recombinator. This class
* simplifies the implementation of tree base crossover implementation, by doing
* the transformation of the flattened tree genes to actual trees and vice versa.
* Only the {@link #crossover(TreeNode, TreeNode)} method must be implemented.
*
* @author Franz Wilhelmstötter
* @version 3.9
* @since 3.9
*/
public abstract class TreeCrossover<
G extends TreeGene, G>,
C extends Comparable super C>
>
extends Recombinator
{
/**
* Constructs a tree crossover with a given recombination probability.
*
* @param probability the recombination probability
* @throws IllegalArgumentException if the {@code probability} is not in the
* valid range of {@code [0, 1]}
*/
protected TreeCrossover(final double probability) {
super(probability, 2);
}
@Override
protected int recombine(
final MSeq> population,
final int[] individuals,
final long generation
) {
assert individuals.length == 2 : "Required order of 2";
final var random = RandomRegistry.random();
final Phenotype pt1 = population.get(individuals[0]);
final Phenotype pt2 = population.get(individuals[1]);
final Genotype gt1 = pt1.genotype();
final Genotype gt2 = pt2.genotype();
//Choosing the Chromosome index for crossover.
final int chIndex = random.nextInt(min(gt1.length(), gt2.length()));
final MSeq> c1 = MSeq.of(gt1);
final MSeq> c2 = MSeq.of(gt2);
crossover(c1, c2, chIndex);
//Creating two new Phenotypes and exchanging it with the old.
population.set(
individuals[0],
Phenotype.of(Genotype.of(c1.toISeq()), generation)
);
population.set(
individuals[1],
Phenotype.of(Genotype.of(c2.toISeq()), generation)
);
return order();
}
// Since the allele type "A" is not part of the type signature, we have to
// do some unchecked casts to make it "visible" again. The implementor of
// the abstract "crossover" method usually doesn't have to do additional casts.
private void crossover(
final MSeq> c1,
final MSeq> c2,
final int index
) {
@SuppressWarnings("unchecked")
final TreeNode tree1 = (TreeNode)TreeNode.ofTree(c1.get(index).gene());
@SuppressWarnings("unchecked")
final TreeNode tree2 = (TreeNode)TreeNode.ofTree(c2.get(index).gene());
crossover(tree1, tree2);
final var flat1 = FlatTreeNode.ofTree(tree1);
final var flat2 = FlatTreeNode.ofTree(tree2);
@SuppressWarnings("unchecked")
final var template = (TreeGene)c1.get(0).gene();
final var genes1 = flat1.map(tree -> gene(template, tree));
final var genes2 = flat2.map(tree -> gene(template, tree));
c1.set(index, c1.get(index).newInstance(genes1));
c2.set(index, c2.get(index).newInstance(genes2));
}
@SuppressWarnings("unchecked")
private G gene(
final TreeGene template,
final FlatTree extends A, ?> tree
) {
return (G)template.newInstance(
tree.value(),
tree.childOffset(),
tree.childCount()
);
}
/**
* Template method which performs the crossover. The arguments given are
* mutable non-null trees.
*
* @param the existential allele type
* @param that the first (chromosome) tree
* @param other he second (chromosome) tree
* @return the number of altered genes
*/
protected abstract int crossover(
final TreeNode that,
final TreeNode other
);
}