Guest User

Untitled

a guest
May 27th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. /*
  2. For this environment simulator we will represent the environment
  3. in a dictionary of key/value pairs. This isn't the most efficient,
  4. but it's easy to implement.
  5. */
  6. % hashtable manipulation utilities
  7. % get the value for a key
  8. % get_attr(key,list,value).
  9. get_attr(Attr,[],[]).
  10. get_attr(Attr,[[Attr,Result]|T],Result).
  11. get_attr(Attr,[H|T],Result) :- get_attr(Attr,T,Result).
  12.  
  13. % set the value for a key
  14. % set_attr(key,value,list,newlist).
  15. set_attr(Attr,Value,[],[[Attr,Value]]).
  16. set_attr(Attr,Value,[[Attr,V]|T],[[Attr,Value]|T]).
  17. set_attr(Attr,Value,[[I,M]|T],[[I,M]|Result]) :-
  18. set_attr(Attr,Value,T,Result).
  19.  
  20. % display the world
  21. write_state([]).
  22. write_state([[A,B]|T]) :-
  23. write('['),write(A),write(','),write(B),write(']'),
  24. write_state(T).
  25.  
  26. /* Here we use some abstract rules that rely on agent specific rules.
  27. this allows us to keep the system generic, and promotes code reuse */
  28. % useful prototypes
  29. % change the state by applying an action rule to it.
  30. actuate(Action,State,NewState) :- action(Action,State,NewState).
  31. % example action rule. Called by actuate.
  32. action(end,State,NewState) :- fail.
  33.  
  34. agent_loop([],State,State,Models,Models).
  35. agent_loop([A|Agents],State,NewState,Models,NewModels) :-
  36. sensor(A,State,Percept),
  37. get_attr(A,Models,Model),
  38. agent_program(A,Percept,Action,Model,NewModel),
  39. write(A),write(' does '),write(Action),nl,
  40. actuate(Action,State,MidState),
  41. write(' After state:'),write_state(MidState),nl,
  42. set_attr(A,NewModel,Models,MidModels),
  43. agent_loop(Agents,MidState,NewState,MidModels,NewModels).
  44.  
  45. % main loop
  46. % start: load the initial state, and execute the first step.
  47. start :- initial_state(X),initial_models(M),step(X,M).
  48. % display the state, build the percept, get the agent's action
  49. % actuate that action, display the results
  50. % step on the new state.
  51. step(State,Models) :- !,write('Step? [Y|N] '),read(X),X='y',nl,
  52. write('Before state: '),write_state(State),nl,
  53. exogenous_action(State,EState),
  54. agents(A),agent_loop(A,EState,MidState,Models,NewModels),
  55. !,step(MidState,NewModels).
  56. % NB: step/1 is recursive, and uses tail-call recursion to pass the
  57. % state between calls. This is a common prolog technique. The execution
  58. % is actually iterative.
  59.  
  60. % vacuum world initial state
  61. % this is where the code gets specific to this domain.
  62. location(a). %this isn't really useful, but I wanted to make it clear
  63. %what my locations are.
  64. location(b).
  65. %this is important. This defines the relationship between the two
  66. %locations in this very simple universe.
  67. connect(a,b).
  68. %and here's my initial state.
  69. %The vacuum is at location a, a is dirty, b is clean.
  70. initial_state([[location,a],[a,dirty],[b,clean]]).
  71. initial_models([]).
  72. agents([sra]).
  73.  
  74. % use this hook to make the world mutate.
  75. exogenous_action(State,State).
  76.  
  77. % global actions - these are the things we can do in Vacuum World
  78. % these define how an action performed by the vacuum impacts the state.
  79. % clean: change the location's state to clean.
  80. action(clean,State,NewState) :- get_attr(location,State,L),
  81. set_attr(L,clean,State,NewState).
  82. % move_left: if my current location(L) is connected like so connect(X,L)
  83. % change my location to X.
  84. action(move_left,State,NewState) :- get_attr(location,State,L),
  85. connect(X,L),set_attr(location,X,State,NewState).
  86. % move_right: reverse of move_left.
  87. action(move_right,State,NewState) :- get_attr(location,State,L),
  88. connect(L,X),set_attr(location,X,State,NewState).
  89.  
  90. % vacuum perceptions
  91. % the vacuum's sensor lets it know which location it's in
  92. % and whether the current location is dirty or clean.
  93. % it cannot tell the cleanliness of other locations.
  94. % This is the Sensor for ALL agents in Vacuum World.
  95. % In a more advanced setting, we could have different sensors for each
  96. % agent.
  97. sensor(X,State,[[location,L],[L,CoD],[moves,Moves]]) :-
  98. get_attr(location,State,L),
  99. get_attr(L,State,CoD),possible_moves(State,Moves).
  100. possible_moves(State,[[left, LP],[right, RP]]) :-
  101. get_attr(location,State,L), can_go(left,L,LP), can_go(right,L,RP).
  102. can_go(left,Loc,R) :- connect(X,Loc),R = true.
  103. can_go(right,Loc,R) :- connect(Loc,X),R = true.
  104. can_go(_,_,R) :- R = false.
  105.  
  106. % finally, the agent program that implements my agent function.
  107. % this example is a simple reflex agent.
  108. % if my current location is dirty, clean it
  109. %
  110. % the simple reflex agent
  111. agent_program(sra,Percept,Action,_,[]) :-
  112. write('Percept:'),nl,
  113. write_state(Percept),nl,
  114. get_attr(location,Percept,L),
  115. get_attr(L,Percept,dirty),Action = clean.
  116. % if I can go right, go right.
  117. agent_program(sra,Percept,Action,_,[]) :- get_attr(moves,Percept,M),
  118. get_attr(right,M,true), Action = move_right.
  119. % if I can go left, go left
  120. agent_program(sra,Percept,Action,_,[]) :- get_attr(moves,Percept,M),
  121. get_attr(left,M,true), Action = move_left.
Add Comment
Please, Sign In to add comment