Implementation:Haifengl Smile Chromosome
| Knowledge Sources | |
|---|---|
| Domains | Genetic Algorithms, Optimization, Evolutionary Computation |
| Last Updated | 2026-02-08 22:00 GMT |
Overview
Chromosome is the core interface in the Smile genetic algorithm package that defines the contract for candidate solutions participating in evolutionary optimization.
Description
The Chromosome<T> interface provides the abstraction layer for any chromosome representation used in genetic algorithms or genetic programming. It is a generic interface parameterized by its own type (T extends Chromosome<T>), enabling type-safe operations between chromosomes of the same kind.
The interface extends Comparable<Chromosome<T>> to support fitness-based comparison, which is essential for selection strategies that sort or rank the population.
The four core operations defined are:
- fitness() -- evaluate and return the chromosome's fitness score.
- newInstance() -- create a new random chromosome (factory method for population initialization).
- crossover(T other) -- produce offspring by combining genetic material with another chromosome.
- mutate() -- apply random changes to the chromosome's genetic representation.
For Lamarckian genetic algorithms, the mutate method may perform local search (e.g., hill-climbing) instead of purely random mutation.
Usage
Implement the Chromosome interface when you need a custom chromosome representation beyond the built-in BitString. Any implementation can be used with GeneticAlgorithm for evolutionary optimization.
Code Reference
Source Location
- Repository: Haifengl_Smile
- File: base/src/main/java/smile/gap/Chromosome.java
- Lines: 1-59
Signature
public interface Chromosome<T extends Chromosome<T>> extends Comparable<Chromosome<T>> {
/**
* Returns the fitness of chromosome.
*/
double fitness();
/**
* Returns a new random instance.
*/
T newInstance();
/**
* Returns a pair of offsprings by crossovering this one with another one
* according to the crossover rate.
*/
T[] crossover(T other);
/**
* Mutates the chromosome randomly. For Lamarckian algorithms,
* this method performs local search such as hill-climbing.
*/
void mutate();
}
Import
import smile.gap.Chromosome;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| other | T | Yes (crossover) | The other parent chromosome for crossover. |
Outputs
| Name | Type | Description |
|---|---|---|
| fitness() | double | The fitness score of the chromosome. Higher values indicate better fitness. |
| newInstance() | T | A new randomly initialized chromosome of the same type. |
| crossover() | T[] | An array of two offspring produced from the crossover operation. |
Usage Examples
Implementing a Custom Chromosome
import smile.gap.Chromosome;
import smile.gap.GeneticAlgorithm;
public class RealChromosome implements Chromosome<RealChromosome> {
private double[] genes;
public RealChromosome(double[] genes) {
this.genes = genes;
}
@Override
public double fitness() {
// Evaluate fitness of this solution
double sum = 0;
for (double g : genes) sum += g * g;
return -sum; // Minimize sum of squares
}
@Override
public RealChromosome newInstance() {
double[] random = new double[genes.length];
for (int i = 0; i < random.length; i++) {
random[i] = Math.random() * 2 - 1;
}
return new RealChromosome(random);
}
@Override
@SuppressWarnings("unchecked")
public RealChromosome[] crossover(RealChromosome other) {
// Simple arithmetic crossover
double[] child1 = new double[genes.length];
double[] child2 = new double[genes.length];
for (int i = 0; i < genes.length; i++) {
child1[i] = 0.5 * genes[i] + 0.5 * other.genes[i];
child2[i] = 0.5 * other.genes[i] + 0.5 * genes[i];
}
return new RealChromosome[]{new RealChromosome(child1), new RealChromosome(child2)};
}
@Override
public void mutate() {
int idx = (int)(Math.random() * genes.length);
genes[idx] += Math.random() * 0.1 - 0.05;
}
@Override
public int compareTo(Chromosome o) {
return Double.compare(fitness(), o.fitness());
}
}