Artificial Intelligence Tutorial 7 - Answers
Artificial Intelligence Tutorial 7 - Answers
1. Suppose you have a variable, x, which can range over the numbers 1 to ! an" a variable, y,
which can range over the numbers # to !.
a$ %xpress this constraint on x formally& ' ( x ) 17
b$ *ow "oes this alter the "omain of x+
1a$ ,eally, this constraint shoul" be seen as -ust a statement of x.s "omain, but to write it as a
constraint formally, we write it is as a set of tuples of values which the variable can ta/e. So in this
case, the constraint woul" be&
0
x
1 231!$,311$,31$,314$,315$,31#$,316$,317$7
because x can ta/e any value greater than ' but less than or e8ual to 17. 9ote that the values are
expresse" as 31!$, 311$, etc., rather than -ust 1!, 11, etc., because constraints ta/e on the form of sets
of tuples of values. It -ust so happens that, in this case, the tuples contain only a single element.
1b$ The "omain of x becomes 21!,11,1,14,15,1#,16,177.
c$ Ignoring the constraint in part a$ an" reverting bac/ to the "omain for x being 1 to !, express the
following constraint on x an" y formally& x : y 1 17.
"$ 9ow use the constraint in part a$ to re"uce the si;e of the constraint in 3c$
1c$ <e must represent the constraint 0
xy
as a set of pairs of values that x an" y can ta/e
simultaneously, as prescribe" by the constraint. <e must bare in min" the values that x an" y are
allowe" to ta/e in"epen"ently 3their "omains$, i.e., that x ranges between 1 an" ! an" y ranges
between # an" #. The constraint is therefore&
0
xy
1 231,16$,3,1#$,34,15$,35,14$,3#,1$,36,11$,37,1!$,3=,'$,3',=$,31!,7$,311,6$,31,#$7
9ote that we "o not have the pair 314,5$ in the constraint, because y cannot ta/e value 5 3it is not in
the "omain of y$.
1"$ The "omain of x has effectively been re"uce" in constraint 3a$ to -ust the values 1! to 17. So,
wherever we see any other value for x in constraint 3b$, we can remove that pair from the constraint.
So, for instance, we can remove the pair 31,16$ because x ta/es value 1 in this solution, which we
/now it cannot "o from constraint 3a$. *ence the altere" constraint becomes&
0.
xy
1 231!,7$,311,6$,31,#$7
e$ >se your answer from 3"$ to ma/e the 0S? specification arc-consistent. <rite "own the final
formal specification of the 0S?.
1e$ To ma/e a 0S? arc-consistent involves removing values from the "omains of the variables which
we /now cannot possibly appear in a solution, because they are forbi""en by a constraint. In binary
0S?s, this is "one by loo/ing at each arc 3a,b$ between a pair of variables a an" b, loo/ing at the
correspon"ing constraint between those variables an" removing any values of a which "o not appear
anywhere in the constraints.
As we only have two variables, x an" y, then we only nee" to worry about two arcs, namely 3x,y$
an" 3y,x$. The constraint from part 3c$& 231!,7$,311,6$,31,#$7 means that, to ma/e the 0S? arc-
consistent with respect to x, we must remove all variables except 1!, 11 an" 1 from the "omain of
x. This ma/es sense, because any solution to the 0S? which "i"n.t have 1!, 11 or 1 as the value for
x woul"n.t be a solution at all, because it woul" brea/ the constraint from part 3c$. *ence the "omain
of x is re"uce" to& 21!,11,17.
<e now have to "o the same for the arc 3y,x$. To ma/e this arc consistent, we nee" to re"uce the
"omain of variable y to -ust& 2#,6,77. *ence, finally, we can state our 0S? as follows&
@ariables& x an" y
Aomains& "3x$ 1 21!,11,17 an" "3y$ 1 2#,6,77
0onstraints& 0
xy
1 231!,7$,311,6$,31,#$7
9ote that we "o not state the constraint 0
x
again, as this has been ta/en care of in the "omain of x.
f$ Suppose we have the 0S? state" in part 3e$, an" this a""itional constraint on x an" y&
0B
xy
231!,6$,31!,7$,311,6$,31,6$,31,7$7.
C9ote that, in real implementations, constraints 0
xy
an" 0B
xy
woul" be combine" into one constraint
because they are both over the same variables, but for the purposes of this exercise, /eep the
constraints separateD.
Suppose further that we have a 0S? solving agent which is using the variable or"ering& Cx,yD an" the
value or"ering C!,1',...,1D for the 0S?.
<hat woul" be the first possible solution that the agent loo/e" at+ <hat woul" be the first actual
solution that the agent foun"+ <hat woul" be the first actual solution the agent foun" if the variable
or"ering was reverse"+
The variable or"ering means that the agent will fix a value for x first an" then range over values for
y, only changing the value for x if it exhausts all possibilities for y. The value or"ering means that it
will try the largest value of x in its "omain first, namely x11. Similarly, it will try y17, then y16,
then y1# for a solution. If it can.t fin" a solution which appears in both constraints, then it will
change x to be x111, etc. So, the first possible solution it will try is x11 an" y17. As 31,7$ is foun"
in neither 0
xy
nor 0B
xy
, this will not be an actual solution. The solution with x being as large as
possible is actually 311,6$, so this will be the first foun" with the variable or"ering of Cx,yD. <ith the
variable or"ering Cy,xD, the first solution foun" will be 31!,7$, because this appears in both
constraints 3hence is actually a solution$, an" it is the solution with the largest value of y.
g$ Suppose that the agent was e8uippe" with a forwar" chec/ing mechanism. If the agent set x to be
value 1!, what woul" the forwar" chec/ing mechanism "o 3temporarily$ to the "omain of y+
,emember what the forwar" chec/ing mechanism "oes& whenever the agent fixes a value, v, for one
variable, it removes values temporarily from the "omains of the variables which have yet to be fixe".
The values remove" are those which are forbi""en any constraint given that v has been fixe". In our
case, with x fixe" to 1!, then 0
xy
contains only 31!,7$ an" 0B
xy
contains 31!,6$ an" 31!,7$. This
means that 0
xy
rules out all values for y except 7. *ence the "omain of y temporarily gets re"uce" to
277 by the forwar" chec/ing mechanism
. CTa/en from !!4 ESc. exam in AI Fworth aroun" 1!GD
In a cryptoarithmetic pu;;le, the variables A, H, 0, A, % an" I can ta/e values 1 to 7. The variables
must all be "ifferent an", when ta/en as "igits, they must satisfy the following sum&
A B
+ C D
-----
E F
In a""ition, A : 0 7, H : A 7, A ( 0 an" H ( A.
a$ Eo"el this problem as a 0S? with two constraints& one constraint on the variables 3A,0,%$ an"
one constraint on the variables 3H,A,I$. <rite your mo"el as a formal 0S?.
b) In light of the constraints, what variables can you remove from the domains of variables A to F?
Write down the domains after the removals.
c$ Suppose the "omains were as in ii$ an" the 0S? solving agent was using a variable or"ering of
2A,0,%,H,A,I7 an" a value or"ering of 21,,4,5,#,6,77. Suppose also that the agent was using
forwar" chec/ing. <hen woul" the first bac/-trac/ occur "uring the search an" why+
"$ Jiven A 1 1, fin" a solution to the pu;;le.
<e may go through this 8uestion in the exam lecture next wee/.
4. Imagine we have a search problem where the solution space is three "imensional, with each
"imension being integer value" an" ranging from 1 to 1!!. That is, solutions to the problem are
triples of integers 3a, b, c$ such that 1 (1 a, b, c (1 1!!.
a$ *ow big is the search space for this problem+
b$ ,epresent the number 17 as a bit string.
c$ *ow many bits will solutions to the problem re8uire+
"$ *ow many possible strings are there of the length specifie" in part 1c+
4a$ There are 1!! possibilities for each of a, b an" c in the solution triple. *ence there are
1!!B1!!B1!! 1 1,!!!,!!! possible solutions.
1b$ ,ea"ing from right to left, as we "o with "igits, tens, hun"re"s, etc. in base ten, the bit string for
17 is& 1!!!1, i.e., 1 lot of 16 : ! lots of = : ! lots of 5 : ! lots of : 1 lot of 1 1 17.
4c$ <e will nee" to represent three integers which coul" be as large as 1!! each. 9ow,
6
1 65 an"
7
1 1=, so to cover integers up to 1!!, we will nee" 7 bits. As we will nee" 4 of these, we will nee"
a total of 1 bits for each possible solution.
4"$ Jiven that each bit can be either on or off 31 or !$, there are
1
1 ,!'7,1# possible bit strings
of length 1. As there are only 1,!!!,!!! possible solutions to our problem, it appears that we will
be searching a space twice as big as we nee". *ence, it woul" be a"visable in this scenario to come
up with a more sophisticate" bit-string representation, as we coul" lose an entire bit from our
solutions an" still have
!
1 1,!5=,#76 solutions in the bit string space 3enough to cover all
possibilities$.
5. Some problems in co"ing theory re8uire the construction of binary co"e wor"s of minimal weight,
i.e., containing as few ones as possible 3with certain constraints$. Ketting w3n$ 1 number of ones in
the binary representation of integer n, suppose we "efine the following evaluation function for
integers&
f3n$ 1 w3n$ if n is evil but not pernicious
1 w3n$B if n is odious but not pernicious
1 w3n$B4 if n is pernicious
Cwhere a number is evil if it has an even number of ones in its binary representation, a number is
odious if it has an o"" number of ones, an" a number is pernicious if it has a prime number of onesD.
Ior those of you without a comprehensive school e"ucation, prime numbers are "efine" as having
exactly two "ivisors, which means that 1 is not a prime 3an" hasn.t been consi"ere" so for over a
hun"re" years$. 9ote that is a prime 3the only even one$.
a$ <hich of these numbers is pernicious+ <hich is evil+ <hat woul" they score using the evaluation
function+
4!, 41, 4, 44, 45
b$ Suppose an agent is using a JA-style search for bit strings of length 6, an" the fitness function
"ivi"es the above evaluation function, f, for an integer by the average of the evaluation function over
all the population Cas we "escribe" in the JA lectureD. Suppose that the bit strings for the numbers
4! to 45 ma/e up the entire population 3with an extra ;ero put on the K%IT han" si"e for integers
which can be represente" with # bits$. <hat number woul" the fitness functions assign each integer+
Ior each integer, how many copies of each are guaranteed to go into the interme"iate population+
5a$ In binary, the numbers are as follows&
4! 1 !1111! 41 1 !11111 4 1 1!!!!! 44 1 1!!!!1 45 1 1!!1!!
9ote that there is an extra ! a""e" to the left han" si"e of 4! an" 41. This basically says that there
are ;ero 4.s, which is fine, as it "oes not change the value of the number. If the ;ero was put on the
right han" si"e, it woul" change the integer being represente" 3effectively by multiplying by two$.
*ence, 4! has four ones in its binary representation, so is evil 3even number of ones$ but not
pernicious 3prime number of ones$. 41 has five ones in its binary representation, so is both o"ious
an" pernicious. 4 has a single one in its binary representation, so it is o"ious. 44 an" 45 have two
ones in their binary representations, so they are both evil an" pernicious 3because is a prime
number$. <ith this information, they will score the following in the evaluation function&
f34!$ 1 w34!$ 1 5
f341$ 1 w341$B4 1 #B4 1 1#
f34$ 1 w34$B 1 1B 1
f344$ 1 w344$B4 1 B4 1 6
f345$ 1 w345$B4 1 B4 1 6
*ence, the number 41 scores the most for the evaluation function.
5b$ The average score for the population of # in"ivi"uals is 35:1#::6:6$L# 1 6.6. *ence the fitness
of the in"ivi"uals will be calculate" as&
fitness34!$ 1 5L6.6 1 !.61
fitness341$ 1 1#L6.6 1 .7
fitness34$ 1 L6.6 1 !.4!
fitness344$ 1 6L6.6 1 !.'1
fitness345$ 1 6L6.6 1 !.'1
The integer parts of the fitness values correspon" to how many copies of the in"ivi"ual are
guarantee" to go into the interme"iate population. This means that number 41 will be put into the
interme"iate population twice for sure, an" another copy will be put in with probability !.7. All of
the other integers have a fitness value less than one, so they are not guarantee" to have any copy go
into the interme"iate population 3although 44 an" 45 have a high probability of getting a copy in$.
c$ ?erform one-point crossover on the bit-strings for 41 an" 4, with the crossover point after the
secon" bit 3counting bits from the K%IT in their representation. <hat numbers are represente" by
the bit-strings for the two offspring+ 0alculate the value of the evaluation function for the two
offspring.
"$ ?erform two-point crossover on the bit-strings for 4! an" 44, with the crossover points after the
first an" the fourth bits 3counting bits from the K%IT$. <hat integers are represente" by the bit-
strings of the two offspring+ 0alculate the value of the evaluation function for the two offspring.
e$ <hy can there be no other in"ivi"ual which scores higher on the evaluation function than the
in"ivi"ual 41+
f$ CMptional F for the mathletes amongst youD. Show that even perfect numbers 3such as 6, = an"
5'6, which are e8ual to half the sum of their "ivisors$ are pernicious. As an asi"e F an AI program
calle" 9umbers<ith9ames ma"e this "iscovery.
5c$ The bit strings for 41 an" 4 are& !11111 an" 1!!!!! respectively. To perform one point
crossover, we must split the strings into a left han" an" a right han" si"e at the crossover point,
which, in this case, is after the secon" bit. So, this gives us&
K*S3!11111$ 1 !1, ,*S3!11111$ 1 1111, K*S31!!!!!$ 1 1!, ,*S31!!!!!$ 1 !!!!
To recombine these using crossover, we put together the left han" an" right han" si"es of the
"ifferent strings&
Mffspring 1 1 K*S3!11111$,*S31!!!!!$ 1 !1!!!!
Mffspring 1 K*S31!!!!!$,*S3!11111$ 1 1!1111
The first offspring is therefore 16 an" scores B1 1 for the evaluation function, as it is o"ious but
not pernicious, an" has binary weight 1. The secon" offspring is 4 : = : 5 : : 1 1 57, an" scores
#B411#, as it is pernicious with binary weight #.
5"$ The bit strings for 4! an" 44 are !1111! an" 1!!!!1 respectively. To combine these bit strings
using two point crossover, we nee" to split each string into three& a K*S part, a EIAAK% part an" a
,*S part. To "o this, we split them at the two crossover points, which in this case is after the first bit
an" after the fourth bit. *ence, after splitting, we get&
K*S3!1111!$ 1 !, EIAAK%3!1111!$ 1 111, ,*S3!1111!$ 1 1!
K*S31!!!!1$ 1 1, EIAAK%31!!!!1$ 1 !!!, ,*S31!!!!1$ 1 !1
To combine these into two offspring, we swap over the mi""le parts, to give us&
Mffspring 1 1 K*S3!1111!$EIAAK%31!!!!1$,*S3!1111!$ 1 !!!!1!
Mffspring 1 K*S31!!!!1$EIAAK%3!1111!$,*S311111!$ 1 11111!
Thus, the first offspring is the bit string for the number , which scores 1 for the evaluation function,
as it is evil but not pernicious. The secon" offspring is 6!, which has evaluation score 1#, as it is a
pernicious number of weight #.
5e$ 41 is pernicious an" has a weight of #. As the biggest multiplication factor 3of 4$ is given out by
the evaluation function for pernicious numbers, any number with lower weight will score less than
41. *ence, a higher scoring integer must have higher weight. As the strings are of length 6, this
means it will have to have weight 6, but then it won.t be pernicious, an" will score 6 only as it is
evil. So, 41 scores the highest possible in the search space.
5f$ ?erfect numbers are so calle" because the first two are 6 an" =, an", apparently, Jo" create" the
earth in 6 "ays, an" ma"e the lunar cycle to be = "ays. 9o-one coul" fin" another perfect number
until %uler came along an" foun" 5'6, which "oesn.t appear to have any "ivine inspiration. It was
very nice when my 9umbers<ith9ames program tol" me that perfect numbers are actually
pernicious, both for the irony factor an" because my frien" Ar. Neremy Jow of >niversity 0ollege
Kon"on invente" pernicious numbers. <hen you write even perfect numbers 3we "on.t /now yet
whether or not there are any o"" perfect numbers$ in binary, surprisingly, you get a prime number of
ones followe" by one fewer ;eros, which obviously ma/es them o"ious an", more to the point,
pernicious. Ior a proof of why this is always so, see the paper here&
http&LLwww."oc.ic.ac.u/LOsgcLpapersLcoltonPaim!P1.p"f
#a$ ,epresent the following functions as graphs
f3a,b$ 1 s8rt3a$ : sin3aBb$ if a ( b
1 cos3a L b$ : s8rt3sin3a$:cos3aBb$$ otherwise
g3a,b$ 1 cos3a L s8rt3sin3b-cos3a$$$$
b$ ?erform a crossover, with the crossover fragment for f3a,b$ being the subtree representing
cos3aLb$, an" the crossover fragment for g3a,b$ being the subtree representing s8rt3sin3b-cos3a$$$.
c$ If an evaluation function calculate" the output value of these functions for the values a11!, b1!,
which of the four programs 3parents an" chil"ren$ woul" be the most fit in"ivi"ual+ C0alculate the
trigonometric functions in "egrees, not ra"iansD.
#a$ The following is a graphical representation for f3a,b$. The subtree in the box is the crossover
fragment 3for part b$.
,emember how the IIKT% no"e wor/s& if the first function below it 3on the left$ is less than the
secon" function below it, then return the thir" function below it, otherwise return the fourth function
below it.
b$ The following is a graphical representation for g3a,b$. The subtree in the box is the crossover
fragment 3for part b$.
#b$ <hen we swap the crossover fragments, the following two offspring are pro"uce"&
These represent the following two functions
x3a,b$ 1 cos3a L cos3 a L b$$
y3a,b$ 1 s8rt3a$ : sin3a B b$ if a ( b
1 s8rt3sin3b-cos3a$$$ : s8rt3sin3a$ : cos3aBb$$ otherwise
#c$ To "etermine which is the most fit in"ivi"ual program, we nee" to plug the values a 1 1! an"
b 1 ! into f3a,b$, g3a,b$, x3a,b$ an" y3a,b$ as follows&
f31!,!$ 1 s8rt31!$ : sin31!B!$ Cthis part is use" because 1! ( !D
1 4.66
g31!,!$ 1 cos31! L s8rt3sin3!-cos31!$$$$
1 !.'#5
x31!,!$ 1 cos31! L cos31!L!$$
1 !.'=5
y31!,!$ 1 s8rt31!$ : sin31!B!$ Cthis part is use" because 1! ( !D
1 4.66
*ence the two most fit in"ivi"uals are f an" y. <e see here that a fit part of the program 3in terms of
scoring highly for the evaluation function$ has been passe" on from parent to offspring, which is a
goo" thing for crossover.