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

preview_player
Показать описание
Genetic Algorithms are a family of evolutionary algorithms which can be implemented in any language (including C++) they solve problems which have no clear solution by generating random solutions and picking the best results then applying a crossover and a mutation to the best solutions before starting the process again.
Due to the "evolving" nature of this algorithm, the search space is greatly reduced in comparison to exhaustive search.

To learn more about Genetic Algorithms:

00:00 Intro
03:20 Initial solutions
06:40 Calculate fitness
07:20 Sort solutions
08:50 Print top initial solutions
12:40 Select top solutions
14:32 Mutation
15:57 Cross over
18:12 Review
22:12 Demo

#c #algorithm #geneticalgorithms #machinelearning
Рекомендации по теме
Комментарии
Автор

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.

Автор

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

Westernaut
Автор

Hii Brother
I am working in similar test case of genetic algorithm in c# which has Fitness function below

private static Func<string, double> Fitness(string goal)
{
return new Func<string, double>(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<string, double> 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

bhavin