Advertisement
Guest User

Untitled

a guest
Jul 14th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/I 8.36 KB | None | 0 0
  1. %------------------------------------------------------------------------------
  2. % A Prolog Implementation of the Wumpus World described in
  3. % Artificial Intelligence : A Modern Approach (Russel - Norvig)
  4. %
  5. % Mandatory Excercise 2007
  6. % v1.0 - Jan. 31, 2007
  7. % Richard O. Legendi
  8. %------------------------------------------------------------------------------
  9.  
  10.  
  11. %------------------------------------------------------------------------------
  12. % Declaring dynamic methods
  13.  
  14. :- dynamic ([
  15.          agent_location/1,
  16.          gold_location/1,
  17.          pit_location/1,
  18.          time/1,
  19.          score/1,
  20.          visited/1,
  21.          visited_cells/1,
  22.          world_size/1,
  23.          wumpus_location/1
  24.         ]).
  25.  
  26.  
  27. %------------------------------------------------------------------------------
  28. % To start the game
  29.  
  30. start :-
  31.     format("Initializing started...~n", []),
  32.     init,
  33.    
  34.     format("Let the game begin!~n", []),
  35.     step([[1,1]]).
  36.  
  37. %------------------------------------------------------------------------------
  38. % Scheduling simulation:
  39.  
  40. step_pre(VisitedList) :-
  41.     agent_location(AL),
  42.     gold_location(GL),
  43.     wumpus_location(WL),
  44.  
  45.     score(S),
  46.     time(T),
  47.  
  48.     ( AL=GL -> format("WON!~n", []), format("Score: ~p,~n Time: ~p", [S,T])
  49.     ; AL=WL -> format("Lost: Wumpus eats you!~n", []),
  50.                format("Score: ~p,~n Time: ~p", [S,T])
  51.     ; step(VisitedList)
  52.     ).
  53.  
  54. step(VisitedList) :-
  55.     make_percept_sentence(Perception),
  56.     agent_location(AL),
  57.     format("I'm in ~p, seeing: ~p~n", [AL,Perception]),
  58.    
  59.     update_KB(Perception),
  60.     ask_KB(VisitedList, Action),
  61.     format("I'm going to: ~p~n", [Action]),
  62.  
  63.     update_time,
  64.     update_score,
  65.    
  66.     agent_location(Aloc),
  67.     VL = [Aloc|VisitedList],
  68.     standing,
  69.     step_pre(VL).
  70.  
  71. %------------------------------------------------------------------------------
  72. % Updating states
  73.  
  74. update_time :-
  75.     time(T),
  76.     NewTime is T+1,
  77.     retractall( time(_) ),
  78.     assert( time(NewTime) ).
  79.  
  80. update_score :-
  81.     agent_location(AL),
  82.     gold_location(GL),
  83.     wumpus_location(WL),
  84.     update_score(AL, GL, WL).
  85.  
  86. update_score(P) :-
  87.     score(S),
  88.     NewScore is S+P,
  89.     retractall( score(_) ),
  90.     assert( score(NewScore) ).
  91.  
  92. update_score(AL, AL, _) :-
  93.     update_score(1000).
  94.  
  95. update_score(_,_,_) :-
  96.     update_score(-1).
  97.  
  98. update_agent_location(NewAL) :-
  99.     agent_location(AL),
  100.     retractall( agent_location(_) ),
  101.     assert( agent_location(NewAL) ).
  102.  
  103. is_pit(no,  X).
  104. is_pit(yes, X) :-
  105.     pit_location(X).
  106.  
  107. %------------------------------------------------------------------------------
  108. % Display standings
  109.  
  110. standing :-
  111.     wumpus_location(WL),
  112.     gold_location(GL),
  113.     agent_location(AL),
  114.  
  115.     ( is_pit(yes, AL) -> format("Agent was fallen into a pit!~n", []),
  116.       fail
  117.     ; stnd(AL, GL, WL)
  118.       %\+ pit_location(yes, Al),
  119.     ).
  120.  
  121. stnd(AL, GL, WL) :-
  122.     format("There's still something to do...~n", []).
  123.  
  124. stnd(AL, _, AL) :-
  125.     format("YIKES! You're eaten by the wumpus!", []),
  126.     fail.
  127.  
  128. stnd(AL, AL, _) :-
  129.     format("AGENT FOUND THE GOLD!!", []),
  130.     true.
  131.  
  132. %------------------------------------------------------------------------------
  133. % Perceptotion
  134.  
  135. make_perception([_Stench,_Bleeze,_Glitter]) :-
  136.     agent_location(AL),
  137.     isStinky(AL),
  138.     isBleezie(AL),
  139.     isGlittering(AL).
  140.  
  141. test_perception :-
  142.     make_percept_sentence(Percept),
  143.     format("I feel ~p, ",[Percept]).
  144.  
  145. make_percept_sentence([Stench,Bleeze,Glitter]) :-
  146.     smelly(Stench),
  147.     bleezy(Bleeze),
  148.     glittering(Glitter).
  149.  
  150. %------------------------------------------------------------------------------
  151. % Initializing
  152.  
  153. init :-
  154.     init_game,
  155.     init_land_fig72,
  156.     init_agent,
  157.     init_wumpus.
  158.  
  159. init_game :-
  160.     retractall( time(_) ),
  161.     assert( time(0) ),
  162.  
  163.     retractall( score(_) ),
  164.     assert( score(0) ),
  165.    
  166.     retractall( visited(_) ),
  167.     assert( visited(1) ),
  168.  
  169.     retractall( isWumpus(_,_) ),
  170.     retractall( isGold(_,_) ),
  171.    
  172.     retractall( visited_cells(_) ),
  173.     assert( visited_cells([]) ).
  174.  
  175. % To set the situation described in Russel-Norvig's book (2nd Ed.),
  176. % according to Figure 7.2
  177. init_land_fig72 :-
  178.    retractall( world_size(_) ),
  179.    assert( world_size(4) ),
  180.  
  181.    retractall( gold_location(_) ),
  182.    assert( gold_location([3,2]) ),
  183.  
  184.    retractall( pit_location(_) ),
  185.    assert( pit_location([4,4]) ),
  186.    assert( pit_location([3,3]) ),
  187.    assert( pit_location([1,3]) ).
  188.  
  189. init_agent :-
  190.    retractall( agent_location(_) ),
  191.    assert( agent_location([1,1]) ),
  192.    
  193.    visit([1,1]).
  194.  
  195. init_wumpus :-
  196.    retractall( wumpus_location(_) ),
  197.    assert( wumpus_location([4,1]) ).
  198.  
  199. visit(Xs) :-
  200.    visited_cells(Ys),
  201.    retractall( visited_cells(_) ),
  202.    assert( visited_cells([Ys|Xs]) ).
  203.  
  204. %------------------------------------------------------------------------------
  205. % Perceptors
  206.  
  207. %%% Institiation error!!!
  208.  
  209. %adj(X,Y) :-
  210. %    world_size(WS),
  211. %    ( X is Y+1, Y   < WS
  212. %    ; X is Y-1, Y-1 > 0
  213. %    ).
  214.  
  215. adj(1,2).
  216. adj(2,1).
  217. adj(2,3).
  218. adj(3,2).
  219. adj(3,4).
  220. adj(4,3).
  221.  
  222. adjacent( [X1, Y1], [X2, Y2] ) :-
  223.    ( X1 = X2, adj( Y1, Y2 )
  224.    ; Y1 = Y2, adj( X1, X2 )
  225.    ).
  226.  
  227. %adjacent([X1,Y],[X2,Y]) :-
  228. %    adj(X1,X2).
  229.  
  230. %adjacent([X,Y1],[X,Y2]) :-
  231. %    adj(Y1,Y2).
  232.  
  233. isSmelly(Ls1) :-
  234.    wumpus_location( Ls2 ),
  235.    adjacent( Ls1, Ls2 ).
  236.  
  237. isBleezy(Ls1) :-
  238.    pit_location( Ls2 ),
  239.    adjacent( Ls1, Ls2 ).
  240.  
  241. isGlittering( [X1, Y1] ) :-
  242.    gold_location( [X2, Y2] ),
  243.    X1 = X2,
  244.    Y1 = Y2.
  245.  
  246. bleezy(yes) :-
  247.    agent_location(AL),
  248.    isBleezy(AL).
  249. bleezy(no).
  250.  
  251. smelly(yes) :-
  252.    agent_location(AL),
  253.    isSmelly(AL).
  254. smelly(no).
  255.  
  256. glittering(yes) :-
  257.    agent_location(AL),
  258.    isGlittering(AL).
  259. glittering(no).
  260.  
  261. %------------------------------------------------------------------------------
  262. % Knowledge Base:
  263.  
  264. update_KB( [Stench,Bleeze,Glitter] ) :-
  265.    add_wumpus_KB(Stench),
  266.    add_pit_KB(Bleeze),
  267.    add_gold_KB(Glitter).
  268.  
  269. % if it would be "yes" -> it would mean the player is eaten ;]
  270. add_wumpus_KB(no) :-
  271.    %agent_location(L1),
  272.    %adjacent(L1, L2),
  273.    %assume_wumpus(no, L2).
  274.    agent_location([X,Y]),
  275.    world_size(WS),
  276.  
  277.    % Checking needed!!
  278.    % adj will freeze for (4,_) !!
  279.    
  280.    Z1 is Y+1, assume_wumpus(no,[X,Z1]),
  281.    Z2 is Y-1, assume_wumpus(no,[X,Z2]),
  282.    Z3 is X+1, assume_wumpus(no,[Z3,Y]),
  283.    Z4 is X-1, assume_wumpus(no,[Z4,Y]).
  284.  
  285. add_pit_KB(no) :-
  286.    agent_location([X,Y]),
  287.    Z1 is Y+1, assume_pit(no,[X,Z1]),
  288.    Z2 is Y-1, assume_pit(no,[X,Z2]),
  289.    Z3 is X+1, assume_pit(no,[Z3,Y]),
  290.    Z4 is X-1, assume_pit(no,[Z4,Y]).
  291.  
  292. % Checking needed!! If its not already in the KB !!!
  293. add_pit_KB(yes) :-
  294.    agent_location([X,Y]),
  295.    Z1 is Y+1, assume_pit(yes,[X,Z1]),
  296.    Z2 is Y-1, assume_pit(yes,[X,Z2]),
  297.    Z3 is X+1, assume_pit(yes,[Z3,Y]),
  298.    Z4 is X-1, assume_pit(yes,[Z4,Y]).
  299.  
  300. add_gold_KB(no) :-
  301.    gold_location(GL),
  302.    assume_gold(no, GL).
  303.    
  304. add_gold_KB(yes) :-
  305.    gold_location([X1,Y1]),
  306.    agent_location([X2,Y2]),
  307.    X1 = X2, Y1 = Y2,
  308.    assume_gold(yes, [X1,Y1]).
  309.  
  310. assume_wumpus(no, L) :-
  311.    retractall( isWumpus(_, L) ),
  312.    assert( isWumpus(no, L) ),
  313.    format("KB learn ~p - no Wumpus there!~n", [L]).
  314.  
  315. assume_wumpus(yes, L) :-
  316.    %wumpus_healthy, % Will be included ...
  317.    retractall( isWumpus(_, L) ),
  318.    assert( isWumpus(yes, L) ),
  319.    format("KB learn ~p - possibly the Wumpus is there!~n", [L]).
  320.  
  321. assume_pit(no, L) :-
  322.    retractall( isPit(_, L) ),
  323.    assert( isPit(no, L) ),
  324.    format("KB learn ~p - there's no Pit there!~n", [L]).
  325.  
  326. assume_pit(yes, L) :-
  327.    retractall( isPit(_, L) ),
  328.    assert( isPit(yes, L) ),
  329.    format("KB learn ~p - its a Pit!~n", [L]).
  330.  
  331. assume_gold(no, L) :-
  332.    retractall( isGold(_, L) ),
  333.    assert( isGold(no, L) ),
  334.    format("KB learn ~p - there's no gold here!~n", [L]).
  335.  
  336. assume_gold(yes, L) :-
  337.    retractall( isGold(_, L) ),
  338.    assert( isGold(yes, L) ),
  339.    format("KB learn ~p - GOT THE GOLD!!!~n", [L]).
  340.  
  341. permitted([X,Y]) :-
  342.    world_size(WS),
  343.    0 < X, X < WS+1,
  344.    0 < Y, Y < WS+1.
  345.  
  346. ask_KB(VisitedList, Action) :-
  347.    isWumpus(no, L),
  348.    isPit(no, L),
  349.    permitted(L),
  350.    not_member(L, VisitedList),
  351.    update_agent_location(L),
  352.    Action = L.
  353.  
  354. %------------------------------------------------------------------------------
  355. % Utils
  356.  
  357. not_member(X, []).
  358. not_member([X,Y], [[U,V]|Ys]) :-
  359.    ( X=U,Y=V -> fail
  360.    ; not_member([X,Y], Ys)
  361.    ).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement