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