You are on page 1of 8

F29AI – Artificial Intelligence and Intelligent Agents

Activity Sheet 3 – Informed Search and Knowledge Representation


These questions are meant to give you practice with some of the lecture material we are covering this
week. For Week 3, we are considering informed search algorithms as decision-making mechanisms for
goal-based agents. We will also explore some ideas in knowledge representation using Prolog. Try to
answer as many questions as you can and think about the material we’ve covered so far in the
lectures and live sessions. When you are finished, you can submit your completed answers on Canvas.
Individual activity sheets will not be marked but general feedback will be provided based on the
common problems that arise.

NAME: ________________________________________ Student ID Number: _____________________

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:

c) Consider the heuristic in the graph. Is it admissible? Why or why not?


2. Consider the above grid representing the state space and transitions in another search problem.
States are labelled with letters. An agent can move from one state to another state provided the two
states are adjacent and not blocked by a wall (the grey squares). Diagonal movement is not permitted.
Each move to an adjacent square costs 1 resource. Assuming S is the start state and G is the goal state,
determine the order in which states are expanded, and the path returned, using the following search
methods and the Manhattan heuristic. Show the states in the fringe in each step, ordered as a priority
queue. When inserting nodes to the fringe, assume that ties are resolved so that states appearing earlier
in alphabetical order are ordered first, and that no state is expanded more than once.

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/

4. Write this simple Prolog program and load it into 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:

i) Does Mary like AI?

ii) Does Mary like John?

iii) What does Mary like?

iv) What does John like?

b) How would you formulate the following rules?

i) John likes anything that Mary likes

ii) Mary likes anyone who likes whisky

iii) John likes anyone who likes themselves


c) Add the rules from b) i) and ii) to your Prolog program. Now, what does Mary like? What does John
like? List the responses to the queries. Was there anything strange about the responses?

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?

You might also like