Implementation:Haifengl Smile Crossover
| Knowledge Sources | |
|---|---|
| Domains | Genetic Algorithms, Optimization, Evolutionary Computation |
| Last Updated | 2026-02-08 22:00 GMT |
Overview
Crossover is an enum defining the types of crossover operations available for bit-string chromosomes in the Smile genetic algorithm package.
Description
The Crossover enum encapsulates three classical crossover strategies used in genetic algorithms operating on binary string representations. Each enum constant implements the abstract apply method to produce two offspring from two parent BitString chromosomes.
The three supported crossover strategies are:
- SINGLE_POINT -- selects one random crossover point. The binary string from the beginning to the crossover point is copied from the first parent, and the rest from the second parent. The second offspring receives the complementary segments.
- TWO_POINT -- selects two random crossover points. The segment between the two points is swapped between parents. This is the default crossover strategy used by
BitString.
- UNIFORM -- each bit is independently and randomly copied from either parent with equal probability (50/50). This provides maximum mixing of genetic material.
All strategies produce exactly two offspring (son and daughter) from two parents (father and mother), maintaining population size during evolution.
Usage
Use the Crossover enum when constructing BitString chromosomes to specify the desired crossover strategy. The default is TWO_POINT.
Code Reference
Source Location
- Repository: Haifengl_Smile
- File: base/src/main/java/smile/gap/Crossover.java
- Lines: 1-136
Signature
public enum Crossover {
SINGLE_POINT {
@Override
public BitString[] apply(BitString father, BitString mother) { ... }
},
TWO_POINT {
@Override
public BitString[] apply(BitString father, BitString mother) { ... }
},
UNIFORM {
@Override
public BitString[] apply(BitString father, BitString mother) { ... }
};
/**
* Returns a pair of offsprings by crossovering parent chromosomes.
*/
public abstract BitString[] apply(BitString father, BitString mother);
}
Import
import smile.gap.Crossover;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| father | BitString | Yes | The first parent chromosome. |
| mother | BitString | Yes | The second parent chromosome. Must have the same bit string length as the father. |
Outputs
| Name | Type | Description |
|---|---|---|
| result | BitString[] | An array of two offspring BitString chromosomes created by combining genetic material from the parents. |
Usage Examples
Basic Usage
import smile.gap.BitString;
import smile.gap.Crossover;
import smile.gap.Fitness;
// Define a simple fitness function
Fitness<BitString> fitness = chromosome -> {
byte[] bits = chromosome.bits();
int count = 0;
for (byte b : bits) count += b;
return count;
};
// Create chromosomes with single-point crossover
BitString parent1 = new BitString(32, fitness, Crossover.SINGLE_POINT, 0.9, 0.01);
BitString parent2 = new BitString(32, fitness, Crossover.SINGLE_POINT, 0.9, 0.01);
// Directly apply crossover
BitString[] offspring = Crossover.SINGLE_POINT.apply(parent1, parent2);
System.out.println("Offspring 1: " + offspring[0]);
System.out.println("Offspring 2: " + offspring[1]);
Comparing Crossover Strategies
import smile.gap.BitString;
import smile.gap.Crossover;
import smile.gap.Fitness;
Fitness<BitString> fitness = ch -> {
byte[] bits = ch.bits();
int score = 0;
for (byte b : bits) score += b;
return score;
};
// Create populations with different crossover strategies
for (Crossover strategy : Crossover.values()) {
BitString[] pop = new BitString[30];
for (int i = 0; i < pop.length; i++) {
pop[i] = new BitString(64, fitness, strategy, 0.9, 0.01);
}
// Use with GeneticAlgorithm...
}