View Javadoc
1   package net.bmahe.genetics4j.core.selection;
2   
3   import java.util.List;
4   
5   import net.bmahe.genetics4j.core.Genotype;
6   import net.bmahe.genetics4j.core.Population;
7   import net.bmahe.genetics4j.core.spec.AbstractEAConfiguration;
8   
9   /**
10   * Functional interface for selecting individuals from a population in evolutionary algorithms.
11   * 
12   * <p>Selection is a fundamental operator in evolutionary algorithms that determines which individuals from the current
13   * population will be chosen to participate in reproduction. The selection pressure influences the direction and speed
14   * of evolution by favoring individuals with better fitness values.
15   * 
16   * <p>Selection operates on a population with associated fitness values and returns a subset of individuals based on the
17   * specific selection strategy. The selected individuals typically become parents for the next generation through
18   * crossover and mutation operations.
19   * 
20   * <p>Common selection strategies include:
21   * <ul>
22   * <li><strong>Tournament selection</strong>: Randomly sample candidates and select the best</li>
23   * <li><strong>Roulette wheel selection</strong>: Probabilistic selection based on fitness proportion</li>
24   * <li><strong>Rank-based selection</strong>: Selection based on fitness ranking rather than raw values</li>
25   * <li><strong>Proportional selection</strong>: Probability proportional to relative fitness</li>
26   * <li><strong>Elitism</strong>: Always select the best individuals</li>
27   * </ul>
28   * 
29   * <p>Selection balances two competing objectives:
30   * <ul>
31   * <li><strong>Exploitation</strong>: Favor high-fitness individuals to improve solution quality</li>
32   * <li><strong>Exploration</strong>: Maintain diversity by giving lower-fitness individuals a chance</li>
33   * </ul>
34   * 
35   * <p>Implementations should be:
36   * <ul>
37   * <li><strong>Stochastic</strong>: Use randomization to prevent premature convergence</li>
38   * <li><strong>Fitness-aware</strong>: Consider fitness values when making selection decisions</li>
39   * <li><strong>Configurable</strong>: Support parameters to adjust selection pressure</li>
40   * </ul>
41   * 
42   * @param <T> the type of the fitness values, must be comparable for ranking operations
43   * @see net.bmahe.genetics4j.core.spec.selection.SelectionPolicy
44   * @see Population
45   * @see Genotype
46   */
47  @FunctionalInterface
48  public interface Selector<T extends Comparable<T>> {
49  
50  	/**
51  	 * Selects a specified number of individuals from the given population based on their fitness values.
52  	 * 
53  	 * <p>This method implements the core selection logic, choosing individuals according to the specific selection
54  	 * strategy. The selection process typically involves evaluating fitness values and applying probabilistic or
55  	 * deterministic rules to choose parents for reproduction.
56  	 * 
57  	 * <p>The selection process should:
58  	 * <ul>
59  	 * <li>Consider fitness values when making selection decisions</li>
60  	 * <li>Return exactly the requested number of individuals</li>
61  	 * <li>Allow for the possibility of selecting the same individual multiple times</li>
62  	 * <li>Maintain the integrity of the original population</li>
63  	 * </ul>
64  	 * 
65  	 * @param eaConfiguration the evolutionary algorithm configuration containing selection parameters
66  	 * @param generation      the current generation
67  	 * @param numIndividuals  the number of individuals to select from the population
68  	 * @param population      the list of genotypes in the current population
69  	 * @param fitnessScore    the list of fitness values corresponding to each genotype
70  	 * @return a new population containing the selected individuals
71  	 * @throws IllegalArgumentException if numIndividuals is negative, if population and fitnessScore have different
72  	 *                                  sizes, or if any parameter is null
73  	 * @throws IllegalStateException    if the selection process cannot complete successfully
74  	 */
75  	Population<T> select(final AbstractEAConfiguration<T> eaConfiguration, final long generation,
76  			final int numIndividuals, final List<Genotype> population, final List<T> fitnessScore);
77  }