MeanAlterer.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.5.0).
003  * Copyright (c) 2007-2013 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.util.object.hashCodeOf;
024 
025 import java.util.Random;
026 
027 import javolution.lang.Immutable;
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 1.0 &mdash; <em>$Date: 2013-12-08 $</em>
043  */
044 public final class MeanAlterer<G extends Gene<?, G> & Mean<G>>
045     extends Recombinator<G>
046     implements Immutable
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     protected <C extends Comparable<? super C>> int recombine(
069         final Population<G, C> population,
070         final int[] individuals,
071         final int generation
072     ) {
073         final Random random = RandomRegistry.getRandom();
074 
075         final Phenotype<G, C> pt1 = population.get(individuals[0]);
076         final Phenotype<G, C> pt2 = population.get(individuals[1]);
077         final Genotype<G> gt1 = pt1.getGenotype();
078         final Genotype<G> gt2 = pt2.getGenotype();
079 
080         final int cindex = random.nextInt(gt1.length());
081         final MSeq<Chromosome<G>> c1 = gt1.toSeq().copy();
082         final ISeq<Chromosome<G>> c2 = gt2.toSeq();
083 
084         // Calculate the mean value of the gene array.
085         final MSeq<G> mean = mean(
086             c1.get(cindex).toSeq().copy(),
087             c2.get(cindex).toSeq()
088         );
089 
090         c1.set(cindex, c1.get(cindex).newInstance(mean.toISeq()));
091 
092         population.set(
093             individuals[0],
094             pt1.newInstance(gt1.newInstance(c1.toISeq()), generation)
095         );
096 
097         return 1;
098     }
099 
100     private static <G extends Gene<?, G> & Mean<G>>
101     MSeq<G> mean(final MSeq<G> a, final Seq<G> b) {
102         for (int i = a.length(); --i >= 0;) {
103             a.set(i, a.get(i).mean(b.get(i)));
104         }
105         return a;
106     }
107 
108     @Override
109     public int hashCode() {
110         return hashCodeOf(getClass()).and(super.hashCode()).value();
111     }
112 
113     @Override
114     public boolean equals(final Object obj) {
115         return obj == this || obj instanceof MeanAlterer<?> && super.equals(obj);
116     }
117 
118     @Override
119     public String toString() {
120         return format("%s[p=%f]", getClass().getSimpleName(), _probability);
121     }
122 
123 }
124