Coding Challenge #35.5: TSP with Genetic Algorithm and Crossover
In Part 1 of this multi-part coding challenge, I introduce the classic computer science problem of the Traveling Salesperson (TSP) and discuss the pitfalls with a brute force solution. In Part 2, I discuss Lexicographic Ordering and demonstrate one algorithm to iterate over all the permutations of an array. In Part 3, I apply this algorithm to a brute-force solution of the TSP problem. Every single route permutation is checked one by one. In Part 4, I attempt to create a solution to the TSP problem with a genetic algorithm, and then I add a โcrossoverโ algorithm in Part 5. Code: https://thecodingtrain.com/challenges/35-traveling-salesperson
p5.js Web Editor Sketches:
๐น๏ธ Part 1: Traveling Salesperson (TSP): https://editor.p5js.org/codingtrain/sketches/FCNAHaCqf
๐น๏ธ Part 2: Lexicographic Order: https://editor.p5js.org/codingtrain/sketches/iYxi30gbl
๐น๏ธ Part 3: TSP with Lexicographic Order: https://editor.p5js.org/codingtrain/sketches/bWPIkEv9s
๐น๏ธ Part 4: TSP with Genetic Algorithm: https://editor.p5js.org/codingtrain/sketches/EGjTrkkf9
๐น๏ธ Part 5: TSP with Genetic Algorithm and Crossover: https://editor.p5js.org/codingtrain/sketches/EGjTrkkf9
Other Parts of this Challenge:
๐บ Part 1: Traveling Salesperson (TSP): https://youtu.be/BAejnwN4Ccw
๐บ Part 2: Lexicographic Order: https://youtu.be/goUlyp4rwiU
๐บ Part 3: TSP with Lexicographic Order: https://youtu.be/9Xy-LMAfglE
๐บ Part 4: TSP with Genetic Algorithm: https://youtu.be/M3KTWnTrU_c
๐ฅ Previous video: https://youtu.be/Cl_Gjj80gPE?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH
๐ฅ Next video: https://youtu.be/rX5p-QRP6R4?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH
๐ฅ All videos: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH
References:
๐ Traveling Salesman on Wikipedia: https://en.wikipedia.org/wiki/Travelling_salesman_problem
๐จ๏ธ Permutation Algorithm Using Lexicographic Ordering: https://www.quora.com/How-would-you-explain-an-algorithm-that-generates-permutations-using-lexicographic-ordering
๐ Array on MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array
๐ Array.includes() on MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
๐ Array.reverse() on MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
๐ ES6 Sets on MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Set
๐พ The Nature of Code Part 2: https://github.com/shiffman/NOC-S17-2-Intelligence-Learning
๐ The Nature of Code: http://natureofcode.com/
Videos:
๐ฅ Improved Pool Selection: https://www.youtube.com/watch?v=ETphJASzYes&list=PLRqwX-V7Uu6YJ3XfHhT2Mm4Y5I99nrIKX&index=23
๐ฅ Genetic Algorithm Playlist: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6bJM3VgzjNV5YxVxUwzALHV
๐ด Live Stream Archive #57: https://youtu.be/r_SpBy9fQuo
Related Coding Challenges:
๐ #29 Smart Rockets in p5.js: https://youtu.be/bGz7mv2vD6g
๐ #51 A* Pathfinding Algorithm: https://youtu.be/aKYlikFAV4k
๐ #69 Evolutionary Steering Behaviors: https://youtu.be/flxOkx0yLrY
๐ #70 Nearest Neighbors Recommendation Engine: https://youtu.be/N8Fabn1om2k
Timestamps:
00:00 Recap from Part 4
00:35 What is crossover?
02:33 Improvement: showing the algorithm running
04:24 Improvement: adding a mutation rate
06:08 Where to apply crossover in the code?
07:10 How to apply crossover?
11:08 Code! Implementing the crossOver() function
15:30 Testing the crossOver() function
16:13 Playing with the variables
17:45 About the ES6 includes() function and Set
18:03 Inefficiency of the dist() function
18:44 Suggestion to improve mutation
21:10 Please share your own variations and improvements!
Editing by Mathieu Blanchette
Animations by Jason Heglund
Music from Epidemic Sound
๐ Website: http://thecodingtrain.com/
๐พ Share Your Creation! https://thecodingtrain.com/guides/passenger-showcase-guide
๐ฉ Suggest Topics: https://github.com/CodingTrain/Suggestion-Box
๐ก GitHub: https://github.com/CodingTrain
๐ฌ Discord: https://discord.gg/hPuGy2g
๐ Membership: http://youtube.com/thecodingtrain/join
๐ Store: https://standard.tv/codingtrain
๐๏ธ Twitter: https://twitter.com/thecodingtrain
๐ธ Instagram: https://www.instagram.com/the.coding.train/
๐ฅ Coding Challenges: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH
๐ฅ Intro to Programming: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA
๐ p5.js: https://p5js.org
๐ p5.js Web Editor: https://editor.p5js.org/
๐ Processing: https://processing.org
๐ Code of Conduct: https://github.com/CodingTrain/Code-of-Conduct
This description was auto-generated. If you see a problem, please open an issue: https://github.com/CodingTrain/thecodingtrain.com/issues/new
#travelingsalesperson #permutation #lexicographicordering #natureofcode #geneticalgorithm #evolution #bruteforce #factorial #arrays #p5js