001 /*
002 * Java Genetic Algorithm Library (jenetics-1.6.0).
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 1.0 — <em>$Date: 2014-02-27 $</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 the
056 * selection probabilities. (The method {@link Population#sort()} is called
057 * by this method.)
058 *
059 * @throws IllegalArgumentException if the sample size is greater than the
060 * population size or {@code count} is greater the the population
061 * size.
062 * @throws NullPointerException if the {@code population} is {@code null}.
063 */
064 @Override
065 public Population<G, C> select(
066 final Population<G, C> population,
067 final int count,
068 final Optimize opt
069 ) {
070 requireNonNull(population, "Population");
071 requireNonNull(opt, "Optimization");
072 if (count < 0) {
073 throw new IllegalArgumentException(format(
074 "Selection count must be greater or equal then zero, but was %s",
075 count
076 ));
077 }
078 if (count > population.size()) {
079 throw new IllegalArgumentException(format(
080 "Selection size greater than population size: %s > %s",
081 count, population.size()
082 ));
083 }
084
085 population.sortWith(opt.<C>descending());
086 return new Population<>(population.subList(0, count));
087 }
088
089 @Override
090 public int hashCode() {
091 return HashBuilder.of(getClass()).value();
092 }
093
094 @Override
095 public boolean equals(final Object obj) {
096 return obj == this || obj instanceof TruncationSelector<?, ?>;
097 }
098
099 @Override
100 public String toString() {
101 return getClass().getName();
102 }
103
104 }
|