
Logic Programming and Artificial Intelligence
3 February 2009
Solutions to LPA Problem Sheet 2
Note: We shall often use the following shorthand notation for trees.
A A
B / \
F = B C
C / / \
D F D E
E
1. (a) A state is an ordered pair (x,y) where x is the number of liters of water in the large jug,
and y is the number of liters of water in the small jug. The initial state is therefore (5,0),
and the goals are all states of the form (x,1). There are four rules:
Dump Big (DB): rewrite (x,y) to (0,y) with precondition that x is not 0.
Dump Small (DS) : rewrite (x,y) to (x,0) with precondition y is not 0.
Pour into Big (PB) : rewrite (x,y) to (x+p, y-p) where p (the amount poured) is
min(5-x,y) with the precondition that y is not 0, and x is not 5.
Pour into Small (PS) : rewrite (x,y) to (x-p, y+p) where p (the amount poured) is
min(2-y,x) with the precondition x is not 0, and y is not 2.
(b) In the following tree, the label before each node indicates which operator was used to
obtain that node.
(5 0) <--------------- Initial state
DB:(0 0)
PS:(3 2)
DB:(0 2)
DS:(0 0)
PB:(2 0)
DB:(0 0)
DS:(3 0)
DB:(0 0)
PS:(1 2)
DB:(0 2)
DS:(0 0)
PB:(2 0)
DB:(0 0)
DS:(1 0)
DB:(0 0)
PS:(0 1) <---- Goal state
(c) The solution is: Pour 2 liters from the big jug into small jug, empty the small jug, pour
2 liters from the big jug into the small jug, empty the small jug, and pour all the water
from the big jug into the small jug.
2. Each state contains two things: 1) the instance of TSP that is to be solved, that is a complete,
undirected, weighted graph, and 2) a non-empty list of vertices of the graph. The latter encodes
the path through the graph that is being grown. In the initial state the list contains only one
element, an arbitrary vertex of the graph. The goal is any state in which the list contains every
vertex of the graph exactly once except that the first vertex and last vertex of the list are the
same. Thus, the list in a goal state is a cycle through every vertex of the graph.
Now consider the operator by considering its effect on an arbitrary state S. Let |V|be the
number of vertices in the graph at S, and let Lbe the vertex list in state S. The idea of the
operator is that it extends the path Lwith an arc. The precondition of the operator is that it
only applies to Sif the length of Ldoes not exceed |V|. There are two cases:
1