Machine Learning using Genetic Algorithms | Vinay Phadnis | Skillshare

Machine Learning using Genetic Algorithms

Vinay Phadnis, Freelance Programmer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
17 Lessons (2h 34m)
    • 1. What is ai or Artificial Intelligence

    • 2. Inspiration for Genetic Algorithms! (Nature)

    • 3. Fitness function

    • 4. Elitism

    • 5. Mating final

    • 6. Introduction about the project

    • 7. coding#1 main method

    • 8. coding#2 new class

    • 9. coding#3 calculate fitness

    • 10. coding#4 create gnome

    • 11. coding#5 mutated genes

    • 12. running#1 running your code

    • 13. coding#6 adding the ai

    • 14. coding#7 implementing the loop

    • 15. coding#8 Mating process

    • 16. coding#9 Finishing touches

    • 17. Running the project

32 students are watching this class

About This Class

In this class we will be focusing on learning Genetical Algorithms used in machine learning in the following modules:

  • Theory: This section will consider the basics of what Machine Learning actually is at its very fundamental level also followed by its difference with classical programming of defining rules beforehand. The main differences between a Neural Network and Genetical Algorithm are also highlighted into this section

  • Genetical Algorithm: The basic concepts are taken care of over here starting from the basics like a fitness function which as I like to call it, a major driver into the direction of learning or output that your program will eventually take up. Elitism, followed by Mating or crossover or mutation which are the key factors responsible for the 'learning' in machine learning are explained well in detail over here.

  • Guess-the-phrase: This is our first programming project based on Python. It is a light-weight project which serves a good purpose of providing clarity into the various aspects of Genetical Algorithm.

  • Path-Finder: This will be our second project which will use the concepts initialised in the first project to a new depth. This will be our first project where we will be having some graphical (non-terminal) output.

    I hope learn some useful skills from this class and use it to create awesome new programs which in turn will be your contribution in making the world a better place


