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