001 /*
002 * Java Genetic Algorithm Library (jenetics-1.5.0).
003 * Copyright (c) 2007-2013 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 import static org.jenetics.util.object.hashCodeOf;
025
026 /**
027 * In truncation selection individuals are sorted according to their fitness.
028 * Only the n best individuals are selected. The truncation selection is a very
029 * basic selection algorithm. It has it's strength in fast selecting individuals
030 * in large populations, but is not very often used in practice.
031 *
032 * @see <a href="http://en.wikipedia.org/wiki/Truncation_selection">
033 * Wikipedia: Truncation selection
034 * </a>
035 *
036 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
037 * @since 1.0
038 * @version 1.0 — <em>$Date: 2013-12-02 $</em>
039 */
040 public final class TruncationSelector<
041 G extends Gene<?, G>,
042 C extends Comparable<? super C>
043 >
044 implements Selector<G, C>
045 {
046
047 /**
048 * Create a new TruncationSelector object.
049 */
050 public TruncationSelector() {
051 }
052
053 /**
054 * This method sorts the population in descending order while calculating the
055 * selection probabilities. (The method {@link Population#sort()} is called
056 * by this method.)
057 *
058 * @throws IllegalArgumentException if the sample size is greater than the
059 * population size or {@code count} is greater the the population
060 * size.
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 if (count > population.size()) {
078 throw new IllegalArgumentException(format(
079 "Selection size greater than population size: %s > %s",
080 count, population.size()
081 ));
082 }
083
084 population.sortWith(opt.<C>descending());
085 return new Population<>(population.subList(0, count));
086 }
087
088 @Override
089 public int hashCode() {
090 return hashCodeOf(getClass()).value();
091 }
092
093 @Override
094 public boolean equals(final Object obj) {
095 return obj == this || obj instanceof TruncationSelector<?, ?>;
096 }
097
098 @Override
099 public String toString() {
100 return getClass().getName();
101 }
102
103 }
104
105
106
107
108
109
110
111
112
113
|