Coding Challenge #29: Smart Rockets in p5.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another coding challenge in this coding challenge I am going to program from scratch something called smart Rockets okay what is this smart Rockets thing well this smart rockets project which I have running behind me you can find a link to it in this video's description is something created by Jer Thorp some number of years ago it was developed in flash which is a environment for programming ActionScript I'm going to do this in JavaScript using the p5.js framework the reason why I love this project is it's a great example of genetic algorithms in the wild at play with an animation physics pace system so what you'll see here is that there are these Rockets being launched from the bottom and they're trying to reach that target up there and they have built into them this DNA this DNA that this virtual DNA that tells them how to move about the screen so let me briefly talk to you about how that works so the Rockets actually have a bunch of like kind of thrusters on them and this is my like pathetic terrible drawing of that and though all of those point in a certain direction so the number of thrusters in the direction that they point in is part of their DNA and then the sequence by which they fire first second third fourth is also part of their DNA so you can imagine like if you had a control panel with five buttons on it you could press those five buttons in some sort of sequence to try to get push it in some efficient way to reach that target so what I need what I need is a couple things I need to ultimately have a rocket object and that rocket object is going to have to have physics built into it I'm also going to need to have some DNA object which is DNA to control the physics and then the DNA object is going to need to have functions like mutation and crossover these are pieces of functionality that exist as part of a genetic algorithm hey if you want to know about genetic algorithms I have a whole set of video tutorials link to the description of this video that go through the algorithm in detail I'm gonna do that again in this video but I'll be programming kind of quickly so if you want the sort of longer explanation of that go back and watch those videos and then the third thing here is I'm going to need a population object and the population object is going to manage an array of these rocket objects so I'm going to make some Rockets let them go have them evolve let them go again have them evolve let them go again and eventually they should make it from the bottom to the top maybe I could put some obstacles in their way that sort of thing let's check on how these rockets are doing you can see a kind of path that there by the way the textsmart rockets here is an obstacle they're trying to figure out how to get around it so I'm going to leave I'm going to leave this browser window open hopefully I won't forget to close it and at the end of the video we'll come back and check on how those smart Rockets are joining now I'm also going to do something a little bit crazy it's not that crazy I'm going to program this all using code pen I have already added the p5 just JavaScript files you can check my video of how to do that in a different video I've already added those and and I just started with basically nothing I have create canvas and background this is the only code I have create a canvas and draw a black background so the first thing I want to do is create this rocket object so I'm going to I'm going to do that with a constructor function and I want my rocket to have a position and I'm going to say create vector I wanted to have a velocity let's say create vector and I want it to have an acceleration and I'm going to say create vector then I'm not so used to using code pen so we'll see how this goes for me I then also want to give it a function and apply force function which where this rocket can receive a force and that force would get added to its acceleration that is Newton's law at play and then I also want to what else do I want to do I want to have an update open that should say this dot apply force equals function I also want to have it have an update function this not update equals function and I want that function I want to do the sort of standard physics algorithm that I do in one of my examples where I have this dot velocity add this dot acceleration and then I want to this dot position add this velocity and then I also want to clear the acceleration at the end by multiplying it by zero because so this is like I just built a physics engine in like 10 seconds basically I have an object that has a position a velocity the acceleration acceleration gets controlled by forces in the environment or forces by its rocket thruster so to speak and the update function is where the physics actually happen okay so I also want to have a function called this dot show I'm gonna I'm just going to be my rocket won't be as pretty and beautiful as jairs smart rocket I'm just going to say I'm just going to make a rectangle at this position X this stop position dot Y and the rectangle will be thin but tall and I'm also actually what I'm going to do is I'm going to say translate I want to rotate it in the direction that it's moving I think that'll be a useful thing to have so I'm actually going to translate to its position and then draw the rectangle at zero zero and I'm also going to say rect mode Center and I am also going to say then rotate by this velocity dot heading so the heading function in in the p5 vector object gives you an angle of the direction that a vector is pointing so I can rotate by the angle the velocity being the direction that it's moving I also want to add around this push and pop and there if I add push and pop around this function then all this rotating and translating won't affect other things so let's let's go now and see if I can create just a single rocket on the screen I'm going to say a rocket equals new rocket and then I'm going to say a rocket dot update and rocket show oh look there it is the rocket in the top left so one thing I want to do is I want my rockets to start at the bottom of the window so we can see there it started there and then let's just say for the sake of argument I give it a velocity pointing up is it moving whoo whoo okay so one thing you'll notice I drew it vertically and the angle of getting is probably ninety degrees so it rotated it so I think I'll just change the way I draw the rectangle to be wide and tall and you can see there now I have a rocket moving up so I have a single rocket that's a good sign I am getting something going here okay I'm looking everywhere because I got a screen here I got a camera here I got a wall here I'm going a little crazy here okay this is good this is good okay now what I think I want to do is I'm gone but now I'm going to create a population you know I don't necessarily need a whole separate object to manage this stuff but I'm going to do it anyway and in the population I want to have an array of rockets and I want to I want to also have a population size we'll just make it like a hundred back I made the font size a little bigger so it'd be easier to see in the video so what I want to say is this dot Rockets index I equals a new rocket and one thing I'm going to do is have their initial velocity just for right now be just so I can see a random a random vector and what I'm going to do now is create a population and I'm going to say the population should update and I'm just going to actually write a function called run and then in the population object itself I'm going to write a function called run and what I want to do there is okay back again and what I want to do is now say oh this dot this dot Rockets whoops I need to say this dot rockets uh rockets index i dot update and this dot rockets index i dot show i'm back after some technical difficulties and there's a big bug in my code it's not missing at this stop but it's almost as bad remember how i said here i need this function called pop i need this function called pop to use push and pop to make it so that this translate and rotate only affects this particular rocket well guess what I did I made a variable named pop for population and that variable pop is now my global variable which overrides the p5 function named pop so I really shouldn't have done that I need to name this something else I'll call it pop you'll for population and you know what I don't know I can actually just say population I'll do this population run and now we should see there's one rocket lifting off and now I can change the population size to like 25 and we can see there we go a bunch of rockets all going off in their random direction let's clean up some things about these Rockets let's make them a little bit smaller let's say no stroke let's give them fill 255 with a little bit of alpha and we should see now there we go so this is these are our smart Rockets lifting off from the bottom of the screen so we're getting somewhere we've got the physic system now if I return to over here in the original smart Rockets there are actually a set of specific thrusters positioned on the rocket that push off a force in a given direction I'm going to do something a little bit simpler what I'm going to do as the objects DNA is I'm just going to create an array the array of DNA is the DNA is just going to be an array and in each spot in that array will be a vector pointing in some at first at least some random direction and what the rocket will do it will read that array each vector at a time and apply that vector as a force so let's look at how that might work I press this button here I okay so what I want to do now is create an object called DNA and in that object I'm going to create an array and I'm going to call that array genes and I'm also going to create like a length a variable and actually I'm going to make this a global variable I'm gonna have a global variable just called lifespan and let's make that 200 right now because I'm going to let each rocket live for 200 frames and so it's DNA needs to have an array of 200 vectors so it's going to apply a single vector each frame of animation so you know right so I'm going to now what I'm going to do is I'm going to go from I to 0 to lifespan and I'm going to in that array I'm going to give it a random vector so what I'm going to do now is I want the velocity vector for each rocket - whoo whoops to start off at zero come on everybody so we can see now here all of the objects just start at the bottom of the screen and they're not moving but I'm going to give each rocket some DNA and then in the update funny crock it also needs a counter variable this dot count and what I'm going to do an update is I'm going to say this dot apply force DNA genes this dot count and guess what I forgot this time so I need this DNA genes dot index discount I know that's a mouthful of stuff but I've made a DNA object in the rocket that DNA object is it has built into it an array called genes it has 200 vectors in it and each frame what I want to do is apply one of those vectors as a force and then I want to increase the count so now we can see what who look at this huh let's run this again you can see them going flying off like crazy so those those forces were rather were rather large so another thing I think I could possibly do in my DNA is I could just make the force a little bit weaker and you can see there we go so they are the Rockets kind of moving off each getting a force and I also want to look I want to be able to see the that would be the kind of the life span itself so what I'm going to do also is I'm going to say life P life P equals create P I'm gonna make a paragraph element and then I'm going to you know what I just realized even though it's nice to have the the rockets have that count I think I'll just make that count a global variable because it's really going to be the same for everyone so var count equals zero and what I'm going to do is say life P dot html' count and then count plus plus so now you can see over here right I've got something showing me where we are and now at some point I'm going to go past the end of that array I'm sure we're seeing some sort of error here count is not defined okay well that's something else where am I getting that this dot count that can go away so at a certain point I'm going to go past that array but that is when the generation would end and I'm gonna reset to pet count back to zero ah are you following me I got lost in my own salt with all my computers and typing and coding thing so here we are and yes here we are we now have a system so far where I have a population of rockets the Rockets have DNA the DNA has embedded into it an array of vectors that tell it how to move around the screen now what I need is a target I need to have a place where these where these Rockets are trying to reach so I'm going to create a variable called target global variable and I'm going to say target equals create vector and let's put it in the middle of the window at the top and what I'm going to do also in draw is I'm going to just draw a nice little ellipse at Target X target dot y 16 comma 16 and we should see that's where they're trying to get to that's our target and then what I want to do is I want to say here if count equals lifespan count plus plus then I want to do so I want to reset let me just reset count back equal to zero right now and less a little trick I'm just going to do this just to sort of see this is not what we want to do but I kind of want the whole thing to start over I don't know the auto format and code back okay so now we should see when this gets to 200 so so we've got the system working here um we have and I'm just going to kind of zoom on this zoom in on this to talk about it for a second okay so over here we have a system now where I am every 200 frames launching 200 rockets into space and I'm counting up to 200 and when I get to 200 I start 200 Rockets over what I'm missing now from this example is the genetic algorithm itself what instead of just creating a completely new random population I want to make a new population out of the Rockets I want to make a new population from from from the previous population of rockets I want them to create children Rockets I want the Rockets with the highest fitness that did the best to pass on their genetic information to the next generation the next step that I need to do in population is I'm going to call say this dot evaluate oh my camera went off so what do I want to do in evaluate I want to run through all of the rockets and I want to calculate their fitness this dot Rockets index I calculate Fitness so I need a new function in each rocket to calculate their fitness so what where where's my rocket object somewhere here right I'm going to write a function now this dot calc fitness equals a function so what should their fitness be well we could say that the closer rocket makes it to the target the more fit it is so I can say VAR d equals distance this dot paws dot X this dot paws why target dot X target dot y code pen couldn't fit that all it's not code pens fault but I don't have enough room to fit that all in one line which is why it broke off but you can see I'm taking the distance now the other thing I want to do though is say that and let's give these Rockets a variable called fitness so and I'm going to say this dot fitness equals one divided by distance right because if the distance is one my fitness is one if the distance is 100 it's further away my fitness should be lower like point zero 1 like 1 divided by 100 so in this sense I now have a fitness of 1 divided by distance okay this is good so in the population back to the population it's hard to have everything in one file I have now related to fitness now the other thing that I need to do if you watch my previous genetic algorithm videos is create a mating pool I want to have a mating pool for which I can pick parents to create the next generation of rockets and in order to pick their parent the parents I want their probability the higher their fitness I want the higher chance of picking them so rockets that did really well or that get closer to the target are more likely to pass their genetic information down to the next generation so what I want to do is create a variable I'm going to call it a mating pool and it's an empty array and the first thing I want to do actually I need to make sure I clear it so every jet every time I do this I want to like make it an empty array again and then what I want to do is I want to I want to normalize all of the Fitness values I think this would be useful to do so one thing I want to do is a while I'm calculating the fitness I want to do max fit and the maximum fitness is zero and if this dot Rockets index I max fit is greater than max fit and this dot Rockets fitness is greater than max fit then I want max fit to equal that particular Rockets fitness so I want to find the maximum fitness what's the highest fitness out of all of the elements and then after I do that I want to go through them all again and I want to take their fitness value and divide it by the maximum fitness so that should normalize them right if the values were the maximum fitness is a thousand the one with a thousand divided by a thousand I'll get the value one all the fitness sizeable between zero and one at the moment there's a few divided by zero issues we could walk we could we could have but but I'm not going to about that too much right now okay another way instead of doing I did just realize though here's another thing I could do let me go back to the rocket this will be kind of I think this is actually kind of useful why don't I instead of doing one divided by distance why don't i map that distance which you know essentially has a range between zero and like the width of the window and give that a value all the way between width and and just sort of invert that value I guess that's kind of the same thing but let me just do that in this case I might not need to normalize it there's all sorts of possibilities I'm just working this out something might go wrong but let's try that and see how that goes and then let me go back to the population right where was that ah and so now what I want to do is I want to loop through it again and I want to get up number n which is equal to each Rockets fitness and I would say times 100 I'm going to do that arbitrarily right now and then I'm going to say for J equals zero J is less than n J plus plus and then what I want to do is say this stopped mating pool dot add and I'm going to add this dot rockets index I so the idea behind this bit of code right here that you've seen already if you watch my other genetic algorithms videos is that what I'm doing is I'm saying basically take the rockets Fitness which is between zero and one so and then multiply by 100 so I now have Fitness values between 0 and 100 a rocket with a value of 100 a fitness of 100 to be in the mating pool 100 times a rocket with a fitness value of 3 should be in the mating pool three times so it still could be picked if it's only in there three times but it'll be picked much more likely if it's in there a hundred times so the next thing that I want to do is I'm going to write a new function for this is natural I'm going to call it natural I'm just going to call it selection I'm going to write a function called selection and in selection what I want to do is pick to parents and so I'm going to say partner parent a and oh I'm going to say this dot mating pool and I want to get a in random index equal so oh you know what you can do in p5 now I'm pretty sure in p5 the random function you can actually just give it the array this is a new feature of p5 if I just say random and then an array it'll just give me a random element from that array so I don't actually have to pick a random index yes so that's parent a now this is parent B so I want to parents and I could do something to like say like oh I want to make sure you know one is not the same as the other but you know I want to make sure they're not the same I don't pick the same twice but I'm not too worried about that okay so I want to pick two and then I want to make a child element and I want to make that child by saying parent a dot crossover parent B so this is an S is now my wish list I want the DNA and this is really actually not the rocket I want I'm going to call this child yeah yeah I'll call it yeah I'll call this child I want to have in the DNA object a function that takes um oh you know what this is actually interestingly enough parent a is a rocket object so what I'm going to do is say dot DNA because I'm going to go and grab the DNA from I'm going to take the random rocket object and grab its DNA because what I want to do is create child DNA I want parent a to crop DNA to cross over with parent V's DNA so that means in the DNA object which is there's gonna be a lot of scrolling in this right here I need a new function and I need that function to be called cross over and in this function I need to create a-another right like child I'm going to call it a new DNA is it is an array and what I want to do is loop through the lengths of the genes and it needs an argument partner so the crossover function needs an argument partner and what I want to do is I'm going to say I'm going to make a bid point midpoint equals a random number between 0 and the length of the array so I'm actually going to pick a random midpoint I could always pick the exact middle but what I want to do is I want to say if I is greater than 0 I want the new DNA to come from this this objects genes otherwise and I know what if I is greater than the midpoint and otherwise I want the new DNA to come from the partners genes and then I want to return new DNA so what I'm doing here is I now have written a crossover function whose going to be a long video I have written a crossover function we can look at it closely here which basically says I am a DNA object and I have received another DNA object I'm going to pick somewhere they're both arrays so I'm going to make a new array and then I'm going to there's some there's a couple there's some slots yeah I got it I realize things I got to fix something but that's fine I'm going to pick a bit point in the length of the array and I'm going to get half you know before the midpoint I'm going to get these genes and after the midpoint I'm gonna get the partner's genes now there's an issue this here is just an array but what I really want is a DNA object so what I want to do is actually say I want a new a new DNA object and it'll make random genes when I do that but I can and I want to fill the genes I'm going to overwrite that this way I could be a little bit more elegant about how I write this in which case I could I'm going to do that now I'm going to be so that's one way I could do it but I'm actually going to keep this as an array and I'm going to call this new jeans and then what I want to do is say return new DNA I'm going to say return a new DNA object with the new jeans which means I now can actually just give the DNA object an array rather than have it be random and in this case in the constructor function here I can have a variable called jeans and I can say if jeans exists then this jeans should be that array otherwise do all this other stuff so now what I'm saying here is in this function I this DNA object could either receive jeans and just create a DNA object from those jeans or if it doesn't receive jeans then it could just make random DNA so now somebody in the chat line 101 telling me that there's an error yes odd this return has to come after that for loop so that is definitely an error okay so now I have my crossover function done the in the population object which is I guess closer to the top here where I have a selection I now have this is child DNA now what I need to do and I want to do this how many times I want to do this first I want to say new population is an array and then I want to loop through and I'm going to say new Rockets new rockets and I want to loop through the existing rockets and I want to do this for every single existing for example single looks and this should say this dot rockets so now what I want to say is new Rockets index I equals a new rocket with that child DNA so this is what I want to do I want to have parent a pick a DNA from one parent parent B pick a DNA from another parent cross over those two DNA's and make a new rocket with that DNA now does my rocket where is the rocket object scrolling like crazy once again now the rocket object should have an optional argument the rocket constructor should have an optional argument called DNA so I could say if it receives DNA then this DNA equals that DNA otherwise it can make new random DNA so once again I now have rockets that can be made from DNA it still will get the default position velocity acceleration but it can get that DNA so let's see what we've got so far where am I in the population now now what I want to do is I have this new population and I just want to say this dot Rockets is now the new Rockets so let's see if things are kind of working in the main program now instead of just making a new random population what I should be able to do is say population dot evaluate I'm sure there's going to be tons of errors here but we'll see what happens population dot I'm almost certain that I forgotten now somewhere population dot selection back and I'm here to correct a few errors because I definitely have some this dot things that are wrong so this should be this year over here should be this genes this should be this genes length and this by the way should not be this dot partner it should be partner dot genes so that wasn't working so that's that sort of helps things there's an exclamation point here which should be telling the unexpected token on line 41 so this not evaluate equals a function okay so let's see that kind of helps that was the problem ah great so we've got an error let's see if I can look in the console this dot mating pool dot add is not a function so line 59 here this stop mating pool I so I'm in thinking in Java land worthy I would use an ArrayList it would be push I mean it would be add but in JavaScript it should be pushed so let's run this again and when it gets to 200 let's see what happens another error let's see what happens back so this is a problem I don't know why I said this dot Rockets equals new Rockets like new Rockets was a function no new Rockets is an array that I want to assign to the current Rockets so I'm going to do that and let's see if we get now it running again okay so it looks like we're getting a new generation look at that you can see how even just that second generation the Rockets are now kind of pointing more straight up so the ones that have kind of kind of made it now there's a bunch of issues with this number one I need to sort of figure out how do I know if the genetic algorithm is working I can kind of see loosely that it is generally working in that if I were to refresh this whole page and run it again you can see they start all going off in random directions and one of them happen to get quite close and you can start to see that over time it looks like maybe more of them are starting to go towards that target so the that array of vectors is sort of evolving towards a bunch of vectors that point straight up so I have the sense the general sense that this is working okay but let's how can I really kind of understand and know if this is working well or not working well well one thing I could do is just like kind of find various things in my code to kind of look at so what I want to do is create I'm going to just say create P Max fit so every time it runs I can kind of see what that maximum fitness value is 363 I'm gonna step to the side here 355 so that makes sense like I'm getting reasons fitness values 366 390 so so that kind of makes sense that's a good sign that that maximum fitness is going up from frame to frame so something's actually working I would be curious to know to kind of just see the range of fitness values so I'm just say console.log this dot rockets let's see if this if I can look in the console here ah there we go I don't know why that wasn't coming out but now I can see it and I can look under at all the rockets and I can kind of see the fitness negative 0.36 negative 0.32 so you can see this is these are the normalized values it's a little weird that I got a negative number in one of them but a Fitness one that was the one that had the maximum fitness so you can see the fitness values are kind of making sense so this is a good sign so I'm happy that I have reasonable fitness values okay I'm back so what I want to do is I want to I want to do some things to improve the fitness function and one thing I just want to do is I don't want to let them go when they hit the target they're done they've achieved their goal and they should get like a super high fitness so one of the things I'm going to do in the rocket itself is I am going up I'm going to look for the rocket and in I guess what I'll just do this in the update function I'm going to check the distance between this particular Rockets position and the targets position ooh and if that distance is less than like I'm just going to say if it's less than like 10 pixels then it's reached the target so let's say let's create a boolean variable here called oh I added something to the code that's kind of making it stutter a little bit but I'm going to add a variable called this completed equals false and when it's reached the target I want to say this dot completed equals true and then and then I only want it to move if it has not completed its goal okay so I only want it to update the physics engine if it hasn't completed to go and also if it has completed what I'm going to do is just say this stop position equals a copy of the targets position so I'm just going to actually move it to the target so this will help actually give each of these the best score possible when they hit the target and you should see none of the Rockets should now go past the target the other thing I think is worth doing is I could say here all right well if this dot completed let's make things that have complete actually completed and reached the target let's take the fitness and multiply it by ten I don't know if that's a good idea but I could just say like you know you're doing really well if you get close but if you actually reach the target your fitness is just like massively bigger so I'm going to refresh the page code pen is refreshing automatically and let this run for a little bit I'm back and you can see that the Rockets have now evolved a nice path of going towards that target so you can see they're all actually going along the same path now one thing I realized that I forgot to add to this genetic algorithms mutation function so I'm going to add that right now and I'm going to put that right here under the crossover function mutation is good in a lot of cases you know the these Rockets can now only ever have the path based on the original random vectors so I could just write a simple function that just loops through all of their genes again and what it does is it just picks a random number and let's just have a mutation rate of 1% meaning if I pick a random number less than point zero one what I want is for that particular gene to act to just become a new random vector so I'm going to pick a new random vector and I am also going to then set its magnitude to 0.1 which i think is sort of the arbitrary length of these vectors that we've decided so now I've added a mutation function as well and I can after in the population object right here after I call cross over after I cross over one parent with another I can also just say then child mutation and so now if we let this run with the mutation function we'll we should see that you know mutation will play a role that ultimately eventually they might evolve the wheel to evolve a even more optimal path because you know you know in this case every single vector just really should just point straight up right that would be the perfect thing for it to evolve eventually now here's something here's an exercise that you can think about adding to this the length of time it takes for the rocket to make it to the target that could absolutely be a part of its fitness right if it if it makes it to the target faster what is the current value of count when it makes the target that's something I could save and defend the lower that count the higher its Fitness so I might leave that as an exercise for you to add in but what I'm going to do right now is I am going to add just a quick obstacle because I think that will be worth it to sort of see so let's make let's make a rectangle in the window whoops I'm gonna draw a rectangle that's like start at X is 200 X 200 Y of 50 it's going to be 200 pixels across and 10 pixels high let me say fill 255 and it should be clearly be much lower down and maybe it should be start at 100 and it should be around there right so there's an obstacle for you that's actually a pretty hard office we'll get around so what I want to do it let's make these variables Rx is what did I say is a 100-hour why is 150 and RW is a 200 m and RH is 10 so what I want to test in the rocket object and where is my rocket here it is what I want to check here is in the update function right this is checking has it reached the target but let's also see if it knocks into that obstacle if pause X is great ARCIC this pause dot X is greater than that our x value and this dot pause X is less than our X plus our W right if it's between there and this dot pause dot y is greater than our Y and this dot pause dot Y is less than the top of the rectangle plus the height our y + rh right in this case is also it's I'm going to say it's dead or it crashed this dot crashed is true so so I want it to stop moving if it's completed Oh move it as long as it's not completed and it's not crashed and we can say the the each rocket now has a variable called disk crashed which is also false and then in the fitness if this stock completed this dot fitness x equals 10 if if this dot crashed let's just set this dot fitness equal to 1 so I'm not going to set it to 0 I probably should set it to 0 but but let's set it to 1 okay so now we should see let's see if these Rockets over time evolved to go around the obstacle okay so you can see now they've it's been running for a little bit but they haven't really evolved to go around the obstacle yet so let's add a few things number one is they need a bit of a longer life span so I'm going to let them go for 400 cycles I'm also want to I also want to make them crash if they hit the edge of the window that I think will help a little bit so let's let me go to the Rockets and in this place where I'm trying to check if they've crashed I can also check things like if this cos x is greater than width or this dot paws X is less than 0 then this dot crashed is also true and I can also do the same thing with the height so I can also I can also I can also have them crash if they go off the edge of the window which I think will help the other thing that I might consider doing is I'm going to make a global variable called max force which is 0.1 right now so I'm going to look for wherever I have Mac 0.1 right here I always set the magnitude to 0.1 I'm going to down change that to the variable max force and I want to set this to max force and there we go and now oh look at this these poor poor Rockets that start going down off the screen it's going to take them a while to evolve out so you know I should give them all I should give them all a little nudge up that might be a nice thing for me to give them but I kind of don't want to but I am going to give them a higher maximum force which I think will help so I'm going to make that maximum force point to which I think and then I also though want to limit their velocity which I think will be helpful just in like make it so they don't sort of spiral out of control I'm in the rocket object I am going to just here say this dot velocity limit like two four and now that's a bunch of things that I've added let's let me zoom in here on our Rockets and I'm going to let this run for a little bit and I'll come back when after it's run for a little bit we'll see how it goes so I've let this run for a little while but unfortunately all the Rockets are crashing and so the crashing just like totally ruins their fitness and actually the ones that crash kind of closer to the target we kind of want to keep so what I'm actually going to do is I'm just going to divide their fitness by ten when they crash and that way we still have the fitness scaled so to speak so let's have this run for a little bit longer and see what happens I'm back so these Rockets have now successfully evolved and you can see that one of them figured out how to get around this target and hit the get around the obstacle hit the target you could see that it had some like accidental weird DNA in it that makes it go kind of like early in this weird pattern they're all kind of doing that so this is where if I were to add something to the fitness function that gave the ones that actually make it to the target a little bit faster a higher fitness they would start to work out the path so that they wouldn't be kind of doing this strange extra thing because as long as they make it to the target before the end of the lifespan they're fine so I'm going to leave on this video I think here there are so many improvements you could make to this I might like to make some of them at some point or come back and do a video you might like to make those improvements and share them with me number one is just design wise you could add color I designed the Rockets you could create a much more interesting maze or course for them you could create obstacle objects and allow the user to move the obstacles around make the target movable so they have to adapt and evolve we could improve the Fitness function in a number of ways one by keeping track of the amount of time to reach the target something that I'll talk about I think I'll make a separate video about this is just talking about why an exponential fitness function and said the linear Fitness function can be something that's quite useful which you could add to this as well look for the next video kind of how I do that um so those are a bunch of ideas of how you could make this system a bit better and in fact one thing that I will do is I'm going to revisit this example in a future video looking at how a neural network or other types of machine learning algorithms might also be used to train agents to move around the screen with a certain path so I hope you enjoyed this rather I think insanely long and scattered scattered a coding challenge the code itself will be available on both github as well as a link to this pen this code pen that I created here so you can actually just go straight to that link and play around with the code there as well okay I hope you enjoyed it and I'll see you in a future genetic algorithms video
Info
Channel: The Coding Train
Views: 356,165
Rating: undefined out of 5
Keywords: challenge, coding, rockets, code, smart, p5.js, javascript, coding challenge, object oriented programming, oop, object oriented, programming challenge, creative coding, daniel shiffman, p5js, nature of code, genetic, genetic algorithm, genetic algorithms, the nature of code, evolution, algorithm, genotype, phenotype, genotype vs phenotype, dna, darwin, evolutionary, evolutionary computing, survival of the fitest, evolutionary algorithm
Id: bGz7mv2vD6g
Channel Id: undefined
Length: 48min 17sec (2897 seconds)
Published: Tue Aug 02 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.