ChatGPT Advanced Data Analysis - The Traveling Salesman Problem

**Preparing for the Traveling Salesman Problem**

The system prompt was created to utilize the data scientist python expert for solving the traveling salesman problem (TSP). The primary application is data science and programming, with skills including proficiency in Python, data mining ability, creating predictive models, running simulations, critical thinking, and developing a systematic approach to data analysis. A standard data scientist python expert system role was assigned to this task.

**The Problem Statement**

The traveling salesman problem is a classic problem in computer science and operations research that involves finding the shortest possible tour that visits a set of cities and returns to the original city. In this case, we have 66 pairs of cities with their corresponding distances in kilometers. The goal is to find the shortest possible route that visits each city exactly once and returns to the starting city.

**The Nearest Neighbor Algorithm**

One of the approaches to solving the TSP is the nearest neighbor algorithm. This algorithm involves selecting the closest unvisited city at each step, until all cities have been visited. In this case, the system prompt ran the nearest neighbor algorithm, resulting in a tour distance of 54,693 kilometers.

**The Two-Opt Algorithm**

Another approach to solving the TSP is the two-opt algorithm. This algorithm involves swapping the edges between two cities that form an invalid tour and finding a new route. In this case, the system prompt ran the two-opt algorithm, which resulted in the same solution as the nearest neighbor algorithm.

**Visualizing the Solution**

To gain a better understanding of the solution, several visualizations were created using Python code. These included a distance matrix heatmap, a tour distance plot, a city connection network diagram, and a distance histogram. The heatmap showed the distances between each pair of cities, while the tour distance plot displayed the total distance traveled by each route. The city connection network diagram showed the connections between each city, while the distance histogram displayed the distribution of distances across all routes.

**Creating an Interactive Map**

To visualize the solution on a map, Python code was used to create an interactive map. This involved uploading the city coordinates and creating animations to display the shortest route. The system prompt ran this code and produced a dynamic map that showed the tour distance and each leg of the journey. Users could zoom in, pan around, and spin the world to get a better view of the solution.

**Conclusion**

In conclusion, the traveling salesman problem was successfully solved using two different algorithms: nearest neighbor and two-opt. The system prompt demonstrated the effectiveness of these algorithms by finding the shortest possible route that visits each city exactly once and returns to the starting city. Additionally, several visualizations were created to gain a better understanding of the solution, including a heatmap, tour distance plot, city connection network diagram, and distance histogram. Finally, an interactive map was created using Python code to display the shortest route in a dynamic and engaging way.

