AI Knowledge & Reasoning
Transcript
[MUSIC PLAYING] BRIAN YU: All right. Welcome back, everyone, to an introduction to artificial intelligence with Python. Last time we took a look at search problems in particular, where we have AI agents that are trying to solve some sort of problem by taking actions in some sort of environment, whether that environment is trying to take actions by playing moves in a game, or whether those actions are something like trying to figure out where to make
turns in order to get driving directions from point A to point B. This time we're going to turn our attention more generally to just this idea of knowledge. The idea that a lot of intelligence is based on knowledge, especially if we think about human intelligence. People know information. We know facts about the world. And using that information that we know, we're able to draw conclusions-- reason about the information that we know in order to figure out how to do something or figure out some other piece of information that we conclude based on the information we already have available to us. What we'd like to focus on now is the ability to take this idea of knowledge and being able to reason based on knowledge, and apply those ideas to artificial intelligence. In particular, we're going to be building what are known as knowledge-based agents. Agents that are able to reason and act by representing knowledge internally. Somehow inside of our AI, they have some understanding
of what it means to know something. And ideally, they have some algorithms, or some techniques they can use based on that knowledge that they know in order to figure out the solution to a problem, or figure out some additional piece of information that can be helpful in some sense. So what do we mean by reasoning based on knowledge to be able to draw conclusions? Well, let's look at a simple example drawn from the world of Harry Potter. We take one sentence that we know to be true. If it didn't rain, then Harry visited Hagrid today. So one fact that we might know about the world. And then we take another fact. Harry visited Hagrid or Dumbledore today, but not both. So it tells us something about the world. That Harry either visited Hagrid but not Dumbledore, or Harry visited Dumbledore but not Hagrid. And now we have a third piece of information about the world that Harry visited Dumbledore today. So we now have three pieces of information now.
Three facts inside of a knowledge base, so to speak-- information that we know. And now we, as humans, can try and reason about this, and figure out based on this information, what additional information can we begin to conclude? And well, looking at these last two statements, Harry either visited Hagrid or Dumbledore but not both, and we know that Harry visited Dumbledore today. Well, then it's pretty reasonable that we could draw the conclusion that, you know what, Harry must not have visited Hagrid today. Because based on a combination of these two statements, we can draw this inference, so to speak. A conclusion that Harry did not visit Hagrid today. But it turns out we can even do a little bit better than that-- get some more information-- by taking a look at this first statement and reasoning about that. This first statement says, if it didn't rain, then Harry visited Hagrid today. So what does that mean?
In all cases where it didn't rain, then we know that Harry visited Hagrid. But if we also know now that Harry did not visit Hagrid, then it tells us something about our initial premise that we were thinking about. In particular, it tells us that it did rain today. Because we can reason if it didn't rain, that Harry would have visited Hagrid. But we know for a fact that Harry did not visit Hagrid today. So it's this kind of reasoning, the sort of logical reasoning where we use logic based on the information that we know in order to take information and reach conclusions. That is going to be the focus of what we're going to be talking about today. How can we make our artificial intelligence logical so that they can perform the same kinds of deduction, the same kinds of reasoning that we've been doing so far. Of course, humans reason about logic generally in terms of human language. That I just now was speaking in English, talking in English,
about these sentences and trying to reason through how it is that they relate to one another. We're going to need to be a little bit more formal when we turn our attention to computers and being able to encode this notion of logic, and truthhood and falsehood inside of a machine. So we're going to need to introduce a few more terms and a few symbols that will help us reason through this idea of logic inside of an artificial intelligence. And we'll begin with the idea of a sentence. Now, a sentence in a natural language like English is just something that I'm saying, like what I'm saying right now. In the context of AI though, a sentence is just an assertion about the world in what we're going to call a knowledge representation language, some way of representing knowledge inside of our computers. And the way that we're going to spend most of today, reasoning about knowledge, is through a type of logic known as propositional logic.
There are a number of different types of logic, some of which we'll touch on. But propositional logic is based on a logic of propositions, or just statements about the world. And so we begin in propositional logic with the notion of propositional symbols. We will have certain symbols that are oftentimes just letters, something like P or Q or R, where each of those symbols is going to represent some fact or sentence about the world. So P, for example, might represent the fact that it is raining. And so P is going to be a symbol that represents that idea. And Q, for example, might represent Harry visited Hagrid today. Each of these propositional symbols represents some sentence or some fact about the world. But in addition to just having individual facts about the world, we want some way to connect these propositional symbols together in order to reason more complexly about other facts that might exist inside of the world in which we're reasoning.
So in order to do that, we'll need to introduce some additional symbols that are known as logical connectives. Now, there are a number of these logical connectives, but five of the most important, and the ones we're going to focus on today, are these five up here, each represented by a logical symbol. Not is represented by this symbol here. And is represented as sort of an upside-down V. Or is represented by a V shape. Implication-- and we'll talk about what that means in just a moment-- is represented by an arrow. And biconditional-- again, we'll talk about what that means in a moment-- is represented by these double arrows. But these five logical connectives are the main ones we're going to be focusing on in terms of thinking about how it is that a computer can reason about facts and draw conclusions based on the facts that it knows. But in order to get there we need to take a look at each of these logical connectives and build up an understanding for what
it is that they actually mean. So let's go ahead and begin with the not symbol. So this not symbol here. And what we're going to show for each of these logical connectives is what we're going to call a truth table. A table that demonstrates what this word not means when we attach it to a propositional symbol or any sentence inside of our logical language. And so the truth table for not is shown right here. If P-- some propositional symbol or some other sentence, even-- is false, then not P is true. And if P is true, then not P is false. So you can imagine placing this not symbol in front of some sentence of propositional logic. Just says the opposite of that. So if, for example, P represented it is raining, then not P would represent the idea that it is not raining. And as you might expect, if P is false, meaning if the sentence it is raining is false, well, then the sentence not P must be true. The sentence that it is not raining is, therefore, true.
So not, you can imagine, just takes whatever is in P and it inverts it. It turns false into true, and true into false. Much analogously to what the English word not means. Just taking whatever comes after it and inverting it to mean the opposite. Next up, and also very English-like, is this idea of and, represented by this upside-down V shape, or this point shape. And as opposed to just taking a single argument the way not does-- we have P and we have not P-- and is going to combine two different sentences and propositional logic together. So I might have one sentence P and another sentence Q, and I want to combine them together to say P and Q. And the general logic for what P and Q means is it means that both of its operands are true. P is true, and also, Q is true. And so here's what that truth table looks like. This time we have two variables, P and Q. And when we have two variables, each of which can be in two possible states,
true or false, that leads to two squared, or four, possible combinations of truth and falsehood. So we have P is false and Q is false. We have P is false and Q is true. P is true and Q is false. And then P and Q both are true. And those are the only four possibilities for what P and Q could mean. And in each of those situations, this third column here, P and Q, is telling us a little bit about what it actually means for P and Q to be true. And we see that the only case where P and Q is true is in this fourth row here, where P happens to be true, Q also happens to be true. And in all other situations, P and Q is going to evaluate to false. So this, again, is much in line with what our intuition of and might mean. If I say P and Q, I probably mean that I expect both P and Q to be true. Next up, also potentially consistent with what we mean, is this word or, represented by this V shape, sort of an upside-down and symbol.
And or, as the name might suggest, is true if either of its arguments are true. As long as P is true or Q is true, then P or Q is going to be true. Which means the only time that P or Q is false is if both of its operands are false. If P is false and Q is false, then P or Q is going to be false. But in all other cases, if at least one of the operands is true-- maybe they're both true-- in which case P or Q is going to evaluate to true.
Now, this is mostly consistent with the way that most people might use the word or, in the sense of speaking the word or in normal English. Though there is sometimes when we might say or where we mean P or Q, but not both. Or we mean sort of it can only be one or the other. It's important to note that this symbol here, this or, means P or Q or both. That those are totally OK. As long as either or both of them are true, then the or is going to evaluate to be true as well. It's only in the case where all of the operands are false that P or Q ultimately evaluates to false as well. In logic there's another symbol known as the exclusive or, which encodes this idea of exclusivity of, like, one or the other, but not both. But we're not going to be focusing on that today. Whenever we talk about or, we're always talking about either or both, in this case, as represented by this truth table here. So that now is not, and an, and or.
And next step is what we might call implication, as denoted by this arrow symbol. So we have P and Q. And this sentence here will generally read as P implies Q. And what P implies Q means is that if P is true, then Q is also true. So I might say something like, if it is raining, then I will be indoors. Meaning it is raining implies I will be indoors is the logical sentence that I'm saying there. And the truth table for this can sometimes be a little bit tricky.
So obviously, if P is true and Q is true, then P implies Q, that's true. That definitely makes sense. And it should also stand to reason that when P is true and Q is false, then P implies Q is false. Because if I said to you, if it is raining, then I will be indoors, and it is raining, but I'm not indoors, well, then it would seem to be that my original statement was not true. P implies Q means that if P is true, then Q also needs to be true. And if it's not, well, then the statement is false.
Also worth noting, though, is what happens when P is false. When P is false, the implication makes no claim at all. If I say something like, if it is raining, then I will be indoors, and it turns out it's not raining, then in that case, I am not making any statement as to whether or not I will be indoors or not. P implies Q just means that if P is true, Q must be true. But if P is not true, then we make no claim about whether or not Q is true at all. So in either case, if P is false, it doesn't matter what Q is, whether it's false or true. We're not making any claim about Q whatsoever. We can still evaluate the implication to true. The only way that the implication is ever false is if our premise, P is true, but the conclusion that we're drawing, Q, happens to be false. So in that case, we would say P does not imply Q in that case. Finally, the last connective that we'll discuss is this biconditional.
You can think of a biconditional as a condition that goes in both directions. So originally, when I said something like, if it is raining, then I will be indoors. I didn't say what would happen if it wasn't raining. Maybe I'll be indoors, maybe I'll be outdoors. This biconditional you can read as an if and only if. So I can say, I will be indoors if and only if it is raining. Meaning if it is raining, then I will be indoors. And if I am indoors, it's reasonable to conclude that it is also raining. So this biconditional is only true when P and Q are the same. So if P is true and Q is true, then this biconditional is also true-- P implies Q. But also the reverse is true. Q also implies P. So if P and Q both happen to be false, we would still say it's true. But in any of these other two situations, this P if and only if Q is going to ultimately evaluate to false. So a lot of trues and falses going on there,
but these five basic logical connectives are going to form the core of the language of propositional logic, the language that we're going to use in order to describe ideas, and the language that we're going to use in order to reason about those ideas in order to draw conclusions. So let's now take a look at some of the additional terms that we'll need to know about in order to go about trying to form this language of propositional logic, and writing AI that's actually able to understand this sort of logic. The next thing we're going to need is the notion of what is actually true about the world. We have a whole bunch of propositional symbols-- P and Q and R and maybe others. But we need some way of knowing, like, what actually is true in the world. Is P true or false, is Q true or false, so on and so forth. And to do that, we'll introduce the notion of a model.
A model just assigns a truth value where a truth value is either true or false to every propositional symbol. In other words, it's creating what we might call a possible world. So let me give an example. If, for example, I have two propositional symbols, P is it is raining, and Q is it is a Tuesday, a model just takes each of these two symbols and assigns a truth value to them, either true or false. So here's a sample model. In this model, in other words, in this possible world, it is possible that P is true, meaning it is raining, and Q is false, meaning it is not a Tuesday. But there are other possible worlds or other models as well. There is some model where both of these variables are true. Some model where both of these variables are false. In fact, if there are N variables that are propositional symbols like this, that are either true or false, then the number of possible models is two to the N, because each of these possible models--
possible variables within my model could be set to either true or false, if I don't know any information about it. So now that I have the symbols-- the symbols and the connectives that I'm going to need in order to construct these parts of knowledge, we need some way to represent that knowledge. And to do so, we're going to allow our AI access to what we'll call a knowledge base. And a knowledge base is really just a set of sentences that our AI knows to be true. Some set of sentences in propositional logic that are things that our AI knows about the world. And so we might tell our AI some information, information about a situation that it finds itself in, or situation about a problem that it happens to be trying to solve. And we would give that information to the AI, that the AI would store inside of its knowledge base. And what happens next is the AI would like to use that information in the knowledge base
to be able to draw conclusions about the rest of the world. And what do those conclusions look like? Well, to understand those conclusions, we'll need to introduce one more idea, one more symbol, and that is the notion of entailment. So this sentence here, with this double turnstile and these Greek letters-- this is the Greek letter alpha and the Greek letter beta-- and we read this as alpha entails beta. And alpha and beta here are just sentences in propositional logic. And what this means is that alpha entails beta means that in every model, in other words, in every possible world in which sentence a is true-- or sentence alpha is true, then sentence beta is also true. So if something entails something else, if alpha entails beta, it means that if I know alpha to be true, then beta must, therefore, also be true. So if my alpha is something like, I know that it is a Tuesday in January, then a reasonable beta might be something like, I know that it is January.
Because in all worlds, where it is a Tuesday in January, I know for sure that it must be January, just by definition. This first statement, or sentence about the world, entails the second statement. And we can reasonably use deduction, based on that first sentence, to figure out that the second sentence is, in fact, true as well. And ultimately, it's this idea of entailment that we're going to try and encode into our computer. We want our AI agent to be able to figure out what the possible entailments are. We want our AI to be able to take these three sentences, sentences like, if it didn't rain, Harry visited Hagrid. That Harry visited Hagrid or Dumbledore but not both. And that Harry visited Dumbledore. And just using that information, we'd like our AI to be able to infer, or figure out, that using these three sentences inside of a knowledge base, we can draw some conclusions. In particular, we can draw the conclusions here
that one, Harry did not visit Hagrid today. And we can draw the entailment two, that it did, in fact, rain today. And this process is known as inference. And that's what we're going to be focusing on today, this process of deriving new sentences from old ones. That I give you these three sentences, you put them in the knowledge base in, say, the AI, and the AI is able to use some sort of inference algorithm to figure out that these two sentences must also be true. And that is how we define inference. So let's take a look at an inference example to see how we might actually go about inferring things in a human sense, before we take a more algorithmic approach to see how we could encode this idea of inference in AI. And we'll see there are a number of ways that we can actually achieve this. So again, we'll deal with a couple of propositional symbols. We'll deal with P, Q and R. P is it is a Tuesday. Q is it is raining. And R is Harry will go for a run.
Three propositional symbols that we are just defining to mean this. We're not saying anything yet about whether they're true or false. We're just defining what they are. Now we'll give ourselves, or an AI, access to a knowledge base, abbreviated to KB, to knowledge that we know about the world. We know this statement. All right, so let's try to parse it. The parentheses here are just used for precedent, so we can see what associates with what. But you would read this as P and not Q implies R.
All right, so what does that mean? Let's put it piece by piece. P is it is a Tuesday. Q is it is raining. So not Q is it is not raining. And implies R is Harry will go for a run. So the way to read this entire sentence in human natural language at least, is if it is a Tuesday and it is not raining, then Harry will go for a run. So if it is a Tuesday and it is not raining, then Harry will go for a run. And that is now inside of our knowledge base.
And let's now imagine that our knowledge base has two other pieces of information as well. It has information that P is true, that it is a Tuesday. And we also have the information not Q, that it is not raining. That this sentence Q, it is raining, happens to be false. And those are the three sentences that we have access to. P and not Q implies R, P and not Q. Using that information, we should be able to draw some inferences. P and not Q is only true if both P and not Q are true.
Well, all right, we know that P is true. And we know that not Q is true. So we know that this whole expression is true. And the definition of implication is if this whole thing on the left is true, then this thing on the right must also be true. So if we know that P and not Q is true, then R must be true as well. So the inference we should be able to draw from all of this is that R is true, and we know that Harry will go for a run, by taking this knowledge inside of our knowledge base and being able to reason based on that idea. And so this ultimately, is the beginning of what we might consider to be some sort of inference algorithm. Some process that we can use to try and figure out whether or not we can draw some conclusion. And ultimately, what these inference algorithms are going to answer is the central question about entailment. Given some query about the world, something
we're wondering about the world, and we'll call that query alpha, the question we want to ask, using these inference algorithms, is does it KB, our knowledge base, entail alpha? In other words, using only the information we know inside of our knowledge base, the knowledge that we have access to, can we conclude that this sentence alpha is true? And that's ultimately what we would like to do. So how can we do that? How can we go about writing an algorithm that can look at this knowledge base and figure out whether or not this query alpha is actually true? Well, it turns out there are a couple of different algorithms for doing so. And one of the simplest, perhaps, is known as model checking. Now remember, that a model is just some assignment of all of the propositional symbols inside of our language to a truth value, true or false. And you can think of a model as a possible world. That there are many possible worlds where different things might be true or false.
And we can enumerate all of them. And the model checking algorithm does exactly that. So what does our model tracking algorithm do? Well, if we wanted to determine if our knowledge base entails some query alpha, then we are going to enumerate all possible models. In other words, consider all possible values of true and false for our variables. All possible states in which our world can be in. And if in every model where our knowledge base is true, alpha is also true, then we know that the knowledge base entails alpha. So let's take a closer look at that sentence and try and figure out what it actually means. If we know that in every model, in other words, in every possible world, no matter what assignment of true and false to variables you give, if we know that whenever our knowledge is true-- what we know to be true is true-- that this query alpha is also true. Well, then it stands to reason that as long as our knowledge base is true,
then alpha must also be true. And so this is going to form the foundation of our model checking algorithm. We're going to enumerate all of the possible worlds, and ask ourselves, whenever the knowledge base is true, is alpha true? And if that's the case, then we know alpha to be true. And otherwise, there is no entailment. Our knowledge base does not entail alpha. All right, so this is a little bit abstract. But let's take a look at an example to try and put real propositional symbols to this idea. So again, we'll work with the same example. P is it is a Tuesday. Q is it is raining. R is Harry will go for a run. Our knowledge base contains these pieces of information. P and not Q implies R. We also know P, it is a Tuesday. And not Q, it is not raining. And our query, our alpha in this case, the thing we want to ask is R. We want to know, is it guaranteed? Is it entailed that Harry will go for a run. So the first step is to enumerate all of the possible models.
We have three propositional symbols here, P, Q, and R, which means we have 2 to the third power, or 8 possible models. All false. False false true. False true false. False true true. Et cetera. Eight possible ways you could assign true and false to all of these models. And we might ask in each one of them, is the knowledge base true? Here are the set of things that we know. In which of these worlds could this knowledge base possibly apply to? In which world is this knowledge base true?
Well, in the knowledge base, for example, we know P. Like, we know it is a Tuesday. Which means we know that these four-- first four rows-- where P is false, none of those are going to be true, are going to work for this particular knowledge base. Our knowledge base is not true in those worlds. Likewise, we also know not Q. We know that it is not raining. So any of these models where Q is true, like these two and these two here, those aren't going to work either because we know that Q is not true. And finally, we also know that P and not Q implies R. Which means that when P is true-- where P is true here-- and Q is false-- Q is false in these two-- then R must be true. And if ever P is true, Q is false, but R is also false, well, that doesn't satisfy this implication here. That implication does not hold true under those situations. So we could say that for our knowledge base,
we can conclude under which of these possible worlds is our knowledge base true, and under which of the possible worlds is our knowledge base false. And it turns out there is only one possible world where our knowledge base is actually true. In some cases, there might be multiple possible worlds where the knowledge base is true. But in this case it just so happens that there's only one. One possible world where we can definitively say something about our knowledge base. And in this case, we would look at the query. The query of R. Is R true? R is true. And so as a result, we can draw that conclusion. And so this is this idea of model checking. Enumerate all the possible models, and look in those possible models to see whether or not if our knowledge base is true, is the query in question true as well. So let's now take a look at how we might actually go about writing this in a programming language like Python. Take a look at some actual code that would
encode this notion of propositional symbols, and logic, and these connectives, like and, and or, and not an implication, and so forth, and see what that code might actually look like. So I've written in advance a logic library. It's more detailed than we need to worry about entirely today. But the important thing is that we have one class for every type of logical symbol, or connective, that we might have. So we just have one class for logical symbols, for example, where every symbol is going to represent and store some name for that particular symbol. And we also have a class for not, that takes an operand. So we might say not one symbol to say something is not true, or some other sentence is not true. We have one for and, one for or, so on and so forth. And I'll just demonstrate how this works. And you can take a look at the actual logic.py later on. But go ahead and call this file, Harry.py.
We're going to store information about this world of Harry Potter, for example. So I'll go ahead and import from my logic module. I'll import everything. And in this library, in order to create a symbol, you use capital S symbol. And I'll create a symbol for rain to mean it is raining, for example. And I'll create a symbol for Hagrid to mean Harry visited Hagrid is what the symbol is going to mean. So this symbol means it is raining. This symbol means Harry visited Hagrid.
And I'll add another symbol called Dumbledore for Harry visited Dumbledore. Now I'd like to save these symbols so that I can use them later, as I do some logical analysis. So I'll go ahead and save each one of them inside of a variable. So, like, rain, Hagrid, and Dumbledore, that you could call the variables anything. And now that I have these logical symbols, I can use logical connectives to combine them together. So for example, if I have a sentence like, and rain and Hagrid, for example--
which is not necessarily true, but just for demonstration-- I can now try and print out sentence.formula, which is a function I wrote that takes a sentence and propositional logic and just prints it out so that we, the programmers, can now see this in order to get an understanding for how it actually works. So if I run Python Harry.py, what we'll see is this sentence and propositional logic, rain and Hagrid. This is the logical representation of what we have here in our Python program of saying and, whose arguments are rain and Hagrid. So we're saying rain and Hagrid by encoding that idea. And this is quite common in Python object-oriented programming where you have a number of different classes, and you pass arguments into them in order to create a new and object, for example, in order to represent this idea. But now what I'd like to do is somehow encode the knowledge that I have about the world, in order to solve that problem from the beginning of class,
where we talked about trying to figure out who Harry visited, and trying to figure out if it's raining or if it's not raining. And so what knowledge do I have? I'll go ahead and create a new variable called knowledge. And what do I know? Well, I know the very first sentence that we talked about was the idea that if it is not raining, then Harry will visit Hagrid. All right. How do I encode the idea that it is not raining? Well, I can use not and then the rain symbol. So here's me saying that it is not raining. And now the implication is that if it is not raining, then Harry visited Hagrid. So I'll wrap this inside of an implication to say, if it is not raining, this first argument to the implication, well, then Harry visited Hagrid. So I'm saying implication, the premise is that it's not raining. And if it is not raining, then Harry visited Hagrid. And I can print out knowledge.formula to see the logical formula equivalent of that same idea.
So I run Python of Harry.py, and this is the logical formula that we see as a result, which is a text-based version of what we were looking at before. That if it is not raining, then that implies that Harry visited Hagrid. But there was additional information that we had access to as well. In this case, we had access to the fact that Harry visited either Hagrid or Dumbledore. So how do I encode that? Well, this means that in my knowledge, I've really got multiple pieces of knowledge going on. I know one thing, and another thing, and another thing. So I'll go ahead and wrap all of my knowledge inside of an and. And I'll move things on a new line just for good measure. But I know multiple things. So I'm saying knowledge is an and of multiple different sentences. I know multiple different sentences to be true. One such sentence that I know to be true is this implication that if it is not raining, then Harry visited Hagrid.
Another such sentence that I know to be true is or Hagrid Dumbledore. In other words, so Hagrid or Dumbledore is true, because I know that Harry visited Hagrid or Dumbledore. But I know more than that, actually. That initial sentence from before said that Harry visited Hagrid or Dumbledore, but not both. So now I want a sentence that'll encode the idea that Harry didn't visit both Hagrid and Dumbledore. Well, the notion of Harry visiting Hagrid and Dumbledore would be represented like this.
And of Hagrid and Dumbledore. And if that is not true, I want to say not that, then I'll just wrap this whole thing inside of a not. So now these three lines, line 8 says that if it is not raining, then Harry visited Hagrid. Line 9 says Harry visited Hagrid or Dumbledore. And line 10 says Harry didn't visit both Hagrid and Dumbledore. That it is not true that both the Hagrid symbol and the Dumbledore symbol are true. Only one of them can be true.
And finally, the last piece of information that I knew was the fact that Harry visited Dumbledore. So these now are the pieces of knowledge that I know. One sentence, and another sentence, and another, and another. And I can print out what I know, just to see it a little bit more visually. And here now is a logical representation of the information that my computer is now internally representing using these various different Python objects. And again, take a look at logic.py if you want to take a look at how exactly it's implementing this. But no need to worry too much about all of the details there. We're here saying that if it is not raining, then Harry visited Hagrid. We're saying that Hagrid or Dumbledore is true. And we're saying it is not the case that Hagrid and Dumbledore is true. That they're not both true. And we also know that Dumbledore is true. So this long, logical sentence represents our knowledge base.
It is the thing that we know. And now what we like to do is we like to use model checking to ask a query. To ask a question like, based on this information, do I know whether or not it's raining? And we, as humans, we're able to logic our way through it and figure out that, all right, based on these sentences we can conclude this and that to figure out that yes, it must have been raining. But now we'd like for the computer to do that as well. So let's take a look at the model checking algorithm that is going to follow that same pattern that we drew out in pseudocode a moment ago. So I've defined a function here in logic.py that you can take a look at called model check. Model check takes two arguments, the knowledge that I already know and the query. And the idea is, in order to do model checking, I need to enumerate all of the possible models. And for each of the possible models, I need to ask myself,
is the knowledge base true, and is the query true? So the first thing I need to do is somehow enumerate all of the possible models. Meaning for all possible symbols that exist, I need to assign true and false to each one of them and see whether or not it's still true. And so here is the way we're going to do that. We're going to start-- so I've defined another helper function internally that we'll get to in just a moment. But this function starts by getting all of the symbols, in both the knowledge and the query, by figuring out what symbols am I dealing with? In this case, the symbols I'm dealing with are rain, and Hagrid, and Dumbledore. But there might be other symbols, depending on the problem. And we'll take a look soon at some examples of situations where ultimately, we're going to need some additional symbols in order to represent the problem. And then we're going to run this check all function, which
is a helper function that's basically going to recursively call itself, checking every possible configuration of propositional symbols. So we start out by looking at this check all function, and what do we do? So if not symbols means if we finish assigning all of the symbols. We've assigned every symbol of value. So far we haven't done that, but if we ever do, then we check. In this model, is the knowledge true? That's what this line is saying. If we evaluate the knowledge, propositional logic formula, using the models assignment of truth values, is the knowledge true? If the knowledge is true, then we should return true, only if the query is true. Because if the knowledge is true, we want the query to be true as well, in order for there to be entailment. Otherwise, we don't know that there-- otherwise, there won't be an entailment. If there's ever a situation where what we know in our knowledge is true,
but the query, the thing we're asking, happens to be false. So this line here is checking that same idea. That in all worlds where the knowledge is true, the query must also be true. Otherwise, we can just return true, because if the knowledge isn't true, then we don't care. This is equivalent to when we were enumerating this table from a moment ago. In all situations where the knowledge base wasn't true-- all of these seven rows here-- we didn't care whether or not our query was true or not. We only care to check whether the query is true when the knowledge base is actually true, which was just this green highlighted row right there. So that logic is encoded using that statement there. And otherwise, if we haven't assigned symbols yet, which we haven't seen anything yet, then the first thing we do is pop one of the symbols. I make a copy of the symbols first, just to save an existing copy. But I pop one symbol off of the remaining symbols, so that I just pick one symbol at random.
And I create one copy of the model where that symbol is true, and I create a second copy of the model where that symbol is false. So I now have two copies of the model. One where the symbol is true, and one where the symbol is false. And I need to make sure that this entailment holds in both of those models. So I recursively check all on the model where the statement is true, and check all on the model where the statement is false. So again, you can take a look at that function to try to get a sense for how exactly this logic is working. But in effect, what it's doing is recursively calling this check all function again and again and again. And on every level of the recursion we're saying, let's pick a new symbol that we haven't yet assigned. Assign it to true. And assign it to false. And then check to make sure that the entailment holds in both cases. Because ultimately, I need to check every possible world. I need to take every combination of symbols
and try every combination of true and false in order to figure out whether the entailment relation actually holds. So that function we've written for you. But in order to use that function inside of Harry.py, what I'll write is something like this. I would like to model check, based on the knowledge, and then I provide, as a second argument, what the query is. What the thing I want to ask is. And what I want to ask in this case is, is it raining? So model check, again, takes two arguments. The first argument is the information that I know. This knowledge. Which in this case, is this information that was given to me at the beginning. And the second argument, rain, is encoding the idea of the query. What am I asking? I would like to ask, based on this knowledge, do I know for sure that it is raining? And I can try and print out the result of that. And when I run this program, I see that the answer is true. That based on this information, I can conclusively say that it is raining.
Because using this model checking algorithm, we were able to check that in every world where this knowledge is true, it is raining. In other words, there's no world where this knowledge is true and it is not raining. So you can conclude that it is, in fact, raining. And this sort of logic can be applied to a number of different types of problems. That if confronted with a problem where some sort of logical deduction can be used in order to try to solve it, you might try thinking about what propositional symbols you might need in order to represent that information. And what statements and propositional logic you might use in order to encode that information which you know. And this process of trying to take a problem and figure out what propositional symbols to use in order to encode that idea, or how to represent it logically is known as knowledge engineering. That software engineers and AI engineers will take a problem

Ask anything
We've read this whole study set, so questions about it get the best answers.