Pure-Python implementations of a genetic algorithm (GA) and simulated annealing (SA) for global minimization of the Cross-in-Tray function, a classic multimodal benchmark with many local minima where gradient-based methods get trapped.
Both algorithms are written from scratch using only the Python standard library.
It is usually evaluated on
The surface is covered in local minima arranged in a cross pattern, which makes it a standard test for global optimizers. See the SFU benchmark reference for details.
Each individual encodes a candidate solution as two chromosomes,
| Component | Implementation |
|---|---|
| Initialization | Population sampled uniformly from |
| Fitness | Share of the population's total objective value (lower |
| Selection | Fitness-proportional (roulette-wheel) selection of two parents |
| Crossover | Child takes |
| Mutation | With 10% probability, |
| Output | Best individual in the final generation |
| Component | Implementation |
|---|---|
| Initialization | Random point sampled from a user-defined range |
| Neighbor move | Scale |
| Acceptance | Improving moves are always accepted; worse moves are accepted with probability |
| Cooling schedule | Linear: temperature decreases by 1 each iteration until it reaches 0 |
Requires Python 3. There are no external dependencies.
git clone https://github.com/ak811/cross-in-tray-optimization.git
cd cross-in-tray-optimizationpython src/Genetic.pyYou will be prompted for:
- Generation count: number of generations to evolve
- Population count: number of individuals per generation
python src/SimulatedAnnealing.pyYou will be prompted for:
- Left / right range: bounds for the random starting point (e.g.,
-10and10) - Start temperature: initial temperature, which is also the number of iterations
Both scripts print the best point found and its objective value:
x = ...
y = ...
F(x,y) = ...
Compare the result against the known global minimum of about
MIT (see LICENSE).
