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 import static java.util.Objects.requireNonNull;
024
025 import org.jenetics.internal.util.HashBuilder;
026
027 /**
028 * In truncation selection individuals are sorted according to their fitness.
029 * Only the n best individuals are selected. The truncation selection is a very
030 * basic selection algorithm. It has it's strength in fast selecting individuals
031 * in large populations, but is not very often used in practice.
032 *
033 * @see <a href="http://en.wikipedia.org/wiki/Truncation_selection">
034 * Wikipedia: Truncation selection
035 * </a>
036 *
037 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
038 * @since 1.0
039 * @version 2.0 — <em>$Date: 2014-08-12 $</em>
040 */
041 public final class TruncationSelector<
042 G extends Gene<?, G>,
043 C extends Comparable<? super C>
044 >
045 implements Selector<G, C>
046 {
047
048 /**
049 * Create a new TruncationSelector object.
050 */
051 public TruncationSelector() {
052 }
053
054 /**
055 * This method sorts the population in descending order while calculating
056 * the selection probabilities. (The method {@link Population#sort()} is
057 * called by this method.) If the selection size is greater the the
058 * population size, the whole population is duplicated until the desired
059 * sample size is reached.
060 *
061 * @throws NullPointerException if the {@code population} is {@code null}.
062 */
063 @Override
064 public Population<G, C> select(
065 final Population<G, C> population,
066 final int count,
067 final Optimize opt
068 ) {
069 requireNonNull(population, "Population");
070 requireNonNull(opt, "Optimization");
071 if (count < 0) {
072 throw new IllegalArgumentException(format(
073 "Selection count must be greater or equal then zero, but was %s",
074 count
075 ));
076 }
077
078 population.sortWith(opt.<C>descending());
079 final Population<G, C> selection = new Population<>(count);
080 int size = count;
081 do {
082 final int length = Math.min(population.size(), size);
083 selection.addAll(population.subList(0, length));
084 size -= length;
085 } while (size > 0);
086
087 return selection;
088 }
089
090 @Override
091 public int hashCode() {
092 return HashBuilder.of(getClass()).value();
093 }
094
095 @Override
096 public boolean equals(final Object obj) {
097 return obj == this || obj instanceof TruncationSelector<?, ?>;
098 }
099
100 @Override
101 public String toString() {
102 return getClass().getName();
103 }
104
105 }
|