smart-dynamic-gravity-tsp-rs

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.

No Dependencies — Only uses Rust standard library.

  • Dynamic Gravity Algorithm — Physics-inspired heuristic with inertia and angle penalty
  • Near-optimal solutions — ~3.0% better than classical greedy on 1000 cities
  • Fast execution — ~0.082s for 1000 cities (110x faster than greedy)
  • 2-opt optimization — Optional post-processing for improved solutions
  • Simple API — Easy to integrate into your projects
  • No dependencies — Only uses Rust standard library

Dynamic Gravity — A novel physics-inspired heuristic that simulates attraction and inertia:

  • Inertia mechanism — Maintains directional memory for smoother routes
  • Angle penalty — Penalizes sharp turns for more natural paths
  • 2-opt optimization — Post-processing for improved solutions

Parameters:

ParameterTypeDefaultDescription
deltaf640.9Inertia coefficient (higher = more inertia)
post_optimizebooltrueEnable 2-opt optimization
max_2opt_iterusize100Maximum 2-opt iterations
angle_penalty_weightf640.3Penalty for sharp turns
use_angle_penaltybooltrueEnable/disable angle penalty
_2opt_windowusize50Search window for 2-opt

Benchmark: 1000 Cities (Random Distribution)

AlgorithmDistanceTime (1000 cities)Speedup vs GreedyComplexity
Dynamic Gravity13,291.140.082s110x faster 🚀O(n²)
Greedy (baseline)13,695.349.107s1x (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).

Add 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:

ProjectDescriptionLanguage
Exact TSP SolverDual-mode TSP solver in Go: exact Branch & Bound with proven optimality, plus heuristic threshold search with reference gap.Go
Smart TSP OracleExact solver with adaptive thresholdingPython
Smart TSP SolverHeuristic solver with Angular-Radial & Dynamic GravityPython
Smart TSP BenchmarkProfessional testing infrastructurePython
smart-pch-tspUniversal PCH path improverRust
smart-dynamic-gravity-tspHigh-performance Rust library for TSPRust

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

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

Links


Created by Alexander Suvorov Sr.

Copyright © 2026, Alexander Suvorov. All rights reserved.

Rust TSP Dynamic Gravity Heuristic Optimization 2-opt