Current location: Homepage > swift > Harvard CS50’s Artificial Intelligence with Python – Full University Course - Ep26

Harvard CS50’s Artificial Intelligence with Python – Full University Course - Ep26

2025-07-11 11:29:50 [solidity] Source: ByteGenius
fordemonstration I can ai taking jobsnow try and printout sentenceformula which is a function I wrote thattakes a sentence and propositional logicand just prints it out so that we theprogrammers can see this in order to getan understanding for how it actuallyworks so if I run python harry. py whatwe'll see is this sentence inpropositional logic rain and Hagrid thisis The Logical representation of what wehave here in our Python program ofsaying and whose arguments are rain andHagrid so we're saying rain and Hagridby encodingthat idea and this is quite common inPython objectoriented Programming whereyou have a number of different classesand you pass arguments into them inorder to create a new and object forexample in order to represent this ideabut now what I'd like to do is somehowencode the knowledge that I have aboutthe world in order to solve that problemfrom the beginning of class where wetalked about trying to figure out whoHarry visited and trying to figure outif it's raining or if it's not rainingand so what knowledge do I have I'll goahead and create a new variable variablecalled knowledge and what do I know wellI know the very first sentence that wetalked about was the idea that if it isnot raining then Harry will visit Hagridso all right how do I encode the ideathat it is not raining well I can usenot and then the rain symbol so here'sme saying that it is not raining and nowthe implication is that if it is notraining then Harry visited Hagrid soI'll wrap this inside of an implicationto say if it is not ring this firstargument to the implication well thenHarry visited Hagrid so I'm sayingimplication the premise is that it's notraining and if it is not raining thenHarry visited Hagrid and I can print outknowledge. formula to see The Logicalformula equivalent of that same idea soI run python of harryp and this is TheLogical formula that we see as a resultwhich is a text based version of what wewere looking at before that if it is notraining then that implies that HarryvisitedHagrid but there was additionalinformation that we had access to aswell in this case we had access to thefact that uh Harry visited either Hagridor Dumbledore so how do I encode thatwell this means that in my knowledgeI've really got multiple pieces ofknowledge going on I know one thing andanother thing and another thing so I'llgo ahead and wrap all of my knowledgeinside of an and and I'll move things ona new lines just for good measure but Iknow multiple things so I'm sayingknowledge is an and of multipledifferent sentences I know multipledifferent sentences to be true one suchsentence that I know to be true is thisimplication that if it is not rainingthen Harry visited Hagrid another suchsentence that I know to be true is orHagrid Dumbledore in other words eitherso Hagrid or Dumbledore is true becauseI know that Harry visited Hagrid orDumbledore but I know more than thatactually that initial sentence frombefore said that Harry visited Hagrid orDumbledore but not both so now I want asentence that'll encode the idea thatHarry didn't visit both Hagrid andDumbledore well the notion of Harryvisiting Hagrid and Dumbledore would berepresented like this and of Hagrid andDumbledore and if that is not true if Iwant to say not that then I'll just wrapthis whole thing inside of anot so now these three lines line eightsays that if it is not raining thenHarry visited Hagrid line n says Harryvisited Hagrid or Dumbledore and line 10says Hagrid didn't V or Harry didn'tvisit both Hagrid and Dumbledore that itis not true that both the Hagrid symboland the Dumbledore symbol are true onlyone of them can be true and finally thelast piece of information that I knewwas the fact that Harry visitedDumbledore so these now are the piecesof knowledge that I know one sentenceand another sentence and another andanother and I can print out what I knowjust to see it a little bit morevisually and here now is a logicalrepresentation of the information thatmy computer is now internallyrepresenting using these variousdifferent python objects and again takea look at logic. if you want to take alook at how exactly it's implementingthis but no need to worry too much aboutall of the details there we're heresaying that if it is not raining thenHarry visited Hagrid we're saying thatHagrid or Dumbledore is true and we'resaying it is not the case that thatHagrid and Dumbledore is true thatthey're not both true and we also knowthat Dumbledore is true so this longlogical sentence represents ourknowledge base it is the thing that weknow and now what we'd like to do iswe'd like to use model checking to ask aquery to ask a question like based onthis information do I know whether ornot it's raining and we as humans wereable to logic our way through it andfigure out that all right based on thesesentences we can conclude this and thatto figure out that yes it must have beenraining but now we'd like for thecomputer to do that as well so let'stake a look at the model checkingalgorithm that is going to follow thatsame pattern that we drew out in Pudocode a moment ago so I've defined afunction here in logic. that you cantake a look at called Model check modelcheck takes two arguments the knowledgethat I already know and the query andthe idea is in order to do modelchecking I need to enumerate all of thepossible models and for each of thepossible models I need to ask myself isthe knowledge based truth true and isthe query true so the first thing I needto do is somehow enumerate all of thepossible models meaning for all possiblesymbols that exist I need to assign trueand false to each one of them and seewhether or not it's still true and sohere is the way we're going to do thatuh we're going to start so I've definedanother helper function internally thatwe'll get to in just a moment but thisfunction starts by getting all of thesymbols in both the knowledge and thequery

(Editor: web3)

Recommended articles
Hot reading