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

io.jenetics.StochasticUniversalSelector Maven / Gradle / Ivy

There is a newer version: 8.1.0
Show newest version
/*
 * Java Genetic Algorithm Library (jenetics-7.1.2).
 * Copyright (c) 2007-2023 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 io.jenetics;

import static java.util.Objects.requireNonNull;

import io.jenetics.util.ISeq;
import io.jenetics.util.MSeq;
import io.jenetics.util.RandomRegistry;
import io.jenetics.util.Seq;

/**
 * {@code StochasticUniversalSelector} is a method for selecting a
 * population according to some given probability in a way that minimize chance
 * fluctuations. It can be viewed as a type of roulette game where now we have
 * P equally spaced points which we spin.
 *
 * 

* Selector *

* * The figure above shows how the stochastic-universal selection works; n * is the number of individuals to select. * * @see * Wikipedia: Stochastic universal sampling * * * @author Franz Wilhelmstötter * @since 1.0 * @version 5.0 */ public class StochasticUniversalSelector< G extends Gene, N extends Number & Comparable > extends RouletteWheelSelector { public StochasticUniversalSelector() { super(true); } /** * This method sorts the population in descending order while calculating the * selection probabilities. */ @Override public ISeq> select( final Seq> population, final int count, final Optimize opt ) { requireNonNull(population, "Population"); if (count < 0) { throw new IllegalArgumentException( "Selection count must be greater or equal then zero, but was " + count ); } if (count == 0 || population.isEmpty()) { return ISeq.empty(); } final MSeq> selection = MSeq.ofLength(count); final Seq> pop = _sorted ? population.asISeq().copy().sort(POPULATION_COMPARATOR) : population; final double[] probabilities = probabilities(pop, count, opt); assert pop.size() == probabilities.length; //Calculating the equally spaces random points. final double delta = 1.0/count; final double[] points = new double[count]; points[0] = RandomRegistry.random().nextDouble()*delta; for (int i = 1; i < count; ++i) { points[i] = delta*i; } int j = 0; double prop = 0; for (int i = 0; i < count; ++i) { while (points[i] > prop) { prop += probabilities[j]; ++j; } selection.set(i, pop.get(j%pop.size())); } return selection.toISeq(); } @Override public String toString() { return getClass().getSimpleName(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy