High-performance Rust library for solving the Traveling Salesman Problem (TSP)
Physics-inspired heuristic with inertia and angle penalty for near-optimal solutions.
smart-dynamic-gravity-tsp-rs — high-performance Rust library for solving the Traveling Salesman Problem (TSP) using the novel Dynamic Gravity algorithm.
Dynamic Gravity is a physics-inspired heuristic that simulates attraction and inertia to build near-optimal routes. It achieves ~3.0% better solutions than classical greedy on 1000 cities while being 110x faster.
Dynamic Gravity — A novel physics-inspired heuristic that simulates attraction and inertia:
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
delta | f64 | 0.9 | Inertia coefficient (higher = more inertia) |
post_optimize | bool | true | Enable 2-opt optimization |
max_2opt_iter | usize | 100 | Maximum 2-opt iterations |
angle_penalty_weight | f64 | 0.3 | Penalty for sharp turns |
use_angle_penalty | bool | true | Enable/disable angle penalty |
_2opt_window | usize | 50 | Search window for 2-opt |
Benchmark: 1000 Cities (Random Distribution)
| Algorithm | Distance | Time (1000 cities) | Speedup vs Greedy | Complexity |
|---|---|---|---|---|
| Dynamic Gravity | 13,291.14 | 0.082s | 110x faster 🚀 | O(n²) |
| Greedy (baseline) | 13,695.34 | 9.107s | 1x (baseline) | O(n²) |
Key insight: Both algorithms have O(n²) complexity, but Dynamic Gravity is 110x faster while also producing better quality solutions (3.0% improvement).
This is achieved through:
sqrt() operations in the main loopAdd to your Cargo.toml:
[dependencies] smart-dynamic-gravity-tsp = "0.1"
use smart_dynamic_gravity_tsp::{City, dynamic_gravity_solve};
let cities = vec![
City { x: 0.0, y: 0.0 },
City { x: 1.0, y: 0.0 },
City { x: 0.0, y: 1.0 },
City { x: 1.0, y: 1.0 },
];
let (distance, path) = dynamic_gravity_solve(
&cities,
0.9, // inertia coefficient (0.0-1.0)
true, // enable 2-opt optimization
100, // 2-opt iterations
0.3, // angle penalty weight (0.0-1.0)
true, // use angle penalty
50, // 2-opt search window
);
println!("Distance: {:.2}", distance);
println!("Path: {:?}", path);
This library is part of the NP Problem Ecosystem — a comprehensive suite of exact and heuristic solvers for the Traveling Salesman Problem:
| Project | Description | Language |
|---|---|---|
| Exact TSP Solver | Dual-mode TSP solver in Go: exact Branch & Bound with proven optimality, plus heuristic threshold search with reference gap. | Go |
| Smart TSP Oracle | Exact solver with adaptive thresholding | Python |
| Smart TSP Solver | Heuristic solver with Angular-Radial & Dynamic Gravity | Python |
| Smart TSP Benchmark | Professional testing infrastructure | Python |
| smart-pch-tsp | Universal PCH path improver | Rust |
| smart-dynamic-gravity-tsp | High-performance Rust library for TSP | Rust |
All projects are grounded in the Position-Candidate-Hypothesis (PCH) paradigm for NP-complete problems.
# Clone repository git clone https://github.com/smartlegionlab/smart-dynamic-gravity-tsp-rs cd smart-dynamic-gravity-tsp-rs # Build cargo build # Run tests cargo test # Run examples cargo run --example basic --release cargo run --example compare --release # Run benchmarks cargo bench # Build documentation cargo doc --open
Publishing:
# Login to crates.io (one time) cargo login # Publish new version # 1. Update version in Cargo.toml # 2. Run: cargo publish # Create and push git tag git tag v0.1.0 git push origin v0.1.0
By using this software, you agree to the full disclaimer terms.
Software provided "AS IS" without warranty. You assume all risks.
Full legal disclaimer: See DISCLAIMER.md
License: BSD 3-Clause License