foundations of computational agents
An algebraic variable, or variable, is used to name a feature. The domain of variable , written , is the set of values the variable can take.
Symbols and Semantics
Algebraic variables are symbols.
Internal to a computer, a symbol is just a sequence of bits that is distinguished from other symbols. In a program we use constants to denote symbols. Some symbols have a fixed interpretation; for example, symbols that represent numbers and symbols that represent characters are predefined in most computer languages. Symbols with a user-defined meaning, but without a predefined meaning in the language, can be defined in many programming languages. Lisp refers to them as atoms. Python 3.4 introduced a symbol type called , but Python’s strings are often used as symbols. Usually, symbols are implemented as indexes into a symbol table that gives the name to print out. The only operation performed on these symbols is equality, to determine whether two symbols are the same. This can be implemented by comparing the indexes in the symbol table.
To users of a computer, symbols can have meanings. A person who inputs constraints or interprets the output of a program associates meanings with the symbols making up the constraints or the outputs. They associate a symbol with some concept or object in the world. For example, the variable , to the computer, is just a sequence of bits. It has no relationship to or . To a person, this variable may mean the height, in particular units, of a particular person at a particular time.
The meaning associated with a variable–value pair must obey the clarity principle: an omniscient agent – a fictitious agent who knows the truth and the meanings associated with all of the symbols – should be able to determine the value of each variable. For example, the height of Hagrid only satisfies the clarity principle if the particular person being referred to and the particular time are specified as well as the units. For example, one may want to reason about the height, in centimeters, of Hagrid in a particular scene at the start of the second Harry Potter movie. This is different from the height, in inches, of Hagrid at the end of the third movie (although they are, of course, related). To refer to Hagrid’s height at two different times, you need two variables.
You should have a consistent meaning for any symbols you use. When stating constraints, you must have the same meaning for the same variable and the same values, and you can use this meaning to interpret the output.
The bottom line is that symbols have meanings because you give them meanings. For this chapter, assume that the computer does not know what the symbols mean. A computer may know what a symbol means if it perceives and manipulates the environment.
A discrete variable is one whose domain is finite or countably infinite. A binary variable is a discrete variable with two values in its domain. One particular case of a binary variable is a Boolean variable, which is a variable with domain , . We can also have variables that are not discrete; for example, a variable whose domain is the real numbers or a range of the real numbers is a continuous variable.
Given a set of variables, an assignment on the set of variables is a function from the variables into the domains of the variables. We write an assignment on as , where is in . This assignment specifies that, for each , variable is assigned value . A variable can only be assigned one value in an assignment.
A total assignment assigns a value to every variable.
The variable may denote the starting time for a particular class. The domain of may be the following set of possible times:
The variable may refer to the height of a particular person, Joe, at a particular time and have as its domain the set of real numbers, in some range, that represent Joe’s height in centimeters. may be a random variable with domain , , which has value if it is raining at a particular time.
The assignment , , means the class starts at 11, Joe is 165 cm tall, and it is not raining.
In the electrical environment of Figure 1.6, there may be a variable for the position of each switch that specifies whether the switch is up or down. There may be a variable for each light that specifies whether it is lit or not. There may be a variable for each component specifying whether it is working properly or if it is broken. Some variables that the following examples use include:
is a binary variable denoting the position of switch with domain , , where means switch is up and means switch is down.
is a discrete variable denoting the status of switch with domain , where means switch is working normally, means it is installed upside down, means it is shorted and it allows electricity to flow whether it is up or down, means it is working intermittently, and means it is broken and does not allow electricity to flow.
is an integer-valued variable denoting the number of switches that are broken.
is a real-valued variable denoting the current, in amps, flowing through wire . means there are amps flowing through wire . Inequalities between variables and constants form Boolean conditions; for example, is true when there are at least amps flowing through wire .
A total assignment specifies the position of every switch, the status of every device, and so on. For example, an assignment may be switch 1 is up, switch 2 is down, fuse 1 is okay, wire 3 is broken, etc.
Crossword puzzles, a popular form of recreation, involve filling in squares on a grid to make words that fit clues. There are two different representations of crossword puzzles in terms of variables:
In one representation, the variables are the numbered squares with the direction of the word (down or across) and the domains are the set of possible words that can be used. For example, one_across could be a variable with domain “ant”, “big”, “bus”, “car”, “has”. A total assignment gives a word for each of the variables.
In another representation of a crossword, the variables are the individual squares and the domain of each variable is the set of letters in the alphabet. For example, the top-left square could be a variable with domain . A total assignment gives a letter to each square.
A trading agent, in planning a trip for a group of tourists, may be required to schedule a given set of activities. There could be two variables for each activity: one for the time, for which the domain is the set of possible times or days for the activity, and one for the location, for which the domain is the set of possible locations where it may occur. A total assignment gives a time and location for each activity.
An alternative representation may have the times as the variables (e.g., each hour for each day), with domains the set of possible activity–location pairs.
The number of total assignments is the product of the number of values in the domains of the variables.
If there are two variables, with domain and with domain , there are six total assignments, which we name as follows”
If there are variables, each with domain size , there are total assignments.
One main advantage of reasoning in terms of variables is computational saving. Consider deciding whether to model in terms of states explicitly or to model the states in terms of binary variables. Many states can be described by a few variables:
10 binary variables can describe states
20 binary variables can describe states
30 binary variables can describe states
100 binary variables can describe , states.
Reasoning in terms of thirty variables may be easier than reasoning in terms of more than a billion states. One hundred variables is not that many, but reasoning in terms of more than states explicitly is not possible. Many real-world problems have thousands, if not millions, of variables.
In many applications, not all possible assignments of values to variables are permissible. A hard constraint, or simply constraint, specifies legal combinations of assignments of values to some of the variables. The set of variables involved in the constraint is the scope of the constraint. A constraint specifies a condition on these variables that is true or false for each assignment to the variables in the scope.
A unary constraint is a constraint on a single variable (e.g., ). A binary constraint is a constraint over a pair of variables (e.g., ). In general, a -ary constraint has a scope of size . For example, is a 3-ary (ternary) constraint.
A constraint can be evaluated in an assignment that assigns a superset of the variables in the scope. The extra variables are ignored. For example, is true of the assignment .
Assignment satisfies constraint if assigns the variables in the scope of and the condition of evaluates to true for restricted to the scope of . Assignment violates constraint if assigns the variables in the scope of and the condition of evaluates to false for that assignment.
If an assignment satisfies a constraint, then any assignment that is a superset of also satisfies the constraint.
Suppose a robot needs to schedule a set of activities for a manufacturing process, involving casting, milling, drilling, and bolting. Each activity has a set of possible times at which it may start. The robot has to satisfy various constraints arising from prerequisite requirements and resource use limitations. For each activity there is a variable that represents the time that it starts. For example, it could use to represent the start time for the drilling, the start time of the bolting, and the start time for the casting. Drilling must start before bolting corresponds to the constraint . Casting and drilling must not start at the same time corresponds to the constraint . Bolting must start 3 time units after casting starts corresponds to the constraint .
Constraints are defined either by their intension, in terms of formulas, or by their extension, listing all the assignments that are true. Constraints defined extensionally can be seen as relations of legal assignments as in relational databases.
Consider a constraint on the possible dates for three activities. Let , , and be variables that represent the date of each activity. Suppose the domain of each variable is .
A constraint with scope could be described by its intension, using a logical formula to specify the legal assignments, such as
where means and and means not. This formula says that is on the same date or before , and is before day , is before , and it cannot be that and are on the same date when is on or before day 3.
The extensional definition of this constraint is defined using the following table specifying the legal assignments:
2 | 2 | 4 |
1 | 1 | 4 |
1 | 2 | 3 |
1 | 2 | 4 |
The first assignment is , which assigns the value 2, the value 2, and the value 4. There are four legal assignments of the variables.
The assignment satisfies this constraint because when restricted to the scope of the relation, namely , it is one of the legal assignments in the table.
Consider the constraints for the two representations of crossword puzzles of Example 4.3:
For the representation in which the domains are words, the constraint is that the letters where a pair of words intersect must be the same.
For the representation in which the domains are letters, the constraint is that each contiguous sequence of letters must form a legal word.
A constraint satisfaction problem (CSP) consists of:
a set of variables
a domain for each variable
a set of constraints.
A solution is a total assignment that satisfies all of the constraints.
Suppose the delivery robot must carry out a number of delivery activities, , , , , and . Suppose that each activity happens at any of times , , , or . Let be the variable representing the time that activity will occur, and similarly for the other activities. The variable domains, which represent possible times for each of the deliveries, are
Suppose the following constraints must be satisfied:
It is instructive for you to try to find a solution for this example; try to assign a value to each variable that satisfies these constraints.
Given a CSP, a number of tasks are useful:
determine whether or not there is a solution
find a solution when there is one
count the number of solutions
enumerate all of the solutions
find a best solution, given a measure of how good solutions are
determine whether some statement holds in all solutions.
The multidimensional aspect of CSPs, where each variable is a separate dimension, makes these tasks difficult to solve, but also provides structure that can be exploited.
CSPs are very common, so it is worth trying to find relatively efficient ways to solve them. Determining whether there is a solution for a CSP with finite domains is NP-complete (see box) and no known algorithms exist to solve such problems that do not use exponential time in the worst case. However, just because a problem is NP-complete does not mean that all instances are difficult to solve. Many instances have structure to exploit.