You are on page 1of 4

TRNG I HC BCH KHOA TP.

HCM Khoa Khoa hc & K thut My tnh

ARTIFICIAL INTELLIGENCE
Lab session 2 STATE SPACE SEARCH
1. OBJECTIVE The objectives of Lab 2 are (1) to introduce the Prologs powerful concept of compound term; (2) to present the cut predicate; and (3) to do the complete solution of state search space illustrated by the means of the 8-puzzle problem. 2. EXPERIMENT 2.1. Compound terms Compound term is a Prolog concept used to describe combined and structured concepts. In Listing 1, what we want to imply in the first fact is MU is a football club whose jersey color is red and the second Chelsea is yet another football club whose jersey color is blue.

red(club(mu)). blue(club(chelsea)).

Listing 1 Usage of compound terms Here we have club(mu) and club(chelsea) are some compound terms used by the predicates red and blue respectively. Unification and resolution, the two great things coining the power of Prolong, can be applied on the compound terms as well as if on atoms. Listing 2 introduces some attempts to find the best player in red and blue jerseys.

bestplayer(mu,ronaldo). bestplayer(chelsea,terry).

TRNG I HC BCH KHOA TP.HCM Khoa Khoa hc & K thut My tnh

bestredplayer(X):-red(club(Y)),bestplayer(Y,X). bestblueplayer(X):-blue(club(Y)),bestplayer(Y,X).

Listing 2 Who is the best player? Example 1. Try the following queries and observe the results. - bestredplayer(X) - bestblueplayer(X) 2.2. The cut predicate The cut predicate, denoted as !, is just another hand to complement with the previously mentioned fail predicate. To motivate the need of cut, lets consider the example given in Listing 3.

color(P,red) :- red(club(P)). color(P,blue) :- blue(club(P)). color(P,unknown).

Listing 3 Find out the colors of the clubs too many results found. Our idea is to find the colors displayed in the jerseys of the clubs. For example, when one query color(mu,X), BProlog should yield the result X = red. Similarly, X should be blue if we query over chelsea. In case the club has not been defined in our KB yet, for instance inter, the result will be, of course, unknown. The idea is nice, but the result is unfortunately not favorable. When queried color(mu,X), BProlog not only answer red, but also unknown. After finding the first solution, BProlog continue finding another. Cant help! It is its mission.

TRNG I HC BCH KHOA TP.HCM Khoa Khoa hc & K thut My tnh

color(P,red) :- red(club(P)), !. color(P,blue) :- blue(club(P)), !. color(P,unknown).

Listing 4 Find out the colors of the clubs too many results found. To solve this problem, it is quite hard (and probably impossible) if we just merely rely on our knowledge. In Listing 4, the cut predicate is used. The cut prevents Prolog to find another solution after recognizing one. It fits perfectly to our intention in this case. 2.3. Search solution for the 8-puzzle problem OK, now we try to solve the first basic toy AI problem, the 8-puzzle one. No, we does not include you, but we only. Your job is to take into consideration the search.pl and setting.pl files. All are done in there. The principle is to use two stacks (or queues, it depends) named open and closed in the working memory of Prolog. Then, in search.pl, we have implemented the following things: - path: it retracts one state out of open, put all of children of the state into closed. If open is empty but the goal is still not reached, it will display the failure message. Otherwise, the goal is displayed. - checkgoal: It checks if the first state in open is the goal or not. - get_children: It generates all of children of the parameter in order to put them into open. - addToOpen: It put a state to the open. Depending on the algorithm used, the corresponding predicate is invoked. - moves: It moves from the current state to another, provided th at neither open and closed are not empty. - go: the first predicate to be run. In file setting.pl, we define problem-specified features, which include the following: - The KB of possible legal moves. For example move(state([X1,X2,X3,X4,0,X6,X7,X8,X9]), state([X1,0,X3,X4,X2,X6,X7,X8,X9])) means that we can move the blank up in the current state (0 represents the blank tile). - algorithm(dfs): It indicates the search algorithm. One may want replace dfs by brfs. 3

TRNG I HC BCH KHOA TP.HCM Khoa Khoa hc & K thut My tnh

- mywrite: Write out a state. - go_p and isGoal: They define the initial state and the goal respectively.

3. EXERCISES 3.1. Try to run the 8-puzzle program successfully. Change the first initial and goal and observe the results. 3.2. Modify the file setting.pl to solve the water-jug problem and the missionaries-cannibals problem (you should not touch the search.pl file). 4. SUBMISSIONS There is no writing submission required. Students run their solutions at their workstations and answer questions from instructors to get marked.

-- End --

You might also like