README
¶
N-Queens Problem (Go + Genetic Algorithm)
This project solves the N-Queens puzzle with a genetic algorithm.
- A chromosome is a permutation of row positions (one queen per column).
- Fitness is based on diagonal conflicts (lower is better,
0is solved). - The solver uses roulette selection, PMX crossover, optional mutation, and bounded population trimming.
Requirements
- Go
1.26+
Quick Start
go run .
Run with custom parameters:
go run . -size 8 -population 1000 -max-epochs 1000 -min-to-mate 5 -max-to-mate 20 -mutation-rate 0.05 -seed 1234
CLI Flags
| Flag | Default | Description |
|---|---|---|
-size |
14 |
Number of queens and board size |
-population |
40000 |
Initial population size |
-max-epochs |
5000 |
Maximum epochs before stopping |
-min-to-mate |
10 |
Minimum offspring count per epoch |
-max-to-mate |
50 |
Maximum offspring count per epoch |
-mutation-rate |
0.03 |
Mutation probability per offspring (0.0 to 1.0) |
-seed |
-1 |
RNG seed (< 0 uses current time) |
-trace |
false |
Set log level to trace |
-debug |
false |
Set log level to debug |
-warn |
false |
Set log level to warn |
-error |
false |
Set log level to error |
Parameter Tuning Guide
Start with these presets, then adjust from there:
- Small boards (
N=8toN=16)-population 500to2000-max-epochs 500to2000-min-to-mate 4to8-max-to-mate 10to30-mutation-rate 0.02to0.08
- Larger boards (
N>=32)-population 10000to50000-max-epochs 3000to10000-min-to-mate 10to30-max-to-mate 30to120-mutation-rate 0.03to0.12
Rules of thumb:
- If the best conflict score plateaus early, increase
-mutation-rateslightly. - If progress is noisy, reduce
-mutation-rateslightly and increase-population. - If runs are too slow, reduce
-populationfirst, then-max-epochs. - Keep
-max-to-matenoticeably higher than-min-to-matefor diversity. - For difficult sizes, run multiple seeds and keep the best result.
Example commands:
# Fast local run for 8x8
go run . -size 8 -population 1000 -max-epochs 1200 -min-to-mate 5 -max-to-mate 20 -mutation-rate 0.05 -seed 42
# Stronger run for 32x32
go run . -size 32 -population 20000 -max-epochs 6000 -min-to-mate 15 -max-to-mate 80 -mutation-rate 0.06 -seed 42
Reproducible Runs
Use a fixed -seed to make runs deterministic.
go run . -size 14 -seed 2026
The configured seed and mutation rate are written to logs at startup.
Output
- The terminal prints the final board.
- Each queen cell shows its conflict count (
00means no conflicts). - Structured logs are written to
app.log.
Example solved 8x8 board:
╔════╤════╤════╤════╤════╤════╤════╤════╗
║ │ │ │ │ │ 00 │ │ ║
╟────┼────┼────┼────┼────┼────┼────┼────╢
║ 00 │ │ │ │ │ │ │ ║
╟────┼────┼────┼────┼────┼────┼────┼────╢
║ │ │ │ │ 00 │ │ │ ║
╟────┼────┼────┼────┼────┼────┼────┼────╢
║ │ 00 │ │ │ │ │ │ ║
╟────┼────┼────┼────┼────┼────┼────┼────╢
║ │ │ │ │ │ │ │ 00 ║
╟────┼────┼────┼────┼────┼────┼────┼────╢
║ │ │ 00 │ │ │ │ │ ║
╟────┼────┼────┼────┼────┼────┼────┼────╢
║ │ │ │ │ │ │ 00 │ ║
╟────┼────┼────┼────┼────┼────┼────┼────╢
║ │ │ │ 00 │ │ │ │ ║
╚════╧════╧════╧════╧════╧════╧════╧════╝
Development
Run tests:
go test ./...
Run benchmarks:
go test -run '^$' -bench PMX -benchmem ./...
go test -run '^$' -bench CountConflicts -benchmem ./...
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.