A high-performance, production-ready Traveling Salesman Problem (TSP) solver written in .NET 9, featuring multiple optimization algorithms, comprehensive logging, native AOT compilation, and cross-platform support.
- ๐ง Multiple Algorithms: Four distinct TSP solving approaches with different trade-offs
- โก Native AOT: Single-file executable with no runtime dependencies
- ๐ Comprehensive Logging: Structured logging with Serilog (console + rotating file logs)
- ๐ Cross-Platform: Windows, Linux, macOS (x64 & ARM64)
- ๐ณ Docker Ready: Multi-stage optimized container builds
- โ Well-Tested: 99 unit tests + BDD scenarios with Reqnroll
- Interactive Mode: User-friendly console interface
- Benchmark Suite: Side-by-side algorithm comparison
- Visual Demonstration: Step-by-step algorithm visualization
- Progress Tracking: Real-time algorithm progress reporting
- Cancellation Support: Graceful interruption of long-running operations
- Multiple City Patterns: Random, circular, and grid generation
Get the latest release from GitHub Releases:
Platform | Binary | Size |
---|---|---|
Windows x64 | TSP-win-x64-{sha}.exe |
~3-5 MB |
Linux x64 | TSP-linux-x64-{sha} |
~3-5 MB |
macOS Intel | TSP-osx-x64-{sha} |
~3-5 MB |
macOS ARM64 | TSP-osx-arm64-{sha} |
~3-5 MB |
# Linux/macOS
chmod +x TSP-linux-x64-*
./TSP-linux-x64-*
# Windows
TSP-win-x64-*.exe
# Build and run
docker build -t tsp-solver .
docker run --rm -it tsp-solver
# With persistent logs
docker run --rm -it -v $(pwd)/logs:/app/logs tsp-solver
# Benchmark mode (non-interactive)
echo -e "2\n15\n5\n" | docker run --rm -i tsp-solver
# Clone
git clone https://github.com/kusl/tsp.git
cd tsp
# Build & Test
dotnet build TSP.sln
dotnet test TSP.sln
# Run
dotnet run --project TravelingSalesman.ConsoleApp
# Publish native AOT
dotnet publish TravelingSalesman.ConsoleApp \
-c Release \
-r linux-x64 \
--self-contained \
-p:PublishAot=true
Algorithm | Time Complexity | Solution Quality | Speed | Best For |
---|---|---|---|---|
Nearest Neighbor | O(nยฒ) | โ โ โ โโ | โกโกโกโกโก | Real-time, initial solutions |
2-Opt | O(nยฒ) per iteration | โ โ โ โ โ | โกโกโกโก | Quick improvements |
Simulated Annealing | O(n ร iterations) | โ โ โ โ โ | โกโกโก | Escaping local optima |
Genetic Algorithm | O(pop ร gen ร n) | โ โ โ โ โ | โก | Best quality, large problems |
- < 20 cities: Nearest Neighbor + 2-Opt (< 100ms)
- 20-50 cities: Simulated Annealing (< 5s)
- 50-100 cities: Tuned Genetic Algorithm (< 30s)
- > 100 cities: Scaled Genetic Algorithm with early stopping
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ TRAVELING SALESMAN PROBLEM SOLVER v1.2.0 โ
โ .NET 9 Implementation โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Main Menu:
1. Interactive Solver - Solve custom TSP instances
2. Algorithm Benchmark - Compare all algorithms
3. Visual Demonstration - See algorithms in action
4. Algorithm Information - Learn about each algorithm
5. Exit
โค Select an option (1-5):
# Version information
./TSP-linux-x64-* --version
# Help
./TSP-linux-x64-* --help
# View logs
tail -f logs/tsp-solver-$(date +%Y-%m-%d).log
TSP/
โโโ TravelingSalesman.Core/ # Core algorithms & data structures
โ โโโ City # Immutable city record
โ โโโ Tour # Tour with distance caching
โ โโโ ITspSolver # Solver interface
โ โโโ TspSolverBase # Base solver with common logic
โ โโโ NearestNeighborSolver # Greedy heuristic
โ โโโ TwoOptSolver # Local search improvement
โ โโโ SimulatedAnnealingSolver # Metaheuristic with cooling
โ โโโ GeneticAlgorithmSolver # Population-based evolution
โ โโโ TspDataGenerator # Test data generation
โ โโโ TspBenchmark # Performance comparison
โ
โโโ TravelingSalesman.ConsoleApp/ # Interactive console UI
โ โโโ Program.cs # Main entry point with menus
โ
โโโ TravelingSalesman.Tests/ # xUnit tests (99 tests)
โ โโโ Tests.cs # Comprehensive test coverage
โ
โโโ TravelingSalesman.Specs/ # BDD tests with Reqnroll
โ โโโ Features/ # Gherkin scenarios
โ โโโ StepDefinitions/ # Test implementations
โ
โโโ Directory.Build.props # Centralized MSBuild settings
โโโ Directory.Packages.props # Central package management
โโโ Dockerfile # Multi-stage container build
โโโ .github/workflows/ # CI/CD automation
- .NET 9 SDK (Download)
- Docker (optional)
- Git
# Run all tests
dotnet test TSP.sln
# With coverage
dotnet test TSP.sln \
--collect:"XPlat Code Coverage" \
--results-directory ./coverage
# Run specific tests
dotnet test --filter "FullyQualifiedName~TspBenchmark"
# Run BDD tests only
dotnet test TravelingSalesman.Specs
The project enforces:
- โ
TreatWarningsAsErrors: true
- โ
AnalysisLevel: latest
- โ Nullable reference types
- โ Latest C# language features
- โ Central package management
- Native AOT reduces memory footprint by ~50%
- Tour distance caching prevents recalculation
- Efficient distance matrix for O(1) lookups
- Nearest Neighbor: < 1ms
- 2-Opt: < 10ms
- Simulated Annealing: < 100ms
- Genetic Algorithm: < 500ms
Multi-stage Dockerfile with:
- Native AOT compilation in build stage
- Minimal runtime dependencies
- Log directory auto-creation
- Support for VS debugging
- Optimized layer caching
# Build
docker build -t tsp-solver .
# Run with resource limits
docker run --rm -it \
--memory="512m" \
--cpus="1.0" \
tsp-solver
Structured logging with Serilog:
logs/
โโโ tsp-solver-2025-01-17.log # Today's detailed logs
โโโ tsp-solver-2025-01-16.log # Yesterday's logs
โโโ ... # 30-day retention
Log levels:
- Information: User actions, results
- Debug: Algorithm internals
- Trace: Detailed execution flow
- Warning: Performance issues
- Error: Exceptions and failures
GitHub Actions workflow:
- Build & Test: Every push/PR
- Docker validation: Container testing
- Native AOT: Multi-platform compilation
- Release automation: Tagged releases
- Artifact upload: Binaries & logs
This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0).
- โ You CAN: Use, modify, distribute, use privately
- โ You CAN: Use commercially (with source disclosure)
โ ๏ธ You MUST: Disclose source codeโ ๏ธ You MUST: Include copyright & license noticesโ ๏ธ You MUST: State changes madeโ ๏ธ You MUST: Share under same license (AGPL-3.0)- โ You CANNOT: Hold liable or remove warranty disclaimers
Important: If you use this software as a network service, you must provide source code to users.
See LICENSE.txt for full terms.
We welcome contributions! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Write tests for new functionality
- Ensure all tests pass (
dotnet test
) - Commit with clear messages (
git commit -m 'Add amazing feature'
) - Push to your fork (
git push origin feature/amazing-feature
) - Open a Pull Request
- Follow existing code style
- Add XML documentation for public APIs
- Write unit tests for new features
- Update README for significant changes
- Simulated Annealing Performance: May take >20s for small problems with default parameters
- Workaround: Reduce
initialTemperature
anditerationsPerTemperature
- Fix: Adaptive parameter tuning based on city count (planned)
- Workaround: Reduce
Kushal Hada
- GitHub: @kusl
- Repository: github.com/kusl/tsp
- .NET team for excellent Native AOT support
- Serilog contributors for robust logging
- xUnit and Reqnroll teams for testing frameworks
- All contributors and issue reporters
๐ฌ Academic Use: If you use this software in research, please cite:
Hada, K. (2025). TSP Solver: A Multi-Algorithm Approach.
GitHub. https://github.com/kusl/tsp