Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Informed Search Algorithms Using Python Libraries

This notebook demonstrates informed search algorithms using existing Python libraries and frameworks, showing how to leverage pre-built implementations for real-world applications.

πŸ“š Libraries & Frameworks Used:ΒΆ

  • NetworkX: Graph algorithms and A* implementation

  • SciPy: Optimization algorithms (Simulated Annealing, etc.)

  • scikit-opt: Genetic Algorithm and other metaheuristics

  • OR-Tools: Google’s optimization tools (Tabu Search equivalent)

  • DEAP: Distributed Evolutionary Algorithms

🎯 Problem: Traveling Salesman Problem (TSP)¢

We’ll solve the same TSP using library implementations to compare:

  • Performance vs custom implementations

  • Ease of use and flexibility

  • Parameter tuning capabilities

  • Real-world applicability

πŸ“¦ Installing/Importing required libraries...
βœ… All libraries imported successfully!
🎨 Plot styling configured
🎲 Random seeds set for reproducibility

1. πŸ—οΈ Problem Setup and Data PreparationΒΆ

Let’s create our TSP instance and prepare the data structures needed for different libraries:

πŸ—οΈ Creating challenging TSP instance...
πŸ“Š This layout is designed to show clear quality differences between algorithms
βœ… TSP instance created with 15 cities
πŸ“Š Distance matrix shape: (15, 15)
πŸ•ΈοΈ NetworkX graph has 15 nodes and 105 edges
πŸ” Problem characteristics:
   πŸ“ Min intercity distance: 1.78
   πŸ“ Max intercity distance: 88.78
   πŸ“ Avg intercity distance: 51.92
   πŸ“Š Distance range: 50.00x
   🎯 This creates multiple local optima - perfect for algorithm comparison!
<Figure size 1000x800 with 1 Axes>

2. 🎯 A* Search using NetworkX¢

NetworkX provides a built-in A* implementation. We’ll adapt it for TSP by solving it as a shortest path problem through all nodes:

πŸ“Š Establishing baseline with simple greedy algorithm...
   🎯 Greedy baseline: Distance=305.88, Time=0.0001s
   πŸ“ This gives us a reference point to measure improvement

🎯 Running A* approach with NetworkX...
βœ… A* (NetworkX) Results:
   πŸ“ Tour distance: 305.88
   ⏱️ Execution time: 0.0008 seconds
   πŸ›£οΈ Tour: [0, 2, 1, 14, 10, 11, 9, 7, 8, 13, 12, 3, 5, 6, 4]
<Figure size 1000x800 with 1 Axes>

3. πŸ”₯ Simulated Annealing using SciPyΒΆ

SciPy provides optimization algorithms including simulated annealing through scipy.optimize:

πŸ”₯ Running Simulated Annealing with SciPy...
βœ… Simulated Annealing (SciPy) Results:
   πŸ“ Tour distance: 283.50
   ⏱️ Execution time: 1.3330 seconds
   πŸ›£οΈ Tour: [7, 8, 9, 11, 10, 14, 0, 1, 2, 13, 4, 6, 5, 3, 12]
βœ… Simulated Annealing (SciPy) Results:
   πŸ“ Tour distance: 283.50
   ⏱️ Execution time: 1.3330 seconds
   πŸ›£οΈ Tour: [7, 8, 9, 11, 10, 14, 0, 1, 2, 13, 4, 6, 5, 3, 12]
<Figure size 1000x800 with 1 Axes>

4. πŸš› Professional TSP Solver using OR-ToolsΒΆ

Google’s OR-Tools provides industrial-strength optimization algorithms. Let’s use their TSP solver:

πŸš› Running OR-Tools TSP Solver (includes advanced heuristics)...

βœ… OR-Tools Results:
   πŸ“ Tour distance: 283.43
   ⏱️ Execution time: 30.0016 seconds
   🎯 Solution status: Good
   πŸ›£οΈ Tour: [0, 14, 10, 11, 9, 8, 7, 12, 3, 5, 6, 4, 13, 2, 1]
βœ… OR-Tools Results:
   πŸ“ Tour distance: 283.43
   ⏱️ Execution time: 30.0016 seconds
   🎯 Solution status: Good
   πŸ›£οΈ Tour: [0, 14, 10, 11, 9, 8, 7, 12, 3, 5, 6, 4, 13, 2, 1]
