Chapter 5 - Satisfying constraints Flashcards
Generate and test
a value is generated for a variable and then tested to see if the value is the desired one.
“Root” of constraint satisfaction problems.
Constraint satisfaction problems are made up of 3 elements:
- number of variables for which value are to be found
- each variable gets a value from some finite domain
- there are constraints to be satisfied among subsets of variables
A “solution” is…
an assignment of a value to each variable such that all the constraints are satisfied
Steps to solve a constraint satisfaction problem…
- generate values
- test to see if all constraints are satisfied
- backtrack as necessary
Example of constraint satisfaction problem:
solution(Variable1,Variable2,. . .,VariableN):-
domain1(Variable1),…domainN(VariableN),
constraint1(Variable,…,Variable),…
constraintM(Variable,…,Variable).
Map coloring:
solution(A,B,C,D,E) :-
color(A), color(B), color(C), color(D), color(E),
+ A=B, + A=C, + A=D, + A=E, + B=C, + C=D, + D=E.
color(red).
color(white).
color(blue).
What does the predicate write(argument) do?
It is used in KB to personalize the output.
When a query will be called (the head of the if-statement in the KB), the output will be given according to what was specified in the write() predicate in the body of the if-statement. (see page 90 of the Lbook for example)
nl
new line
Anonymus variable
used in problems where the goal is not finding the value, but ensuring there is one.
Example:
child(_, Sue) –> does Sue have a child?
Not important WHICH child, important is she has a child.
uniq() predicate
takes n arguments and ensures that they are all different from each other.
num() predicate
can test or generate the numbers from X to Y that are unique
Try to explain what happens in figure 5.5 at page 93 of the book - Sudoku problem-
How does this solution predicate work? It needs to generate sixteen values. It starts by using uniq(R11,R12,R13,R14) to generate four distinct values for the variables of row 1. Then it does the same for rows 2, 3, and 4. At this point, all sixteen variables have values. It then tests uniq(R11,R21,R31,R41), which is for the variables of column 1. If these four values are distinct, it continues and tests the distinctness of columns 2, 3, and 4; if they are not distinct, the test fails, and it backtracks to generate new values for the variables in row 4 or 3 or 2 or perhaps all the way back to row 1, as necessary. Once all the column tests succeed, the solution predicate tests uniq(R11,R12,R21,R22), which is for the variables of the NW quadrant. If these four values are distinct, it tests the quadrants NE, SW, and SE in turn; if they are not distinct, it again backtracks all the way to the rows and tries to generate new values.If all the column and quadrant tests are successful, a solution has been found, and the program is done.
Number in Prolog
sequence of one or more digits: they can appear anywhere a constant can appear.
Arithmetic realtions in Prolog:
less than: < greater than: > less than or equal to: <= greater than or equal to >= equal =:= equal : Variable is E
Must variables in arithmetic relations be already instantiated?
Yes, unless we are using the “is” operation: in that case, we have to make sure that the query is in the following format:
- LHS: variables with a term such as X = 4 and the variable we want to calculate, say Y, WITH NO VALUE IN THE LHS: additionally, the “Y is..”
RHS; arithmetic expression, that defines Y, such as X + 4
Example:
(Y=2, X is) (Y+4.)
X=6, Y=2
Yes
NB: the parentheses are not part of the code, they were put there by me to highlight the LHS and RHS.
Is
q(Y), p(Y+4, Z) a legal query?
No, because Y+4 is not a legal term