Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Computer Science

Genetic Algorithms

Not exactly a question, FYI

I'm taking a course (outside of Treehouse) on complexity and complex systems, and I just finished the course on genetic algorithms (aka GA's). I was fascinated by the content, and so I decided to build a simple Python program to find "strategies" for playing a simple game.

I found it really interesting, and so I wanted to share my understanding of GA's with the community.

A genetic algorithm is a sort of machine-learning algorithm that "evolves" over time. For example, a long time ago, the ancient dinosaurs dominated the world. But eventually their species died out. More often, though, species over time better adapt the situation, thus "evolving." Computer scientists discovered that this could be applied to AI, too, so out sprang the idea of a "genetic algorithm."

In a genetic algorithm, there are bits of "DNA", or sequences of digits. (it could be letters, too, but let's stick to using digits.) The idea of a genetic algorithm is that two parents create offspring with a mix of their DNA with some minor tweaks added. Then those children have their own children... etc.

Imagine that you had each piece of DNA represent two "strategies" for a game. We have some method of encoding that strategy as a string of numbers 1-6:

Strategy 1:

Strategy 2:

Now imagine if you could somehow "mesh" those two strategies together to make a child. How would you do that? Well, you could split the sequences somehow, and recombine it:

New strategy:

Notice that the first part of this strategy is somewhat like that of the first strategy, and the last bit looks somewhat like the last part of the second strategy. Also note that there are a few random tweaks in addition to that. Now, we can test this strategy and see if it is successful. If so, great! We came up with a new, working strategy!

Now imagine that we started with 1000 strategies. We could filter out the bad strategies, and pair up the good strategies randomly, and combine the strategies to produce new strategies, and then we could take those strategies, and repeat.

We continue to do this as many times as we want, then we can select the best strategy out of the remaining strategies, and that will be the final result! The computer can execute that strategy, and it may even outwit a human being!

Now, of course gameplay AI isn't the only thing a genetic algorithm can help make. It can even generate random music/art just by listening/looking at multiple pieces of art/music, and somehow combining them!

GA's are very fascinating and worth learning, and so I hope enjoyed this topic! Please leave comments below.

Happy coding! ~Alex

Dane Parchment
Dane Parchment
Treehouse Moderator 11,075 Points

Well one particular use for this @Balazs Pukli is that you can use a genetic algorithm to solve an optimization problem. As each successive generation may get closer to finding a solution that is most optimal for solving the problem provided.

Balazs Peak
Balazs Peak
46,160 Points

Nice. Thank you for clarification. I'm researching optimization methods heavily, so this will certainly come up once again in my future.

1 Answer

Balazs Peak
Balazs Peak
46,160 Points

Thanks for sharing! I'm not so sure about the exact edge of this whole thing yet. I'll have to wrap my head around it. Some time in the future I will learn more about this since I'm interested in machine learning.

The basic idea is very obvious, it's only that I don't understand how come this thing will be productive. It feels like you could achieve the same thing by trying random values, instead of bits and pieces from existing set of values. But most certainly, there are situations where this is useful.