Evolution 3.0 : solve your everyday problems with genetic algorithm
Mey Maayan Akiva · Reversim Summit 2018 transcripts · Markdown
Human subtitles from YouTube. Click a time to play the video from that point.
00:11 Hi everyone, thank you for waking up just for me. I am honored and I'm super excited because it's my first time here talking in Reversim . So excited Let's start. So my name is Mei. I am an nodejs developer I'm a software engineering graduate and I'm a huge Star Wars fan. Yes. These are our R2D heels So I have to tell you about my last semester in college So during the last semester of college I had 10 courses left to take 10 and oh, yeah, I had to work three times a week and Having to balance between work and college I had to have the best Timetable so let me show you how I assemble this timetable So when I have to start to assemble my timetable, this is what I had to do. I have the list of courses and
01:13 lectures are mandatory and Tutorials you can choose from this list So let's say we want to choose the calculus course because that's what I want to do So I choose the mandatory lecture on Sunday and also the tutorial at 9:00 a.m In the morning because that's my favorite hours. So we chose these courses But when I choose a class I want to consider my own constraints So the first constraint I had to consider was the minimum school days I don't want to be there if I don't have to and I didn't want to have too many clashes between classes well, I can't be in two different places at the same time and Also, I wanted to have minimum gap between classes because I didn't want to wait for a class for more than at one hour So let's go back to the list of courses and see if what I chose was good. Okay, so I chose the the lecture of assembly. It's 9:00 a.m
02:14 In the morning on Sunday and also the tutorial and it's at the same day at the same hour That's it. I violated the Clashes constraint and sometimes it might be okay to violate this constraint. Maybe I don't have to be at the entire Lecture, but I want to try to avoid Doing that. So let's try to re choose this class so now I went for the Wednesday tutorial of calculus, let's see how this timetable looks and Okay, that looks good because I don't have any clashes I don't have any gaps But I have to go three times a week and I might want to try to re choose again Maybe I can do it a little better okay, so because we've violated the minimum schooldays constraints, let's try to re choose again and Okay, so now I re choose the assembly tutorial I moved it to Wednesday oh My god, that's a perfect timetable because now I only have to go two times to collage
03:18 there is only one hour gap between classes and there are no Clashes at all. So notice how it took us about three to four minutes to assemble this timetable It was only two courses and well, I prepared this example, I knew what to expect so it was easy But try to imagine if you had to do that with ten courses, that's a lot So I had a problem But not really I'm a programmer. I can program my own timetable generator Okay, so I had to define my problem My problem is a constraint satisfaction Problem. What does it mean? It means that I have constraints and a solution is A time table and a time table that answers all the constraints is the very best solution so Just like a good student What I did was I turned to recursion because that's what you know what when you're in college
04:19 I tried to solve this problem with one of the most famous constraint satisfaction algorithm It's called backtracking So backtracking what it does is it goes through every possible solution? And it searches for the very best solution and if it exists that what will be returned out of the backtracking so when I inserted two or three courses it worked great, but then I Inserted seven courses in eight and I waited and waited and waited and the answer from the recursion never returned A Quick calculation suggested that I might even wait two and a half million years for that Solution so I did have that time but semester was about to begin. I had to revise my strategy Maybe I don't need the very best solution. Maybe a good enough solution is also a good enough for me. So Everyone we're going to talk about today the genetic algorithm
05:22 Okay, before we begin, let's take a look at a quick background Okay flowers. Let's say that the red color is the strongest color just because that's my favorite color so if we have the initial population and We combine them together the best qualities will move forward to the next generation and then this generation creates another generation and the best qualities the fittest qualities will stay on forward to the next and next the next generation and We are a very good generation ourselves and I think nature did a pretty good job so why not use that to solve my problem So Genetic algorithm. Let's see how we use that in our problem of the time table world Okay, we start with generating an initial population Then we move forward to the fitness step. This is where we grade each solution
06:25 Meaning that we determine how good a solution is afterwards we have the entire these three steps the selection the crossover and the mutation all of them together are called the Reproduction part and this is where we create new solutions out of the existing ones And how do we stop the algorithm? well There are two ways either we can stop after a number of generations or we can stop after an amount of time and it's important for me to say this it just Because you let the algorithm run for a longer time. It doesn't mean you'll get better results. It's not necessarily so it's all about exercising trial and error Okay Let's dig deeper into this algorithm and see how it goes All right the first step generating an initial population So we have the timetable problem. Just please notice that over here It's number instead of names because I thought it will be easier for you to see just assume that these are classes
07:31 Okay, so a solution is a timetable many solutions they construct a population and many population is all of these populations together they are many generations Okay, how did I construct a solution or the initial solution? what I did was I randomly chose courses out of the courses list and I inserted them into a timetable so even if we got Not-so-good timetable. Let's say I had too many clashes all of the courses I had to be at all the different places at the same time. It doesn't matter I'm going to deal with the invalid solutions later on in the fitness step So right now what we're doing and the first step is just generating random solutions even if they're invalid solutions Okay the size of the population. So what worked for me was a hundred solutions a
08:33 hundred solutions per generation How did I find that out? again, It was all about Exercising trial and error So if you have a wide range of solutions for your problem I suggest that you'll have a wider range of Population because then you can cover a wider range of solutions per iteration but don't have A too big of population because then it will influence the performance of your algorithm. So again, Exercising trial and error is what works here. Okay Now that we have a population each of the solution is Randomized. So what I want to do now is grade each solution I want to determine if a solution is good enough or not or how good a solution is. How do I do that? So The grade for each time table is constructed
09:36 out of the straight three constraints that I mentioned earlier I worked with lower is better because I wanted to avoid negative grades and Let's take a look at how at how I did that Okay, I'm going to start with the simplest constraint and it's just the number of days that I have to be in college so each Time there is a day It gets a penalty Let's take a look at an example. It's the simplest way to explain So a penalty per day is 10 points and here we have two days it means that the grade for this time table for this constraint is 20 but it's only this constraint the amount of days per timetable got the grade 20 So now let's move forward to the next constraint the gaps So a gap got 10 points
10:38 Penalty per hour so over here. There are five hours gaps over there in Sunday There are three hours and then two more hours on Wednesday. So all in all That's 50 the grade for this time table for this constraint is 50 So the entire grade for this time table that is constructed out of all of the constraints Will be 70 because we had 50 for the gaps Then there are no clashes at all. So it's a zero right there and 20 for number of school days. So remember lower is better. Let's take a look at it Another example, I think it will help you to understand better Okay, I want to talk about the clash of this constraint because this is a problematic one So if I have too many clashes Then it's an invalid solution because I really can't be in so many places at the same time
11:39 But sometimes if I have to miss just the first half of a class or the first half of a lecture That's okay for me. That's a valid solution. So this is why I wanted the clashes to have a higher penalty than the other constraints but still Every solution that is generated is actually valid solution. I just wanted it to have a higher grade So it will express that this is not a good solution not like the other ones so over here you can see we're already at 100 and It's just for this constraint. So that's pretty high because oh what happens here too many clashes Okay, so the overall grade for this time table is a 120 so it's not such a good grade But I still want to have this time table as an option In the entire generation
12:39 Okay This is the entire fitness function I would like to advise you again to exercise trial and error. This is what helped me determine The size of the penalties how big do I want to have one penalty higher than the other one? So that's what helped me again Trial and Error Okay, now the next steps are the reproduction, what we have is A Generation each solution is graded Now What I would like to do is to generate new solutions out of the existing ones. How do I do that? We start with the selection step so the selection steps talk about choosing the parents and Those parents will generate children or not children just solutions Okay, what did I do? I chose to work with the roulette wheel selection. Why? because this selection
13:41 Helps me to have better solutions to have better chance of being chosen as parents How did I do that? Oh, it's really important for me to say that there are many ways to do the selection part But this one worked great for my project. So again, what did we say? Yes, trial and error Okay, let's take a look at a quick example Let's say that We have only four time tables as our generation each time table is now graded over here You can see the Fitness grade. Okay, so Now I wanted the grade to express the chance of being chosen Not the fitness. So if green is the best time table I wanted this time table to have the best chance of being chosen as a parent So now it got the highest grade. I summed all these grades up because I wanted to normalize the results and now what you see over here
14:47 these are Percentage percentage of what Of the chance to be chosen as a parent all we have left to do Generate a random number between 0 and 100 and that's the parent You're going to choose whether it's this one or the other one But they all have the chance of being chosen Ok, we chose the parents now, what did we want to do? children Ok, let's move forward. So I chose the 1 point crossover The crossover is the step where you generate new solutions out of the existing ones How do I do that? You select a single point to cross the Solutions and then you construct a new solution From the two old solutions and I chose this Crossover because it kept the order of my solution It didn't mess around all the courses and this is how I generated a new solution again
15:50 There are many ways to do the crossover. It depends on the way your solutions are built. It depends on your project and Again, what did we say exercise trial and error? Okay this is my favorite part of the algorithm and Before we move on into talking about mutation. I would like you all to stop zoom out of the timetable world and come with me and Take a look at a genetic algorithm game. Yes, there is a game of genetic algorithm It's out there online and this is a frog. Yep. you're looking at a frog right now What does this game is doing? Okay, let me see So this frog is a solution the Frog what it wants to do is to jump as further as possible Many solutions they are a generation all of them together Try to jump and jump as better as possible
16:52 So now over here we can see each solution trying to make its way all the way and for those of you in the back who can't see this is generation number 12, and the Fitness is 14% and now if we move forward to more generations what we will be able to see is That all these solutions they jump together. It's almost like one solution. They're kind of the same. I mean Where is the evolution part what what is going on here? Why is it all going to a local maximum? The Fitness is still 14% we reached a local maximum because we couldn't get a higher Result so what we do? We take a look at this game once again, but now I inserted mutation to some of the population What does it mean it means that I might throw in an extra leg? Maybe I'll throw in an extra muscle
17:52 but now what happens is that I managed to generate a bigger range of solutions that I didn't have before I couldn't get these solutions only by Getting two solutions together so mutation helped me avoid the local maximum Come back with me to the timetable world. How do we mutate a solution of a timetable? So we have this timetable and I wanted to mutate it. So what I do I go back to the list of courses I randomly choose a different class And I insert it to the same timetable What happened here? I got a a worse solution because now there is a clash but it doesn't matter. I want to keep those Invalid solutions. I want to have them because this is what creates the bigger range of solutions
18:54 Okay, that was the reproduction part yay and That's the entire algorithm Know I know you all have so many questions Mae. What happened in that semester. Did you have the best timetable? Yes, I did I had the best time table But I did not go to any class because well it was better just to study on my own It was much faster. And then that's it So I know what you want. You want some code. Let's take a look at some code But before we do take a look at some code I would like to say that this project is Out there on github. I did that with another good friend of mine and we use the wonderful API It's all written in C#. And I advise you to go take a look. Okay? So, I don't know if you can see it very well, but I'll just read it to you so what happens here It's the code for executing the entire algorithm so the input will be the list of courses and the list of constraints and then the output will be
19:57 The last generation So when I returned the last generation, it means that it's the best generation so far What I did is before I returned it to the user. I Sorted all of the results that I had and this way the user could look at the best result first, so We start with generating the initial population by sending it the courses list then we activate the fitness function once before the loop And Now we have the loop over here I chose to have 20 generations and this is the algorithm loop So what happens in that loop is that we go in we have the reproduction part and this is where we produce new Solutions out of the existing ones and then we activate the fitness function Which is the one that grades each solution again, and again and again until we have the last generation
20:59 Ok Now we can talk about trial and error I don't know if I've mentioned it before but if I didn't I'm going to do that again trial and error everybody ok So I have a little story to tell you One day I came to my friend and I asked her Liron she's sitting right here: "Liron AM I FAT?" and she said well, you're not fat , but you just have to Maybe eat better. Maybe plan your diet a little bit better and I said, well, you know what? I don't know how to plan my diet But what I do know is how to write code So I told her listen Liron you're going to help me out on this one And we're going to create together a diet planner or in its nicer name the nutritionist and that's what we did We work together and we develop the entire genetic algorithm from scratch for this project what does it do so it gets a list of products that you have in your fridge
21:59 And also a list of constraints The amount of calories you want to eat per day the amount of fat you want to eat per day the carbs. Oh Just whatever you want to insert right there As a constraint and then what it returns is a list of products that you have to eat per day if you want to maintain your diet and So we started to work on the algorithm at all. It went wonderfully We had a great time, but then it was super slow because our range of solutions was just huge So what we did we started to investigate each part of the algorithm and we found out that our selection part We used the roulette wheel selection. And if you remember it goes through every solution and it normalizes the solutions so this is what influenced the performance Of the project so we have to exercise trial and error and we search for a different
22:59 Selection and we found out that tournament selection has a better performance So how does it work the tournament selection it goes like this you have the entire population Out of this population you randomly choose The tournament pool which is this array our pool was just four so we randomly choose Four solutions and out of these four solutions. We returned the fittest one so this is the tournament selection and it's way better because we did not have to go through all of the solutions And this is how we had a better performance because Well, it just worked much much better Okay code How does it go? so we get the entire Population as an input and then we want to construct the tournament array We are going to do that inside this loop, like I said our tournament array was the size of four
24:01 so getting randomized solutions and Inserting it into the tournament pool and than returning the fittest solution and this solution Will be the parent of the next generation. Okay, I will activate this function Twice one for the first parent and then again the second parent and then I can combine these two Solutions in the crossover step and create a new solution many new solution - We have the new generation That's it oh my god So let's summary things up We started with talking about the problem that I had we talked about the timetable problam I didn't want to work on trying again and again to generate my own timetable with pen and pencil So I turned to backtracking which is a great solution if you want the very best solution because it goes through
25:07 Every possible solution and if there is the very best solution it will return it to you But it might take some time So this is why I turned to genetic algorithm Then we talked about the entire flow of the algorithm we start with having the initial population and there I said that my population was generated randomly And even if I had Invalid solutions. I kept them which is okay because I handled those invalid solutions in the fitness step Remember the Fitness step is all about grading your solutions remember right there that you want to have a function that will generate a range of solutions a range of a Range of grades not solutions and you already have them but you want to have a function that will generate a range of grades after you graded each solution there is a reproduction part where we generate new solutions out of the existing ones and Don't forget about my favorite part the mutation part over there. Don't go wildcard try
26:12 Again, and again until you see that the mutation is not too high, but not too low that it has its part but it helps you to generate new solutions that you didn't have before And I highly advise you to go to the github take a look at the code that Liron and I wrote or any other place really I love this algorithm. It's very very simple And if you try to do a project like this yourself Tell me about it. I'd love to hear Questions somebody please. Yeah Yeah, so it's a great question I'm going to repeat it So at the mutation part the question was if I get a solution that I already have in the generation yeah, so the question was do I check if I already have this solution in the generation or Okay, so previous generations. I didn't check And the timetable project I didn't check at all I didn't care because I had a wider range of solutions I had so the population size was a hundred so but for the
27:14 Nutritionist I checked because I had smaller and it was more important for me to be as efficient as possible So it depends on on that. Yeah I'm gonna answer in English and I'm gonna pick the question because it's important for the video Okay. So the question was did I go to any genetic algorithm course or did I learn that somewhere? No YouTube internet But and what yeah. Oh So what happened was really I really tried to work with backtracking I had one day to finish the project that was the final project of the semester and I had to search for something better because I literally didn't get an answer and This is when I found out the genetic algorithm is good for constraint satisfaction problems. Just like this Internet help me That's it. I had no choice. I I needed an answer like really quick
28:17 Anything no, it was all just understand how often Yeah, so what you do is I chose to do 30% of the population. So if I have 10 solutions, I mutated 3 out of these solutions and for the nutritionist project I actually did an extra step I took out the very best solution each time and because this way I kept that the best solution won't be mutated and I just did the mutation on the entire rest of the generation, yeah So the the rate is about how many? Solutions you want to do the mutation on Anything else yeah, yeah Okay, I'll repeat the question yes so what I was asked is if I have any statistics regarding whether the
29:19 Lasts or the best solution was generated by any specific step were like was it by the Mutation or was it randomized or was it just by the crossover? So I don't think that it's possible, but that's just my opinion I don't think it's possible that it will only be generated by one step because each time that I start with the initial population I Insert them all them Okay I'm making a huge mess out of those solutions because it goes through every step before I even get to the chance of checking the fitness of this solution so I don't even have a way to check maybe I can insert a check before the fitness function and also Just after they initial population. That's a good that's a good question. I can check that The question was How did I know what to change each time because there are many parts of this sort of this algorithm right that's the question
30:19 Okay, how do I know if I got stuck on a local Maxima? Okay, so that's I'll start with this one How do I know if I got stuck in a local Maxima and then I'll go to how do I know which part I want? to change so local Maxima What happens is when you generate them when you start the algorithm and I just printed out all of the generations So I saw the fitness of each solution in each generation and if I see that the fitness Doesn't change just like we signed the game it stayed on the same fitness level again and again it just always it was always in the range like a very small range like it was always stuck on like a 14% or it was always stuck on this 30. It doesn't matter if it's a 30 is a high I still want to see a range of solutions out of grades I don't want to see only one grade over there. If and if it's like 30 31, I want more than that So this is where I knew I reached a local maximum. So usually it means that I want to change the mutation rate This is what like what I saw okay, it might change in different project
31:21 But for me, when I changed the amount of mutation that I did the amount of solutions that were mutated Then I saw that I solved the local maximum problem and okay. So the other question was how do I know which part to change? so what I did is that I is just started by writing the entire algorithm and Then I changed every time only one part and I saw if I got better Fitness range if I got better fitness results if I got higher results, sometimes I changed something and I got lower results So I knew I had to go back to the previous Structure that I had before this change So every time I change only one part of the algorithm and move forward by the results that I got Okay, we're done thank you guys
← Developers' Communities: Building your Personal Brand and Creating Real Impact · Event Sourcing Scaling - Pay Less, Do More →