Read a sample: 1. Meet Your Meal-Choosing Program
It is nearly time to eat, and you have a familiar problem: you know a few things about what you want, but you do not know what to make.
Perhaps you want a vegetarian meal. Perhaps you have limited time. Perhaps you would rather not eat rice again. A friend could listen to those preferences, consider a few possibilities, and suggest dinner. A small computer program can do something similar—provided we tell it what choices it can consider and how to decide among them.
In this book, you will build that program. It will ask for a few preferences, follow rules that you write, and recommend a meal when its small menu contains a suitable choice. When the stated requirements cannot be met by any supported meal, it will say so instead of pretending otherwise.
The finished project will be modest. It will not inspect your refrigerator, browse restaurant menus, or invent recipes. It will not know what you enjoyed yesterday unless you supply that information and add rules for using it. Its value is that you can see how a decision is made, test whether the decision fits the rules, and improve those rules when they fall short.
That makes the meal selector a useful first step into rule-based artificial intelligence. You do not need to understand Python, programming, or AI terminology before starting. We will build the ideas gradually, beginning with the decision itself.
Imagine a version of the finished program offering three meals:
- Bean tacos
- Vegetable stir-fry with rice
- Chicken and rice
For this preview, suppose the program knows a few simple facts about those choices. Bean tacos and vegetable stir-fry are vegetarian; chicken and rice is not. The tacos and chicken and rice are marked as quick options, while the stir-fry is not. Both rice dishes contain rice; the tacos do not.
These are labels for our hypothetical menu, not claims about every possible recipe. Actual preparation time and ingredients depend on how a meal is made. Later, we will choose and document a fixed set of meals and attributes for the version we build.
A conversation with the selector might look like this:
> Program: Do you require a vegetarian meal?
> You: Yes
> Program: Do you require a quick meal?
> You: Yes
> Program: Would you prefer to avoid rice?
> You: Yes
> Program: Recommendation: Bean tacos
> Program: Why: They meet your vegetarian and quick-meal requirements and match your preference to avoid rice.
The program has not tasted the tacos or judged them delicious. It has compared the answers with the information and rules available to it. In this example, bean tacos satisfy both requirements and also fit the stated preference.
Notice the difference between a requirement and a preference. A requirement is a condition the recommendation must meet. A preference is something the user would like when possible, but which the program may treat as flexible. The program should make that distinction clear rather than quietly deciding that a requirement no longer matters.
For instance, imagine the user requires a vegetarian meal, requires a quick meal, and says rice is fine. Under our preview labels, bean tacos still qualify. But if none of the supported meals met both requirements, the appropriate response would be a no-match message—not a recommendation that ignores one of the answers.
A no-match result is still a result. It tells the user something useful about the program’s limited menu: given the stated conditions and the information available, this selector has no suitable recommendation to offer.
A rule is an instruction that connects a condition to an action. In everyday language, it often has this shape:
> If a condition is true, do something.
You use rules like this without thinking much about them. If rain is expected, take an umbrella. If the library is closed, return the book tomorrow. A meal selector can use the same basic structure:
> If the user requires a vegetarian meal, consider only supported meals marked vegetarian.
Another rule might say:
> If more than one eligible meal remains and the user prefers to avoid rice, favor an eligible meal marked as containing no rice.
The second rule matters because it does not turn a preference into a requirement. It helps choose among meals that have already met the requirements. If avoiding rice is only a preference, the selector may still suggest an eligible rice dish when no eligible rice-free dish is available. Its explanation should make that trade-off visible.
A collection of rules, together with the information those rules use, forms a rule-based system. The system receives information, checks conditions, and produces an outcome according to explicitly stated instructions.
The word explicitly is important. A human might say, “Pick something that sounds good,” and expect another person to fill in the gaps. A computer program needs a more precise account of what information to check and what to do with it. “Sounds good” could mean quick, familiar, inexpensive, spicy, or simply appealing at that moment. Unless we define a way to represent and use one of those ideas, the program cannot reliably act on it.
Precision does not require complicated language. A clear rule can be short. The challenge is making sure that all the short rules work together.
Let us look more closely at the first conversation. The answers were:
- Vegetarian required: yes -…