Implementation:Haifengl Smile GA Parent Selection
| Knowledge Sources | |
|---|---|
| Domains | Genetic Algorithms, Optimization, Evolutionary Computation |
| Last Updated | 2026-02-08 22:00 GMT |
Overview
Selection is a functional interface defining how chromosomes are selected from the population as parents for crossover in the Smile genetic algorithm package.
Description
The Selection interface provides a single abstract method apply that takes a sorted population (ascending by fitness) and returns a selected chromosome. The interface includes four static factory methods that return pre-built selection strategies:
- RouletteWheel() -- fitness-proportionate selection. Each chromosome is selected with probability proportional to its fitness. Negative fitness values are automatically shifted to be non-negative.
- ScaledRouletteWheel() -- a variant of roulette wheel that subtracts the worst fitness from all chromosomes to improve selective pressure when fitness variance is low across the population.
- Rank() -- rank-based selection. Chromosomes are assigned selection probabilities based on their rank in the sorted population rather than raw fitness values. This avoids dominance by a single very-fit individual but may lead to slower convergence.
- Tournament(int size, double probability) -- tournament selection. Randomly selects
sizeindividuals and returns the best with the given probability, the second-best with probabilityp*(1-p), and so on. This is the default and most commonly used strategy (default: size=3, probability=0.95).
Tournament selection is recommended as the primary selection technique because it is simple, parallelizable, not sensitive to fitness function details, and tunable via the tournament size.
Usage
Use the Selection factory methods to obtain a selection strategy, then pass it to the GeneticAlgorithm constructor. The default constructor of GeneticAlgorithm uses Selection.Tournament(3, 0.95).
Code Reference
Source Location
- Repository: Haifengl_Smile
- File: base/src/main/java/smile/gap/Selection.java
- Lines: 1-180
Signature
public interface Selection {
/**
* Select a chromosome with replacement from the population.
* The population should be in ascending order of fitness.
*/
<T extends Chromosome<T>> T apply(T[] population);
// Factory methods
static Selection RouletteWheel();
static Selection ScaledRouletteWheel();
static Selection Rank();
static Selection Tournament(int size, double probability);
}
Import
import smile.gap.Selection;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| population | T[] | Yes | An array of chromosomes sorted in ascending order by fitness. |
| size | int | Yes (Tournament) | The number of individuals to include in each tournament. |
| probability | double | Yes (Tournament) | The probability that the best individual in the tournament wins (0.0 to 1.0). |
Outputs
| Name | Type | Description |
|---|---|---|
| apply() | T | A single chromosome selected from the population to serve as a parent. |
Usage Examples
Basic Usage
import smile.gap.BitString;
import smile.gap.Fitness;
import smile.gap.GeneticAlgorithm;
import smile.gap.Selection;
Fitness<BitString> fitness = ch -> {
byte[] bits = ch.bits();
int count = 0;
for (byte b : bits) count += b;
return count;
};
BitString[] population = new BitString[50];
for (int i = 0; i < population.length; i++) {
population[i] = new BitString(100, fitness);
}
// Use tournament selection with pool size 5 and 90% best-wins probability
Selection selection = Selection.Tournament(5, 0.90);
GeneticAlgorithm<BitString> ga = new GeneticAlgorithm<>(population, selection, 1);
BitString best = ga.evolve(200);
Using Different Selection Strategies
import smile.gap.Selection;
// Roulette wheel: proportional to fitness
Selection roulette = Selection.RouletteWheel();
// Scaled roulette wheel: better for low-variance populations
Selection scaledRoulette = Selection.ScaledRouletteWheel();
// Rank-based: avoids dominance by single very-fit individual
Selection rank = Selection.Rank();
// Tournament: the default and most commonly used
Selection tournament = Selection.Tournament(3, 0.95);