"WEBVTTKind: captionsLanguage: enin today's video we are going to try to solve the traveling salesman problem using Code interpreter and create some cool visuals so what exactly is the traveling salesman problem here we can see according to Wikipedia it's a given a list of cities and the distances between each pair of the Cities what is the shortest possible route that visits each City exactly once and returns to the origin city that is the problem we're gonna face you can see here is kind of the solution you can see here we have the shortest route but this is a lot of cities we are gonna do 12 so I have prepared for some data for that so let's take a look at that here you can see the data I have collected we have cities we have distance in kilometers we have the cities so this is a pair so this is the distance from Cairo to Rome 21 87 kilometers long right and we have 66 points like this so this is what we are going to upload to the code interpreter to try to solve a problem so let's just head over to the call interpreter and take a look at our prompt let's take a look at the system prompt I created for this task so you can see I uploaded the data we just had a look at ignore all previous instructions here is your system directives then I created this system prompt I wanted to use for this assignment so just a name primary application data science and programming Aid so it's kind of a data scientist python expert it has some skills like Proficiency in Python data mining ability to create predictive models run simulations critical thinking develop systematic approach to data analysis and coding challenges so basically quite a standard data scientist python expert system role and I wanted to give you some example on how you can solve the tsp problem so nearest neighbor that's an algorithm we have the brute force and we have the pairwise exchange so I think we're going to just gonna run two of them to kind of double check if we get the same result right and your task is to solve the given traveling salesman problem here or the problem so basically just give instructions to the problem and let's think about this in a step-by-step way and solve the problem so I think we're ready let's just hit submit okay so you can see here it's going for the nearest neighbor algorithm here first okay that means I think I said at The Brute Force algorithm requires too much computational complexity so it's going to pick the nearest neighbor we're gonna try to run the other one to later okay so we have an answer here so you can see we go Cairo Rome Paris London Oslo New York City right and we ended up with 54 693 kilometers that is the shortest route now let's try the pairwise algorithm okay so the two opt algorithm ended up with the same result so I'm going up for that is correct then there's no way for me to know for sure but uh we don't gonna do any advanced computational thing on this but since we both got the same result I'm expecting this is correct let's do some visualizations of this to see if we can do something cool right so I'm just gonna go now be very creative and create five very interesting and cool visualizations of the solution to the problem we have a distance Matrix heat map we have a tour distance plot city connection network distance histogram and I see the distant bar plot okay so here we have the heat map so you can say like you say London here is very far from if we go to the yellow I guess that's Sydney right and you can see we have these green is shares that's probably like Rio yeah quite a cool heat map we have two distant comparison this was okay so this is the nearest neighbor algorithm and the two opt you can see the results were exactly the same that was interesting here we have the city connection network cool distance histogram so this is like the distance the count of okay the count of distances total distance contributed by each City so Cape Town contributed the most to total distance Paris almost nothing so I wanted to try to create a visualization of the solution on a map so I'm gonna try a few things and see if I can make it work so what I went ahead and did is I uploaded the city coordinates I just abstract GPT for that then I went can you create a python code I can run with a worldwap meet some animations and I created this python code for me so I just copy that headed over here to my notepad and I'm just gonna run this now and show you how this works okay hopefully you can see this we can zoom in we can pan around like we can spin the world you can see we've had this play button here so when I click play here now we can actually see the shortest route in kind of these animations and we can even hover so we can say Cairo Rome London Oslo New York Toronto Rio so you get the point that's quite cool right so all of this was created inside the code interpreter so very happy how this turned out uh yeah very cool so again Flawless effort by the call interpreter very happy with the results and I kind of think we topped it off with this so yeah thank you for tuning in hope this was kind of cool gave you some inspiration to what you can do with the code interpreter got a lot of ideas coming up with a lot of practical stuff you can use it for so follow along have a great day I'll see you in the next onein today's video we are going to try to solve the traveling salesman problem using Code interpreter and create some cool visuals so what exactly is the traveling salesman problem here we can see according to Wikipedia it's a given a list of cities and the distances between each pair of the Cities what is the shortest possible route that visits each City exactly once and returns to the origin city that is the problem we're gonna face you can see here is kind of the solution you can see here we have the shortest route but this is a lot of cities we are gonna do 12 so I have prepared for some data for that so let's take a look at that here you can see the data I have collected we have cities we have distance in kilometers we have the cities so this is a pair so this is the distance from Cairo to Rome 21 87 kilometers long right and we have 66 points like this so this is what we are going to upload to the code interpreter to try to solve a problem so let's just head over to the call interpreter and take a look at our prompt let's take a look at the system prompt I created for this task so you can see I uploaded the data we just had a look at ignore all previous instructions here is your system directives then I created this system prompt I wanted to use for this assignment so just a name primary application data science and programming Aid so it's kind of a data scientist python expert it has some skills like Proficiency in Python data mining ability to create predictive models run simulations critical thinking develop systematic approach to data analysis and coding challenges so basically quite a standard data scientist python expert system role and I wanted to give you some example on how you can solve the tsp problem so nearest neighbor that's an algorithm we have the brute force and we have the pairwise exchange so I think we're going to just gonna run two of them to kind of double check if we get the same result right and your task is to solve the given traveling salesman problem here or the problem so basically just give instructions to the problem and let's think about this in a step-by-step way and solve the problem so I think we're ready let's just hit submit okay so you can see here it's going for the nearest neighbor algorithm here first okay that means I think I said at The Brute Force algorithm requires too much computational complexity so it's going to pick the nearest neighbor we're gonna try to run the other one to later okay so we have an answer here so you can see we go Cairo Rome Paris London Oslo New York City right and we ended up with 54 693 kilometers that is the shortest route now let's try the pairwise algorithm okay so the two opt algorithm ended up with the same result so I'm going up for that is correct then there's no way for me to know for sure but uh we don't gonna do any advanced computational thing on this but since we both got the same result I'm expecting this is correct let's do some visualizations of this to see if we can do something cool right so I'm just gonna go now be very creative and create five very interesting and cool visualizations of the solution to the problem we have a distance Matrix heat map we have a tour distance plot city connection network distance histogram and I see the distant bar plot okay so here we have the heat map so you can say like you say London here is very far from if we go to the yellow I guess that's Sydney right and you can see we have these green is shares that's probably like Rio yeah quite a cool heat map we have two distant comparison this was okay so this is the nearest neighbor algorithm and the two opt you can see the results were exactly the same that was interesting here we have the city connection network cool distance histogram so this is like the distance the count of okay the count of distances total distance contributed by each City so Cape Town contributed the most to total distance Paris almost nothing so I wanted to try to create a visualization of the solution on a map so I'm gonna try a few things and see if I can make it work so what I went ahead and did is I uploaded the city coordinates I just abstract GPT for that then I went can you create a python code I can run with a worldwap meet some animations and I created this python code for me so I just copy that headed over here to my notepad and I'm just gonna run this now and show you how this works okay hopefully you can see this we can zoom in we can pan around like we can spin the world you can see we've had this play button here so when I click play here now we can actually see the shortest route in kind of these animations and we can even hover so we can say Cairo Rome London Oslo New York Toronto Rio so you get the point that's quite cool right so all of this was created inside the code interpreter so very happy how this turned out uh yeah very cool so again Flawless effort by the call interpreter very happy with the results and I kind of think we topped it off with this so yeah thank you for tuning in hope this was kind of cool gave you some inspiration to what you can do with the code interpreter got a lot of ideas coming up with a lot of practical stuff you can use it for so follow along have a great day I'll see you in the next one\n"