Build a Genetic Algorithm From Scratch In C++ 🧬 💻

Поделиться
HTML-код
  • Опубликовано: 28 янв 2025

Комментарии • 12

  •  2 года назад +27

    Nice demonstration.
    Three points to improve performance (since that's probably the reason you're using C++ in the first place), in order of less important to more important:
    1) Take advantage of moving vector elements instead of copying them, so instead of std::copy, use std::move and instead of push_back, use emplace_back. In this particular example, it does not matter, but when using more complex objects (that use heap allocation for example) in the Solution struct, it can make a big difference.
    2) Do not call the fitness function in its own loop, but have a constructor method so that it is called when you generate x, y, and z. Less iterating means better performance.
    3) Most importantly, you do not need to sort the entire vector of ten thousand elements to get the top thousand. The Standard Library provides you with the std::nth_element algorithm exactly for this purpose.

  • @CallousCoder
    @CallousCoder 2 года назад +2

    Very neat and cool ❤

    • @TheBuilder
      @TheBuilder  Год назад

      now what's a problem that actually requires a genetic algorithm to solve

  • @asterlookanalytics9853
    @asterlookanalytics9853 7 месяцев назад

    Fantastic. clean and precise

  • @Westernaut
    @Westernaut Год назад

    I am only wondering: why we need to literally watch you code? This is common on RUclips, I realize. Just maybe, you might be one to explain...

    • @TheBuilder
      @TheBuilder  Год назад +9

      The RUclips algorithm between 2014~2020 liked it when people watched videos longer, regardless of enjoyment. This is why all the top IT channels have 10+ hour videos where they slowly type code out. I think they changed it now to favour retention and session time over pure watch time, which is why I'm moving to a format where I quickly explain a concept and ask you to watch the next video. Check my latest videos to get an idea. Hope that helped.

    • @TheBuilder
      @TheBuilder  Год назад +4

      If you want to see a channel that is thriving under the new algorithm, check out fireship

  • @ahmedalbadry3800
    @ahmedalbadry3800 2 года назад

    can i get the source code please

    • @TheBuilder
      @TheBuilder  2 года назад +3

      Took me a while to find it but here you go gist.github.com/TheBuilder-software/11e4018eb33921b5dfb1f059018d6fdd

    • @ahmedalbadry3800
      @ahmedalbadry3800 2 года назад +1

      @@TheBuilder thank you so much appreciate it

  • @bhavin716
    @bhavin716 2 года назад +1

    Hii Brother
    I am working in similar test case of genetic algorithm in c# which has Fitness function below
    private static Func Fitness(string goal)
    {
    return new Func(chromosome => {
    double total = 0;

    for (int i = 0; i < goal.Length; i++)
    {
    if (goal[i] != chromosome[i])
    {
    total++;
    }
    }

    return 1.0 / (total + 1);
    });
    }
    I need to complete a function which Find Binary Genetic String which accepts above function parameter as input also other
    parameter is probMutation and probCrossover are provided floating point numbers that represent the chance of their respective modification occuring.
    public static string FindBinaryGeneticString(Func fitness,
    int length, double probCrossover,
    double probMutation)
    {
    //need to complete logic here
    }
    Above function should return a binary string of '0' and '1' of chromosome Length that has a fitness score of 1 as computed by getFitness.
    Can you help me to iterate over this function logic i am bit confuse over logic