# The Traveling Salesman with Simulated Annealing, R, and Shiny

**Category: R | Todd W. Schneider**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

## Make your own tour with the interactive Shiny app

Here’s the Shiny app that lets you pick up to 30 cities on the map, set some parameters of the annealing schedule, then run the actual simulated annealing process (or just click ‘solve’ if you’re lazy). Give it a shot below! Bonus points if you recognize where the default list of cities comes from…

The app is hosted at ShinyApps.io, which is currently in alpha testing, so I’m not entirely sure how reliable it will be. If you want to run the app on your local machine, it’s very easy, all you need to do is paste the following into your R console:

1 2 3 |
install.packages(c(“shiny”, “maps”, “geosphere”), repos=“http://cran.rstudio.com/”) library(shiny) runGitHub(“shiny-salesman”, “toddwschneider”) |

## Code on GitHub

The full code is available at https://github.com/toddwschneider/shiny-salesman

## Around the World in 80,000 Miles

Here’s another animated gif using a bunch of world capitals. The “solution” here is almost certainly not the global optimum, but it’s still fun to watch!

$(function() {

$("a[data-id=4055922]").removeAttr("data-id").removeAttr("target").removeAttr("onclick").attr("href", "#salesman-app").attr("data-notab", true)

var setOffsets = function() { var windowWidth = $(window).width() $("#shinytsp-container, .world-animation").each(function(i, el) { var $el = $(el) if (windowWidth > $el.width()) { $el.offset({'left': (windowWidth - $el.width()) / 2}) } else { $el.offset({'left': 20}) } }) }

setOffsets()

var resizeTimeout = null $(window).resize(function() { clearTimeout(resizeTimeout) resizeTimeout = setTimeout(setOffsets(), 100) })

$(window).scroll(function() { var $tsp = $("#shinytsp-container")

if (!$tsp.attr("src") && $tsp.offset().top - $(window).scrollTop() - $(window).height() < 500) { var appNames = ["traveling-salesman", "traveling-salesman-2", "traveling-salesman-3", "traveling-salesman-4", "traveling-salesman-5"] var appName = appNames[Math.floor(Math.random() * appNames.length)] $tsp.attr("src", "http://todd.shinyapps.io/" + appName) } }) })

**leave a comment**for the author, please follow the link and comment on their blog:

**Category: R | Todd W. Schneider**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.