001 /*
002 * Java Genetic Algorithm Library (jenetics-5.2.0).
003 * Copyright (c) 2007-2020 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 */
020 package io.jenetics;
021
022 import static java.lang.Math.min;
023 import static java.lang.String.format;
024
025 import java.util.Random;
026
027 import io.jenetics.util.BaseSeq;
028 import io.jenetics.util.MSeq;
029 import io.jenetics.util.Mean;
030 import io.jenetics.util.RandomRegistry;
031
032 /**
033 * <p>
034 * The order ({@link #order()}) of this Recombination implementation is two.
035 * </p>
036 *
037 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
038 * @since 1.0
039 * @version 5.2
040 */
041 public class MeanAlterer<
042 G extends Gene<?, G> & Mean<G>,
043 C extends Comparable<? super C>
044 >
045 extends Recombinator<G, C>
046 {
047
048 /**
049 * Constructs an alterer with a given recombination probability.
050 *
051 * @param probability the crossover probability.
052 * @throws IllegalArgumentException if the {@code probability} is not in the
053 * valid range of {@code [0, 1]}.
054 */
055 public MeanAlterer(final double probability) {
056 super(probability, 2);
057 }
058
059 /**
060 * Create a new alterer with alter probability of {@code 0.05}.
061 */
062 public MeanAlterer() {
063 this(0.05);
064 }
065
066 @Override
067 protected int recombine(
068 final MSeq<Phenotype<G, C>> population,
069 final int[] individuals,
070 final long generation
071 ) {
072 final Random random = RandomRegistry.random();
073
074 final Phenotype<G, C> pt1 = population.get(individuals[0]);
075 final Phenotype<G, C> pt2 = population.get(individuals[1]);
076 final Genotype<G> gt1 = pt1.genotype();
077 final Genotype<G> gt2 = pt2.genotype();
078
079 //Choosing the Chromosome index for crossover.
080 final int cindex = random.nextInt(min(gt1.length(), gt2.length()));
081
082 final MSeq<Chromosome<G>> c1 = MSeq.of(gt1);
083
084 // Calculate the mean value of the gene array.
085 final MSeq<G> mean = mean(c1.get(cindex), gt2.get(cindex));
086
087 c1.set(cindex, c1.get(cindex).newInstance(mean.toISeq()));
088 population.set(individuals[0], Phenotype.of(Genotype.of(c1), generation));
089
090 return 1;
091 }
092
093 private static <G extends Gene<?, G> & Mean<G>>
094 MSeq<G> mean(final BaseSeq<G> a, final BaseSeq<G> b) {
095 final MSeq<G> result = MSeq.ofLength(a.length());
096 for (int i = a.length(); --i >= 0;) {
097 result.set(i, a.get(i).mean(b.get(i)));
098 }
099 return result;
100 }
101
102 @Override
103 public String toString() {
104 return format("%s[p=%f]", getClass().getSimpleName(), _probability);
105 }
106
107 }
|