Docsity

Logic Programming and Artifical Intelligence, Solution Sheets - Computer Science 2, Study notes of Computer Numerical Control

Toggle sidebar
bg1
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
pf2

Partial preview of the text

Download Logic Programming and Artifical Intelligence, Solution Sheets - Computer Science 2 and more Study notes Computer Numerical Control in PDF only on Docsity!

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 L be the vertex list in state S. The idea of the operator is that it extends the path L with an arc. The precondition of the operator is that it only applies to S if the length of L does not exceed |V |. There are two cases:
  • If the length of L is |V |, then the last arc of the cycle must be added and it must return to the first vertex. Thus the only state that is produced is that which results from adding to the end of L a copy of the first element of L.
  • If the length of L is strictly less than |V |, then L must be extended to a new vertex. Thus, for each vertex of the graph not in L, a new state is produced by adding that vertex onto the end of L.

The cost of each operator is the weight of the arc that it adds to L. The TSP is a minimization problem, so in our problem representation we seek a minimum-cost goal. The reason we put a vertex into L in the initial state is because a cycle through every node can be thought of as a path starting at any node. In other words, every cycle that starts at vertex V 1 can be thought of as a cycle that starts at vertex V 2. Thus we can arbitrarily force the choice of the first node and reduce the size of the search tree without loosing any solutions.

  1. (1) Breadth first: A B C D E F G H I + other possibilities) (2) Uniform cost: A B C E F J K (3) Depth first: A B D G H C E I + other possibilities) (4) Iterative deepening: A A B C A B D C E F A B D G H C E I + other possibilities)
  2. Depth-first search. Since the entire search tree must be searched, it does not matter what order we do it in, as long as we are guaranteed to examine every node. 1 Iterative deepening expands each node multiple times, so it is less efficient than depth first. Depth first is preferred because it uses less memory than breadth-first.
  3. (a) Breadth-first search, iterative deepening, uniform cost search (if all operators have cost greater than some ). (b) All but depth-first search with a depth limit. (c) Breadth-first search and iterative deepening. (d) Uniform cost search (if all operators have cost greater than some ).
  4. Consider a search tree in which all paths are finite and terminate at the same depth–say d– and all, or almost all, paths terminate at a goal. In such a search tree, depth first search will quickly descend to depth d and find a goal. On the other hand, iterative deepening will generate every node whose depth is less than d multiple times before finding a solution.
  5. According to the Russell and Norvig book, iterative deepening was first used by Slate and Atkin in their CHESS 4.5 game-playing program.
  6. (a) procedure DFS(NODE) if NODE is a goal state then return NODE else begin let CHILDREN be {OP(NODE)|OP is an operator whose precondition us satisfied by NODE} for each CHILD in CHILDREN do if DFS(CHILD) is not failure then return DFS(CHILD) return failure end (b) In depth-first search the list L is used as a stack. In the backtracking algorithm, L gets embedded into the system’s runtime execution stack. On each recursive call to DFS, CHILDREN is pushed onto the stack; these are the same nodes that would have been pushed onto L.

(^1) Given that the order of search doesn’t matter, the uniformed search methods are preferable to the informed methods, since there is no point wasting time examining the states to choose which node to expand next.