Professional Documents
Culture Documents
1. Consider the above graph representing the state space and transitions in a search problem. The
nodes (states) are labelled with letters and the edges (transitions) have numeric costs. Each node also
has a heuristic value denoted by h. Assuming S is the start state and G is the goal state, determine the
order in which states are expanded, as well as the path returned, using the following search methods.
Show the states in the fringe in each step, ordered as a priority queue. Assume that no state can be
expanded more than once and that when inserting nodes to the fringe, ties are resolved so that states
appearing earlier in alphabetical order are ordered first (i.e., A before B).
a) Best-first search
Fringe:
States expanded:
Goal path:
b) A* search
Fringe:
States expanded:
Goal path:
a) Best-first search
Fringe:
States expanded:
Goal path:
b) A* search
Fringe:
States expanded:
Goal path:
3. Work through the brief Prolog tutorial at: http://www.macs.hw.ac.uk/~rpp6/teaching/F29AI/prolog/
likes(mary,ai).
likes(mary,whisky).
likes(john,mary).
likes(john,whisky).
a) Formulate the Prolog queries for the following questions and state the answers that Prolog returns:
d) What happens if you add rule b) iii) to your program and ask what John likes? What are the
responses?
5. Consider the grid from Question 2:
a) Create a Prolog program to represent the spatial relationship that certain squares in the grid are
“north” of (i.e., above) other squares in the grid. Use facts of the form:
north(a,s).
to indicate that square “a” is north of square “s”. What facts do you need to add to your program to
represent all the “north” relationships in the grid?
b) Add facts to your program to represent the spatial relationship that certain squares in the grid are
“east” (i.e., to the right) of other squares in the grid. Use facts of the form:
east(d,a).
to indicate that square “d” is east of square “a”. What facts do you need to add to your program to
represent all the “east” relationships in the grid?
c) Write a Prolog rule to define the concept “south”, i.e., that a square is south of (below) another
square.
d) Write a Prolog rule to define the concept “west”, i.e., that a square is west (left) of another square.
e) Write Prolog rules to define the concept “adjacent”, i.e., that a square is adjacent to (immediately
beside) another square in any direction.
f) How would you write a Prolog rule to define the idea that a square is two steps away from another
square in the grid? Test that your program works by giving some example queries and their answers.
6. The following Prolog program can be used to solve the Towers of Hanoi problem:
https://en.wikipedia.org/wiki/Tower_of_Hanoi
move(1,X,Y,_) :-
write('Move top disk from '),
write(X),
write(' to '),
write(Y),
nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
a) Enter the program in Prolog and ask the query: move(3,left,right,centre). What’s the
output? Is it correct?
b) Looking at the program and output, explain what you think each of the parameters means in the
move query.
c) There are 2 definitions of the rule move in the above program: move(1,X,Y,_) and
move(N,X,Y,Z). Analyse the program and briefly explain what you think each of them does?
7. ONLY ATTEMPT THIS QUESTION IF YOU HAVE TIME: A famous problem in AI is Blocks World, where a
robot must stack blocks in a certain configuration but can only do so by moving one block at a time.
There are plenty of solutions available, such as this one written in Prolog:
http://www.macs.hw.ac.uk/~rpp6/teaching/F29AI/prolog/blocksworld.pl
Enter the above program in Prolog. Three scenarios are defined, problem1, problem2, and problem3,
which can be run by typing the name of the problem as a Prolog query, e.g.:
problem1.
Look at the definition of the problems in the source code and run the problems in Prolog. What do you
think the problem rules mean? What do you think the output means? Is it correct?