Coding Challenge #35.4: Traveling Salesperson with Genetic Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ย. 2024

ความคิดเห็น • 126

  • @abdirahmanmohamud1353
    @abdirahmanmohamud1353 ปีที่แล้ว +4

    This man is an alien, the way he teaches is incredible, as soon as video starts the video ends without know that you spent 20 mints, thanks man

  • @ethanchua2335
    @ethanchua2335 4 ปีที่แล้ว +3

    This dude gives an authentic coding experience.

  • @justinlangley8972
    @justinlangley8972 2 ปีที่แล้ว +8

    So glad I found this channel. Even if you're using another language, these videos are incredibly helpful to learn from.
    I'm doing a project for a class involving the traveling salesperson problem and was thinking of using ACS or ACO, and it's interesting how similar it is to the genetic algorithm.

  • @husseintammam4691
    @husseintammam4691 7 ปีที่แล้ว +2

    Hi I read The Nature of Code a couple of years ago, and I loved it! It was a super fun read and the code was really easy to follow. It was my intro to artificial intelligence and the beginning of my addiction. Now I'm studying computer science and AI in uni and your book is part of the reason why :)
    I randomly started watching some of your videos a few days ago and when you mentioned your book I really felt the nostalgia. Your TH-cam channel looks awesome btw and I'm probably gonna binge-watch a bunch of your videos when I have the time! I love you work man. THANK YOU FOR YOUR CONTRIBUTION TO HUMANITY!

    • @TheCodingTrain
      @TheCodingTrain  7 ปีที่แล้ว +1

      Love hearing this, thank you!!!

  • @ΛεωνίδαςΓκώγκος
    @ΛεωνίδαςΓκώγκος 5 ปีที่แล้ว +11

    I like how this guy can't even offend an index of an array

  • @LeahLundqvist
    @LeahLundqvist 7 ปีที่แล้ว +5

    man. i love how well you explain things, i tried siraj's (don't know how to spell it) videos but it feels like you have to have a phd in math to understand, really looking forward to neural nets 🙌

  • @sethlivingston1310
    @sethlivingston1310 7 ปีที่แล้ว +1

    i really love his videos. It makes progamming more fun than studying

  • @spicytuna08
    @spicytuna08 6 ปีที่แล้ว

    this factorial number, we don't have a sense how long it takes. this video really shows the importance of algorithm.

  • @tomek3633
    @tomek3633 2 ปีที่แล้ว

    Great stuff! Looking forward to see part II of it!

  • @Alex-Eldridge
    @Alex-Eldridge 7 ปีที่แล้ว +2

    Just a note on the shuffle algorithm you made, it will not produce every possible order. By swapping an even number of times, you only get half of the total permutations. All the orders created will be even permutations, and there is no way for the shuffle to produce an order that is only one, three, or five swaps away. This is the parity theorem of permutations. Helpful information when making a shuffle algorithm! Here's a link to more info on even and odd permutations. www.efgh.com/math/algebra/permutations.htm

  • @Spaceizcool
    @Spaceizcool 7 ปีที่แล้ว +23

    27:54 IT UPDATED TO OPTIMAL SOLUTION

    • @DavidWMiller
      @DavidWMiller 7 ปีที่แล้ว

      I wanted to yell at the screen when he didn't notice that.

  • @TGCByJesus
    @TGCByJesus 5 ปีที่แล้ว +3

    Thank you. Your lecture has helped me a lot.

  • @SergioPolimante
    @SergioPolimante 5 ปีที่แล้ว

    Your videos are really helpful and very fun to watch, you are very fun! Thanks for the content

  • @xzesstence8862
    @xzesstence8862 5 ปีที่แล้ว +1

    thank you for this great tutorial, i really enjoyed this series

  • @mrbubbly4536
    @mrbubbly4536 7 ปีที่แล้ว

    Wow... everything you did seems so complicated! Nice Work!

  • @shaytal100
    @shaytal100 7 ปีที่แล้ว +1

    I think you need to implement the possibility of switching more than 1 pair of cities per mutation. Otherwise the algorithm can be stuck in a local minimum.
    In other words: It can be, that switching any possible single pair of cities leads to a longer distance while switching 2 or more cities would further lower it.
    Really like your videos! They are educational and entertaining!

    • @TheHpsh
      @TheHpsh 7 ปีที่แล้ว

      yes, but that will probably be a problem anyway, the traveling salesman problem are well known to be a pretty hard to do with genetic algorithm.

    • @shaytal100
      @shaytal100 7 ปีที่แล้ว

      Yeah I would think so too. The number of possible solutions grows with n!. And there will be lots of local optimals.
      But intelligent combinations of former generation sequences might make it an efficient algorithm. But this is not an easy problem.
      Looking forward to the next part.

    • @TheHpsh
      @TheHpsh 7 ปีที่แล้ว

      i have one idea, about using epigenetic, and using a complete route as a gene.
      it will need to have many alternative routes to randomly select between, but cross breading might help removing bad ones.
      but think it will be slower then brute force, since it will often remove good routes to.

  • @MrAashish24
    @MrAashish24 7 ปีที่แล้ว +29

    Hello Sir I am your big fan

    • @EDToasty
      @EDToasty 6 ปีที่แล้ว +1

      Hello Sir I am your big fan

    • @sagarkapasi099
      @sagarkapasi099 6 ปีที่แล้ว +1

      Hello Sir I am your big fan

    • @Johncena-bk1lg
      @Johncena-bk1lg 6 ปีที่แล้ว +1

      Hello Sir I am your big fan

    • @Marcus-jf4hu
      @Marcus-jf4hu 6 ปีที่แล้ว +1

      Hello Sir I am your big fan

    • @alecday3552
      @alecday3552 5 ปีที่แล้ว

      th-cam.com/video/mif77aOd9ns/w-d-xo.html

  • @MrCmon113
    @MrCmon113 6 ปีที่แล้ว +2

    Omg it found the best solution while he wasn't looking! XD

  • @Tracks777
    @Tracks777 7 ปีที่แล้ว +2

    Great! Keep it up!

  • @mohamedmhmood2940
    @mohamedmhmood2940 7 ปีที่แล้ว

    you are awesome.. you are best programmer I've ever see

  • @yuvalyehezkel5558
    @yuvalyehezkel5558 7 ปีที่แล้ว

    lol you didnt notice it optimized it behind your back :D 27:49
    i LOVE your videos btw, keep doing what you do!

    • @ritikkhatri
      @ritikkhatri 6 ปีที่แล้ว

      27:56 to be precise

  • @SourabhBhat
    @SourabhBhat 7 ปีที่แล้ว +6

    Is it calculating at the frame rate?
    I mean, is the new distance calculated once per frame and rendered to screen? or is it only rendering the current possibility that is there in the array at the time of rendering? I guess it would be too slow to calculate only once per frame. I suppose it would be better to do the calculation in a separate thread which can continuously crunch the numbers.

    • @Wuzzysbrand06
      @Wuzzysbrand06 7 ปีที่แล้ว +5

      Yes, the draw function is called once per frame, so everything inside of it is executed once per frame. That's just how the p5.js library works. You can add custom flags to skip things when something happens (e.g. don't draw the path again if the "bestEver" hasn't changed). That might speed things up a bit. You're right about it being better to do in a seperate thread, however, JavaScript is single thread only.
      There are things called "Web Workers" that allow for multithreading in JS but that's a whole different tutorial and there are limitations with that as well, as Web Workers don't have access to things like the document object, the window object etc. I'm not sure how suitable it is in conjunction with p5.js, however, I have to admit that I don't have experience with it either.
      Doing this multithreaded would be better in a language that supports parallelization properly.

    • @Garfield100
      @Garfield100 7 ปีที่แล้ว +1

      Just loop the whole thing so it does it multiple times each frame :D
      No need for complicated threading and stuff

    • @SourabhBhat
      @SourabhBhat 7 ปีที่แล้ว

      @GarfieldGaming I think, if we do that on the the same UI thread, it will freeze the canvas.

    • @Wuzzysbrand06
      @Wuzzysbrand06 7 ปีที่แล้ว

      @GarfieldGaming that would make it choppy and wouldn't really be any faster than doing it once each frame, because you're still calculating everything sequentially.

    • @Garfield100
      @Garfield100 7 ปีที่แล้ว

      +Wuzzybrand06 It's capped at 60, that's why it remains at 60 even if it could go faster :D basically it calculates one frame and when it's done it does nothing for the rest of the 1/60th of a second period. I've done this multiple times and it's a neat trick because you can't seem to change the framerate cap :/ (It really does work!)

  • @DeathRuNNerVST
    @DeathRuNNerVST 7 ปีที่แล้ว

    Daniel i love you so much .. your recent videos are soo good

  • @Victor_Marius
    @Victor_Marius 6 ปีที่แล้ว +3

    Hello! I found a quick way to shuffle with order.sort((a, b) => Math.random() > 0.5). But this is not be the best way to do it.

  • @theWorldOfIss
    @theWorldOfIss ปีที่แล้ว +1

    Sir how to implement multiple traveling salesman problem using NSGA-2 in python.

  • @frodeflem9353
    @frodeflem9353 7 ปีที่แล้ว

    What I would give, to be able to do the things you are doing, for a living!

  • @peterhayman
    @peterhayman 7 ปีที่แล้ว

    haha those horns sound like the intro to jump around

  • @niklasfrank7847
    @niklasfrank7847 7 ปีที่แล้ว

    A LSTM or NEAT coding challenge/tutorial/example would be interresting and nice. Also nice videos ;-)

    • @TheCodingTrain
      @TheCodingTrain  7 ปีที่แล้ว +1

      It's coming!

    • @niklasfrank7847
      @niklasfrank7847 7 ปีที่แล้ว

      The Coding Train Nice! Always wanted to know more about those two!

  • @davidalejandromurocampa1005
    @davidalejandromurocampa1005 ปีที่แล้ว

    How a genetic algorithm could be applied to the set covering problem (SCP)?

  • @SetTheCurve
    @SetTheCurve 5 ปีที่แล้ว

    It seemed like the solutions were not only changing order, but changing the location of the dots. What am I missing?

  • @virenlakum
    @virenlakum 6 ปีที่แล้ว +2

    Travelling salesman Problem states that it should start and end at same city...

  • @Tracks777
    @Tracks777 7 ปีที่แล้ว +3

    When is your next video? :D Keep it up!

  • @elderbinks22
    @elderbinks22 7 ปีที่แล้ว

    I've got a challenge that I would love to see. I want to see genetic algorithm implemented in a rc car with some sort of distance tracker. so that it can make its way through the obstacle course. i understand it would take a long time. But id still love to see it

  • @arifhussain-qx1ef
    @arifhussain-qx1ef 4 ปีที่แล้ว

    I'm anxiously looking for implementation of genetic algorithm in timetable scheduling problem

  • @charbelsarkis3567
    @charbelsarkis3567 6 ปีที่แล้ว +2

    could you possibly make this with google maps? what if i want to deliver packages to houses as fast as possible.

    • @TheCodingTrain
      @TheCodingTrain  6 ปีที่แล้ว

      Great idea! Check out: github.com/cvalenzuela/Mappa

    • @charbelsarkis3567
      @charbelsarkis3567 6 ปีที่แล้ว

      The Coding Train thank you

  • @i.i
    @i.i 7 ปีที่แล้ว

    I like your way of doing things
    the best programming channel on TH-cam can you please use some ES6 functions and stuff
    we want to learn some modern JS

  • @Farpezio
    @Farpezio 6 ปีที่แล้ว +2

    *deletes order, "next I need to create order..."

  • @Johnnyboycurtis
    @Johnnyboycurtis 6 ปีที่แล้ว +1

    I get this guy is trying to be fun and silly, but I wouldn't mind if he could take it down a notch. Aside from that, it's quality content. He doesn't dive deep into the theory of GAs, but enough to get novice programmers started.

  • @owclassy308
    @owclassy308 7 ปีที่แล้ว +1

    Next: Traveling Salesperson with Dynamic Programming

  • @armanshirzad4409
    @armanshirzad4409 3 ปีที่แล้ว

    awesome, good energy, easy to learn only one question where can i find the source code?? fast reply would be appreciated due to my project deadline, All The Bests

  • @SimonTiger
    @SimonTiger 4 ปีที่แล้ว +1

    /20:42 "2-Dimensional space" Why does it have to be 2-dimensional!? The mathematically appropriate way to discribe it is in a 1-dimensional space!

  • @afqradeon4757
    @afqradeon4757 7 ปีที่แล้ว +1

    Hi, when picking one why not pick the one with the BEST fitness (like hightest fitness) ?? instead of taking it via random number

  • @wassilchoujaa3478
    @wassilchoujaa3478 4 ปีที่แล้ว

    TSP is best solved with sat (constraint based) solver AFAIK

  • @tips-today463
    @tips-today463 3 ปีที่แล้ว

    Sir , how can i perform a scheduling with genetic algorithm

  • @wyxas
    @wyxas 7 ปีที่แล้ว +3

    im your bigger fan

    • @ivansivak93
      @ivansivak93 7 ปีที่แล้ว

      Vytautas I am a bigger fan,

    • @wyxas
      @wyxas 7 ปีที่แล้ว

      dont lie

  • @earthbender731
    @earthbender731 3 หลายเดือนก่อน

    Isn't the original name The traveling salesMAN?

  • @flaviaawan6297
    @flaviaawan6297 4 ปีที่แล้ว

    How to calculate fitness function without path cost?

  • @fayezbayzidify
    @fayezbayzidify 7 ปีที่แล้ว

    you are amazing !

  • @krystianzawadzki975
    @krystianzawadzki975 7 ปีที่แล้ว +1

    Oh, didn't even notice I was this quick :D

  • @brianbouf8303
    @brianbouf8303 7 ปีที่แล้ว

    i am your biggest fan.

  • @peterbonnema8913
    @peterbonnema8913 6 ปีที่แล้ว

    at 8:52 I assume that you haven't read the text: directly above that piece of code it says: "Here's what the implemententation looks like in Javascript, not that you should use it:". And direct below the code it says: "This is bad, and we can do better." hahaha

  • @ThisIsntBrandon
    @ThisIsntBrandon 7 ปีที่แล้ว

    I'm the most big fan

  • @kaz372
    @kaz372 4 ปีที่แล้ว

    Would in effect never work with a mute mutate function ;)

  • @shuvamshah5970
    @shuvamshah5970 7 ปีที่แล้ว

    can't we do this using Prim's or Kruskal's algorithm of Minimum spanning tree?

    • @mebamme
      @mebamme 7 ปีที่แล้ว

      this video is about the shortest route that visits all nodes. A tree on the other hand might have branches, which isn't exactly a route you can follow (unless you backtrack, but that wouldn't be short anymore.)

    • @officialniknak
      @officialniknak 7 ปีที่แล้ว +1

      Prim's and Kruskal's would indeed give an minimum spanning TREE, but the Traveling Salesman is looking for the shortest PATH among the vertices. A tree is not necessarily a path

    • @shuvamshah5970
      @shuvamshah5970 7 ปีที่แล้ว

      mebamme yeah exactly... I got it

    • @simpletongeek
      @simpletongeek 7 ปีที่แล้ว +1

      you're talking about Christofide approximate algorithm. once you have the tree, you can traverse it, skipping the backtracks. it provides a pretty good solution relatively quickly.

  • @akruijff
    @akruijff 5 ปีที่แล้ว

    How about fitness = -d?

  • @aimenmansouri7648
    @aimenmansouri7648 7 ปีที่แล้ว +8

    did university is so important in learning computer sience can i learn alone and how ?

    • @Lanr1s
      @Lanr1s 7 ปีที่แล้ว +1

      في 90 ثانية - in 90 sec interested too

    • @aimenmansouri7648
      @aimenmansouri7648 7 ปีที่แล้ว +1

      حسب علمي مستوى الجامعات العربية متدني جدا معظم الخرجين يزعمون انهم تعلموا علوم الحاسب مستقلين عن الجامعة

    • @aimenmansouri7648
      @aimenmansouri7648 7 ปีที่แล้ว +1

      حسب علمي مستوى الجامعات العربية متدني جدا و معظم الخريجين يزعمون انهم تعلمو علوم الحاسب مستقلين عن الجامعة

    • @adud6764
      @adud6764 7 ปีที่แล้ว +4

      Depends on what you want to do with it.
      Do you just want to create some personal applications and delve into stuff that is shown here on the channel? No, you probably dont need a university degree. Some things might fly over your head but if you invest enough time and look into the background mathematics or whatever you will be able to do it.
      But if you want a solid job in software development you will probably be required to have a degree. Even if you can show a lot of working experience employers will consider people with a degree over you, even if they lack the experience. I think they want this mathematic/algorythmic background in employees.
      At least that is the way where I am from.

    • @i.i
      @i.i 7 ปีที่แล้ว +1

      انا احب شرح هذا الكائن لكن المشكلة انه يستخدم مكتبة مو مشهورة وتنفع في الرسم بس

  • @kocakOFarc
    @kocakOFarc 4 ปีที่แล้ว

    java code for flying sidekick traveling salesman problem please

  • @SimonTiger
    @SimonTiger 4 ปีที่แล้ว

    Traveling Salesman problem

  • @wouter11234
    @wouter11234 7 ปีที่แล้ว

    Watching this in a car, on a laptop with linux.
    I'm happy with myself
    xD

  • @adonis1168
    @adonis1168 6 ปีที่แล้ว

    22:23 mutation

  • @raderadovic7886
    @raderadovic7886 7 ปีที่แล้ว

    where I can find the whole code? It's much easier for me to follow

  • @patuitar1117
    @patuitar1117 4 ปีที่แล้ว

    Why not just use dijkstra's algorithm

  • @OrangeC7
    @OrangeC7 6 ปีที่แล้ว +1

    4D travelling salesperson problem, you say? 🤔

  • @bechirjamousi6696
    @bechirjamousi6696 4 ปีที่แล้ว

    But the TSP needs to return back to the city he starts with! Am i right?

  • @Drone256
    @Drone256 4 ปีที่แล้ว +1

    For decades there was something similar called the traveling salesman problem. You are being very PC about your computer science. Lol.

  • @vasugupta9824
    @vasugupta9824 6 ปีที่แล้ว

    swaaaap

  • @adonis1168
    @adonis1168 6 ปีที่แล้ว

    17:50 next gen

  • @Tymon0000
    @Tymon0000 7 ปีที่แล้ว +1

    Woah your coding is messy as hell xD

  • @zakcodes3507
    @zakcodes3507 7 ปีที่แล้ว

    I watched the stream and I remembered that your algorithm to associate a fitness with a probability really annoyed me so here's mine which I use in my NEAT algorithm and is way more efficient and looks like yours a little:
    var fitnesses = [];
    function pick() {
    // Get the fitness sum
    var fitnessSum = 0;
    for (var i = 0; i < fitnesses.length; i++)
    fitnessSum += fitness[i];
    // Pick a random one
    var prob = fitnessSum * random();
    fitnessSum = 0;
    for (var i = 0; i < fitnesses.length; i++) {
    fitnessSum += fitness[i];
    if (fitnessSum > prob)
    return i; // or fitness[i] depending on what you want
    }
    }
    I'm probably going to do a pull request about it soon but it really annoyed me because I had to fight with a guy who didn't understand what I was doing because he was sure that the only way to pick a member depending on the fitness was to use percentages

  • @alexchase1856
    @alexchase1856 6 ปีที่แล้ว

    GENERIC not genetic ffs

  • @kennethstewart4SPToRead4FREE
    @kennethstewart4SPToRead4FREE ปีที่แล้ว

    Hasn't the traveling salesman been solved through path of least resistance?

  • @sabecraftgamingandmore1364
    @sabecraftgamingandmore1364 7 ปีที่แล้ว

    13 factorial is 6,227,020,800 (6 billion, 227 million, 20 thousand, 8 hundred)!

  • @juandavidnavarro
    @juandavidnavarro 4 ปีที่แล้ว

    Can you try this problem with Tabu Search?

  • @Jkauppa
    @Jkauppa 3 ปีที่แล้ว

    or you just send multiple salesmen, to every city, one straight line

    • @Jkauppa
      @Jkauppa 3 ปีที่แล้ว

      merge partial best paths

    • @Jkauppa
      @Jkauppa 3 ปีที่แล้ว

      you could display the achieved path length

    • @Jkauppa
      @Jkauppa 3 ปีที่แล้ว

      permutate partial populations

    • @Jkauppa
      @Jkauppa 3 ปีที่แล้ว

      minimize path length

  • @hadidihosney
    @hadidihosney 5 ปีที่แล้ว

    How can I modify this code to add my own points please can anyone help

  •  6 ปีที่แล้ว

    Please sub this video in English