001 /*
002 * Java Genetic Algorithm Library (jenetics-3.5.0).
003 * Copyright (c) 2007-2016 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.Hash;
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 3.0
042 */
043 public final class MeanAlterer<
044 G extends Gene<?, G> & Mean<G>,
045 C extends Comparable<? super C>
046 >
047 extends Recombinator<G, C>
048 {
049
050 /**
051 * Constructs an alterer with a given recombination probability.
052 *
053 * @param probability the crossover probability.
054 * @throws IllegalArgumentException if the {@code probability} is not in the
055 * valid range of {@code [0, 1]}.
056 */
057 public MeanAlterer(final double probability) {
058 super(probability, 2);
059 }
060
061 /**
062 * Create a new alterer with alter probability of {@code 0.05}.
063 */
064 public MeanAlterer() {
065 this(0.05);
066 }
067
068 @Override
069 protected int recombine(
070 final Population<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 final int cindex = random.nextInt(gt1.length());
082 final MSeq<Chromosome<G>> c1 = gt1.toSeq().copy();
083 final ISeq<Chromosome<G>> c2 = gt2.toSeq();
084
085 // Calculate the mean value of the gene array.
086 final MSeq<G> mean = mean(
087 c1.get(cindex).toSeq().copy(),
088 c2.get(cindex).toSeq()
089 );
090
091 c1.set(cindex, c1.get(cindex).newInstance(mean.toISeq()));
092
093 population.set(
094 individuals[0],
095 pt1.newInstance(gt1.newInstance(c1.toISeq()), generation)
096 );
097
098 return 1;
099 }
100
101 private static <G extends Gene<?, G> & Mean<G>>
102 MSeq<G> mean(final MSeq<G> a, final Seq<G> b) {
103 for (int i = a.length(); --i >= 0;) {
104 a.set(i, a.get(i).mean(b.get(i)));
105 }
106 return a;
107 }
108
109 @Override
110 public int hashCode() {
111 return Hash.of(getClass()).and(super.hashCode()).value();
112 }
113
114 @Override
115 public boolean equals(final Object obj) {
116 return obj instanceof MeanAlterer && super.equals(obj);
117 }
118
119 @Override
120 public String toString() {
121 return format("%s[p=%f]", getClass().getSimpleName(), _probability);
122 }
123
124 }
|