001 /*
002 * Java Genetic Algorithm Library (jenetics-3.0.0).
003 * Copyright (c) 2007-2014 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@gmx.at)
019 */
020 package org.jenetics;
021
022 import static java.lang.String.format;
023 import static org.jenetics.internal.math.base.subset;
024 import static org.jenetics.internal.math.random.indexes;
025
026 import java.util.Random;
027 import java.util.function.IntFunction;
028
029 import org.jenetics.util.RandomRegistry;
030
031 /**
032 * <p>
033 * An enhanced genetic algorithm (EGA) combine elements of existing solutions in
034 * order to create a new solution, with some of the properties of each parent.
035 * Recombination creates a new chromosome by combining parts of two (or more)
036 * parent chromosomes. This combination of chromosomes can be made by selecting
037 * one or more crossover points, splitting these chromosomes on the selected
038 * points, and merge those portions of different chromosomes to form new ones.
039 * </p>
040 * <p>
041 * The recombination probability <i>P(r)</i> determines the probability that a
042 * given individual (genotype, not gene) of a population is selected for
043 * recombination. The (<i>mean</i>) number of changed individuals depend on the
044 * concrete implementation and can be vary from
045 * <i>P(r)</i>·<i>N<sub>G</sub></i> to
046 * <i>P(r)</i>·<i>N<sub>G</sub></i>·<i>O<sub>R</sub></i>, where
047 * <i>O<sub>R</sub></i> is the order of the recombination, which is the number
048 * of individuals involved int the {@link #recombine} method.
049 * </p>
050 *
051 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
052 * @since 1.0
053 * @version 3.0 — <em>$Date: 2014-12-28 $</em>
054 */
055 public abstract class Recombinator<
056 G extends Gene<?, G>,
057 C extends Comparable<? super C>
058 >
059 extends AbstractAlterer<G, C>
060 {
061
062 private final int _order;
063
064 /**
065 * Constructs an alterer with a given recombination probability.
066 *
067 * @param probability The recombination probability.
068 * @param order the number of individuals involved in the
069 * {@link #recombine(Population, int[], long)} step
070 * @throws IllegalArgumentException if the {@code probability} is not in the
071 * valid range of {@code [0, 1]} or the given {@code order} is
072 * smaller than two.
073 */
074 protected Recombinator(final double probability, final int order) {
075 super(probability);
076 if (order < 2) {
077 throw new IllegalArgumentException(format(
078 "Order must be greater than one, but was %d.", order
079 ));
080 }
081 _order = order;
082 }
083
084 /**
085 * Return the number of individuals involved in the
086 * {@link #recombine(Population, int[], long)} step.
087 *
088 * @return the number of individuals involved in the recombination step.
089 */
090 public int getOrder() {
091 return _order;
092 }
093
094 @Override
095 public final int alter(
096 final Population<G, C> population,
097 final long generation
098 ) {
099 final Random random = RandomRegistry.getRandom();
100 final int order = Math.min(_order, population.size());
101
102 final IntFunction<int[]> individuals = i -> {
103 final int[] ind = subset(population.size(), order, random);
104 ind[0] = i;
105 return ind;
106 };
107
108 return indexes(random, population.size(), _probability)
109 .mapToObj(individuals)
110 .mapToInt(i -> recombine(population, i, generation))
111 .sum();
112 }
113
114 /**
115 * Recombination template method.
116 *
117 * @param population the population to recombine
118 * @param individuals the array with the indexes of the individuals which
119 * are involved in the <i>recombination</i> step. The length of the
120 * array is {@link #getOrder()}. The first individual is the
121 * <i>primary</i> individual.
122 * @param generation the current generation.
123 * @return the number of genes that has been altered.
124 */
125 protected abstract int recombine(
126 final Population<G, C> population,
127 final int[] individuals,
128 final long generation
129 );
130
131
132 }
|