1. What is ai or Artificial Intelligence: Now, the main question which arises in our minds, is how does much learning actually work on a farm? Direct 11. Okay, so now let's understand this by taking in a problem. OK, so we want to create a program which uses this particular database over here to actually recognize 110 digits. Okay, so this isn't a W s off handwritten digits, which I have returned with my Now we have to create a which can recognize these digits are classified them as actually being a door 19 So basically input perimeter. We can put any off these images on the output. We should get something between a 12 and nine. Okay, so now to do this, if you had to refer toa classical programming approach, okay, then you would have toe manually, look through their database, and you would have toe put in all these characteristics. Okay, So you recognize one, you will have a straight line. Okay, then the organizer to you will have a court followed by a straight line. They recognize a key. You can have two girls below on another, okay. And so on. You can go on telling our heart. According hard coding is basically a process off putting in all the rules inside a court. Okay, so you can go on hard. According each and every one of these routes. Okay, so now, after this, you have basically clear their on which recognize your digits. Okay? It may work well on the database, which you have seen. Okay, Because I live in a dark. You have created the roots. But what if someone else tries to draw a 70 r a t Differently? Are the four differently, then program? Absolutely. Fields. I'm basically a data program. You need to pushing a whole change off court are basically are a whole lot of new rules. Okay, so now, instead, off doing this, we have a new way off solving problems card machine. Let me. Okay. So, warden, machine learning does is back. Initially, you have created a blank new program, okay? And then you start giving your information, or you start gaining it. Okay? Cleaning is the process when you actually give the required intelligence on artificial intelligence to your app. Okay, so now you will give it an image off one. You were held up that this is a one on intern will do. Its thing are the machine learning program will do its thing on. Then spit out who, which will then tell us that one has a straight line or there is a slight course. But this will be no in quarter to be a in a program. Okay, so now you repeat this process again. So now you person on image or 100 enemy image, often here, and then you end up there. This isn't it. By entering the value on, then happen again. Do its thing on. Then it will slowly understand that here does something which has to close. Okay, So this is how you passing millions and millions of images. Are you in your morning before? Okay. After attaining the modern, you have a certain off rules. Ready? Okay. On the process or the dome in machine learning applications. The early Rico are daily defeat. Classical programming applications is that you can turn up absolutely insane rules. Okay, you can, Even with the rules which you have would have never thought that this might exist in this particular dessert. So then, in orderto basically use that you have to clear the new machine learning program. And now how everything works out for nominal level is that if you are having a classical programming approach, you pass in the import data, you pass in the rules and you get output. Okay, this is the classical approach. Now, when you are trying toe train your machine learning program, then you pass in the import you pass in the output and in the machine learning program gives you the rules, as are put. Okay. When you do this millions and millions off times your holes are really well accustomed to your training. The passer Oh, are in a general sense it can now will recognize those digits. So then you convert this into again a classical programming approach, you passing the rules, which was created by much loving application. Then you again given the import, which is a completely new 100 busier and then you get output, which is what we actually want. So from now on, don't trying difference here classical programming with actually machine learning o r e i programming, as I would like to call it, they both are really interconnector. And they bought steps which follow another. Well, that's pretty much it about this session. One thing which I wanted to cover is there in genetically goaded them in the particular genetic alarm. Guarded. Um, you have a limitation. Okay. So if you are a real human being, which, as you know you are on, you might be having some characteristics which are not presented either off your bedrooms, but present in your grant bedrooms. Okay, so that is how some genes are. So what do you mean, submissive? In a generation on the foreign generation, they can show up. Okay, so this is the type. All programming are logical. Master leverage. Nature possesses Bart in our course or for this syllabus. Genetically pollution or theoretical algorithm in machine learning is pretty much limited to west one generation. Okay, so if you have some quality in the child, it will be present in either of the band. Okay, This is done to minimize complexity or minimize computational resources. Require Onda. We all don't have hired GPO's to run. Our machine learning platforms are on for you to do that effectively, I support that limiting this level of complexities pretty much enough because again, our task is also not that completely treatable. required this pipe of randomness. Okay, so it died out off. Well, let's go on with comparing their differences off your in network on genetics going. Okay, So these are two very important branches off creaming off machine learning where you actually train your program by these two. My heart. Is there more methods as well? But these are used pretty often. So, Emmanuel, in their work, you basically have north which say yes or no. Based on the week. Yes. We're tries to simulate the human being. Okay, so then you haven't input over here. And slowly you go on having an output on once you deter mine. How far away from the output you actually are? That's when you start a process off back population where you actually fueling on their dials on. Then you again go close to output. So imagine as if you are putting in some values and all of these lexie imports, and then you have or diets or knobs over here, maybe hundreds off them, or even thousands off them on with all of them on the end, What you get is an output. Okay. So, for instance, in this particular case, well feeding on the pixels off the 100 image and under the output as either off the numbers . Okay, then you have much dinner ticket. Go to them. Okay, then it basically runs in cycles. Okay, So you have you have one generation on, then you did mind how much, Uh, near our how much accuracy everyone off those individuals is having. Okay, so if my required answer is it and I have passed an image often hear about three on, then they found off hundreds of people in the generation on you. 20% get answers. Three, the doc. You deceased, 20 blessing. So the final goal is to increase the accuracy. Okay, so then you again have this particular matter that you actually sort of the population into people who have higher fitness or people are guessed correctly than people who are nearly with the guests on people who are absolutely fine. Okay, from this, a small portion of the population is going straight out here in the process. Corn elitism on the other portion of the population, maybe the rest 40% off the population is actually used to throw in the process off, creating new offsprings on, That's it. Factors other entire new generation is popular and the rest of the population in the previous generation issue because that is not good at solving the problem. Well, here, this is how you slowly go ahead after generation after generation on be better at solving the problems. Okay, so I guess that's here to attend enough comparison between a funicular time genetically guarding them in the next fuels sessions. Well, be discussing in more detail about how genetically algorithm actually work. So this might seem a little bit explore those off theory right now, but just mean bear with it, I'm listening toe everything very carefully because once you understand this, you can literally create wonders out there with programming. Okay, toe. Except for this video, if you have any doubts like me know in the queue, any section down here on I also have some new links are some YouTube videos or some descriptions or some tutorials linked in the video sources section. Tal I was coming to go ahead and water and whatever you can grab, learn our toe, get information I hear coming do the work because that is when you get in a whole lot of information. Are you, pro sister. And then you can say that you have got some degree of knowledge. 2. Inspiration for Genetic Algorithms! (Nature): the question over here is that why is machine learning actually necessary or of what does this force all about? Okay, so now to start with, machine learning enables you to solve a simple problem. Okay? It is as simple as that. You just have to solve this problem. So now you can do this even by implementing some classes and programs like something which is not a machine learning programs. Like a bunch of if statements Jeff Loops, which are working together. OK, but what machine learning actually makes it different this dark. It has the ability to improve itself over and over again by running again and again. OK, so for a reiteration or every cycle of your program, you are actually getting more and more better accelerating the problem. Okay, so that doesn't work. We call the fitness function off. The lost function will be actually deter mined. How far away are we from our particular problem or a particular dooming? Okay, so now why This is out of the way. Let's get on to see what actually is the basic inspiration off machine learning are genetic algorithms, to be precise. Okay, So now genetically good. Adams are found A Lord and nature. In fact, the desert off. You watching this particular video? Our niece turning over here are speaking in front of you is actually a dessert off genetically evolution. Okay, so we have slowly observed some changes in mankind or in the animal species around us which are slowly like them towards, but are surviving. Okay, so all of them were again trying to fight the problem off survival. Okay, The problem of survival has more branches. Like the problem of finding food on the problem off being prayed to others on the problem of funding for finding food. All of these are interconnector. Come on. Another huge problem offset away. Okay, So in order to tackle this chili, mankind has evil nature. Has this system set in progress? The each of you are organisms slowly walls to solve that problem better. Okay, so now to illustrate this, I got three examples. Okay, the 1st 1 is off camera. Okay, so the camera was originally from the horse family. Okay. So you can see some clear changes in a camel and a force. Okay. The 1st 1 is the long legs. The 2nd 1 is the hump on the back on the 3rd 1 is actually the pick islands, which the camel have has. Okay, so now these legs are slowly developed by Mother Nature toe actually protect the camel's body from the intense heat off the side below. OK, so Canada usually set toe walker commute on their desert. That the signed is pretty much you turn up. So in order to protect themselves from that, their legs absolutely born to be longer than usual. Okay, the hump on the camel's back actually store some important new parents are maybe four material water resources sometimes. Okay. So again, that is not really but in their desert. So slowly you have a camera developing the hump on its back. Okay, Then you have a camera which has take idot's. Okay, I'm doctors again. You do the reason that the camera always wants to preserve the my share in its eyes. So that doesn't feed kindness in the density of nature. Okay, the next one is off Beatles. Okay, so be tens. Well, originally off two colors, one of them was brown and one of them waas a lighter shade of brown. Okay, so now if you look at a tree on. You have both of these details. So then, though light one is actually an easier prey for someone who is one who is trying toe, he'd be that so I turned them down. Okay, So, doctors, how you weigh have seen a huge decline in the population are meters which have the light brown color and instead of the dive brown needles, so wife did not. Okay, So doctors an example of genetic drift Where the whole population shift towards one Captain , your one quality. Okay. Next up, we have the American bison. Okay, so this particular case makes it other than especially because there was nothing wrong in the American Bison. Okay, It was excessively hunt down. They the stage where you're tired reached almost the list of endangered species. Okay, Slowly, the hunting was made illegal, earned the species recovered, but the problem was back. After all this hunting stop, there were very few license actually left. Okay. Now, as the population is growing going, so let if suppose there are five bison. So in the next generation that maybe eight or 10 by since and slowly the population went multiplying on But as you had a very limited starting population, The final population are the current population, which we have right now has very little political characteristics. Okay, so if you look at Bison's in general, almost all of them will look the same because doctors why they have the initial similar DNS scriptures over time. There are some variations. OK, so now let's look into how this actually happened. Okay? Doctor is particularly important because that is a We're going to engineer a program. Okay, so now so think about how this actually happened. We have to go back to the example of the camera. Okay, So now a camel had long did develop along the legs. But how did the camel actually do it so initially think like this? Their camels are part of the horse family has accepted earlier. But slowly there was some camels who are slightly longer legs, very small margin as compared with out there ones okay. And the ones were slightly shorter. Legs were close to the ground when they are entering or when they are forced to work toe the natural habitat with those desert, naturally does shorter ones are the ones where the shorter legs found it difficult to satellite. Okay, So as generations passed by, we have less and less camels who have shorter legs actually trying toe meat and produce some offsprings which again will have the same characteristics from their parents. Okay, so this is how slowly, slowly the population off camera so hard short legs slowly branta me on. Well, ended up having camels which have a long legs. Okay, So this is how we actually really engineer a problem towards solving some familiar tasks. So are the first example. We'll be doing the guest. The phrase game where anything which is similar to the camel is basically a theater by us. So, for instance, the camel retired shark legs had very low fitness because because we were to die soon. Similarly, our program, if we have a phrase defined if I IES nor close to the Phillies, then in our fitness function, that has a lower value, so it has a chance to die faster. Okay, so that is basically how e I are genetically Gordon Thomas found in nature. Okay, so next up, we have the process off How you actually get aviation in the appropriation Okay, So what? I'm meant to be our focus over here in most to parents and discards, actually production. Okay, so in this particular matter, both off the parents have their chromosomes. Chromosomes are set off. Jeans or detainees. Okay, be any is a single strand was minus one characteristic. Okay, So if you are supposed making the racing car that their one offer to the enabler tell us how it will react to write tons. OK, then. One offer being one offers the neighbors tell us how it will react with the left. Tons and one off. It's being able to tell us how, Willard, brake or accelerator. How fast will it done? Okay, so like this on sex off their DNA together, according Okay. So if the petting has one of their chromosome on, then while creating the offspring, this is happening biologically. So this might seem a heavy biology course right now, but trust me, when we started developing our programs are APS. Then you will really find this useful on later on. This will. So you immense clarity in the topic. Okay, so now basically, we have the process off producing new offspring so individually, each being me is basically compared to there is a random chance off this off Springer in the beginning from this parent or this parent. Okay, so now, after this process is done, you basically have a picture off these detainees. Okay, So if you are making a new offspring racing car from experience than the right turn may be influenced by the parent number one on the left on may be influenced by the better number two. Okay, This is how the process off variation is actually created inside. Genetically going on, there is again a very small chance that completely random being is generated in the offspring. This was that being is not a part of the parent. One are bearing two on that is basically random religion returning to the child. Okay, So this is how what I need you over here is that you need to appreciate the beauty behind how this machine learning are theoretical evolution. I go to them is actually referring to nature inspired by nature on. You should actually start appreciating how nature has really well part on these things. Okay, so that's it for this one. 3. Fitness function: Okay, so now next. Move on, door focused. We are very main task, which is unity. Good. Adams. A Z would have read from this course title the deer out of the week. Lex Dex starter. Because we have to discuss three main topics. Okay, in general, going them the 1st 1 is an important function. The second dollars 80 deism turned off. 3rd 1 is making next get started with the baby plus topic, which is the fitness function. Okay, so off Riklis function on a lost function is a way to determine how close are we are achieving our particular target, which is the problem that you want to solve toe. How close are we for it being sort? Okay, so now the very first thing which comes into our mind when we think off fitness and go to them our fitness function is the word fitness. Fitness is actually used in machine learning or in the medical Gordon student mine. How better are you to solve a problem? Okay, help back that you on instant a lost function is actually used toe. Tell you how bad you are, solving the problem. Okay? So you can use anyone off plus function on a lost function. But in the end, you actually have to configure two things. The 1st 1 is if you are using a fitness function, the value or freaked less to be as high as possible on on the other end. If you are using the lost function, the value off loss should be as low as possible. Okay, so now with this out of the way, let's look at some examples. The first example you do you want to look over here is the fact is, off our very own project Indigenous guest office. Okay, so that is been we actually decided off freeze beforehand. And then the guy gives extra random tries on with each try the n e. I helped far. Is it a little dark by implementing a lost function? Guess we compared the string. Oh, are we? Compare are particularly important in the eyes input. And then we try to match it letter by letter, every letter which has missed increases its law school. Okay, So essentially, if you have an e i, which gives out completely random guests, which is really bad ass. But you warned then it will have a really high lost money. Okay, in the end, we have two doors down the loss of value. So a second example which I would like to point in over here is off. We actually creating a program which finds the shortest part between two points. Which us? A breaker by line. Okay, so now over here, we have to define a new fitness function where we first of all, have toe taken toe according number off steps which we have taken toe reach their new part on In the second case, we also have to look from How far are we going from the line? Okay, so with the combination of these tools para meters, we can actually get their mind. How freaked are a model is actually becoming on the gold over here are still in. Please. Differentness court. Next up, we have a car racing game on game which gun? Our drive on the I can't drive a car on tour to retracts with like you left right? Go ahead. A big are you adjust your spirit. According so then over here, our fitness function can consist off to values. Okay, so the 1st 1 is the number off times we actually hit to anyone off the site. So this can be the lost function which things about how many times we hit the sites on the fitness function can be our distance from the starting point all this time on, so be used as a lost function when we compare the distance from the endpoint. So initially, even the eyes direct eliminated job. We have very high lost value. So we really from the starting point. And we haven't progressed much towards the end point. That is when the guy has a really large awesome. You okay, so dark, sir. I guess it is pretty clear about what a fitness function and lost function actually is. So now let's proceed to the next process. Are the next step, which is it isn't 4. Elitism: the process off. Elitism basically means toe given elite status to some part of our population. Okay, so now imagine that you have your particular population play a car racing game. So each one of them your Turks on down on, then you sort them according to their fitness function to the most freak people over here. For Lord by fitness courting, very pleasing. Now, this is how you sort of the population. So now some of their top 10 or 20% of our population, which has the highest fitness score you want to retain a great. So this part of the population is actually given directly to the next generation. This is something which is not found in nature, but it is pretty much done inside genetic God damn machine. Who can doctors? Because we don't have a biological lifespan inside machine. Let me because everything is running inside a computer. Okay, so now imagine as if you have the top 10% other top 20% off your appropriation, which is very fit. According to this particular situation, it may not be well at all. They told me the problem which we want to solve. But now you have a diet to configure over here. Okay, if you give on our too much off your current ambition, Okay. So ah, hair. When we cleared the I guess the freeze up or program in Baikonur be defining variable baby , Actually, name it. Age is a factor. It is a pleasure beach, and that is very actually given a numerical number. Likes it then to actually pass on 10% off this generation, which is the fitness directly to the next one. Okay, So what? Here dying. But you have to tell you, is this particular factor So you passen too much off your current generation into the next one on you end up with very less. Variability are very less genetically Difference were very less very output. And slowly your progress get slower. Okay? You eventually do end up in the eye model, which I saw the problem, but you may require 15 hundreds generations if you're alert with about 40% on instead, you may require District 8000 generation if you do it with their up 10%. Okay, so they're dying, but you have to on this side is actually balanced. But the other side is also a tricky. So you pass on to little off your current generation and you have the little people into the new generation which are actually trying to compete are actually trying to clear l things. Okay, so, doctors, when you actually lose a certain part of your current fitness function as well, So that doesn't mean if you find a good balance in between board your elitism. Factors were actually being That is a major factor inside the progress off machine learning applications. So any machine learning application you are the salt of the progress are the goal. Final goal should be to basically, it's on the problem. But going following on a sub gold where people require the lowest amounts off steps. Okay, so it does not practically If you want to do a simple program which can solve a simple task , and you need to require you need to hire basically online loud ai machine, it may require 10 to 12 days. Okay, if there is somebody where you can get this down toe maybe for days or two days, or even one day or even within us, is by speaking in this age is a factor. Okay, so this is a very, very important factor which affect the spear on accuracy off your program 5. Mating final: Now we have moved on to the final process off genetic algorithms, which is scored meeting. We're meeting basically involves two parents are two individuals from this particular generation on. They combined their genes in such a way that they have now created in your offspring on the final goal is there This offsprings would be some more better in solving their task than the parents are than anyone off the patterns. So now do this legs first going toe Some details on how this individual is actually created . Okay, so every individual are repairing. Every parent is again an individual. So these individuals have okay, chromosome is something which affects the behaviour or affect output off work that particular individual will generate. Now chromosome is again broke up in tow. Smaller spans off beings. Okay, so an individual can have different beings. So now the enemy is responsible for a single characteristic. So, for instance, if you're playing an e I, which simulates the dinosaur game president in Google chrome and you don't have an internet connection, so then basically, what you want aren't answered. Your gene often individual is maybe their distance when you will start jumping when an obstacle comes near to you. The 2nd 1 is how long will you jump? Okay, so these are some examples where you actually and court something in your d n e. Okay, this is basically done by the Did you go to the White Ranger itself? Again and again. Okay, so now imagine as if you have a ticket. Okay. This sticker can give in three values. The 1st 1 is sparing one bearing too. You have a random. Okay, so let's imagine that you have. You can generate random number between zero. And one. It can be 0.4 or 0.8 hours. 0.91 91 0.54 Now what we have done and guess the freeze up is back. See, you want to copy more and more genes from the parents and sometimes taken her and, um, okay, because we also want to preserve the progress which the has actually done until now. Andi also adding a random factor. Sudar, If all of the parents are the same fitness level, then slowly we have that I or we have a random import which, by luck, are by Gordon will actually creates an individual which is slightly better, which increases the fitness score slightly. So then, after some generation, you have the thickness score increased again. So back individual again, start meeting with a new one. From this generation, I'm the offspring again, Has a 50% probability toe, Get the updated off the random gene. Okay, so this is how what we have done and gets the free is that if the number is in between 0 to 0.45 then we take in the gene from paying one. If somebody is three things you don't want for 5 to 0.9, then we take in the genes from parent to on. If the numbers between 0.91 that is when we import a random gene. Okay, so now I guess this is a pretty clear explanation off how meeting the process of making actually works. Okay, I didn't have a lot off. The source is linked to this lecture on the resources section or the north section. So I really recommend you go and watch because that will further increase the clarity in her brain about the stop it on. Once you are crystal clear about the topic. We can proceed to the next session where we on the next section, where we actually start programming stuff on, including our very first application. Okay, but it is really crucial that you understand all of these factors. It is fine even if you don't do it. Burke, in order for you create something on your own. You do understand equipment is my goal through this course or what I want to put for to you is not very. You can immediate what I have done. I learn something from it. But instead I want to put forth something which can make you gay people off producing something on your own. Oh, you can make your own genetically guarded them so it can solve a particular task. Or making me I play a game on your own. And don't do that. What I recovered as you go through and here on, if you don't understand anything girly on Goto everyone off the north. So the resource is which I've been awarded in the lessons 6. Introduction about the project: Hi. I guess. Ah, lot off, Teddy really seems boring. So now, instead of spending more time on Terry, let's start with our very first project. Okay? So in this particular project will be creating a machine learning program which will use genetic evolution or maybe natural selection, and we can make it guests off phrase which we have predator mind. Okay, so we want to make our program. Guess this particular freeze. Okay, so now it is not as simple as you might think it. OK, so this program particularly has defined sect off letters. That is maybe lower case babies a closer than upper case here. Closer than all the numbers and all the punctuation marks on it Basically first comes up with random in the statement. Dined every time it has elector, that is not the same with the one in are required. Freeze. So, for instance, if it comes up with this particular phrase, so then over here this letter is not the same, followed by this one followed by this one. So as you can see, not a single record over here matches to the corresponding one below. Okay, Space is also a character. Okay? So now over here. For every letter that we don't match, we actually increase the score by one. Okay, so over here, uh, if there are 25 letters which have not matched with our original freeze, the score is 25. So naturally are finally amiss to make the score zero. Okay, so for Dr Will use natural selection or genetic algorithm, and we'll design a bought which will basically forced guests random statement and then slowly check if it is near toe this particular freeze by actually checking the lowest score . OK, so if, for instance, we start with a batch off 100 100 individuals, o R 100 program boards on everyone off them gets to generate a random sequence. Okay, so now some of them are naturally closer to our expected outcome, and some off them are more far off. Okay? No one. No one is exact without outcome, and the probability of getting that is really low. Okay, so then which actually remove half off them, who are really far off from our particular phrase, Will score is really high. Then we take the upper half, which has the lower score, and we actually start the process off meeting in with within them. Okay, so now they have generated 50 offsprings, which have the mixed qualities off both off the boards. Okay. So naturally, they will be somewhat more closer toe our particular phrase. So this process goes on and on on and slowly after that, after maybe hundreds of generations, we we'll actually see your board, which can recognize our program organizer. Freeze. Okay, so now this is a particularly long phrase for the board to guests, so but yeah, let's tried out. Okay, so now over here, I have the program running on, as you can see, so if we look up here, Okay, so this is basically the output which is coming in their terminal. So if you look over here, you will see that the gold has basically generator completely random guesses. OK, so and this is still somewhat similar toe our particular outcome. But if we look at okay, so if we look at the starting generations, as you can see, this is a pretty random outcome. Okay? Are now as you proceed, I heard you can see that the outcome is getting closer and closer to our expect. Er freeze Okay, so now over here, if you have. If you can see, there are It is only lacking with two letters. Okay, on. Now let's see if our eyes actually ableto crack Arcor. Okay, So now the thing with you need to actually keep in mind over here is that we can also create a program without using any particular AI which will guess the same thing. Okay, So, for instance, if you are over here, you can guess A to Z. Everyone off the letters one by one. And check if you are any closer to the final score. OK? But that will consume a huge amount off steps. Okay, so over here you have 26 possibilities for the lower case than you have 26 which becomes 52 . Then you have nine Lecter's witches again. 61. And then even if you have nine symbols of the 70 so 70 earned. What? Here? If we look at this, this is the 456789 10. So this is at least authority. Character Long of freeze. OK, so 70 in 2 17 to 70. And like this, we go on doing 30 times. Okay? So it is something indoor. 10 arrays to power 30. Okay, on. Still, we are not taking the sevens in between, so it is actually more okay, So can you just imagine that remained us amount off step, which we would need if we want to hear something like that. Okay. But now, using AI or using machine learning, we have actually basically ran our program, and we can see an outcome within their 27,000 step. Okay, this is a pretty good task, okay? And this has the thickness zero. So fitness is the score. Okay, so now this is the lowest score with we have a TV on. And yes, this is pretty much is our goal. Okay, So now, as you're clear with water project actually is And what we are going toe build. So now, without wasting any more time, let's get started by actually writing the court 7. coding#1 main method: Hi and welcome back. So over here I have a new document and sublime text. So first we have to save it and are prefer locations or in file. Click on. Save on over here. You will be basically prompted toe. Save the file on your preferred location. So we're here. I have a folder called According And Over here. Let's make a new folder guard. I guess the phrase. Okay. And inside against the freeze. Basically, we want toe save the filers. Mean dark B y. So over here we are going to use Spuyten for the according off this particular game. So the extension is dark P Y Okay, So now what? Here are you basically have sublime text open, and now we have to basically start building our court. Okay, so now at the very first stage, we have to first build our main matter. Okay, So now, toe, build our main matter. Basically, we have to use their deaf keywords or deaths turns for a definition. Okay, So deaf and f mean. And the new order colon on, then basically, whatever your type should have some space over here and died basically indicates that everything is under the main function. Okay, so this is the main my 30 for stuff all on. Basically, the thing is that we want Tobe basically initialize some court even above the mean method. Okay, so at the end, when everything is completed, we want toe executive, some court, which is above here. Okay, so if we directly run the file, the by turns could basically executes the court line by line. Okay. So any court above them in my third will be executed for stern. We don't want that to happen. Okay, so now we have toe give a reference to the main matter. So whenever the file start, we have toe tell by 10 that you have to start with the main matter. Okay? So to do this, we have toe basically are another command so that this give Then you have to underscore. Then it is named. Then again, toe underscores is equal toe single in ordered commas. Then again, double underscore mean on again double underscore. Okay. And then you give a colon over here earned over here. You just referenced the mean matter. Okay, so over here, basically what you have done is dark. You have basically created a new instance off your own file Where the name off your file If you enter the file by typing this particular thing in their terminal So whenever we are running, our particular file will type Buyten's space mean dot B Y. So basically, we're entering our filenames over here. If we have entered a file name Onda, then we basically have to enter the mean mattered. Okay, on this is basically how it works. So inside the main matter, that's where we'll start building our core. Okay, so now let's start with writing. Our very first statement on that is we have to basically initialize a population size. Okay, so how this will work is dark. You have tow, basically, first, create generations. Okay, so every generation will have a particular population size on every one off their population will guess a word according to their chromosome. Okay, so the chromosome is basically and harder quarter property off that particular individual, as we would call it or that particular self inside a generation. Okay, So imagine as if we are having the generation off 100 people on. We have asked them to do a particular task. Okay? so some of them are better doing it than the others. Okay, so now suppose that we saw it. Everyone off them according to how they actually performed in that task. Okay, so if we want to basically sort someone by their running speed eso naturally someone will have the highest on experience. Someone will have the lowest running spirit. So if we sort all of those 100 people's accordingly, then finally we can say that we have a good salted generation, okay? And then how genetically I'll go to them actually works is dark. You have to basically pass on the top 10% off the people directly to the next generation. Okay, Who are the best performers? Okay, under the remaining 50% 0 are more basically have to do the process off mating where they create new individuals on those new individuals are basically passed on to the next generation. And this is how the next generation is built. And all this happens within the top 50% off the top 60%. Okay, So this is how what is expert expected is that you have a crossover off people who are in the top 60% and with some changes in between them. On that is how you basically generate a new generation. Okay, are naturally the new generation again has 100 people. 100 people in it on those people are naturally somewhat better than doing their task as compared to their parents generation. Okay on, then, this process is for the repeater. So those people also are basically sorted according to their running speeds or their fitness, as we would call it in this particular program on. Then we again have their top 60% which produces the next generation. So naturally as generations go on, you are slowly becoming better and better doing that particular task. Okay. And afterwards, we have to define a particular criteria when we'll actually stopped the process. Okay, so that is when you have actually reached the fitness which you want. Okay. So in our previous lecture off the introduction, we have actually seen the program running. Where, after the 50,000 to generation, we have actually got a pretty good text turn. After that. Are the 54th tower 54,000 generation. The fitness was actually zero. Okay, fitness in this case is the amount off others. Okay. So as the amount off editors are zero, we have basically reached ideal goal. Okay, so I guess that's enough often introduction again off genetically, I'll go to them and how we're going to use in this case. Okay, so now let's get started on to the actual building. 8. coding#2 new class: So now we basically have to start by declaring some constant inside our main method. Okay, so now click on the main MMA third line are not click enter so that you are actually writing everything after some space from the beginning off the line. Okay, if you are over here, just presser tab so that you and you have basically left some space or discord indentation on. And basically, everything which has some space entered from the start is inside the main matter. And that is why we don't have space over here, Okay, Because we don't want it to be inside the main matter. Okay, so now the first constant which we after declare is the population size. Okay, so now over here, let's type population population size, and we basically have to be certain good population size. So if you have ah, huge population in one generation, then it just consumes a lot of computational power on If you don't have a lot of population in one generation than the chances off you solving the problem, actually, in a particular number of generations is very small. Okay, so you have to basically heard the golden sport in between. So now basically, we have toe make the population size somewhere around 100 to 200. Okay, so in this case, let's make the population size 100. Okay. Okay. So now we have assigned a value 100 So the population size variable. Okay, So the problem which we have over here is dark when we write some court above the main mattered on If that particular court wants to refer the population size very build, and there is actually no way where it can do it. So instead, let's declare the population size above here. Okay, let's remove the spaces. And now inside the mean MMA third, let's again refer toa this particular population size variable. Okay, so to do that, what we have to do is use the global keyword. Okay, so this population size is a global variable, and we want to referred it inside our main matter. So the word is global. On inside global. We have population size. Okay, works well. So now what we have to basically do is we have toe again first basically create a new generation. Okay, Aren't do dark. We have to basically initialize a new area called population. Okay, So now population will not be used throughout the course. So you don't need to make it a global variable. Okay? And then we are basically going to run everything in a loop. So we want to have some step where you want to break out off the loop. Okay. And that step is a variable which will basically tell us if we have solved the problem or not. So over here, let's make a new variable cord sold. Okay? And currently this is forms. Okay, so now we have basically initialized three variables, and now we have to initialize one more variable called legend ation number. Okay. And currently we are in the first generation, so some things should be declared a global and something should be declared local. Okay, local is inside this particular mean matter. So anything which you right over here. If you create a nude, a definition over here, or a method over here, then that particular matter cannot refer to these variables. Okay, It has to a leader for this particular variable. So that is why you have to choose wisely which variable you want to make global, in which we want to make a local. Okay, So another question with comes toe someone's mind. Is that why don't we declare everything is global. Okay, so by doing dart, we actually reduce the amount of variables we have flowing free. So if you have a new definition over here and if you are getting want to assign population on after sometime, If this variable is referring to the population, then the change will be made throughout. Okay, so if someone tries to change population size, it will be the change for everyone. Okay, so that is what we don't want an all of the cases to happen. And that is why we create we leave some variables as local. Okay, so now we have toe basically clear the initial population, So the initial population will basically be completely random and they will put out completely random guesses. So now over here, we have to use a for loop. Okay, So that keyword thus far on and then we have underscored so this underscored is basically ah where to refer everything inside a population size. Okay, So in then you have the range word. So the injuries on method which basically takes in in Peter argument. Okay, so this form ah, loop will be executed these many times. OK, so now let's pass in our human. So that is population size. Okay, so this for Luke will basically be executor to population size, number of times, which is indirectly 100 number of times. Okay, so now let's Arda pulling over here. And now what we have to do is we have to basically create a new individual. Okay, So now, toe, create a new individual. We have to basically give the properties or the stencil off how an individual will actually be okay. So? So to do that, make some space above the mean mattered on over here. We have to write a new class. Okay, So the new classes individual okay, And this individual will basically be an object. Okay, So what we have done over here is that we have created a new class, and that is an object. So whenever you want, you can create new individuals as and when you want. So if you want to clear 200 new individuals, that can be done by just typing in one loop. Okay, so now in the class we have, Let's first right. Some comments, Okay? Always. You should enter comments on your court so that it is really able for you whenever you reference it later on. Okay. So commanders written by typing in the hash sign on after the Hosein largest type A glass. Which three presidents? No, our plane, often individual in our generation. Okay, so this is basically a class which will re president the outline off an individual in the new generation. So now let's proceed over here be any object which you want to create. You basically have toe for 10 and initialize method. Okay, so now to port in and initialize my third, which is that whenever you create a new individual. Okay, so any new individualist created by passing in those are humans. I guess when I look scared that matter. So over here you have their deaf keyword again on double underscore, he neared again. Double underscore. So dis turned for initialization turned over here. In any matter inside the class, you basically have the self argument. Okay, on with the self, you want to also make a new argument called the chromosome. Okay. On this particular argument will basically contain their data required toe affect the behaviour often your individual. Okay, so now let's are the colon on click Enter. So I noticed that you have again left some space after death. Okay? And that is particularly important because you want to write this court inside the innit method. Okay, so now over here, let's first used the chromosome Very well. So we have self dark chromosome is equal to chromosome. Okay, so now we have the individual object on inside the object we have. Ah, in it mattered on. We also have some variables inside of the class. Okay, so chromosome is one off those variables, and over here, self, not class. Self dot Chromosome basically means start create a new variable inside this particular individual. Okay, on the value of that variable called chromosomes will be the value, which we specify. Okay, so this particularly makes sense so that in it matter can be explained like this. If you want toe, basically, if you have creator or database, which contains all off your employees in your company, okay. And now, if you want to add a new employees, then you basically have to first entered his name, his age, and this salary. Okay, so those can be their arguments over here, so it can somewhat be, like name in an age on salary. Okay, so whenever you cleared the new class, you basically type new individual, And inside the bracket, you will give the name the age under salad E. Okay, these are some basics off object oriented programming in Python. So I recommend you do some research on there so that you just have some more clarity. But this is also fine as the best type of learning happens when you are actually building something. So Okay, now we have basically initialized our object turned over here. We have given the chromosome, they're able to the class. Okay, Now we have toe define, um, 1/3 which will actually contain the fitness. Okay, so we have a new matter over here, so self, not fitness. Okay, so currently, fitness is a variable, Okay? It can be a value from 01234 In this case, fitness is basically anything which is not referring. Our freeze. Okay, so self dot fitness. So this is again off variable called fitness. Just like chromosome on. This is This is the first to a new matter called Self Lord are now We have to basically create a new matter inside this class which will calculate the fitness by looking guard the chromosome. Okay, So, by looking at how the particular individual will behave, we have to create a method which will then calculate the fitness. Okay, so let's name it, Cal. Underscore, kelp calculate. Underscore fitness. Okay, so and now we have to basically call this method. So now let's first create this matter. Okay, so now over here again after taking and 30 will be on the same indentation level as the previous to command. So you have to go to their definition 11. Okay, As you want to create new definition. Okay, so over here, click top once air. Now you're under definition indentation level. Okay. Indentation is basically just the space left from the margin. Okay, so now over here, we have to clear the new matter called calculated fitness. Okay, so now this calculate fitness mattered basically takes in the same argument. So next time definition, calculate, witness on over here. We want to pass in some arguments on We have basically initialized every required stuff over here. So the only argument which is required itself. Okay, so now, uh, the next process is dark. We have toe basically are a new first. We have to add a cooling over here, and now we have to add a new comment. Okay, so now in the comment, we have to write, ah matter to calculate no thickness. Well, on individual based on the okay. So every individual will again have a chromosome, and based on that chromosome, we have to calculate its fitness. Okay, so now we have to basically first define our target. Freeze. Okay, so that we have something to compare it to calculate the thickness. Okay, so now over here, let's define our target. Freeze. Okay, so inside target, we basically have toe pretend the statement which you want to achieve. So over here, let's type in. I love machine learning. Okay, I'm now, um we have to basically are some more cord inside. The calculate fitness mattered where we will actually come to know how far we are from this particular freeze. Okay, so now Alexis, recap what we have done till now. So we have created a mean matar on inside the main mattered. We have. Ah. So basically, to refer to me in my third, we have created a material which can refer toe the main matter. Okay, So by comparing the name Melissa, if you are running mean dark fightin by the command line, you basically have entered this particular if statement and through that you have entered the mean mattered okay, aren't from the main MMA. Third, we have to implement some court over here as well, which will basically refer toa are new class which we have just created. So then we created a new class and inside the class we are Did any an initializing mattered earned inside the initializing matured were afford toe the calculate fitness matter which we then added the calculate fitness My third and we added some comments. Okay, so and the next particular session we will are some court inside this calculate fitness mattered where we will actually calculate the percentage error or the thickness or a numerical value based on how far the chromosome will actually behave Based on the target freeze 9. coding#3 calculate fitness: Hi and welcome back. So now let's complete their task, which were left in the last video, which was to complete the calculate fitness matter. Okay, so now we have basically created a new target string Over here. Spring is basically text. Okay, so I love machine Learning is our target string over here. So now we want to use it. So what? Here, let's go on a new line on over here. As you can recollect over here, as we have referenced the population size variable, we have used the global keyword. Okay, So similarly, we have to use the global keyword over here as well. So global gotta get Okay, so now over here, we have to first initialize the thickness score. Okay, so the variable is fitness, people do zero. Okay, So this is how we have first mayor, Fitness zero. Okay, Now we have to basically go on their task off, actually calculating the fitness and changing the value on this variable. Okay, so now you can recollect we have already used a fitness variable over here. Okay, so this is the same self dot fitness, which we are talking over here as well. Okay, So Now we have to basically start a four loop. Okay, So far. Now we are going toe. Use two new variables that IHS dub you ascend a beauty. Oh, let's say CS and city O K. C s Comma city. Okay, so now Okay, let's name the sea G N C P. Okay, so this is a character from generator, and this is character from fixed. Okay, so now we have to take this inside. Now we're going to use the command called zip. OK, so in zip, we have toe given to new arguments. The 1st 1 is self dot chromosome, as we have discussed over here. So we basically are going toe comparator chromosome with the target string over here. Okay, so we are going to enter self door chromosome and Mama target. Okay, So zip, basically in a for loop, you are goingto go through this loop again and again, OK? So whenever you have anything inside zip, the zip method actually changes both off the variables. According to the new ah, a new cycle of the loop. Ok, so in zip, when you are going through the second time, then you have the self dot chromosome but the target variable is also changed for the second generation. So but here, as you can see, we are not changing the variable. But it is always safe to include a zip inside a for loop. Okay, so now over here. Let's give a Colin Now click. Enter. So noticed that were again inside up for loop over here. Okay, so make sure other to have left some space over here. Okay, so now inside the for loop, we again have an if statement on inside of the if statement. Basically, we have to check if GS and GT are equal. Okay, so now G s c g and C p r basically the particular characters which we want toe compare. Okay, so if this is the particular okay, this is our target phrase. Okay, on if this is the phrase generator by our chromosome or our individual. Okay, so now we have to compare. So if the very first character that this a n p are not equal, then we have to increase the fitness by one. If the second characters are equal, then we don't have to do anything. If the third characters are not equal, then we have to again increase the fitness by one. Okay, so so on. We have to move until we have completed their target string. So that is why we have included target over here as well. Okay, so now, if not naturally, the next condition is C G is not equal dough. So you'd be okay then We basically have toe again. Um, and other cool. And over here on inside our colon. Now, it is our choice if we want to include the text on the next line or on the previous line. So that's included on the next line so that it just looks cleaner. So fitness and plus equal toe in space one. Okay, so now, plus equal toe actually adds this particular number toe the current fitness value. Okay, so fitness plus equal toe basically means zero plus one. Okay, so whatever is in the thickness, variable. Just are the one to it, OK? And it makes sense, as we are basically comparing words which are not equal. Okay, so now over here, go on. A new line are now basically are came over here is to give the fitness world back. Okay, so now let's go back to the four loop level to click backspace to time so that you are on this level are now over here. Let's give back fitness So the come on is return thickness. Okay, so now things are starting to make sense. OK, so over here we are inside the initialize command. And over here we have a reference to the fitness common. Okay, so by which we have a reference doc calculate fitness. My thought. So now inside the calculate fitness method, we basically execute all the scored and give back this fitness world. Variable. Okay, so all this basically gets this variable back. Okay, So after the loop, whatever the value of this variable is, we get it back. Okay? So pretty simple till now. Okay, So now in the next session, we'll basically cover some new methods. 10. coding#4 create gnome: Okay, so now we have to basically create our initial population. Okay, So, insider for loop over here in the main mattered Moved here the legs First art of comment above the four local. It's add a comment which say's the air initial population. Okay, are now inside the for loop. Now we have to basically cleared a new individual. Okay, I now are to create a new individual. Okay, so over here, if you look at it, you need a new chromosome. Okay, So now over here, we have to generate a new crew. Muslim. Okay, so we have two tasks over here. Generate a new chromosome on. The second task is to generated a new individual on I do. The population variability population ready. Okay. So over here we have initialized a population agree on. We basically oneto create a new individual by creating a new chromosome. And then I lingered into the population. Okay, so now, to create a new ah chromosome, we have toe add some more methods. Okay, so now over here, let's call the new chromosome as genome. Okay. And you know, Miss individual dark created, you know. Okay, So now this is a new method which we will shortly be writing inside the individual class. So that will basically create a new chromosome on. Now, by using the chromosome, we have toe basically create a new individual. Okay, so now, finally, if we give this, uh, give a very able a new name, which is new, the underscore individual. And now we have toe basically create a new instance off the individual class. So that is capital individual. And within the bracket do Pastor Gino. Okay, So, doctors, how you have created a new chromosome over here in this statement, and now you're creating a new individual based on that particular chromosome on by using this particular class. Okay, so now finally, we have toe added this individual inside our population, Ari. Okay, so now I days have a command, go reserve him 1/3 basically built in called upend. Okay. And that just adds the particular text inside the attic. Okay, so now the commander's population dart a friend on inside this, we passing the new individual. Okay, So, like this in the first reiteration or the first running cycle of this four loop, we have added a new individual inside this population, and this cycle will run population size many times. Okay, so the cycle will basically run 100 times on. You'll finally will have 100 individuals inside a population section. Okay, so now, pretty cool. Now, below the inert. My third, we have to clear out of the new created, you know, method. Okay, so now over here, make some space on and on the same definition level we have toe are a new method called create genome. Okay, I now create genome is, um, 1/3 which will be referenced from outside off the classes Will. Okay, so now go do that. We have toe are a class matter tag outside off it. Okay, so they're text is act classmethod. Oh, Karen. Inside the last My third section. You have a new definition on the name off. The definition is clear. Genome. Okay, so create underscore. Genome on the cleared underscore genome will again take the self flood. Okay, so now entered a cool. And after that And go on a new line and make sure you're on the correct indentation level. So now we're, like, sucking out of coming over here, which is create a new chromosome. Okay, So now over here, we have toe again. Use their target Variable. Okay, so now basically, we have toe create a new chromosome, which will be as long as their target. Okay, so now if you don't want to create US chromosomes, which are super longer, super short, because whatever we do after that, we won't get close to our target. Okay, so now we have to go somewhere. Use their target dot lent or the length off their target. Okay, so now we have to first take in the target variable. So now, as you can recollect, we have to use the global keyword again. So global target. Okay, now, on a new line, we have toe keep a look at its lent. Okay, so now we have toe make a new variable, uh, genome. Uh, underscore. Lend. So now this is basically the length off are required. Genome on this will be equal toe the lend off their target variable. Okay, so l e n gives back the length off the target. Variable. Okay, so pretty good until now. So now we have to basically give back new ah outcome are we have to give back on Eugene Um Okay, so now over here, let's first used the return variable return keyword. And after return, we basically have toe like first people return in square brackets. So now inside return. We have to now again give back a new chromosome. Okay? So to give back a new chromosome, let's make another method. Cord mutate. There begins. Okay, so this basically takes a lot off genomes are in, basically merges them and gives back on Eugene. Okay, so we are going toe career, the mutated genes. We're going to use the mutated genes matter from our particular class. It has not yet been creator. And we'll do work in a minute. On after that, you have to again use the for variable. And for underscore refers to for the range in at end on, we have Dean on Gotland. Okay, so now let me explain what is actually going on over here. Okay, so now we are referencing the mutated genes matter in this particular class for as many times as the genome land. Okay, so now we have to basically create a new genome for a gene. Okay. Genome is basically a series off jeans connected together. Okay, So now we have to create a new Jean every time. So we have to basically create a new gene over here, over here as well. So, like this. If the length is, let's say 16 then we have to create 16 Eugene's. Okay, so now over here, we can't just use the mutated gene ski words. So we have to write in self dot muted religions. Okay, Now we have to clear new mattered again on back my thirties mutated genes. Okay, so now, above our particular clear dino matter alerts again, create a new definition. Let's call this mutate. Er Dean's okay. Mutation is basically a process off. Combining multiple genes genes in tow today or, in this case, a genome. Okay, auto Crowism. So now, inside the mutated Gene Smith third lets out of Poland 1st 1st we have to make this a class mattered as we are not going toe reference it from within the class. So as this is a class mature, the method is not actually executor while the program is compiling. Okay, so now inside the last method, we have to now basically create a new gene. Okay, so now for the initial uh, purpose. Or for the initial few executions, we have to basically give back on random Dino. Okay, so for years, a completely random little Muslim. Okay, good. Almost human genome are the same things. Okay, so now over here, Lex again used the genes variable. Okay, so now what we have to do is we have to basically giving over here some possible outcomes. OK, Are some possible places where the with the gene can take? Okay, so now over here we are just including apart in lower case. Okay, so now let's cleared a new spring over here on Let's colored jeans. Okay. Inside the jeans section, let's are clean order commas. Okay? And now after this, we have to basically, given all the possible values which a gene can take. Okay, so doctors lower case all All forward, So a B c d e f g h i j k l m n o p r s t u v w x lies there. Okay, so make sure that you don't miss anything, because if you miss elected over here on if by mistake, you have their collector in your target spring, then your court will be running forever and forever because there will be no way off actually coming to that Dean or come into their targets section. Okay, so now we have lower case. All for birds. So now we have to insert, although uppercase so again a B c d e f g h i j k l m n o p You are s t u v w x wiser. Okay, so now we have all the electors completed Over here. So now let's are some numbers. Okay. So 123456789 on zero as well. Now let's artist space Because we also have space over here. So I think the spaces, like trying, bordered on after the space. Like started some special characters. So we have a coma. We have full stop. We have dash, we have underscore. Then we have cooler semi colon. Then we have a colon on. Then we have an exclamation mark. Then we have the axe symbol. The dollars involved a percent sign. Well, I guess that isn't after closing brackets. And Okay, so I guess this is enough. And after you have entered all the possible outcomes over here again enter the three single inverted commas. Okay, so now this is basically the possible outcomes for the genes candidate. Okay, so now you have basically these all these characters which can take place for which can be an individual, Dean. Okay, it may seem a bit confusing right now, but it is pretty easy once you get the hang off it. Okay, so now we have to basically create the mutated gene section. Okay, so first, let's ah, recap what we have done till now. So were first referred the mean matured by this if section inside the mean meth. Or we are using a for loop where we basically reference to the individual class. On inside the individual class, we have the inert matter which thereafter uses the calculated fitness method which is over here on that basically calculates the fitness based on are a deviation or our distance from the actual Stargate spring are generated. Spring. Okay, So after that, we have created a new, methodically clear genome because that is required over here in the for loop to initialize the very first empty Eddie on inside the clear to genome, we have a new mutate. Er Dean's reference. So we have created a new mutated gene section on. Yes. In the next session, we will basically clear. Ah, new mutated gene signed. Return it as well. Use the random of Tulin beytin. 11. coding#5 mutated genes: So in the previous session, we have created a new genes variable. And now we have to basically create a completely new chromosome using the possible outcomes . So are in the genes variable outside. Okay, so now first we have to take in the genes very bell. And to do that, we have to use the global keyword. Okay, So global. Then you have jeans over here. Okay, so now you have to basically create basically give back a single gene, which is any random stuff from this particular possible or this possible, uh, string. Okay, so now you have tow create a new variable called Gene On That is equal toe random off Dean's. Okay, so now first, let's use the random Tulin Beytin. So on the very top of the file are a new statement, which is an import statement. So now we have toe import, and, um okay, so now again, inside the mutated gene section. Now we can use the random tool. So Gina's the random, not Joyce. So now random basically has an inbuilt method called choice, which basically gives back anything from a string. So any random variable from a string, you can also do this Us get the particular Lecter. You can basically clear. Get the length off this on. Create a new random number from zero to that land on Get the particular Lecter, which is our doctor address. So that is just a lot off serious work. And instead, I prefer the choice matter, as it is basically the one stop solution for it. Now, over here, you need to enter the genes. Uh, variable. And now we have to basically return Ardian, which we have creator. So we're done. So now we have returned our Jeanne. Okay, so now looks pretty good. Till now, we have basically created a new genome. Very. We have used a mutated gene section, Um, 1/3. And inside a mutated, the genes may 3rd. We are basically using the random tool to generate any random character from this particular spring over here, where we given the possible characters. Okay, so yes. Now, with this, we basically complete the first step, which is the creation off our first random population. So over here, if you can see our main method, we have completed at least the for loop. Okay, so now after that, we basically move onto the next method in the next video or in an accession which will be so actually implement the men nuts and bolts off the whole algorithm, which is to pass on the fittest to the next generation and basically process their meeting and creating off a new offspring. And that is how will slowly improved a program. And finally, we can definitely check how the program works, okay? 12. running#1 running your code: Okay, so now let's first, we have basically returned a lot off course. That is around 63 lines off course 61 basically in if we remove the white spaces off course . So now basically, we have to check if everything works. Well, okay, because that is also crucial. Step when you are writing new programs to check if everything is running fine. Okay. So now we have basically created a new population. Random population at first. So now alerts doing thing. Now, let's try and bring our particular, uh, genome, which we have cleared her till now. Okay, so that will basically test the working off these two methods. Okay, Which is clear to genome and mutated genes. Okay, so now we have toe rain tarp, articular dino. Okay, So now in print, we have to use the world S T r, which makes whatever type of variable we have into a spring on inside the str tag. Let's give in the genome. Okay, so now basically, what this outcome is what is the expected outcome is dark. We basically warned toe cr different genome 100 number off times. Okay. On that should also be a random one. and should not contain any letters apart from this. Okay, Seems interesting. So now I have a terminal open over here in my current working directory. So if we a list over here, I have my main dart fight and file over here. So first, let's save this particular file. Now let's go into the terminal. Are now. Let's type fighting's base. I mean, not p y. Okay, so as we're typing mean dot p y doctors basically very we will refer toa this particular section. Okay, so now we have an error. Okay, So when you have errors, don't at all feel de motivated. In fact, that is a good way to refine our court. Okay, so now we have an error on to the main function, and I know what it is. So on the main function we forgot toe are the brackets. No gift for now. Let's save it again. Again. There. Terminal on. Now let's run it again. Okay, so now we have one more letter. Okay, So finally, after reading all of this, the last plane which says mutated genes takes no arguments. Okay, One given okay. On the problem which we have made over here is dark, mutated genes is not taking any argument. Okay, Every other method is taking the self are human, Does it is inside the class solar It's are the self argument over here. And now let's save it again. Okay, so now open the terminal are now Let's first clear Everything up are now again. Run the fight. Okay, so now, as you can see over here, we basically are seeing. Okay, this is one object. Okay, so this is our generator chromosome. That is 94 Q j Q eight R h r j f l So this is basically our generator chromosome I Now, let's again check if we have successfully created a new individual based on this chromosome . Okay, so now let's remove our print statement over here from the main matter. Okay, I now go outside of the four loop, so go on to the indentation level of the F over here and now over here. Let's are the new print statement on this will basically S t r. And inside str we have toe portend our population. Okay, so now with this, we can successfully see if we have new individuals or not. So we basically are expecting to see an individual object output. Okay, so I wanted to save this again. I know. Over here, under terminal again. Enter me, not B y. Okay, so now we are successfully seeing individual objects. Are this particular location Okay, so this doesn't matter. So the location is actually the memory location on Yes, we have successfully created new individuals. Are if you have time, you can certainly count each one of these. But I suppose there these are 100 off them. Okay, so now our court is, first of all, successfully running. So give yourself a good pack on your back as you have successfully created a good working, object oriented court on if you have any new editor. So if you have faced any new editors, write it down into the question and answer section on, I'll be more than happy to answer it. We can even basically used the 100 of students which will sign upon this court. And you can also ask them for help on those students can also answer your doubts if anyone of them are seeing the same errors. Okay, so I guess Yes, that's it for this session. I Now let's proceed on to part do off gets the free section where we'll actually cleared the men return brains off our court which will actually be the passing off the free test on the emulation off the popular or the individuals who are not very fair time than meeting and then creation off new offsprings. 13. coding#6 adding the ai: high on Let's get started Back now in the previous section, we have actually created all the basics required toe make our core functional where we actually created the individual object on, ordered some variables and then created the main mature on Finally ordered. And you have statement to make our filed run from the command line. Okay, so now it is time for the second part in the guest. The phrase section, our game or activity, As you would like to call it very well, actually had in the AI part. Okay, So this is where we actually designed the brain so far. Particular algorithm on make it capable of solving the required problem. Okay, so now we have to basically start with the process off. First, we have initialized a population, and now we have to start with the process off, actually making the genetic algorithm work. And to do that, we have toe do some things like pass on the fittest from this generation to the next generation and then use their stuff the population for mating and producing offspring, czar. And then we all Tonito ardent some random jeans, sometimes in order to also create their diverse city. And this can happen like thousands off time. So in the example which I have showed you so new, we actually have done this around 54,000 times. So this is the for loop where we'll create our initial population. So now we have to basically create another loop which can run those 54,000 times. Okay, so now we have initialized a variable over here called Sold. Okay, on we have toe run this loop. Until we have reached the solution on when we have reached the solution, we will somewhere change the value off salt. True. Okay. And we have to run this particular loop till the sold value is proof. Okay, so now for that, we have to are the while loop so vile. Not sorry. Okay, So this basically in short means that till the value off soldiers falls on this loop. Okay, so now we have toe first off, all sort the population according to their fitness. Okay, so now what this actually means is that we have no, We have initialized our basic initial population. And now we have to first, it has basically solved the problem. Once basically tried solving the problem once by generating a random string. Okay, so now we have to return mine. Who off them are the 50 s turn who are not earned. The free test will actually survive. Okay, so now lets for start a comment which says sort of population according to their fitness. Okay, are now to do this, we have toe again. Use the same population. Variable. Okay, so now again, population is equal to sort of. There police are date is ah, commander with just, uh, basically sorts everything. And so falls. They're older according to what we want. Okay, So sorted. I know what here, the entire population, Because that is what we want to sort. Now we have to enter the criteria in which we want to actually solve this. So over here there is another argument called key Equal dough. So now inside a key, we have to give the fitness command. Okay, so we have to basically sort the population according to their fitness over here. In order to use fitness, we have to create a one line function. Okay, because fitness is not just a really better dis population dot Fitness on over here every time you Salter, it takes one by one on the objects in the population a day, and then you can name it in the variable X. So then you have to basically use extort fitness. Okay? And to do all of this, we have to put a one line function on its core. Lamela, I know we have X because that is what you want to name the object which takes from population to salt every time I now ex, will actually be using extort fitness. Okay, so now we have basically the population, which is against Salter by referring towards earlier values according to their fitness. Okay, so next up, we have to first off all data mine if somewhere the individual has ah, zero thickness. Okay. So before proceeding to the further steps, which is actually the process off adding the brains to the AI, we have to determine if this is not the last generation where an individual has reached zero thickness. Okay, so thickness of zero basically means start. It has properly solved the problem and exactly come up with this particular target phrase. Okay, so when it has come up with this are our school zero our fitness school as we have defined it over here because every character which is not equally increase the fitness score by one . So Venator Zero. It means that all the characters are equal. Okay, so now if we have the score at zero, we have to get out off the loop for Karen to do that. We have already sorted the population of the area. So now we have to just start an if statement If population are now with the square brackets , we are actually referring toe the over her if you have I. So you actually mean the ICT position? Object in population. Okay, So zero means the very first object, then four means the fifth object now, like on it starts from zero. Okay, So if the very first object in population and now we have to get this fitness, and now it can be either less than or equal to zero. Okay, the list Dan, perimeter is not needed over here. But you know what? No. Where things can go wrong, so I drink less than perimeter can also be helpful. Okay, so now over here, if we are used, the less than perimeter less than or equal to. And now, after the condition and terror colon ongoing a new line. So just keep in mind that you have some space from the if so that you are on the proper indentation 11. Okay, so now over here, we have to go for stuff. All change the sold variable. Okay, so when I was sorry is equal to true. Okay on then. We also have to get out off the loop by I think the very common. Okay, So now over here, we check for the fitness off the first person or the foster individual and difficult zero are less than zero. We change the value of salt. True on. After doing that, we make sure that the Via Loop is not against starter. Okay? And then we break out off the loop. OK, so let's add a comment over here disobedient out of the loop. Then we have solved the problem. Okay, so now let's go on a new line on now. Alerts come out off the if statement on Go on the indentation level off the eye. Over here. Now we have to basically create a new generation, okay? And that is very. We will store all the offsprings which are created by the meeting process off the existing population. Okay, so now this will be the new population. So the next generation is about to be creator. So new underscore generation on. Let's make this an MP idea first. Okay, so now what we have to do is we are going to introduce three were things. Okay, The 1st 1 is elitism. Where we actually pass on the fit test off these this generation directly on to the next generation. Okay, on the 2nd 1 is the process off meeting, and the 3rd 1 is the actual creation off the new generation. Okay, so now what we have to do is we have to perform elitism. Okay, so I wrote a comment over here, which is for a phone nudism. Okay, so now what we have to do is, let's say if we want to transfer the top 10% off this particular generation directly on to the next generation, then that is done in this process. OK, so now what we have to do is we have to add a new variable. Likes Collard is I know acidic will do in teacher off. Basically. Now we have to calculate the 10% off the population size. Okay, so now over here, let's make another variable on. Let's call it elitism. Factor again. Currently. Let's make this 10. I know what here alerts our commanders our comment as well. Which is the person to value off elitism, Toby, for reform. Okay, so now essentially convert any particular So basically, to find 10% off the population size over here right now, you can directly tell that it will be 10 Bart in order to do that over here in our court. So over here, inside the in teacher, what we have to do is multiply first ofall population size and elitism factors over here, as we are using, um, the variable elitism factor. So we need to first take it in over here. So inside of them mean method, like start a new global variable looks for this elitism factor. So now over here, what we have to do is we have to our elitism factor. Then multiply it, then multiply it by a single population size. Okay? And now we have two divided by hundreds. So that bigger the actual value off. How many times do we have to basically transfer all of the free test off this generation? Okay, so now let's are more tactics around this. I know what Here. Let's divided by 100. Okay, so the reason we are using in over here instruments for Impey, Jer. And when we are in command over here, make sure that we get the whole number output, because that is what we want. So we won't turn out. Come off. 456 We don't want the 6.34 output. Okay, because we can't dance. Flooded the 6.342 member into the next generation. We can transferred either six or three current plans for seven. So in Peter basically ignores the fractional part. Okay, so right now we're getting go in, Peter, because we have some good values. But over here, if I make this 100 travels, then we may not get any easier. Okay? So that is just why, for some safety purposes earned toe Norton later program crash. We have entertaining Peter over here. Okay, so now we know. How many times do we have toe move? The current population toe the new one. So now we have toe basically at all of these to the new generation. So now we have new generation dark extend on inside X turned. We put in population because that is our current population. Okay, let me clear this again. Population is our current population, which we have generated over here, which is the initial population. Okay. And then the new generation are Let's call this new population. Okay, So new population basically is the second generation which we are creating. Okay, so now a new population dart ex turned. Ah. Now we have a reference to our population at hearing over here inside square records just turned her colon s. Okay, This is a short skirt and fightin, which basically lets us do the same command. Multiple amount off time. Okay, so now Colon s basically tells the fighting that you have to take s number off objects from this particular population at air and transferred to the new population at a Okay, so now we have basically completed the process off elitism. Okay, so now I guess that search for this one likes again Recap what we have done till now. because it can be somewhat confusing. So we have added a new via loop with the condition off sold. Then we have added a process off sorting the population according to the fitness. Then we have added a new ah, some new court where we actually break out of the loop and make sure that the loop is not executor again by when we have actually sold a problem or when the thickness off a population is zero. Then we have created a new population over here, which will be the next generation on. Then we have performed nudism where we have basically created and in PR which gives us the amount off people, individuals which we want to transfer onto the new population. And then we have done that move out here, okay? 14. coding#7 implementing the loop: in this session. Now we have to concentrate on actually creating the mating process on the process of creating a new offspring on adding it to the generation. Okay, so now I suggest you look at this video very carefully because this is the point where things can get a little bit complicated. Okay, so now there's first start. A newcomer. New comment over here on the comment is but a form the process. Automating. Okay, so now we have toe again. Use some part off the population where we will basically clear the new offsprings by the process off meeting. Okay, so now, naturally, as we want Oh, use all of the fill all of the rest of the generation on to also make it. Basically, we have to talk feeling the new population over here. As you know, right now, every population should have the size off the population size variable. Okay, so now over here in our new population command or the variable, we have filled up 10% off the population by using elitism factor. Okay, so another rest 90% is actually remaining. Okay. That is what we have to create by producing the offspring's Okay, so now we have toe get 90% over here. Okay? So now we have to basically subtract 100 from elitism factor from hundreds. So that s l will get the remaining percentage. Okay, so now over here, let's make a new variable card meeting. Underscore. Factor on this will be 100 minus elitism factor. Okay, so now we have toe again. Create a new value. Our and indeed er value by calculating the meeting factor percentof the population size. So basically, we have to get back their 10% of the population size. Okay, so now let's copy this. Come on. It's basted. Here are now instead off elitism factor Over here, we have meeting factor. Okay, so it makes sense. So now we have toe again. Run the process off, creating new offsprings the s many times. Okay, so over here, this is 90 because our population sizes 100. Okay, so now to do that, we have to add a for loop. Okay. So far. Underscores times for everything in at the end, off its okay. And now, after this leg side of colon, go on the new line on and make sure that you're on the new Internation level, as you have left some space after the four f inundation level off, four are now over here. We have to basically choose to parents. Okay, so that is faring under school one. I'm bedding under 42 On these will be the parents whose Gene, Deanna or chromosome. We will use toe, make a crossover and then create offspring. Okay, so now this is equal to We have to use the random keyword over here again. So that is random, not a choice. And now again, we have toe use only the 50% off the fittest population. Okay, so now we have the total 100% population off the previous inundation on. We have toe only use the top 50% as parents and who will meat and produce new offsprings. Okay, so now let's again create a new variable over here where we can call a meeting person. Okay? And let's make this 54 now. Okay, So over here again, we love toe. Use the meeting percent variable by the global world. So global meeting percent on now. Over here inside our parent, one core, it is random. Not choice on inside the brackets. We have to enter population on, then. Square brackets on. We have colon. Finally have meeting percent over here. Okay, So what we have done, actually, is that we have taken the 50 entries off the meeting person to value on. We have actually used it. Toe, get into the population. Variable on. We have randomize the choice so you can take any one off the person from the population on call it apparent One. Okay, so now alerts at the same thing over here. Okay, but now we have a small problem over here. Okay? Meeting person currently is 50 over here on that. This perfectly fine when we have a population size off 100. Okay. And what this command actually does is that it uses their top 50 and trees in the area. Okay, so now if for some reason we change the population size to, let's say 40 on and if the random generator take creates a random number off 47 then we are actually looking for the 47th element in an area which only has 40 elements. Okay, so are you getting the problem over here? So now, in order to do that we have toe again. Guardian of person sign. Okay, so now over here, we have toe take the actual population. Okay, so let's call this really underscore. Population are real size on. This is again equal toe in Peter Value off. Now we again take meeting percent. We multiply it by population size on its hard this into a round. Brackets are now looked delighted by 100. Okay, are now over here in start off meeting. Purse earned. Lakeside real size. Okay, so now it will work. Well, if we have 40 over here as the population size, it will actually calculate 50% off 40 which will actually be 20. And then it will refer to the top 20 individuals in the population. Ari. Okay, so now we have initialized to new parents, So legs are some comments over here. I notice how we are also using variables for every small thing. Okay, so we can also hard core the value 50 over here. Okay? So insert off. Actually doing all of this, we can just change the rial size to 50 because we know that we are going toe use their top 50%. But when you are actually running the court. And if you feel like tweaking these values, so if you feel like introducing some more off elitism so you can change it to 20 on, then we again have everything changing down. If you change the meeting percent for some other value, if you use don't lit up 20% and then you have some different outcome. And that is where you deter mined the best possible outcome where you use the least amount of generations. Okay, because that is the ultimate goal to spend less and less computational power to actually solve the problem. Okay. And that is why we are keeping all their dials upstairs. Okay, Now let's get back to our fairing too. So now go on a new line. And now we have to create a new child. Okay, so child is now we want to actually introduce another method inside the individual class. Okay, so if you go up over here, we have the individual class on inside the individual class. Now we want to our another variable called meeting. Okay, So now child equal toe bearing 21 dark made on over here. We pretend our human pairing too. Okay, so we are using Parent once met her off meeting, and that's when we put in the human pairing too. Okay, so now we go up here inside our individual class on now, inside the individual class. Let's are anouma Third God made. Okay. And we have to do this below the career Dino method. Okay, So make sure you're on the same indentation level us the d off death on now. Let's are the new definition called me aren't over here will be taken to arguments which itself It should be the required argument over here. As we heard, mr this argument last time we are encountered an error so self are now over here. If you look, we have entered a parent over here. Okay, so now that is the second variable. So we're here. We have self on parent under school tour. Let's call this second bedding. Okay. Next are cooling over here. Now, let's go on the new line. They're well for started coming over here on right. Okay, so now we have created a new offspring by using two parents. Okay, so now they're to go on the new line Now. First we have to initialize the chromosome off the child. Okay, so now that's first initialize a new variable called child Underscore chromosome. Ok, Lexx city collect that chromosome is actually the genetically material inside an individual which actually tells us about the behaviour are how that individual will behave. Okay, so now the logical part will be toe use parent one on bearing two inside a child chromosome . Okay, so I guess that's it for this one. Let's recap everything What we have done till now. So we have added a new four loop where we have ah where we are going toe create authority Make or continue the process off meeting that we have initialized to parents Learn those two patterns will then be using the population are toe do the process off meeting those parents will be the only top mating person are the top 50% and then we'll do all the meeting process and we have to do this for in the range s So we have to basically do this 90% of the time because the rest of the time it is filled by the elitism factor. Okay, So pretty much makes sense. If you have just ah, confused between meeting factor and meeting person day. Let me clear it right now. So see, meeting percent is actually the percentage off the initial population, which you will use us parents. Okay, so in this case, it is 50. So we are going to use their top 50% off the population as parents, Okay? And then we have to fill in the rest. Nine people sent off for a new generation by using these parents, so the 90% value is actually the meeting factor. 15. coding#8 Mating process: Okay, so now let's complete the mere to method. Okay, so now we have basically initialized on you. Empty area for the child. Chromosome on. Now, what we have to effectively do is that take some genes from either off the parent and then marched them together to actually make a chromosome. Okay, a chromosome is actually a certain off jeans. Okay, so now what we have to do is we have to first define a for loop. Okay. So far, we have We basically want to take two genes. That is Jean off, parent one. And enough parent, too. So let's call it G P one. Omar gp two. Okay. And then we have seen followed by the zip ward. Then we have self dark chromosome on. We have second bedding, not journalism. Okay, so now we basically want to cycle through these two values. Okay, So in the first situation, we love the first value off the self chromosome that is the first parents chromosome earned . This first value of the second parents chromosome. Okay, lets art of pulling over here. Let's go on a new line again. Keep in mind the indentation levels. You need to be one, our top further from four. Okay, so now we have to first make up a random number. Okay, so let's collard Prabha problem or probability on this will be random thought learned them . Okay, so this command is basically usedto generate a random number. Okay, so now the next. In the next point, we have to basically consider three possibilities over here. OK? We can either take a gene from the parent one. We can either take a gene from parent to, or in some cases, we actually have toe in salt a random gene. Okay, so that is when both off the parents are actually seem so then not one pointer doesn't make a difference if you have taken the gene from parent one or parent. So you have to in some cases, insert a random gene so that you have some variation in the population under population is just not stagnant. And slowly you at least have someone who can solve the problem. Okay, so now we have toe right a lot off. If cases Okay, so if first of all probably be is less done 0.45 So this random dot random word basically gives us a number between zero and one. Okay, Any random number. So now if probability is less than 0.45 Now, let's give a Poland again again. Keep in mind the indentation level over here. So now we have toe Big giant underscore chromosome. So this is basically leader Ari off child chromosome. So now, child, underscore Chromosome dart Offend. On over here, we have to put 10. Let's say Jean off. Parent one. Okay, so what we are doing right now is if the probability is less than 0.45 or if the random generated number is less than 0.45 then take the gene from the parent one. Okay, I'm now. Similarly go back to the I indentation. Level off the if statement on. Now, let's write in. I live over here, which it is the short form for else if in Brighton. Okay, so now else, if probably team is less done. 0.90 So ready 0.90 on then, like sort of colon over here. So now, child are underscore chromosome daughter pain. And now we have to take the chromosome from the second parents That is gp two. Okay, so now notice start. If anything is less than 0.9, it is also basically if anything is less than 0.45 it is also less than 0.9. Okay, so now if you look at this, there can be a nesting off conditions. OK, but what we are doing is that we have insulted a lift over here. Okay, So this else if so, if the condition is not this on the condition is this, then you basically executing this command. Okay, so this in short means if the value of probability is greater than prob if the value of problems greater than 0.45 or less than 0.9, then we execute this common. Okay, so now after this, finally, we have a simple else common. Okay, lets start of Poland after that are now we have to basically john the data. Random chromosome. So child, underscore chromosome dart. A pained appendix basically are in short. Now, you actually have a method over here, which is mutated the genes. Okay, so now they're just start himself dark. Mutated the genes. Okay, so now let me explain what we have done over here. So we are referencing toe to genes. That is the gene from parent one under gene from parent to, and we're cycling through them. So now noticed that all of them will have the same land because we have defined the land somewhere over here. Okay, we have to find the leant over here. On that is the Lenti call to our targets doing okay. So then we have the same length strings and then recycled them by using the zip method on then V generate a random number between zero and one on. If the number is less than 0.45 we take the gene from Parenteau one. And if the number is more than 0.45 but less than 0.9, then we take the gene from parent to I. If the number is not sitting in both of these cases, that is, if the number is greater than 0.90 now in less than one, that is when we actually make or random gene. So we going to self dot mutated genes on over here Inside mutated the genes. We actually and a Florida cleared genome function and that basically uses random gene. Okay, so we get a random, You know what? Here. Okay, then I guess that's it for this one. Now, in the next video. Well, actually continue with our court And I would highly recommend that you spend some time on this particular recession and just try to understand what is happening here. The court very carefully. Like this is how I referred to the court. So I start with the import statement I look for the men matter somewhere over here. Ok, I look for this command. So now if I have started the file, I go into the main matter. So this is like the entry point off my app. So then there's going toe, the main mattered. And now I look at these variables. I see that they are declared upstairs over here. Then I look at the four loop. Then I look at all the methods. So first his individual are clear genome. Okay, so this is the individual class, and over here we have a clear genome function. Okay, so that is when I read this function. Then I go back to them in matter on now. we create a new individual. So let's look at the initializing matter. So in it, when we go back to the for loop on, then we have population not upends. Over here we inserted our first individual and this happens population size number of times . So essentially we have a population at first. Then we enter the while loop. That is where we start the population. And after sorting the population, we actually look for three factor. The 1st 1 is elitism and the 2nd 1 is mating. Okay. And the 3rd 1 is creating the generation. Okay, so then we perform elitism. So then we have declared some variables over here. Toe, see, how much do we have? Toe? Proceed further in elitism and then we have the meeting process. So what? Here we have the reference to the mating mattered. So now alert to go to the me to my third over here are now within the mere matured. Now we're generating a random number and that is actually the the main deciding factor in which, with Jean do we are to our chromosome. Okay, 16. coding#9 Finishing touches: So in the previous session, we actually created the meat. Mattered are now in this session. We have toe made the court working. Basically. Okay, So now, um, you should have everything completar up till the else keyword over here and now after the else keyword, we have to just return the value. Okay, so now go on the same level as the f off for because that is the indentation level inside a mate matter. Okay, so over here. Now, let's record. Now we have toe again. Create a new individual, Okay? Based on R chromosome. Okay, so now let's get a new individual by typing individual earning two brackets. We have child chromosome. Okay, so now we have basically successfully completed the meat matter gets. Now, let's go back to our my loop. Okay, So over here now we have created a new child. Okay? Now, what we have to do is we have to basically are the child into the new power generation of the new population. Okay, so now the next commanders knew population new population, dark trained. Now we are in the child over here. Okay, So now it seems to work pretty well on Now we have basically completed the generation off creation off completely new generations. So now we have to replace the value off the new population and the existing population. Okay, so now the new population will actually take place off the existing population, and the court will run again on. Then we'll have a completely new population again. Okay, so over here, now, below the four loop, make sure that you're on the indentation level off the loop, so you should be on the line off F. So now, over here. Let's type in population, which is our variable over here. So population. And this should be cool too. The new population. Okay, so now we have assigned a population toe the new generation. Okay, so now everything seems to be falling in place. Just one thing is left, and that is but ending the output. Okay, so now go on the new line. Over here. And now let's ardent a new print. Common. Okay. So, putting. And now within brackets, we have to basically put in a long string over here. So now let's starting to our double and ordered commas. So what? Here first, we have the generation number. So we have the generation over here, and then we have plus for Lord by Let's look for a generation variable. Okay, so this is the generation over here. Okay, so now we have to port in generation over here. So tell us, don't forget to r and S t r before adding any variable onto the upper incline because that converts orto springer and then you can print it easily s t r. And over here we have the generation variable. Okay, Then again, we have Plus, Now we have toe basically show in the output which the free test are the first person in the generation has actually created. Okay, so then we have the creator spring or let's caller just the spring or the output or the guest. Let's call it the guests. So inside double in order. Commas. We have guests than colon and then we again have a plus, followed by str earned inside the brackets. Now we have we have to refer to the population variable so population and within population . Let's look at the very first person or the individual on we have toe. Look at this chromosome. Okay? Chromosome is basically the output which he have he has created. Okay, so now we have spring converting everything to spring on. We're referencing to the very first member in the population at a on we are taking up his order chromosome or its chromosome. Okay, so now after this, let's again out of plus again in double in water commas. I've been fitness over here because that is something that you want to see as well. So fitness again plus sp R I over here. Let's type in fitness. Okay, so we have Ah, fitness variable declared over here inside the calculate fitness matter. Okay, so, yes. Now we have the fitness mattered. Fitness variable referenced over here as well. So this is our output for now on. Yes, there are pretty much completes our core, but we just have one crucial step toe perform on that is to increase the generation number . Okay, Every time we are completing the while loop, we have to increase a generation number. Otherwise, we'll always be juggling into their to same generations. Okay, so now we have generation on, then we have the less equal to again. And this is one. Okay, So plus equal to basically adds this value to the existing value of generation. Okay, so now yes, I guess that's it from the courting point off view in the next session. Let's go on and run the score and let's see if we have come across any errors. 17. Running the project: Okay, So welcome. Back in the previous session's O. R. In the previous maybe five or six lectures, we have spent a lot of time creating the guest, the phrase game or up. Now. Yes, it is. Timeto finally run out app and check if we have any other. So, first off, all as you are using sublime text press command s on Mac or control s on windows, too, for savior work. And then open a terminal with your current working location on. Yes. Now let's ah, run the script. So we might encounter a lot off errors over here. But that's what we are over here for so on. That's what you have paid for. So let's go step by step and solve it. One of them. Okay, So the commanders fighting on we have mean dart B y. That will basically run this file. Okay, so now forced off all contracts, we have our very first error on the other. Sees that it is a name error and it means global name fitness is not defined. Okay, so let's look at our file back. So Okay, this terrorists President online number 115 Okay, so 115 is basically our opinion statement on over here. Okay, Is referring to the fitness video, but learned Yes, I know what problem is happening over here. Okay? We are not referencing to any population. Okay, so now over here instead, off thickness, you have toe first off all reference to the population at a on in the first segment or the first individual under dart fitness. Okay, because fitness is actually a class variable or a variable insider individual class. Okay, so in order to see of this again Now, let's go to our terminal again. Okay, so we have a pretty messed about put. Okay, So if these room out over here, let's first off or look for a sample output. Okay, so now over here. As you can see, it is giving out the generation numbers. Right. Okay, so the generation is 44 4 and now, actually, the guests is something fishy. Over here, we have a bound matter. Tight mutated genes off class on this whole output. We actually have our text over here, but we want to convert toe. Basically, we want to convert it to show us word out a sentence. Okay, We don't want this. My third output. Okay, so now this a This looks like a serious problem. So let's look. Art are mutated. Genes matter. Forced. Awful. Okay, that is what it is telling us to look from here. Okay, So alerts goto our class section over here are so classy. Individual over here. Let's look at the mutated. The genes matter. Okay, So mutated genes Matar mutated. The genes may 3rd. Looks fine. Okay, we're referencing a jeans variable over here. Then we are creating a random gene, and we have just giving your back. Okay, so I don't think there is a problem over here, So let's look at the places where we are actually referring to mutated the genes. Okay, so now the short Curtis command fr if you are on windows or this control, if on legs just type mutated, the dean's over here. Okay, So mutated is also fine, So Okay, this is our very first used it. This is basically to find a particular world in the file. Okay, so this is the first occurrence on this is absolutely fine. Then over here, we have used the mutated genes turned clear genome 1/3 also looks good. Okay, then we have a mutated the genes option over here as well. Okay, but this is where things have started to getting get a bit problematic because, as I can see, over here, we are referent reference, referring toa a method inside a class on this is actually the same packs off variable. Okay, So the thing which we are missing over here are these two round brackets, okay, Now fight and actually knows that mutated genes is a matter. Okay, so in our to save it again. So press control cierto terminate what? You're running. Type in clear. And now the a paddle on day. Okay, so now over here, Basically, we can see that we have come to a pretty good guests. Okay, so right now we are basically displaying a list on the last level of fitness is actually one. Okay, so let's see why the fitness is actually one over here. Okay, so let's go back to our core on. Let's look into the loop. Okay, I'm now over here. Let's look at the condition where we break out off the loop. OK, so this is the population at a on we are referencing toe the zero to variable onda zero variable. Then we're comparing its thickness. Ah, now we are looking. If the thickness is less than or equal to zero on, then we actually make the sold flag through. Okay, so that is when we are actually breaking out off the loop. So I don't see any problem over here right now. I think so that the problem is over here. Okay. As over here we are saying that the population is actually equal to the new population. But the new population was generated in ah, half Ezzard manner. So instead, what I think is that we should actually sort the population okay, are in the command to sort of population is the same. Okay, so now I don't want to waste any more time over here. As you can see, we have this well and running. It is I l o V e machine learning on over here. You just have a B which should be replaced by a space. And that is perfectly fine. Okay, we are seeing other fitness one over here because over here we are just referencing toe the zeroth popular D wrote individual in the population. Okay, The best individual in the population can be on any index. Okay, It can even be the 52nd. So if we just sort the population up over here on, I guess Yes, that word again. And other projects. So first off, I'll give a huge part on your bag because you have successfully created an application where you are actually using an the iron using genetic allow algorithm toe, actually create new individuals by the process off meeting on you have also implemented elitism factor or elitism eyes the phenomena Very trance for some individuals. Who are the clear winners off one generation to the next one on? Yes. We have now successfully achieved our task off actually getting their target string as we want. Okay, so I guess that's it for this session. Now, in the next session, we'll come up with another such application or another such usage off genetic algorithm in machine learning. And, yes, stay tuned for it. If you have any doubts or any different errors other than what I have specified over here, you can definitely write it down in the question and answer section in this you Demi course , I'll be more than happy to answer. Turn even more than neither many students who are eagle toe learn. So your question might just help someone else and also search if that same question is already being asked. Okay, so now stay tuned for the next chapter or the next session, where we'll create a new application using genetic algorithm.