001 /*
002 * Java Genetic Algorithm Library (jenetics-4.0.0).
003 * Copyright (c) 2007-2017 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@gmail.com)
019 */
020 package io.jenetics;
021
022 import static java.lang.Math.min;
023 import static java.lang.String.format;
024 import static java.util.Objects.requireNonNull;
025
026 import io.jenetics.internal.util.Equality;
027 import io.jenetics.internal.util.Hash;
028 import io.jenetics.util.ISeq;
029 import io.jenetics.util.MSeq;
030 import io.jenetics.util.Seq;
031
032 /**
033 * In truncation selection individuals are sorted according to their fitness.
034 * Only the n best individuals are selected. The truncation selection is a very
035 * basic selection algorithm. It has it's strength in fast selecting individuals
036 * in large populations, but is not very often used in practice.
037 *
038 * @see <a href="http://en.wikipedia.org/wiki/Truncation_selection">
039 * Wikipedia: Truncation selection
040 * </a>
041 *
042 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
043 * @since 1.0
044 * @version 4.0
045 */
046 public final class TruncationSelector<
047 G extends Gene<?, G>,
048 C extends Comparable<? super C>
049 >
050 implements Selector<G, C>
051 {
052
053 private final int _n;
054
055 /**
056 * Create a new {@code TruncationSelector} object, where the worst selected
057 * individual has rank {@code n}. This means, if you want to select
058 * {@code count} individuals, the worst selected individual has rank
059 * {@code n}. If {@code count > n}, the selected population will contain
060 * <em>duplicate</em> individuals.
061 *
062 * @since 3.8
063 *
064 * @param n the worst rank of the selected individuals
065 * @throws IllegalArgumentException if {@code n < 1}
066 */
067 public TruncationSelector(final int n) {
068 if (n < 1) {
069 throw new IllegalArgumentException(format(
070 "n must be greater or equal 1, but was %d.", n
071 ));
072 }
073
074 _n = n;
075 }
076
077 /**
078 * Create a new TruncationSelector object.
079 */
080 public TruncationSelector() {
081 this(Integer.MAX_VALUE);
082 }
083
084 /**
085 * This method sorts the population in descending order while calculating
086 * the selection probabilities. If the selection size is greater the the
087 * population size, the whole population is duplicated until the desired
088 * sample size is reached.
089 *
090 * @throws NullPointerException if the {@code population} or {@code opt} is
091 * {@code null}.
092 */
093 @Override
094 public ISeq<Phenotype<G, C>> select(
095 final Seq<Phenotype<G, C>> population,
096 final int count,
097 final Optimize opt
098 ) {
099 requireNonNull(population, "Population");
100 requireNonNull(opt, "Optimization");
101 if (count < 0) {
102 throw new IllegalArgumentException(format(
103 "Selection count must be greater or equal then zero, but was %s",
104 count
105 ));
106 }
107
108 final MSeq<Phenotype<G, C>> selection = MSeq
109 .ofLength(population.isEmpty() ? 0 : count);
110
111 if (count > 0 && !population.isEmpty()) {
112 final MSeq<Phenotype<G, C>> copy = population.asISeq().copy();
113 copy.sort((a, b) ->
114 opt.<C>descending().compare(a.getFitness(), b.getFitness()));
115
116 int size = count;
117 do {
118 final int length = min(min(copy.size(), size), _n);
119 for (int i = 0; i < length; ++i) {
120 selection.set((count - size) + i, copy.get(i));
121 }
122
123 size -= length;
124 } while (size > 0);
125 }
126
127 return selection.toISeq();
128 }
129
130 @Override
131 public int hashCode() {
132 return Hash.of(getClass()).value();
133 }
134
135 @Override
136 public boolean equals(final Object obj) {
137 return Equality.ofType(this, obj);
138 }
139
140 @Override
141 public String toString() {
142 return getClass().getName();
143 }
144
145 }
|