9.6: Genetic Algorithm: Improved Fitness Function – The Nature of Code


Hello welcome to another genetic
algorithms video, in this video I want to talk about an improved fitness function,
now there are so many different ways you can improve a fitness function in ways
that you could design and think about a fitness function and I less mean this
video to be like here’s one thing but I just want to use this video as a way to
start thinking about how this fitness function is your creative thing that
you’re creating and anything you could imagine you could possibly do with that
function. So if you recall this particular example which is a search
problem it’s a simulation running a genetic
algorithm to evolve the phrase “To be or not to be” and every time I run this it takes it’s typically taken between
somewhere between like 300 and 600 generations to get to the correct phrase
and you can see it took five hundred fifty four generations
so let’s talk about the fitness function [Coding Power!!] let’s talk about the fitness function and the way to fitness.. if i were to
graph that fitness function right? This is number of, the x axis is the
number of characters correct ok? so the more characters
you get correct the higher the fitness right? Yay more characters correct higher the
fitness and this works because I might have you know in the case I might have a
phrase that’s like 20 characters long and I have you know 16 characters
correct versus 17 correctors.. characters correct, this as a higher fitness it’s going to be more likely to be
picked as a, in the selection process to have its genetic information passed to
the next generation but there’s a problem here what if this
phrase were not 20 characters long but what if it were two thousand or two
hundred thousand or two million characters long? Let’s think about the fitness values of
certain random phrases so one random phrase might
have 391 263 characters correct and another one might have
390 264 characters correct now this one is better than this one and in fact just one more character correct it’s
really a lot better than the other one and we really want to make it more
likely to have its genetic information passed down to the next generation but if you take this value and divide it by 2
million and you take this value and and divide it by 2 million, the number you get from this
value is only a tiny bit bigger than this one so in other words this value is only a
tiny bit bigger than this value but I really want that one that’s a little bit
a tiny bit higher to be much more likely to be picked because I want my evolution
to happen efficiently and faster and all of that so a simple way around this is to make
the uhh.. to make the fitness function exponential what if I could have the number of
charac.. the more characters you have correct the more.. the higher your fitness is
exponentially and there are a variety of ways you can do this I could say the
fitness is 2 to the n (2^n), being n is the number of characters correct I could take n squared or n to the
4th power you know I could probably come up with
some other fancy or math to do but I just want to get you thinking that these
are the types of issues that you want to think about and in fact let’s go back to
this program and I’m gonna go to the fitness function which is here right I’m
just gonna say is a very simple thing I’m gonna say this.fitness equals power this.fitness squared so this is me
taking fitness to the second power and I could’ve just said this.fitness times
this.fitness but whatever I’m doing this.fitness squared and now let’s run
it again and I.. you’ll have to sort of trust in me at this point but I run
this about four or five times and it’s solving it between about three hundred
and six hundred generations let’s run it again. We can play some music
while we wait.. there’s no music… ♪♫ Oh I didn’t have to wait very long – 240.
Let’s run it again ♪♫♩ ♪ ♫ ♬♪♫ ♭ ♩ ♪ ♫ ♬ ♭ ♪♫ boy it’s taking a long time… boy this is not good That was an anomaly, let’s run it again ♪♫♪♫ 217 – that’s pretty good. ♪ ♫ ♬♪♫ ♭ ♩
(music playing) 236, you can
see I’ve improved and I could even go..
I could go to the 4th power right? ♪ ♫ ♬♪♫ ♭ ♩ Aah 120 the wrong button.. refresh ♪ ♫ ♬♪♫ ♭ ♩ 89, so you can see that that exponential fitness function really
has improved things and so this is something you could consider. So you
could also go back to the Smart Rockets example as an exercise, find the Smart
Rockets example that I made in the previous coding challenge and see if you could
use that distance, the distance to the target which is part of the fitness, and
use an exponential function to see if you can make that particular evolution
happen you know more quickly or kind of work more efficiently
okay thanks for watching and I’ll see you in another video soon. Subtitles by the Amara.org community

Leave a Reply

Your email address will not be published. Required fields are marked *