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