001 /*
002 * Java Genetic Algorithm Library (jenetics-1.6.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.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 1.0 — <em>$Date: 2014-02-15 $</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 * @throws IllegalArgumentException if the {@code probability} is not in the
065 * valid range of {@code [0, 1]} or the given {@code order} is
066 * smaller than two.
067 */
068 protected Recombinator(final double probability, final int order) {
069 super(probability);
070 if (order < 2) {
071 throw new IllegalArgumentException(format(
072 "Order must be greater than one, but was %d.", order
073 ));
074 }
075 _order = order;
076 }
077
078 /**
079 * Return the number of individuals involved in the
080 * {@link #recombine(Population, int[], int)} step.
081 *
082 * @return the number of individuals involved in the recombination step.
083 */
084 public int getOrder() {
085 return _order;
086 }
087
088 @Override
089 public final <C extends Comparable<? super C>> int alter(
090 final Population<G, C> population, final int generation
091 ) {
092 final Random random = RandomRegistry.getRandom();
093 final int order = Math.min(_order, population.size());
094 final IndexStream stream = IndexStream.Random(
095 population.size(), _probability
096 );
097
098 int alterations = 0;
099 for (int i = stream.next(); i != -1; i = stream.next()) {
100 final int[] individuals = subset(population.size(), order, random);
101 individuals[0] = i;
102
103 alterations += recombine(population, individuals, generation);
104 }
105
106 return alterations;
107 }
108
109 /**
110 * Recombination template method.
111 *
112 * @param <C> the fitness result type
113 * @param population the population to recombine
114 * @param individuals the array with the indexes of the individuals which
115 * are involved in the <i>recombination</i> step. The length of the
116 * array is {@link #getOrder()}. The first individual is the
117 * <i>primary</i> individual.
118 * @param generation the current generation.
119 * @return the number of genes that has been altered.
120 */
121 protected abstract <C extends Comparable<? super C>> int recombine(
122 final Population<G, C> population,
123 final int[] individuals,
124 final int generation
125 );
126
127
128 }
|