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