Skip to content

Built a scratch-based solution to the Traveling Salesman Problem visiting every (x, y) point once, no overlaps, no retracing. No canned math, no shortcuts. Just raw distance logic and manual optimization for a clean, closed loop with pure spatial precision.

Notifications You must be signed in to change notification settings

BadNintendo/TravelingSalesmanProblem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿงญ TSP Visualizer & Optimizer TSP.py

A scratch-built, axis-separated Traveling Salesman Problem (TSP) solver with real-time 2D/3D animation using Matplotlib. This implementation avoids bundled heuristics and prioritizes semantic clarity, reproducibility, and extensibility.

๐Ÿš€ Features

  • Nearest-neighbor path construction
  • 2-opt optimization for path refinement
  • Memoized Euclidean distance calculation
  • Real-time animated rendering (2D or 3D auto-detect)
  • Interactive zoom and manual step-through
  • Validator-ready output with timing and distance metrics

๐Ÿ“ฆ Requirements only to Draw results

pip install matplotlib

๐Ÿง  Algorithm Overview

Phase Description
nearest() Builds initial path using greedy nearest-neighbor logic
optimize() Applies 2-opt swaps to reduce total path distance
dist() Memoized Euclidean distance between cities
render() Dynamically draws path in 2D or 3D space with zoom and pan support
update() Animation frame handler for progressive path rendering
zoom_handler() Mouse scroll zooms in/out and pans viewport
click_handler() Mouse click manually advances animation frame

๐Ÿงฎ Usage

Replace check_array = cities with your own list of city dictionaries:

cities = [
    {'name': 'A', 'x': 0, 'y': 0},
    {'name': 'B', 'x': 1, 'y': 1},
    {'name': 'C', 'x': 2, 'y': 0},
    # Optional 3D: add 'z' key
]

Then run the script:

python tsp_visualizer.py

๐Ÿ“Š Output

Initial Distance: 123.456
Initial Solution Time (ms): 4.321
Optimized Distance: 98.765
Optimization Time (ms): 2.109
Sorted Path Length: 11
Start: {'name': 'A', 'x': 0, 'y': 0}
Penultimate: {'name': 'K', 'x': 5, 'y': 2}
End: {'name': 'A', 'x': 0, 'y': 0}

๐ŸŽฅ Demo

  • Auto-detects 2D or 3D based on presence of 'z' key
  • Colors each segment using Tableau palette
  • Click to manually advance frames
  • Scroll to zoom and pan

๐Ÿงฉ Extensibility

  • Swap in alternate heuristics or optimization strategies
  • Extend dist() for geodesic or weighted metrics
  • Integrate with external validators or lexicons

About

Built a scratch-based solution to the Traveling Salesman Problem visiting every (x, y) point once, no overlaps, no retracing. No canned math, no shortcuts. Just raw distance logic and manual optimization for a clean, closed loop with pure spatial precision.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages