TruncationSelector.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.0.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.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 &mdash; <em>$Date: 2014-12-28 $</em>
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         population.sortWith(opt.<C>descending());
080         final Population<G, C> selection = new Population<>(count);
081         int size = count;
082         do {
083             final int length = Math.min(population.size(), size);
084             selection.addAll(population.subList(0, length));
085             size -= length;
086         while (size > 0);
087 
088         return selection;
089     }
090 
091     @Override
092     public int hashCode() {
093         return Hash.of(getClass()).value();
094     }
095 
096     @Override
097     public boolean equals(final Object obj) {
098         return Equality.ofType(this, obj);
099     }
100 
101     @Override
102     public String toString() {
103         return getClass().getName();
104     }
105 
106 }