<Figure size 1000x800 with 1 Axes>

5. 🧬 Genetic Algorithm using SciPy and Custom Implementation¢

Let’s implement a genetic algorithm using SciPy’s optimization framework:

🧬 Running Genetic Algorithms...

πŸ”¬ SciPy Differential Evolution:
   πŸ“ Distance: 291.58
   ⏱️ Time: 1.0952s

🧬 Custom Genetic Algorithm:
   πŸ“ Distance: 291.58
   ⏱️ Time: 1.0952s

🧬 Custom Genetic Algorithm:
   πŸ“ Distance: 283.50
   ⏱️ Time: 0.4033s
   πŸ“ Distance: 283.50
   ⏱️ Time: 0.4033s
<Figure size 1800x600 with 3 Axes>

6. πŸ”οΈ Hill Climbing and Local Search using SciPyΒΆ

SciPy provides several local optimization methods. Let’s use them for hill climbing:

πŸ”οΈ Running Hill Climbing Algorithms...

πŸ”¬ SciPy Local Optimization (L-BFGS-B):
   πŸ“ Distance: 766.82
   ⏱️ Time: 0.0017s
   βœ… Converged: True

πŸ”οΈ Simple Hill Climbing (2-opt):
🚫 Hill climbing stuck at local optimum after 25 iterations
   πŸ“ Distance: 310.54
   ⏱️ Time: 0.0026s
   🚫 Stuck at local optimum: True
<Figure size 1800x600 with 3 Axes>

7. πŸ“Š Comprehensive Algorithm ComparisonΒΆ

Now let’s compare all our library-based implementations side by side:

πŸ† ALGORITHM COMPARISON RESULTS
====================================================================================================
πŸ“Š Distance comparison (lower is better):
----------------------------------------------------------------------------------------------------
                   Algorithm  Library  Distance  Improvement (%)  Time (s)  Quality Rank
       OR-Tools Professional OR-Tools   283.430            7.300    30.002             1
   SciPy Simulated Annealing    SciPy   283.501            7.300     1.333             2
    Custom Genetic Algorithm   Custom   283.501            7.300     0.403             2
SciPy Differential Evolution    SciPy   291.580            4.700     1.095             4
    Simple Greedy (Baseline) Baseline   305.875            0.000     0.000             5
                 NetworkX A* NetworkX   305.875            0.000     0.001             5
        Simple Hill Climbing   Custom   310.542           -1.500     0.003             7
         SciPy Hill Climbing    SciPy   766.815         -150.700     0.002             8

🎯 QUALITY ANALYSIS:
============================================================
πŸ₯‡ Best Solution: OR-Tools Professional (OR-Tools)
   πŸ“ Distance: 283.43
   πŸ“ˆ Improvement over baseline: 7.3%

πŸ₯‰ Worst Solution: SciPy Hill Climbing (SciPy)
   πŸ“ Distance: 766.82
   πŸ“‰ Worse than baseline: 150.7%

⚑ Fastest Algorithm: Simple Greedy (Baseline) (Baseline)
   ⏱️ Time: 0.0001 seconds
   πŸ“ Distance: 305.88

πŸ“Š Quality Range: 483.39 units (170.5% variation)
🎯 This shows clear differences in algorithm effectiveness!
<Figure size 1600x1200 with 5 Axes>

9. 🎯 Summary and Next Steps¢

This notebook demonstrated how to leverage existing Python libraries for informed search algorithms, comparing their performance against the Traveling Salesman Problem. We explored professional-grade tools and their practical applications.

πŸ† Key Findings:ΒΆ

  • OR-Tools provides the highest quality solutions with minimal coding effort

  • SciPy offers excellent balance between ease-of-use and performance

  • NetworkX excels for graph-based problems and academic research

  • Library implementations often outperform custom code due to optimized backends

  • Parameter tuning remains crucial regardless of implementation choice

πŸ“š Additional Resources:ΒΆ

πŸ’‘ Remember:ΒΆ

β€œThe best algorithm is the one that solves your problem effectively within your constraints. Libraries provide powerful tools, but understanding the underlying principles helps you choose and tune them wisely.”