Pusdo Code Ai

1,140 views

Published on

This is Pusdo Code for AI logic

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,140
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Pusdo Code Ai

  1. 1. 1 INTRODUCTION 1
  2. 2. 2 INTELLIGENT AGENTS function TABLE -D RIVEN -AGENT( ¨§¦¤¢  ©  ¡¥£¡ ) returns an action static: ¨§¦¤¢  ©  ¡¥£¡ , a sequence, initially empty ¤¨© ¡ , a table of actions, indexed by percept sequences, initially fully specified append §¦§¢  ©  ¡¥£¡ to the end of¨§¦¤¢  ©  ¡¥£¡ % # quot; © ¥ $!! L OOKUP( , ¡ ©  ¡ ¥ £ ¡ (§'¨© ¨§¦¤¢  ) return $!¤ # quot; ©¥ Figure 2.8 function R EFLEX -VACUUM -AGENT( [ 4!$¨2 $!10) 3© © # quot; © ¥ quot; , ]) returns an action if 7© £ !§6 5 4!$¨2= 3© © then return 9¥ ¤¢3 8 else if # quot; © ¥ quot; $!!$0)@ = A then return © D C '1@ B # quot; © ¥ quot; E $!!$0)@ else if = then return ©G 0H¡ F Figure 2.10 function S IMPLE -R EFLEX -AGENT( §¦§¢  ©  ¡¥£¡ ) returns an action static: §(I§£ ¡ 3 , a set of condition–action rules P1¨0 % ¡© © I NTERPRET-I NPUT( 02§¢  ©  ¡¥£¡ ) % ¡ 3 P(I§£ RULE -M ATCH( , ¡ 3 ¡ © © ¤4§£ $Q0 ) %$!! # quot; © ¥ RULE -ACTION[ ] I§£ ¡ 3 return $!¤ # quot; ©¥ Figure 2.13 2
  3. 3. 3 function R EFLEX -AGENT-W ITH -S TATE( ¨§¦¤¢  ©  ¡¥£¡ ) returns an action static: $Q0 ¡© © , a description of the current world state §(I§£ ¡ 3 , a set of condition–action rules #$!¤ quot; ©¥ , the most recent action, initially none % ¡© © P1¨0 §¦§¢  $!¤ $Q0 ©  ¡¥£¡ # quot; ©¥ ¡© © U PDATE -S TATE( , , ) % ¡ 3 P(I§£ RULE -M ATCH( , ¡ 3 ¡ © © ¤4§£ $Q0 ) %$!! # quot; © ¥ RULE -ACTION[ ] I§£ ¡ 3 return $!¤ # quot; ©¥ Figure 2.16
  4. 4. 3 SOLVING PROBLEMS BY SEARCHING function S IMPLE -P ROBLEM -S OLVING -AGENT( R§¦¤¢  ©  ¡¥£¡ ) returns an action inputs: R§¦¤¢  ©  ¡¥£¡, a percept static: 2 S¡ , an action sequence, initially empty $Q0 ¡© © , some description of the current world state 1)'C quot; , a goal, initially null §(§'¦  T ¡ quot; £ , a problem formulation U PDATE -S TATE( , ) P1¨0 ©  ¡¥£¡ ¡© © % ¡© © 02§¢  $Q0 if is empty then do 2¤ S¡ F ORMULATE -G OAL( $)C % quot; ) 1¨0 ¡© © %UT¤¤2R  ¡ quot;£ F ORMULATE -P ROBLEM( quot; ¡© © $)'C $Q0 , ) T§(§'quot;¦£  ¡ S EARCH( ) V2¤ % S¡ S2¡ F IRST( ) $!! % # quot; © ¥ R EST( ) S¦¡ W2 % S¡ return $!¤ # quot; ©¥ Figure 3.2 function T REE -S EARCH( , T ¡ quot; £ §(§'¦  7 C¡© £© $Q12!0 ) returns a solution, or failure initialize the search tree using the initial state of §(§'¦  T ¡ quot; £ loop do if there are no candidates for expansion then return failure choose a leaf node for expansion according to $Q12!0 7 C¡© £© if the node contains a goal state then return the corresponding solution else expand the node and add the resulting nodes to the search tree Figure 3.9 4
  5. 5. Figure 3.17 if then return £ else return ¡0I3 6$¤G qquot; 3 sQ©4¤¥ r a¡££ 3 ¥¥ qquot;© 3 ¢22§I0)quot; s¨I¤¥ else if then return © 42¤2£ 3¡ 0I3 6$¤G v© I0¤2£ ¡ £ uw 3 ¡ if = then true Pr¢22§£I0)quot; qsquot;¨©I3¤¥ % a¡£ 3 ¥¥ s¨I¤¥ © I0¤2£ qquot;© 3 3¡ R ECURSIVE -DLS( © T , , ) 6p6 §¡(§'¦  $¤0§¡§0¢30 T quot;£ £ quot; ¥¥ RtI0§2£ % © 3 ¡ else for each in E XPAND( , ) do T¡ quot;£ ¤¤'¦  ¡')# a quot; £$¤2¤§0¢0 quot;¡¥¥ 3 else if D EPTH[ ]= then return quot;© 3 qsQ4¤¥ © 6pTH ¡ a quot; ')b# if G OAL -T EST[ ¡ a quot; ')# ](S TATE[ ]) then return S OLUTION( ) ¡ a quot; ')b# T ¡ quot; £ §(§'¦  false P¢22§I0)quot; ¨I¥ % r a¡££ 3 ¥¥ qquot;© 3 function R ECURSIVE -DLS( , , ) returns a solution, or failure/cutoff © T 6p6 T ¡ quot; £ ¡ a quot; §(§'¦  ')b# return R ECURSIVE -DLS(M AKE -N ODE(I NITIAL -S TATE[ 6p6 © T T ¡ quot; £ §(§'¦  T ¡ quot; £ §(§'¦  ]), , ) function D EPTH -L IMITED -S EARCH( , ) returns a solution, or failure/cutoff © T 6pH T ¡ quot; £ ¤¤2R  Figure 3.12 return §$¤0§§0¢0 £ quot;¡¥¥ 3 add to £ quot;¡¥¥ 3 §$¤0§§0¢0 D EPTH[ ] D EPTH[ ]+1 ')# ¡ a quot; % , ) $!¤ ')b# # quot; ©¥ ¡ a quot; PATH -C OST[ ] PATH -C OST[ ] + S TEP -C OST( ¡ a quot; ')# , % ACTION[ ] # quot; ©¥ $!¤i% PARENT-N ODE[ ] ¡'a)quot;b#h% S TATE[ ] © I0¤2g% 3¡£ a new N ODE % f ]) do )b# ¡ a quot; for each , ¤¤'¦  T ¡ quot; £in S UCCESSOR -F N[ ](S TATE[ e 3¡ # quot; ©¥ ¦© I0¤2£ $!¤ d the empty set % £ quot;¡¥¥ 3 c§$¤0§§0¢0 ) returns a set of nodes T ¡ quot; £  X ¡ a quot; ¤¤'¦¨)b# function E XPAND( 'I6§0G ¡ C # £ I NSERT-A LL(E XPAND( ¤¤'¦  ')# T ¡ quot; £ ¡ a quot; , ), ) `'I6§0G % ¡ C # £ then return S OLUTION( ) ¡ a quot; ')# if G OAL -T EST[ ')b# ¡ a quot; ] applied to S TATE[ ] succeeds ¤¤'¦  T ¡ quot; £ R EMOVE -F IRST( ) I'H§0G ¡ C # £ % ¡ a quot; P')# if E MPTY ?( ) then return failure ¡ C # £ I'6§§G loop do ) 'I6§0G ¡ C # £ I NSERT(M AKE -N ODE(I NITIAL -S TATE[ T ¡ quot; £ §(§'¦  ]), % ¡ C # £ `I'H§0G ) returns a solution, or failure ¡ C # £ GX T ¡ quot; £ 'I6§0HY§(§'¦  function T REE -S EARCH( 5
  6. 6. 6 Chapter 3. Solving Problems by Searching function I TERATIVE -D EEPENING -S EARCH( ¤¤'¦  T ¡ quot; £ ) returns a solution, or failure inputs: §(§'¦  T ¡ quot; £ , a problem for xHa % D©  ¡ 0 to y do RtI0§2£ % © 3 ¡ D EPTH -L IMITED -S EARCH( ¤¤'¦  T ¡ quot; £ , !R'a D©  ¡ ) uv© I0¤2£ w 3¡ if cutoff then return tI0§0£ © 3 ¡ Figure 3.19 function G RAPH -S EARCH( 'I6§0H€¤¤'¦  ¡ C # £ GX T ¡ quot; £ ) returns a solution, or failure 2$@¥ % a ¡ quot; an empty set % ¡ C # £ `I'H§0G I NSERT(M AKE -N ODE(I NITIAL -S TATE[ ]), ) T ¡ quot; £ §(§'¦  ¡ C # £ 'I6§0G loop do if E MPTY ?( I'6§§G ¡ C # £ ) then return failure P')# % ¡ a quot; R EMOVE -F IRST( ) I'H§0G ¡ C # £ if G OAL -T EST[ T ¡ quot; £ ¤¤'¦  ](S TATE[ ¡ a quot; ')# ]) then return S OLUTION( ¡ a quot; ')b# ) if S TATE[ ¡ a quot; ')# ] is not in then a ¡ quot; 2$@¥ add S TATE[ ] to )b# ¡ a quot; 2$@¥ a ¡ quot; % ¡ C # £ `'I6§0G I NSERT-A LL(E XPAND( , ), ) T ¡ quot; £ ¡ a quot; ¤¤2R  ')# ¡ C # £ 'I6§0G Figure 3.25
  7. 7. 7 Figure 4.13 £ quot; D C ¡ # % © #¡££ 3 $2¢1§‰hRR§0§I¤¥ ] © #¡££ 3 R¤2§I¥ if VALUE[neighbor] VALUE[current] then return S TATE[ j a highest-valued successor of © #¡££ 3 ¢¤2§I¥ % £ quot; D C ¡ V$24$@¤b# loop do ]) T ¡ quot; £ ¤¤'¦  M AKE -N ODE(I NITIAL -S TATE[ % © #¡££ 3 R¢¤2§I¥ , a node £$24$@¤b# quot; D C ¡ local variables: , a node R§0§I¤¥ © #¡££ 3 inputs: , a problem ¤¤'¦  T ¡ quot; £ function H ILL -C LIMBING( ) returns a state that is a local maximum ¤¤'¦  T ¡ quot; £ Figure 4.6 if then return © 42¤2£ 3¡ 0I3 6$¤G v© I0¤2£ ¡ £ uw 3 ¡ , [ ] RBFS( , , 1$6$§¤© $i§6pH ¤c@gƒ 0¤2 ¤¤2R  ‘ ¡ d © # £ ¡ X © T G ˆ hf © ¡ T ¡ quot; £ ) % 0§0 ‚ tI0§2£ ©¡ © 3 ¡ the second-lowest -value among £ quot;¡¥¥ 3 §$¤0§§0¢0 ‚ Pe61b§¤¨t1 % ¡ d © # £ ¡ © if then return ©¡ , [ 0§0 ‚ 2It6$¤G ] ¡ £ 3 6p6t g™0¤0 P‚ © T G ˜ —©¡ – the lowest -value node in £ quot;¡¥¥ 3 §$¤0§§0¢0 ‚ % ©¡ R0¤2 repeat [s] ‘— ¡ a quot; – X ‘ ˆ ” ’ ‘ ˆ ˆ ‡ … ƒ H¤')# P‚ §)s“•“)sbH‰1†„% ‚ for each in do £ quot;¡¥¥ 3 §1¤0¤00¢0 if is empty then return , y ¡2£43 6$¤G §$§0¤§2R2 £ quot;¡¥¥ 3 E XPAND( , ) T¤¡¤'¦  ')# quot;£ ¡ a quot; c§$¤0§§0¢0 % £ quot;¡¥¥ 3 if G OAL -T EST[ ]( ) then return¡ a quot; ')b# ¡© © $Q0 ¤¤'¦  T ¡ quot; £ ‚ function RBFS( , , ) returns a solution, or failure and a new -cost limit © T Hp6 G ')b# ¤¤'¦  ¡ a quot; T ¡ quot; £ RBFS( y , M AKE -N ODE(I NITIAL -S TATE[ T ¡ quot; £ §(§'¦  ]), ) ¤¤'¦  T ¡ quot; £ function R ECURSIVE -B EST-F IRST-S EARCH( ) returns a solution, or failure T ¡ quot; £ §(§'¦  EXPLORATION 4 INFORMED SEARCH AND
  8. 8. 8 Chapter 4. Informed Search and Exploration function S IMULATED -A NNEALING( , T ¡ quot; £ §(§'¦  ¡ 3 a ¡ D ¥ I$2¢§¤ ) returns a solution state inputs: ¤¤'¦  T ¡ quot; £ , a problem (I$¦¢¤¤ ¡ 3 a ¡ D ¥ , a mapping from time to “temperature” local variables: R§0§I¤¥ © #¡££ 3 , a node , a node 10b# © k¡ l , a “temperature” controlling the probability of downward steps M AKE -N ODE(I NITIAL -S TATE[ R¢¤2§I¥ % © #¡££ 3 ¤¤'¦  T ¡ quot; £ ]) for 1 to do y R© % [ ] © 431a2¢¤§g% l ¡ ¡ D¥ R§2£§43¤¥ © #¡ £ l if = 0 then return R#§2§43¤¥ © ¡££ a randomly selected successor of 10b# % © k¡ ©R#§¡2£§£43¤¥ VALUE[ ] – VALUE[ 10¡b# © k ] % om n if 0 then©$k2¡‰#hR©R§¡0§£I¤¥ % # £ 3 ˜ gm n tsfrPip s q else $2‰#hRR§0§I¤¥ © k¡ only with probability % © #¡££ 3 Figure 4.17 function G ENETIC -A LGORITHM( $!!$I04  # quot; © 3   quot; , F ITNESS -F N) returns an individual inputs: $!1I§4  # quot; © 3   quot; , a set of individuals F ITNESS -F N, a function that measures the fitness of an individual repeat empty set $!$@I§4  ¤b# % # quot; © 3   quot; u ¡ $!!$I04  # quot; © 3   quot; loop for from 1 to S IZE( ) do 1quot;!©$@4R§4  # 3  quot; R ANDOM -S ELECTION( vk % , F ITNESS -F N) #$!$@I§4  quot; © 3   quot; R ANDOM -S ELECTION( w7 % , F ITNESS -F N) 7 k R EPRODUCE( , ) 6'§¥ % a D a D %v@6'¤¥ if (small random probability) then M UTATE( a D @H¤¥ ) add to $!$@I§4  ¤‰# # quot; © 3   quot; u ¡ @6'¤¥ a D $!$@I§4  ¤bgx$!!$I04  # quot; © 3   quot; u ¡ # % # quot; © 3   quot; until some individual is fit enough, or enough time has elapsed return the best individual in $!!$I04  # quot; © 3   quot; , according to F ITNESS -F N 7 k function R EPRODUCE( , ) returns an individual 7 k inputs: , , parent individuals % # L ENGTH( ) k % `¥ random number from 1 to # k return A PPEND(S UBSTRING( , 1, ), S UBSTRING( , ¥ # {y¥ 7 z ’ , )) Figure 4.21
  9. 9. 9 function O NLINE -DFS-AGENT( ) returns an action | | inputs: , a percept that identifies the current state static: © I0¤2£ 3¡ , a table, indexed by action and state, initially empty a22£$@§2‰I3 ¡ quot;  k¡ # , a table that lists, for each visited state, the actions not yet tried a2¡e9¤¦!1¤e2‰I3 ¥ £© 9 ¥ # , a table that lists, for each visited state, the backtracks not yet tried , , the previous state and action, initially null | if G OAL -T EST( ) then return §¨0  quot;© | if is a new state then % | 22$@00‰I3 a ¡ £ quot;   k ¡ # [ ] ACTIONS( ) | if is not null then do [ , ] | h% tI0§2£ © 3 ¡ | 2¡e¤¦!©19¤¥e2‰I3 a 9¥ £ add to the front of # [ ] if [ ] is empty then | 22$R§0bI3 a ¡ £ quot;   k ¡ #  §quot;¨©2 if | 2e¤¦!1¤e2‰I3 a¡ 9 ¥ £© 9 ¥ # [ ] is empty then return else 3 | © I0¤¡2£ an action such that [ , ] = P OP( % w | 2e¤¦!1¤e2‰I3 a¡ 9 ¥ £© 9 ¥ # [ ]) else P OP( [ ]) ¡ quot;  k¡ # | a22£$@§2‰I3 v % | h` % return Figure 4.25 function LRTA*-AGENT( ) returns an action | | inputs: , a percept that identifies the current state static: © I0¤2£ 3¡ , a table, indexed by action and state, initially empty } , a table of cost estimates indexed by state, initially empty , , the previous state and action, initially null | if G OAL -T EST( ) then return §¨0  quot;© | } } | ~% | ” if is a new state (not in ) then [ ] ( ) unless is null | h% tI0§2£ [ , ] © 3¡ h [ ] foƒ % } LRTA*-C OST( , , 3¡ © 42¤2£ [ , ], } ) ƒs ‚ ¦ € ACTIONS v % | } © I0¤2£ | 3¡ | an action in ACTIONS( ) that minimizes LRTA*-C OST( , , [ , ], ) | h` % return } | function LRTA*-C OST( , , , ) returns a cost estimate | iQc” ‘ „ˆ if is undefined then return else return — | „ – ˆg‘ | 02eQˆ … ‡ ’ „X †X „ Figure 4.29
  10. 10. CONSTRAINT 5 SATISFACTION PROBLEMS ¥   function BACKTRACKING -S EARCH( ) returns a solution, or failure return R ECURSIVE -BACKTRACKING( , ) ¥ W‰   Š function R ECURSIVE -BACKTRACKING( ,  Q¥ ©R§¡gTRe@02$ # # C ) returns a solution, or failure if is complete then return R¤g¢e@001 © #¡ T # C ©R§¡gRe@02$ # T # C S ELECT-U NASSIGNED -VARIABLE(VARIABLES[ ],   © #¡ T # C ¥ R¤g¢e@001 Q¤¥ , )  % £ V$$d for each in O RDER -D OMAIN -VALUES( , , ) do   © #¡ T # C £ ¥ R§gR$20$ $1d 3 ¡Rt1$d if is consistent with R#§gR$20$ © ¡ T # C Q¥   according to C ONSTRAINTS[ ] then ¡ 3 Rt1$d add = to #¡ T C ©¢¤g¢#e@00$ Š R3 $$d £$1d ‰ ¡ C  Q¤¥ ©¢#¤¡gT¢#e@00$ R ECURSIVE -BACKTRACKING( , ) RtI0§2£ % © 3 ¡ if © I30¤2£ ¡ then return 2£It6$¤G uvtI0§0£ ¡ 3 w © 3 ¡ remove = T # C ©¢#¤¡gR$@00$ from £ Š ¡R3 $$d $$d ‰ return 2It6$¤G ¡ £ 3 Figure 5.4 10
  11. 11. 11 function AC-3( Q¥   ) returns the CSP, possibly with reduced domains inputs:   Q¥ , a binary CSP with variables X    Ž Š‘o‹ X g‹ X i‰ Œ ‹ local variables: R¤R¤S ¡ 3¡ 3 , a queue of arcs, initially all the arcs in Q¥   while §RS ¡ 3¡ 3 is not empty do % “ R‘ p‹ X o‹ ˆ ’ R EMOVE -F IRST( ) ¡ 3¡ 3 R¤RS if R EMOVE -I NCONSISTENT-VALUES( “ ‹ X’ ‹ ) then for each in N EIGHBORS[ ] do ” †‹ ’ o‹ add ( ’ ‹ X ™‹ ) to” R¤RS ¡ 3¡ 3 function R EMOVE -I NCONSISTENT-VALUES( “ ‹ X’ ‹ ) returns true iff we remove a value $¤•v2$$o¤2£ ¡ G % a¡ d quot; T¡ k for each in D OMAIN[ ] do ’ ‹ if no value in D OMAIN[ ] allows ( , ) to satisfy the constraint between – “ ‹ 7 k ’ ‹ and “ ‹ k then delete from D OMAIN[ ]; ’ ‹ ¡R3§£©h%va2¡$d$quot;o¤2£ T¡ return 2$$o¤2£ a¡ d quot; T¡ Figure 5.9 function M IN -C ONFLICTS( , ¨0 ioT ¥  ¡© k   ) returns a solution or failure inputs: Q¥   , a constraint satisfaction problem ¨0 ioT  ¡© k , the number of steps allowed before giving up R¢¤2§I¥ % © #¡££ 3 an initial complete assignment for ¥   for = 1 to ¨0 ioT  ¡© k do  ¥ if is a solution for © #¡££ 3 ¢¤2§I¥ then return © #¡££ 3 R§0§I¤¥ V$1d % £ a randomly chosen, conflicted variable from VARIABLES[ ]¥   £ $1d d the value for `3 $1d % ¡ that minimizes C ONFLICTS( , , 3, £  Q¤¥ ©R#¤¡2£§£I¥ d 1$d ) set R§¡2£§£43¤¥ © # in Rt1$d w 1$d ¡ 3 £ return 2It6$¤G ¡ £ 3 Figure 5.11
  12. 12. 6 ADVERSARIAL SEARCH function M INIMAX -D ECISION( 1¨0 ¡© © ) returns $!¤—$ # quot; ©¥ # inputs: ¨$¨0 ¡© © , current state in game % wd M AX -VALUE(state) return the # quot; ©¥ $!¤ in S UCCESSORS( $Q0 ¡© © ) with value d function M AX -VALUE( 1¨0 ¡© © ) returns Rt1$˜!6t6Ip ¡ 3 d 7© © 3 if T ERMINAL -T EST( 1¨0 ¡© © ¡© © ¨$¨2 ) then return U TILITY( ) y gwd ™ % X R for in S UCCESSORS( ) do¡© © $Q0 % wd M AX(v, M IN -VALUE( )) return d function M IN -VALUE( $Q0 ¡© © ) returns Rt$1˜!H 6Ix ¡ 3 d 7© © 3 if T ERMINAL -T EST( ¡© © 1¨0 ) then return U TILITY( ¡© © ¨$¨2 ) y wd % X R for in S UCCESSORS( ) do¡© © $Q0 % wd M IN(v, M AX -VALUE( )) return d Figure 6.4 12
  13. 13. 13 function A LPHA -B ETA -S EARCH( ) returns an action ¨$¨2 ¡© © inputs: ¨$¨0 ¡© © , current state in game % wd M AX -VALUE( , , ) ¡© © y ™ ¨$¨0 y ’ return the # quot; ©¥ $!¤ in S UCCESSORS( $Q0 ¡© © ) with value d function M AX -VALUE( › š 1¨0 ¡© © , , ) returns R3 $$p6t6!4x ¡ d 7© © 3 inputs: ¨$¨0 ¡© © , current state in game , the value of the best alternative for š MAX 1¨0 ¡© © along the path to , the value of the best alternative for › MIN along the path to ¨$¨2 ¡© © if T ERMINAL -T EST( ¡© © 1¨0 ) then return U TILITY( ¡© © ¨$¨2 ) y gwd ™ % for X R in S UCCESSORS( ) do ¡© © $Q0 wd % M AX(v, M IN -VALUE( , , )) › š ›Ÿžœ if then return d % šM AX( , v) š d return function M IN -VALUE( › š $Q0 ¡© © , , ) returns 3 $1˜6 6Ip ¡ d 7© © 3 inputs: ¨$¨0 ¡© © , current state in game , the value of the best alternative for š MAX 1¨0 ¡© © along the path to , the value of the best alternative for › MIN along the path to ¨$¨2 ¡© © if T ERMINAL -T EST( ¡© © 1¨0 ) then return U TILITY( ¡© © ¨$¨2 ) y ~wd ’ % for X R in S UCCESSORS( ) do ¡© © $Q0 wd % M IN(v, M AX -VALUE( , , )) › š š jžœif then return d › % › M IN( , v) d return Figure 6.9
  14. 14. 7 LOGICAL AGENTS function KB-AGENT( §¦§¢  ©  ¡¥£¡ ) returns an $!! # quot; © ¥ static: E, a knowledge base ¢¡ © , a counter, initially 0, indicating time T ELL( E ¢¡ , M AKE -P ERCEPT-S ENTENCE( , )) © ¨§¦¤¢  ©  ¡¥£¡ $!! % # quot; © ¥A SK( E , M AKE -ACTION -Q UERY( )) ¢¡ © T ELL( E¢¡ , M AKE -ACTION -S ENTENCE( , )) © 1! # quot; © ¥ +1 h© © % #$!©¤¥ quot; return Figure 7.2 function TT-E NTAILS ?( , ) returns E ¢¡ or š ¡ 3£ R§© ¡ t1G inputs: , the knowledge base, a sentence in propositional logic E ¢¡ š , the query, a sentence in propositional logic quot; T 7 %P $2g10a list of the proposition symbols in E ¢¡ and š return TT-C HECK -A LL( , , , )E x¡ quot; T 7 — – $2g10 š function TT-C HECK -A LL( , , , E ¢¡ ¤')oT $2g10 š ¡ a quot; quot; T 7 ) returns ¡ 3£ R§© or $¤G ¡ if E MPTY ?( $2g10 quot; T 7 ) then if PL-T RUE ?( E , x¡ ¡ a quot; ¤)oT ) then return PL-T RUE ?( , ¡ a quot; §')oT š ) else return R§© ¡ 3£ else do % £ F IRST( $2g10 ); quot; T 7 R EST( ) % ©¡ R0§2£ quot; T 7 $2g10 return TT-C HECK -A LL( , , , E XTEND( 0¤2£ š ¢¡ ©¡ E ¤')o§eR§§X £ ¡ a quot; TX ¡ 3£© ) and TT-C HECK -A LL( , , ©0¤2£ š ¢¡ , E XTEND(¡ E ¤¡')o§¤t$¤HX £ a quot; T X ¡ G ) Figure 7.12 14
  15. 15. 15 function PL-R ESOLUTION( , ) returns E ¢¡or š ¡ 3£ R§© ¡ $¤G inputs: , the knowledge base, a sentence in propositional logic E ¢¡ š , the query, a sentence in propositional logic %f¤I$@¥ ¡ 3 the set of clauses in the CNF representation of W€•¢¡ š ¥ ¤ E ŠW‰ ¦§‰# % u¡ loop do for each , in ¡ 3 §I$@¥do “ § ’ § f!R¤$t1¤¤2£ % © # ¡ d quot; ¡ PL-R ESOLVE( , ) “ § ’ § if R§ed $¤§0£ © #¡ quot;¡ contains the empty clause then return R§© ¡ 3£ ©¢#¤¡ed $¤§¡2£©¨—§‰h¦§‰# quot; u¡ # % u¡ ¡ $¤G if §I$@•«§‰# ¡ 3 ¥ ª u ¡ then return §‰#™c§I$@¬`¤¤41¤¥ u ¡ ¨ ¡ 3 ¥ % ¡ 3 Figure 7.15 function PL-FC-E NTAILS ?( , ) returns E ¢¡ or S R§!© ¡ 3£ $¤G ¡ inputs: E, the knowledge base, a set of propositional Horn clauses ¢¡ S , the query, a proposition symbol local variables: RI$2¥ © # 3 quot; , a table, indexed by clause, initially the number of premises 22§§)6 a¡££¡ G # , a table, indexed by symbol, each entry initially t1G ¡ ¤I a #¡ C , a list of symbols, initially the symbols known to be true in E ¢¡ while eb¤') a #¡ C is not empty do P OP(b§I) a #¡ C ) % ­  unless   22§¤H a¡££¡ G # [ ] do ¡R3§£©g%   22§¤)6 [ ]a¡££¡ G # for each Horn clause in whose premise ¥   appears do decrement ¥ RI$0¥ [ ] © # 3 quot; if ¥ ¢410¥ © # 3 quot; [ ] = 0 then do ¥ if H EAD[ ] = then return S R§!© ¡ 3£ P USH(H EAD[ ], ) ¥ b§I) a #¡ C return $¤G ¡ Figure 7.18
  16. 16. 16 Chapter 7. Logical Agents function DPLL-S ATISFIABLE ?( ) returns ¡ 3£ R§!© or $¤G ¡ inputs: , a sentence in propositional logic f¤I$@¥ % ¡ 3 the set of clauses in the CNF representation of P $2g10 % quot; T 7 a list of the proposition symbols in return DPLL( , , ) — – t12g$2 §I$¤¥ quot; T 7 ¡ 3 function DPLL( §')oT $2g10 §I$@¥ ¡ a quot; quot; T 7 ¡ 3 , , ) returns ¡ 3£ §!© or ¡ ¤t$¤G if every clause in is true in ¡ 3 ¤I$@¥ then return ¡ 3£ R§© ¡ a quot; §')oT if some clause in is false in ¡ 3 §I$¤¥ then return ¡ ¤t$¤G ¡ a quot; §')oT ® PRt1$d % ¡ 3 ¤')oT ¤¤41¤¥ t$¦g$0 ¡ a quot; ¡ 3 quot; T 7 , F IND -P URE -S YMBOL( , , ) ® ® t12g$2 ¤I$@¥ quot; T 7 ¡ 3 ¤)o§Rt$1§X £ ¡ a quot; T X ¡ 3 d if is non-null then return DPLL( , – , E XTEND( ) ® PRt1$d % ¡ 3 §')oT §I$@¥ ¡ a quot; ¡ 3 , F IND -U NIT-C LAUSE( , ) ® ® t12g$2 ¤I$@¥ quot; T 7 ¡ 3 ¤¡a)quot;oT§X¡Rt$1d§X £ 3 if is non-null then return DPLL( , – , E XTEND( ) % ® F IRST( ); % ©¡ R0¤2£ $2g10 R EST( quot; T 7 ) quot; T 7 t$¦g$0 return DPLL( , 0§2£ §I$¤¥ © ¡ ¡ 3 , E XTEND( , , )) or §')oT R§© ® ¡ a quot; ¡ 3£ DPLL( , 0§2£ §I$¤¥ © ¡ ¡ 3 , E XTEND( , , )) ¤')oT $¤G ® ¡ a quot; ¡ Figure 7.21 function WALK SAT( , , k ¡ 3   y¯ ioT   §I$@¥ ) returns a satisfying model or ¡ £ 3 2It6$¤G inputs: ¤I$@¥ ¡ 3 , a set of clauses in propositional logic   , the probability of choosing to do a “random walk” move, typically around 0.5   y¯ ioT k , number of flips allowed before giving up a random assignment of % ¡ a quot; e¤')oT / to the symbols in ¡ ¡ 3£ $¤G R§!© ¡ 3 ¤I$@¥ for to k R  y¯ ioT do z w §¡I3$¤¥ if satisfies ¡ a quot; §')oT then return ¡ a quot; ¤)oT % ¡ 3 P¤41¤¥ a randomly selected clause from that is false in ¡ 3 ¤I$@¥ ¤')oT ¡ a quot;   with probability flip the value in of a randomly selected symbol from ¡ a quot; §')oT I$@¥ ¡ 3 else flip whichever symbol in I$¤¥ ¡ 3 maximizes the number of satisfied clauses return 2It6$¤G ¡ £ 3 Figure 7.23
  17. 17. 17 function PL-W UMPUS -AGENT( 02§¢  ©  ¡¥£¡ ) returns an $!¤ # quot; ©¥ inputs: ©  ¡¥£¡ R§¦¤¢  , a list, [ , £¡©© ¡ °¡¡£ D¥ #¡© ¤¨6 eC i020§ ¤b§0 , ] static: , a knowledge base, initially containing the “physics” of the wumpus world E ¢¡ , , quot; £ #$!©$Q©R#§¡Q§1quot; 7 k , the agent’s position (initially 1,1) and orientation (initially © D C $@§£ ) 2¨606id a¡© , an array indicating which squares have been visited, initially $¤G ¡ $!¤ # quot; ©¥ , the agent’s most recent action, initially null $@  # , an action sequence, initially empty update , , a¡© # quot; © © #¡ £ 2¨606id $!1¨R§§$quot; 7 k , based on # quot; © ¥ $!e if then T ELL( ´ $c± ¢¡ ³ ² E, ) else T ELL( , )¤¤2 D¥ #¡© ´ $²“—¥ ¢¡ ³ ± E if then T ELL( ´ $xµ ¢¡ ³ ² , E ) else T ELL( , ) i002§ ¡ °¡¡£ ´ ³$¢µ—¥ ¢¡ ² E if then '¦$~­$!¤ £ C % # quot; ©¥ ¤!6t$C £ ¡©© %#1quot;!©¥ else if is nonempty then P OP( # $R  ) # $@  else if for some fringe square [ , ], A SK( ¶ , ) is or ‘ ³“ ’ —„¤ ³“ ’ £ ¥ ˆ ¢¡ · ¥ ¡ 3£ R§!© E for some fringe square [ , ], A SK( ¶ , ) is ¡t1G then do ‘ ³“ ’ ·  ³“ ’ £ ˆ ¢¡ ¸ E % # $R  A*-G RAPH -S EARCH(ROUTE -P ROBLEM([ , ], #$!$Q©R§Q§1quot; 7 k quot; © #¡ £ , [ , ], ¶ a¡© ¦606id )) P OP( ) $@  # ­$!e % # quot; © ¥ else a randomly chosen move $!! % # quot; © ¥ return $!¤ # quot; ©¥ Figure 7.26
  18. 18. 8 FIRST-ORDER LOGIC 18
  19. 19. 9 INFERENCE IN FIRST-ORDER LOGIC ¹ 7 k function U NIFY( , , ) returns a substitution to make and identical k 7 k inputs: , a variable, constant, list, or compound 7 , a variable, constant, list, or compound , the substitution built up so far (optional, defaults to empty) ¹ if = failure then return failure ¹ k else if = then return 7 ¹ k else if VARIABLE ?( ) then return U NIFY-VAR( , , ) ¹ 7 k 7 else if VARIABLE ?( ) then return U NIFY-VAR( , , ) ¹ k 7 else if C OMPOUND ?( ) and C OMPOUND ?( ) then k 7 return U NIFY(A RGS[ ], A RGS[ ], U NIFY(O P[ ], O P[ ], ))k 7 k 7 ¹ k else if L IST ?( ) and L IST ?( ) then 7 k return U NIFY(R EST[ ], R EST[ ], U NIFY(F IRST[ ], F IRST[ ], )) 7 k 7 ¹ else return failure function U NIFY-VAR( , , ) returns a substitution ¹ k $1d £ inputs: $$d £ , a variable k , any expression , the substitution built up so far ¹ if ¹ U»¦Š d º £ $$I$$1d ‰ then return U NIFY( , , ) ¹ k $$d ¹U»¦Š $14º )‰ else if d ¼ then return U NIFY( , , ) ¹ $1d £$$d else if O CCUR -C HECK ?( k $$d £ , ) then return failure else return add / to ¹ Š k $$d ‰ £ Figure 9.2 19
  20. 20. 20 Chapter 9. Inference in First-Order Logic function FOL-FC-A SK( , ) returns a substitution or E x¡ š ¡ t1G inputs: , the knowledge base, a set of first-order definite clauses E ¢¡ , the query, an atomic sentence š local variables: ¤b# u¡ , the new sentences inferred on each iteration repeat until §‰# u¡ is empty Š W‰ —¤b# % u¡ for each sentence Ex¡ £ in do %R$S ‘ ½•‘   ¤  `Œ §ˆ   ¤   S TANDARDIZE -A PART( ) £ ¤  ¤ Œ   ¹ for each such that S UBST( ,¹ ) = S UBST( , ‘   ¤ |Œ   ¹   | ‘   ¤ ) ) Ex¡ | ‘ sXX |Œ      for some in S ¹ S UBST( , ) % | S | S if is not a renaming of some sentence already in or E ¢¡ u¡ ¤b# then do add to §‰# | S u¡ š | S U NIFY( , ) % ¾ ¾ 6$¤G ¾ if is not then return add to ¢¡ E ¤‰# u¡ return $¤G ¡ Figure 9.5 function FOL-BC-A SK( , , ) returns a set of substitutions E ¢¡ quot; ¹ t$)C inputs: , a knowledge base E ¢¡ $)'C quot; , a list of conjuncts forming a query ( already applied) ¹ , the current substitution, initially the empty substitution ¹ Š W‰ local variables: §¤y0R1 £¡ u # , a set of substitutions, initially empty $)C if quot; is empty then return '1‰ Š ¹ % | S S UBST( , F IRST( )) ¹ $)C quot; for each sentence in where S TANDARDIZE -A PART( ) = E ¢¡ £ £ €Œ §ˆ ¤   ‘ Àž‘   ¤ ) ¿ ½   % | ¹ and U NIFY( , ) succeeds | S S % quot; u ¡ ft1)'C ¤b# R EST( ) Á  X    f‘ sX Œ   – — $)'C quot; % £¡ u # f§¤y0R1 FOL-BC-A SK( , , C OMPOSE( , )) E ¢¡ $)quot;C §‰#u¡ ¹ | ¹ £¡ u # §§y0R$¨ return §¤y0¢$ £¡ u # Figure 9.9 procedure A PPEND( # quot; © 3 # © # quot; ° k $!$¢R6!R$0¥ i 7 ) , , , ) 6$¦© % £ G LOBAL -T RAIL -P OINTER() if k ) —– = and U NIFY( , ) then C ALL( ) ° i 7 # quot; © 3 # © # quot; $!1R¢6R10¥ R ESET-T RAIL( ) 6$¦© £ % v N EW-VARIABLE(); N EW-VARIABLE(); N EW-VARIABLE() % Ãk % y° k i k if U NIFY( , [ — ]) and U NIFY( , [ — ]) then A PPEND( , , , ° i ° $!!$¢R6¢$0¥ ° 7 k # quot; © 3 # © # quot; ) Figure 9.12
  21. 21. 21 procedure OTTER( , ) ¤'§43 $¤ ¡ quot; inputs: $¤ quot; , a set of support—clauses defining the problem (a global variable) ¤'§43 ¡ , background knowledge potentially relevant to the problem repeat % clause the lightest member of $§ quot; move from ¡ 3 I$@¥ to ¡ ¤¤I3 quot; $¤ P ROCESS(I NFER( , ), $quot;¤ ¤'§43 I$@¥ ) ¡ ¡ 3 until $¤ quot; —– = or a refutation has been found function I NFER( (§'¤I3 I$@¥ ¡ ¡ 3 , ) returns clauses resolve I$@¥ ¡ 3 with each member of ¤'§43 ¡ return the resulting clauses after applying F ILTER procedure P ROCESS( $¤ ¤I$@¥ quot; ¡ 3 , ) ¡ 3 I$@¥ for each in do ¡ 3 ¤I$@¥ P¤41¤¥ % ¡ 3 S IMPLIFY( I$@¥ ¡ 3 ) merge identical literals discard clause if it is a tautology [% quot; `$§ — I$@¥ ¡ 3 ] quot; $§ I$@¥ ¡ 3 if has no literals then a refutation has been found ¡I$@¥ if 3 has one literal then look for unit refutation Figure 9.19
  22. 22. 10 KNOWLEDGE REPRESENTATION 22
  23. 23. 23 Figure 11.3 ‘ ‘ quot;© X ̈ A ¦'Qy“@¤© ɤ †$¦0b“@¤© ¦¥ E FFECT: ‘ T quot; £ G X ̈ A ‘ quot;©ˆ©£ quot;  £ A ‘ T quot; £ Gˆ © £ quot;   £ A ¨s¤§$4¨6 Ǥ †$¦§@¤§$4¨6 Ǥ @'b$@ ® ¤ ™120c@¤© A ‘ ̈ ¡ # ‘ T quot; £ G X ̈ P RECOND : §'Qy€$¦0b“@b7 Å P$!¤¥ A X ‘ quot;© X T quot; £ G X ̈ ˆ # quot; © ‘‘ Ì ˆ ¥ ¦X … P# Ä „¤ 4PX … ¤© A E FFECT: ‘ † ˆ ‘ †ˆ©£ quot;  £ A ‘ ̈ ¡ # ˆ quot; C£ Ê 4¤§$4¨6 Ǥ b@‰$@ ® ¤ ‘ … ¢'$ ˆ¤ I`“@¤© Ǥ bX … P# Ä ‘ † X ̈ A ‘ Ì ˆP RECOND : §4P“X … “)@Rf$P$!¤¥ A X ‘ † X Ì ˆ a quot; # Í ˆ # quot; © ‘‘ Ì ˆ ¦X … P# Ä ¤ 4PX … ¤© ¦¥ E FFECT: ‘ † ˆ A £ quot;  £ A ‘ ̈ ¡ # ˆ quot; C£ Ê ‘4†ˆ¤©§$4¨6 Ǥ @'b$@ ® ¤ ‘ … ¢'$ ˆ¤ 4`“@¤© Ǥ IPX … ¤© A ‘ † X ̈ A ‘ † ˆ P RECOND : X‘ † X Ì ˆ a ˆ # quot; © §4`“bX … “e)quot; F P$!¤¥ A ‘ Æ Å Ž ˆ A ¡ È Œ ˆ ˆ ¦‘ vI8 X v§ ¤© ɤ ‘ PÅ yX v§ ¤© A 1)quot; Ë ‘ Æ Å ˆ ©£ quot;  £ A ¦‘ w48 §14¨H ɤ ‘ PÅ ¦¤§$4¨6 ¦¤ ¡ Ȉ©£ quot;  £ A ˆ ¡ # ˆ ¡ # Ž ‘ Ž £ ‰$@ ® ¤ ‘ Œ £ ‰1 ® ¤ ‘ v§ ¢Q1 Ÿ¤ ‘ v§ R$ •¤ˆ quot; C£ Ê Œ ˆ quot; C£ Ê ¡ È ˆ A Æ Å ˆ A ¡ È ˆ A ‘ PÅ yX Ž £ ¤© ɤ ‘ w48 X Œ £ ¤© Ǥ ‘ PÅ yX Ž § ¤© Ǥ ‘ v48 X Œ § ¤© A 6¢# Ä Æ Å ˆ ˆ © PLANNING 11
  24. 24. Figure 11.7 ‘ Æ ˆ £ ¡ Ê ¦‘ ¼ XiÑQˆ`# •¥„¤ ‘ ¼ I$2 Ÿ¤ 1¤ l iQ`# Æ ‘ ¡ X ш E FFECT: , ‘ ¼ u{Qˆ ¤ ¤Qˆb¤)@ Eɤ ¤ÑQI$2 Ÿ¤ ‘ ¼ iQP# Æ w Ñ ‘Ñ 9¥ quot; ‘ ˆ £ ¡ Ê X ш P RECOND : X X ш ¡ §‘ ¼ iQ(§' l quot; l e1ШP$!¤¥ A ¡ d quot; ψ # quot; © ¥ ‘ ˆ £ ¡ ¦‘ – I$2 ʕɤ ‘ ¼ iÑQP# •¥„¤ ‘ ¼ ˆ412( ˆ¤ ‘ – iQ`# Æ X ˆ Æ £ ¡ Ê X ш E FFECT: ,‘ – u w ¼ ˆ ¤ ‘ – Qˆ ¤ ‘ ¼ {Qˆ uw Ñ uw Ñ ¤ ‘¤Qb9§) Eɤ ‘ – 4$¦( ˆ¤ ‘¤QˆI$2¡ Ÿ¤ ‘ ¼ iQP# Æ Ñˆ ¥ quot; ˆ £ ¡ Ê Ñ £ Ê X ш P RECOND : X §‘ – X ¼ iQe1ШP$!¤¥ A X ш ¡ d quot; ψ # quot; © ‘ ¦‘ § X µ `# ˆ¤ ‘ µ P# Æ 1)quot; ˆ Æ X Έ ˆ ‘ ˆ £ ¡ Ê ¦‘ § 4$¦( ˆ¤ ‘ µ I$2 Ÿ¤ x4$¦( ˆ¤ Ë ˆ £ ¡ Ê ‘ Έ £ ¡ Ê ‘ § b¤)@ ɤ ‘ ˆ 9¥ quot; E µ ˆ 9¥ quot; E b¤)@ ɤ xb¤)@ ɤ ‘ Έ 9¥ quot; E ‘ ¡ ˆ Æ ‘ ¡ i(§' l X § P# ˆ¤ i¤' l X ˆ Æ µ P# ˆ¤ 1¤ `# Æ 6¢# ‘ ¡ l X Έ ˆ © Ä Figure 11.5 ‘ ‘ ¡ X © ¦i(1k A §1 Å ˆ A ¥ ¤© ¦„¤ R412£ §$ Å ¤© ¦Y¤ ‘ a # 3 quot; X© ˆ A ¥ ‘ 9 # 3 X ¡£ ˆ A ¥ ‘ ¡ X ¡ £ 444§£ l 2$4  8 ¤© ¦„¤ i$k A 2$4  8 ¤© ¦„¤ R412£ Ë 2$4  8 ¤© ¦¥ ˆ A ¥ ‘ a # 3 quot; Ë X ¡£ ˆ A E FFECT: P RECOND : , © D C #£¡ '1@R§¤$d Æ $$2¡ F P$!¤¥ A ¡ d ˆ # quot; © ‘ ¡ X ¡ £ ˆ A ‘ a # 3 quot; X ¡£ ˆ A ¦‘)(1k A e2$4  8 ¤© ɤ R412£ Ë 2$4  8 ¤© ¦¥ E FFECT: ‘ X© ˆ A ¥ ‘ a # 3 quot; X ¡£ ˆ )¡(1k A §$@ Å ¤© ¦É¤ RbI$¦£ Ë 2$4  8 ¤© A P RECOND : ‘ ¡, X ¡£ ˆ © ˆ # quot; © i$k A 2$4  8 P# Æ I3 ® P$!¤¥ A ‘‘ # quot; X© ˆ A ‘ ¡ X © ˆ A ¦¢abI3$¦£ Ë §$@ Å ¤© Ǥ )(1k A §$@ Å ¤© ¦¥ E FFECT: ‘ ¡ X © ˆ i$k A ¤1 Å ¤© A P RECOND : , ‘ ¡ X © ˆ ¡ d quot; T ˆ # quot; © 1$k A §$ Å ee$o§¡ B P$!¤¥ A a quot; ˆ A ‘ 9 # 3 X ¡£ ˆ A ‘¦‘¢b#I3$¦£ Ë X¡2£$4  8 ¤© Ǥ ¢4I§£ l e2$4  8 ¤© ¦¥ E FFECT: ‘ 9 # 3 X ¡£ ˆ 4¢I§£ l 2$4  8 ¤© A P RECOND : , ‘ 9 # 3 X ¡£ ˆ ¡ d quot; T ˆ # quot; © 444§£ l 2$4  8 ee$o§¡ B P$!¤¥ A ‘ ‘ ¡ X ¡ £ ˆ ˆ ¦)(1k A e014  8 ¤© A s1)quot; Ë ‘‘ 9 # 3 X ¡£ ˆ A ‘ ¡ X © ˆ ˆ © ¦4¢I§£ l 2$4  8 ¤© ɤ i$k A §$@ Å ¤© A 6¢# Ä Planning 11. Chapter 24

×