1 / 17

Revision at the Beginning of the 2 nd Semester (repeated slides)

Revision at the Beginning of the 2 nd Semester (repeated slides). Small Coursework (40%) – Please complete it asap Go to: http://www.cems.uwe.ac.uk/~ryang/prolog/top.html Week 2 ( simp + cango2) Week 3 (cango3 +cango4) Week 4 (subset + nth_item ) Week 5 (shift + cal_route ).

uriah
Download Presentation

Revision at the Beginning of the 2 nd Semester (repeated slides)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Revision at the Beginning of the 2nd Semester (repeated slides) Small Coursework (40%) – Please complete it asap Go to: http://www.cems.uwe.ac.uk/~ryang/prolog/top.html Week 2 (simp + cango2) Week 3 (cango3 +cango4) Week 4 (subset + nth_item) Week 5 (shift + cal_route)

  2. In the original program, the relation arrow(X,Y) represents a directed link from X to Y. The relation cango(X,Y) means that there is a path from X to Y cango(X, Y):- %% base arrow(X, Y). cango(X, Y):- %% recursion arrow(X, Z), cango(Z, Y). % another example: ancestor(X, Y):-% base parent(X, Y). ancestor(X, Y):-% recursion parent(X, Z), ancestor(Z, Y). You must fully understand this program!(what, why, how) also its extensions (how to use lists) b a d c e f g h An ancestor is a parent or (recursively) the parent of an ancestor  - from wiki

  3. cango(X, Y):- arrow(X, Y). cango(X, Y):- arrow(X, Z), cango(Z, Y). ?- cango(a,h). a b c e d f d g g f h hhhhh Prolog uses ‘cango’ with a depth first search. ‘cango’ is a general program not just for this given cube. How does ‘cango’ program work? b a d c e f g h Observation: change the order of arrow, then check if the order of solutions changes

  4. In the original program, the relation arrow(X,Y) only represents a link from X to Y, not Y to X. How do we extend it to a bi-directional link? Easy – using disjunction: next(X, Y):- arrow(X, Y). next(X, Y):- arrow(Y, X). Is ‘cango’ really a general program? b a d c e f g h cango(X, Y):- next(X, Y). cango(X, Y):- next(X, Z), cango(Z, Y).

  5. Introduced circles into the map. There is a danger of being trapped in a circle! ?- cango(a,h). a b c e a d b c a a What happens after replacing ‘arrow’ by ‘next’? b a d c e f g h cango(X, Y):- next(X, Y). cango(X, Y):- next(X, Z), cango(Z, Y).

  6. We must add something to avoid being trapped in a circle. Solution 1: using an extra list to store the path generated so far, only extend a new node which is not in the list Solution 2: no u-turn. it is restricted to certain type of maps where circles are only created by u-turn. Q: Is ‘cango’ a general program? A: It only works if no circle in the map. b a d c e f g h Q-block

  7. Solution 2 (we will use this!) • For some maps, there are no circles as long as u-turn is not allowed. In this case, the easy solution is to add an extra test to disallow u-turn. next(a, b, north). next(b, a, south). ...... % PreDir is the direction before X cango(X,Y, PreDir):- next(X,Y, Dir), not_u_turn(PreDir, Dir). cango(X,Y, PreDir):- next(X,Z, Dir), not_u_turn(PreDir, Dir), cango(Z , Y, Dir).

  8. Two ways to define not_u_turn • Define ‘u_turn’, then make use of \+ (not) : u_turn(south,north). u_turn(north,south). u_turn(west,east). u_turn(east, west). not_u_turn(X,Y): - \+( u_turn(X,Y) ). • Define ‘not_u_turn’ directly : not_u_turn(south, west). not_u_turn(south, east). ....... not_u_turn(start, _).

  9. Move to real program, more information needed in the ‘next’ % (From,To, Direction, WhichSide, Distance) next('2q43', '2q49', south, right, 1). next('2q49', '2q43', north, left, 1). next('2q49', 'area3', south, left, 1). next('area3', '2q49', north, left, 1). next('area3', '2q46', south, left, 3). next('2q46', 'area3', north, right, 3). next('2q46', '2q47', south, left, 2). next('2q47', '2q46', north, right, 2). .......

  10. Your ‘cango’ program should have6 parameters cango(X,Y,Path,Dir,Dist,PreDir) Path and Dir are lists which are exactly same as cango4 Dist is a list of distances PreDir indicates previous direction. You need to use it to compare with the next direction. no u-turn ! For example ?- cango(‘2q43’, ‘2q47’, P, Dir, Dist, start). P = [2q43,2q49,area3,2q46,2q47] Dir = [south,south,south,south] Dist = [1,1,3,2]

  11. Put them all together :-[map]. % include the file map.pl test(X, Y):- cango(X, Y, Path, Dir, Dist, start), cal_route(Dir, Dist, NewDir, NewDist), write(NewDir), nl, write(NewDist), nl.

  12. A little help on ‘cal_route’ (week5’s task) • The following program reduces a sequence of same items into to one item reduce([X],[X]). % if only one item % nothing to reduce reduce([X,X|T], R):- reduce( [X|T], R ). reduce([X,Y|T], [X|R]):- X \== Y, reduce( [Y|T], R ). ?- reduce([a,a,b,b,b,c], R). produces R = [a,b,c]

  13. Start to work on chatbot You need to copy three files: • map.pl http://www.cems.uwe.ac.uk/~ryang/prolog/code/map.pl • A dummy chatbothttp://www.cems.uwe.ac.uk/~ryang/prolog/code/chat.pl • Something for reading input http://www.cems.uwe.ac.uk/~ryang/prolog/code/readin.pl

  14. chatbot:- print_welcome, conversations. conversations:- repeat, % prolog built-in print_prompt(you), readin(S), % defined in file % readin.pl gen_reply(S,R), print_prompt(me), write_list(R), is_quit(S). is_quit(S):- member(bye, S). gen_reply(S, R):- % totally random respones_db(random, Res), % create a random number length(Res, Length), Upper is Length+1, random(1, Upper, Rand), nth_item(Res, Rand, R). respones_db(random, [ [hello, '!'], [hi, there , '.', this, is, not, a, hello_world, program, '!'], [it, is, a, nice, day, '.'], [have, you, talked, to, me, before, '?'], ['.', '.', '.', what, do, you, mean, '?'] ]). A Simple Chatbot – only producing random responses

  15. | ?- chat. Welcome! I am a chatbot Please finish your line with a full stop, ?, or ! user Hi. bot1 : hello ! user |: what is your name? bot1 : have you talked to me before ? user |: no, I don't think so. bot1 : hi there . this is not a hello_world program ! user |: cool. bot1 : sorry , i am only a single minded chatbot . user |: ok, i don't mind. bot1 : . . . what do you mean ? user |: I mean I like you. (cont.) | bot1 : sorry i cannot remember everything you said . user |: ok. bot1 : sorry , i am only a single minded chatbot . user |: you said that. bot1 : sorry i cannot remember everything you said . user |: no problem. bot1 : oh ...... ok user |: good. bot1 : can you say it again ? user |: good!!!! bot1 : have you talked to me before ? user |: getting boring now, bye. bot1 : bye !

  16. After Copying chat.pl, you need to add your ‘subset’, ‘nth_item’, and the following ‘write_list’ - Output a list - The following program prints out elements in a list and puts one space between them. write_list([]):- nl. %% print a new line write_list([H|T]):- write(H), write(‘ ‘), write_list(T).

  17. A simple way to link chatbot to ‘cango’ program • In get_reply(S,R) check if S has a pattern ‘to ... from ...’ if yes, call your ‘cango’, then generate the reply. pattern_tofm([to, X, from, Y|_], X, Y). % yes! pattern_tofm([_|T], X, Y):- % carry on checking pattern_tofm(T, X, Y).

More Related