All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jenetics.MeanAlterer Maven / Gradle / Ivy

There is a newer version: 3.6.0
Show newest version
/*
 * Java Genetic Algorithm Library (jenetics-3.2.0).
 * Copyright (c) 2007-2015 Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter ([email protected])
 */
package org.jenetics;

import static java.lang.String.format;

import java.util.Random;

import org.jenetics.internal.util.Hash;

import org.jenetics.util.ISeq;
import org.jenetics.util.MSeq;
import org.jenetics.util.Mean;
import org.jenetics.util.RandomRegistry;
import org.jenetics.util.Seq;

/**
 * 

* The order ({@link #getOrder()}) of this Recombination implementation is two. *

* * @author Franz Wilhelmstötter * @since 1.0 * @version 3.0 */ public final class MeanAlterer< G extends Gene & Mean, C extends Comparable > extends Recombinator { /** * Constructs an alterer with a given recombination probability. * * @param probability the crossover probability. * @throws IllegalArgumentException if the {@code probability} is not in the * valid range of {@code [0, 1]}. */ public MeanAlterer(final double probability) { super(probability, 2); } /** * Create a new alterer with alter probability of {@code 0.05}. */ public MeanAlterer() { this(0.05); } @Override protected int recombine( final Population population, final int[] individuals, final long generation ) { final Random random = RandomRegistry.getRandom(); final Phenotype pt1 = population.get(individuals[0]); final Phenotype pt2 = population.get(individuals[1]); final Genotype gt1 = pt1.getGenotype(); final Genotype gt2 = pt2.getGenotype(); final int cindex = random.nextInt(gt1.length()); final MSeq> c1 = gt1.toSeq().copy(); final ISeq> c2 = gt2.toSeq(); // Calculate the mean value of the gene array. final MSeq mean = mean( c1.get(cindex).toSeq().copy(), c2.get(cindex).toSeq() ); c1.set(cindex, c1.get(cindex).newInstance(mean.toISeq())); population.set( individuals[0], pt1.newInstance(gt1.newInstance(c1.toISeq()), generation) ); return 1; } private static & Mean> MSeq mean(final MSeq a, final Seq b) { for (int i = a.length(); --i >= 0;) { a.set(i, a.get(i).mean(b.get(i))); } return a; } @Override public int hashCode() { return Hash.of(getClass()).and(super.hashCode()).value(); } @Override public boolean equals(final Object obj) { return obj instanceof MeanAlterer && super.equals(obj); } @Override public String toString() { return format("%s[p=%f]", getClass().getSimpleName(), _probability); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy