AI Labs - Constraint Logic Programming

Lab 13/12/2006


Part 1: SWI-Prolog

On the laboratories PCs it is installed the SWI Prolog version 5.6.20. The Prolog interpreter can be started with the command plwin.exe, more informations can be found in the SWI Prolog documentation or FAQ. Basically the main commands and features are:

Exercise 1.1

Suppose we are working with the following knowledge base:

hasWand(harry).
quidditchPlayer(harry).
wizard(ron).
wizard(X) :- hasBroom(X),hasWand(X).
hasBroom(X) :- quidditchPlayer(X).

How does Prolog respond to the following queries? (use the SWI-Prolog to verify the answers)

  1. wizard(ron).

  2. wizard(hermione).

  3. wizard(harry).

  4. wizard(Y).

Exercise 1.2: CSP in Prolog

Find all the colour assignment for the map of Australia using the following Prolog code.

colourable([WA, SA, NT, Q, NSW, V, T]) :-
	        rgb(WA), rgb(SA), rgb(NT), rgb(Q), rgb(NSW), rgb(V), rgb(T),
	        WA \= NT, NT \= Q, Q \= NSW, NSW \= V,
	        SA \= WA, SA \= NT, SA \= Q, SA \= NSW, SA \= V.

rgb(red).
rgb(green).
rgb(blue).

Part 2: Using CLP(FD) solver

Use the CLP(FD) solver of SWI-Prolog to solve the following problems. You can use the example shown during the lecture as a skeleton to write the solution. Examples, together with a description of the predicates available in the CLP(FD) library, can be found here.
Remember to include the line

use_module(library('clp/bounds')).

to import the CLP(FD) library. Note that CLP numeric predicates are prepended with the # symbol.

Exercise 2.1: Cryptarithmetic

Find the solution to the cryptarithmetic puzzle TWO + TWO = FOUR by encoding the problem using the CLP(FD) solver of the SWI-Prolog.

Exercise 2.2: Vertex colouring

Given a graph and a fixed number of colours, the problem consists in assigning a colour to each vertex of the graph. Colouring must be proper, in the sense that no adjacent vertices are assigned the same colour. Use the SWI-Prolog CLP(FD) solver to find the 5-colouring (i.e. using 5 colours) of the following graph:

graph

Exercise 2.3: Scheduling and Constraint Logic Programming

A scheduling problem consists in a given set of tasks with associated duration; in addition, tasks may have precedence constraints among them (e.g. debugging must be performed after the code has been written). The purpose of this exercise is to show how to use a Constraint Logic Program system to devise a scheduling.

In this lab we use the following encoding of the problem into CSP over the integer domain:

Consider the following scheduling problem with 7 different tasks:

Task Duration
t1 16
t2 6
t3 13
t4 7
t5 5
t6 18
t7 4

and the following precedence relation: