 Hey guys, it's MJ the student's act tree and in this video. I want to show you a little site project that I've been working on Basically, it's a start to artificial intelligence and what I've done is I've started off very simply by trying to make a AI that can beat me in the game of rock paper scissors so let's go open up the app and Then I'm going to talk through the code and and how I did it. So as you can see the graphic design is absolutely beautiful You know, it's actually gorgeous. Okay. Anyway jokes aside, let's get into it So basically with the game rock paper scissors rock beat scissors scissors beats paper Paper beats rock. Okay, we all know the rules Let's say for example, I choose rock Okay, it will come it says the AI beat me and It's getting got one point Because the AI chose to go paper now the very first play we do is is random You know, each these things here show the probability distribution that the AI is going to be choosing And as we can see right in the beginning, it's 33% basically for for each of them What this is doing over here at the bottom is it's recording my pattern And this is going to be very important when it comes to to changing this decisions So because I lost with rock, okay I don't want to do rock again because I want to I want to change to paper So go to paper. Oh, and there we go. I won so now one with paper and you can see the stats It still doesn't have enough information of my gameplay to change the stats, but we can see that When I alternate and I win I got one point So because I like paper, I'm going to go paper again. Okay. Oh, look at that Now because I've chosen paper twice in a row. Okay, so I won because the AI chose rock. I've got two points This is where things start getting interesting. So by alternating my win I've got another point and look how the odds have changed. So now The AI is thinking that I'm going to go paper again And you can see scissors has increased the the weighting of the probability there. So if I got paper again, oh look at that I lost and Scissors came in then it adjusts and you can see this is what the probability distribution is showing So that's kind of the game and I mean Let's go look into the code to see how exactly I did it How did I make this this clever little program to beat me at Ching Chong Cha? Oh, that's what we call it chance of Africa Ching Chong Cha, but I think rock paper scissors is the more correct name Okay, so let's check it out. What I do is I start off with my weights. These are my variables. Oh, I must just confess I'm not a programmer. So my code is going to be a little bit messy. It's not in nice methods So I do apologize for that programming is just something I'm Trying to learn on the side because to me it's almost as important as reading as writing in Tomorrow's world. So I highly encourage everyone to learn how to program anyway These are my weights The chance it'll be paper the chance it'll be rock and then scissors will be the subtraction of those two Then I've given my AI a whole bunch of variables for it to think so it's going to count whether I'm winning so when I play the game I Certain things can happen if I win do I repeat my style or when I win do I alternate or When I lose I repeat myself or when I lose do I change my style and different plays can have different combinations You might play so that when you win you play the same thing again Or somebody else might have it so when they lose they alternate So every person will have their own unique strategy and my AI will try to figure it out what it is My AI also has its own little personality in the fact that it has this confidence variable and this forget Limit I've set it to five and three I mean I can extend this program to make it so that this confidence and this forget limit are random variables and Then what the AI will do is it will play against multiple people and it will see at what confidence and at what forget limit Did it perform optimally and then it will set that but it will first randomly choose and determine So that is something that could develop it further Then there's the actual game choices, you know what it actually chooses how to calculate the result and all that And then I do give it a little bit of memory because what I'm doing is I'm looking at Not only your current play, but also your previous one to make a bit of a pattern I could extend this AI to make it even look at The past part so it looks at your first play your second play and then your current play To even make it even more complicated But remember this is just a beginning introduction to AI. I'm doing this just as a little side project So yeah, it's not not for a work or anything. So I'm just having fun with it Okay, so what will happen is if you push that rock button, this is what happens Okay, it counts that you did rock not that I'm using this at the moment But it's just a nice thing to keep track of the user chooses rock then we generate a random number between 0 and 100 and then if this random number is less than the paper chance the AI will have chosen paper and Basically, what we're going to be doing is adjusting These guys over here The weights there you can see that if the AI Wins its confidence grows by one and we can see where confidence comes in and the score goes up by one Then we're also going to record how you know, what type of player you are Are you a repeating player or are you an alternating player starts picking that up? Then what we do Over here is we now look at if the random variable was greater than Less that was a greater number It then looks is it above paper chance and rock chance and then if it is within this number Then the AI has chosen rock and rock on rock is a draw and then it does the whole Calculation of repeating the self will draw or the alternating style draw depending on what the history was So the first time you play the game, there's no history So the code will just skip this and then again with scissors you can see scissors it loses So the confidence gets reduced And then your same thing happens again, so you can see my coat is a little bit messy I should maybe have methods to make it more streamlined, but I just was I was just interested in getting this thing done then So it either determines whether it does rock paper or scissors and then it adjusts the strategy. So if I lost It will look at if my alternate style is greater than my repeat style for losing It will adjust the the probability. So here, let's say I chose rock and My alternate style or my history it takes that into consideration and here We see confidence is how much we actually decreasing the chance by so if the AI is very Confident of how it's playing then this change will be quite drastic. It might be 10% or something like that if What you call it the AI is You know it starts losing we can see the confidence decreases and it's not going to be making as well changes like this So the worst case scenario is that my AI reverts back to 33% for Rock paper and scissors so that you can only beat it then by a luck You can't outsmart my AI. So if you are figuring it out, it will go back to that defense But the whole idea here is that I Mean if I keep going Rock and I keep losing It's going to change the weights so that it can predict what I'm going to do next This does get a little bit complicated. I mean knowing which one to change by which one else Let me just try to think it through so for Example what this is saying is that let's say I lost Okay, and this week gets confusing because you have to think I lost but the AI won so when the AI wins How does it change its game? Okay, so let's say? Let's see what we can do here. So this is worth rock. Okay, so I chose rock and I lost Which means the computer chose paper Okay, if I Let's say I'm repeating myself. So let's say I did rock and I lost so let's look at this here So if I chose rock I lost and I played rock again Then my repeating style or my game style is that I repeat the same move when I lose Which means the AI is going to adjust it's going to increase For rock and it's going to increase for paper And then what this does because it doesn't want to go scissors because if I'm going rock rock rock It doesn't want to go scissors So it will increase rock a bit and it will increase paper by more because that's for the win and that's for a draw It wants to avoid the loss and then it will do the opposite if I'm alternating. So it does get a little a little bit confusing and then you're then I come and then each Button ends off by displaying the score and I was just displaying. I think we're actually make the actual game I'm not going to show people what the AI's brain is thinking because then you can also beat it like that people must play The game and be like, oh this AI. How's it doing it? It's really cool What I also do is I like to make the AI forget So let's say you go paper paper paper paper paper paper, you know, and you you build up a whole series or something Then the AI is going to be oh, you're you're always repeating and then suddenly you change your style And you're going to start beating the AI. So what it does is that if If the alternate style win is greater than the repeat style win plus this forgetting limit It will reduce so it will it will kind of forget so that if a player changes their strategy The AI can adapt quickly towards that and then what I also do is this is not a very neat way But just I want to keep the weights in balance So I don't want it to be, you know, like a hundred percent that it always goes paper I want to keep it so that there is that random Element to it so that when you play against this AI There still is a little bit of luck involved and Then also this this course is a lot of bugs is what happened was my confidence would go negative and then it would absolutely Screw up everything. So I've just made a little if statement that if the confidence goes below one just put it back to two So that it doesn't go absolutely haywire because then the opposite stuff started happening There was a big scratch brain scratch, but we finally figured that out and Then you're it record your history and this was also just a little bit of diagnosis Diagnostics to show me what was going out while I was busy programming it and then you can see having been very tidy it's basically the same thing when you do choose paper just all the things are a little bit different and the same happens with scissors as well and Yes, I know good programming Conventions would be to put all of these stuff in a method and then just call that method But job I made this it didn't take that long to make it took a little bit longer to debug and make sure that it performed properly but at the end of the day that is My first little artificial intelligence In making a game the the dream is not to make it for the American football game, which is going to be a Lot more complicated, but I think this is the first step in trying to take a player's behavior Figure out what he's going to do next and not be Deterministic by saying oh he went rock rock. We definitely going to go paper next time My philosophy is that an a ish is or information or knowledge It's probabilities and so instead of saying okay We definitely going to go paper next just increase the likelihood of going paper because we know humans Do you have a little bit of that irrational factor? You know the person might just suddenly change their mind and you do want a probability nature to to incorporate that and job that is My little artificial intelligence if you have any questions feel free to pop them in the comment section If you have any advice for me, please let me know like I say I am brand new to machine learning and AI So I am willing to learn so if you have any suggestions, please let me know Enjoy. Thanks guys for watching Christmas is coming up and then I'm going to stop making some more actuarial science videos. Thanks for watching. Cheers