001 /*
002 * Java Genetic Algorithm Library (jenetics-3.8.0).
003 * Copyright (c) 2007-2017 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
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 int count = 0;
100 if (population.size() >= 2) {
101 final Random random = RandomRegistry.getRandom();
102 final int order = Math.min(_order, population.size());
103
104 // Selection of the individuals for recombination.
105 final IntFunction<int[]> individuals = i -> {
106 final int[] ind = subset(population.size(), order, random);
107 ind[0] = i;
108 return ind;
109 };
110
111 count = indexes(random, population.size(), _probability)
112 .mapToObj(individuals)
113 .mapToInt(i -> recombine(population, i, generation))
114 .sum();
115 }
116
117 return count;
118 }
119
120 /**
121 * Recombination template method.
122 *
123 * @param population the population to recombine
124 * @param individuals the array with the indexes of the individuals which
125 * are involved in the <i>recombination</i> step. The length of the
126 * array is {@link #getOrder()}. The first individual is the
127 * <i>primary</i> individual.
128 * @param generation the current generation.
129 * @return the number of genes that has been altered.
130 */
131 protected abstract int recombine(
132 final Population<G, C> population,
133 final int[] individuals,
134 final long generation
135 );
136
137 }
|