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