06 1024x768

06 1024x768



wriloBsUI))-

witeti«UH IT))write(H). nl. writetist(T).

lhi> prcdicutc wriics one element of the list on cacli llnc. as nl rcquires the outpatracan controller to hegin a ncw linę. If we wi$h to writc out a list in rcvcrscd order the reonht prcdicatc musi eonie bcforc the write command. This guaramees that the list b traend 16 the ciki bcfbrc any element b wtitten. At that tinie the last element of the list i» uiiaa foliowej b> cach prcccding element as the recursivc control comes back up to the lop.A i6«ne«ńeofafai*nldbc

rewie wntet*t([ ]).

rovcrso writolistflH |TJ)revorso_writeli8t(T), write(H). nl.

The roader should nut writelist and reverse writolist with traco to obserw* the bekań ofthcscprcdicatcs.

RM Rccursivc Scarch in PROLOG

In Scction 5.2 we introduccd the 3 x 3 knightb tour problem for the prodicatc cnleuWIf reprwented the boird squnres for the knight movcs like this:

1

2

3

4

5

6

7

8

9

The lepi mes arc rcprcscntcd in PROLOG using a move prcdicatc. The path picfcft deline* an algorithm for tinding a path of zero or morc moves bctwcen ii' argument*. Nefc that path n defmed rccur>ivciy:

movc(1.6). move(1,8). movo(2.7). move(2.9).


move(3.4). move(3.8). move(4t 3). move(4.9).


move(6.7). move(6,1). move(7.6). move{7.2).


movo(8.3). movo(8.1). movo(9,4). movo(9.2).


P«h(Z.2).

path(X. Y) > move(X. W). not(been(W)). assert(been(W)). path(W, Y).

This dcfinńion of path b a PROLOG impiementation of the algorithm óefed • Chapter 5. As noced abmc. assert b a built-in PROLOG prcdicatc that “4 has Ac udc effect of płacing its argument in the databasc of spccificatioiB. TbeW® prcdicatc is Med to rccord peesiousły visited States and avoid loops.

Thił usc of the been prcdicatc violates the program designers goal ofctcatbfp^ c«e calculuł spccilications that do not use global variablcs. Thus been(3), wheoasowl

PART VI / IANGUAOE S FOR Al PROBLEM SOLYING

into the dnhibnsc. is indeed a fact avai!nblc to any olher procedurę in the data baw and »» >ucli. has globul cxtcnsion. Gven morę iinportant. crcating globu) structurcs to altcr program contro) violntcs the basie lenet of the production system model, where the logie (of problem spccifications) is kepi separatc from the contro! of the program. Herc bttn struclom wcrc created as globnl spccifications to modify the cxccution of the program itsełf.

As we proposcd in Chapter 3. a list may be usod to kccp trach of visitcd States and rimt kccp the path cali front looping. The momber prcdicatc is uscd to dętcet dup lica te Mes (loops). This approach rernedies the problcms of using global been{W) assertion*. The FROLOG-bascd spccification of the fotkming elauses exactly implcmenis the deplh-lirst graph scarch with the backtracking algorithm of Chapters 3 and 5

path(2.2. L).

path(X. V. D > movo(X. Z). noł(mombor(Z. L)). path(Z. Y. (Z ILD.

BMag the mambar prcdicatc as defined previously.

The third parameter of path is the (ocal variablc reprcsentln® the list of States that ha\e already bccn visiicd. When a ncw stale is generated (using the move prcdicatc) and this stale b not already on the list of yisited States. not(member(Z. L)). it is placed on the front of the stale list (Z IL] for the ncxt path cali.

It shoułd be noted that ail the parameters of path are local and their currcnf va!ucs depend on wbcrc they are callcd in the graph scarch. Gach rccurshc cali adds a stale to this list. If all coniinuations from a ccrtain stale fail. then that particular path cali fails. Whea the interpreter backs up to the parem cali. the third parameter. representing the list of tatra yisitcd. has its prcvious value. Thus. stales are added to and dełeted from this list as the backtracking scarch moves through the graph.

When the path cali finally succccds. the lirst iwo parameters are idcntical. The third parameter is the list of States yisitcd on the solution path. in rcserac order. Thus we can pnnt out the sleps of the solution. The PROLOG spccification for the knights tour problem using lists and a dcpth-flraf scarch cmploying backtrack may be obtaincd by using this definition of path with the move spccifications and mambar predicatcs jusr presented.

The cali to the PROLOG interpreter path(X.Y,[X]). where X and Y are replaccd by aumbcni bctwccn I and 9. flnda a path from stale X to siatę Y. if the path cxists. The third parameter initin/izes the path list with the starting stale X. Notc tłuu there is no ty ping dislinclion in PROLOG: the fint iwo panimeters are any representalion of stale* in the problem spocę and lito third is a list of States. Unification makes this gcncrulization of panem małe hi ng acroas data types possible. Thus. path is a generał dcpfh-firaf scarch affonthm that may be uscd with any graph. la Scction 14.3 we usc this «o implcmcnt a production system solution to the farmer, wolf. goat. and cabboge problem, with State «pccificafion* rcplncing square mimbers in the cali to path.

We now present the solution for the 3x3 knighth tour. It b łefi as an exercbe to iołvc •be fuli f x 8 knights tour problem in PROLOG. (See escrcbcs m Chapten 14 and 15.) for this (race we rcfcr to the two parts of the path algorithm by number

1.    b pafh(Z. Z. L).

2.    is path(X. Y. L)move(X Z). not(nu)mbor(Z. L». path(Z. Y. (Z ILJ).

CHAPTER 14 / AN INTROOUCTION TO PROLOO 613


Wyszukiwarka

Podobne podstrony:
Lose It March 16(1) bahti MAG * WHY CANT I GET RID OF THE FAT ON MY THIGHS? J AND 5 O
htdctmw 104 • Now this is morę like it! Look at panel 1—look at this close-up of the monster breakin
ape 041 SECTION III. THE NECK. It is a mistakc to suppose that thc developmem of the muscles on thc
Ilpńcjfit Tlowers andCrests Cut it as ilfustrated. rotate one hałf of the card, and voila! The motif
values when it becomes Iow. This process typically consumes about 3% to 5% of the total gas transpor
BUW 1 (276) [1024x768] dcmencję, można przeczytać w artykułach Alana Crbasa I łn Serotemm Receptor C
image003 Want to write for IA’sfm? Please send for thc description of our needs first; write us for
Slide6 OWNIT See the information from the Engłish side of the card and then write it in Polish in yo
IMG 06 (2) c^o£u^yu<.
DSC06 (2) $ k
“For each word write only the dots and dashes of the Ts’ and ‘t’s’ it contains and you have—.—for
IMG5 Test 7 (Modules 13 and 14) 1 Look at the programme. Imagine you are telling the group about it

więcej podobnych podstron