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