You are on page 1of 54
ARTIFICIAL INTELLIGENCE Third Edition Program Listings Elaine Rich Microelectronics and Computer Technology Corporation Kevin Knight Carnegie Melion University Shivashankar B Nair UT Guwahati | Tata McGraw-Hill Publishing Company Limited NEW DELHI McGraw-Hill Offices New Delhi New York St Louis San Francisco Auckland Bogota Caracas Kuala Lumpur Lisbon London Madrid Mexico City Milan Montreal San Juan Santiago Singapore Sydney Tokyo Toronto “hee — Artificial Intelligenée, Second Baltion Blaine Rich and Kevin. Knight MeGraw HAIL, 1991 THs cous may be freely copied and used for edvéational or rasaarch purposes. ALL software written by Kevin Knight. Conmente, bugs, Inprovenents te knight@es .emt.edu A-STAR SEARCH eareLiep™ (aefetriict on + search node t 9 ab parent children, state) (defvax topant) (aetvar *clocea*) (detvar’ *nodes-expanded*) 22 Functions for manipulating the open and closed lists, The implementation bP As straigntforvard; the open list is kept ordered hy the f values of ite 1) nodes, smallest frst. A more efficient Implementation vould use priority #4 queues to sliow fast insertion and deletion of best nodes. (doton make-onpty-nedelist () il) {@efun ompty-nodelist? {nodelist) (nol nodelist) | (@ofun add-to-nodelist (nade nodelLet) (ond ({eq nodelist “elosea) {setq *closoe* (cons node *closed*))) (eq sedelist. “open {cond ({0F (null *open*) (< fan-€ node) (sn-£ (ear topent}))) {setq *opent (cons node *opent))) ie {do U(x topenty ty (eae topent))) (ee (mail 9) Ke (an=£ node) (on-€ (car ¥)))) (cond ({n011 ¥) (sett (cdr x) (ist node) )) Co (sett (cde x) (cone nede ¥)))) {aotg x y) taoeg y (ear yN)I009 (defun xomove-front-of-nodelist (nodelist) (Gefun expand-bestneds (eond ({eq nedolist ‘opan) (let ({Eirstnode (car topeat})) toetq *opent (ede topent?) Elrstnode) ) cc {error "Error. Can only remove from open List.*))}) (@efun Cind-node-in-nodelist (state jodel ist) (elnd-if W (anda in) (egustates. state (sn-state n)7) (Lf (eq modelist ‘open) topent telosedt})} (dsfun chango-priority-of-node (node nodelist) (eond (eq Redelict open) {satq topent (delete node *opent)? (ada-to-nodelist node “open! } te (error "Beror: can only’ change priorities of nodes on open: List.")))) 37 Fonction A-STAR does a heuristic search fren the start to the goal state, 2} retuming a solution path. It maintains two lists of nodes, called open 37 and closed, and expanda nost promising nodes first. (defun avstor (start éoptional vefbose) {setq *nodes-axpanded* 0) {set topen* (makemompty-nodelist) ) (aetq *elosed* (nake-enpty-nedelist) } (let ((start-node (makeran ‘ratate start ° ih (hourlstic start) (heuristic start) children nid tparentni1)}) (add-tornodelist_ start-nede "open) ) (do ((faiture (enpty-nodelist? *open*y) (goal~found: nit)) (or fallure goal~found) (if failure *o sglution.* (oxtract-a-stax-path goal-found))) (print (mnapear #! (lambda (a) (11st [an-state 8) (an-£ 4))) Aopent)) fet (ibestnode (rensve-front~of-nodelist. open) }) {add-to-nodelist besthode “elosed) (cond ({goal-state? (snstate bostnode)) j2tq goal-found bestnade)) (t (expand-bestnode bestnode verbose))))}) Function EXPAND-BEST-HODE takes the node with the best heuristic score and expands it. If the successors are aZready on the open or closed Lists, it updates heuristic scores! ethezwise, it adds then to the open dust. (bestnode verbose} (when verbose (format € "Expanding soda ~d-A" (an- (sett *nodss-expanded* (1+ tnodes-expanded) ) (et ((successors (expand (sn-state bestnode) ))? (do ({8 successors (cdr 3))) (quit 9) Alt) (et* (fence (ear a)) (eld-open (Find-node-tn-nodel (cond (o1d-open (add~child bestnode ol tate bestnode))) st succ open!) stop 2c -apen} 2 Ree TUN ee LB feed lore Artificial Intelligence, Second Edition Elaine Rich and Kevin Knight, Mesraw Hil, 1991 ‘This code may be: freely copied and used for eduéational or research purposes. ALL software written by Kevin Knight, Comments; bage, Inprévenant to knight@es .oma.eda W ‘SACKPROPAGATION ALGORITHM (SINGLE, BINARY OUTPUT) "backpzop.116p" “A. Backpropagation algorithn (single, binazy eatput) To uso this program, you most set up three input f1lest a "txt", a Tetrain®, anda "test". Bere da an examples Bile xor.txt: azn B01 Test after every W epochs: 100 Learning rate (ota): 0:35 Monentum: (alpha) + 0:90 Noise: ° ‘raining data: xor-train Testing deta: ~ xor test. File xortradn: Fue ° 1 oo on ‘The main {ile contains settings for various parameters. There are separate files for training and testing data, To run, ¢all (backprop *xor.txt*) (or whatever the filename is). ‘The program will periedically append its results to the end of the “xor.txt” file. This version of backpropagation 4s geared toward networks vith 2 single, binary output. Te provides periodic analyses ‘of how woll the nstwork 1s predicting the oltput bit ag learning progresses 37} vartanter. (eefvar OUTPUE-LAYER nil) (detvar STRUCTURE ni) (detvar TorAL-EFOCHS 0) (Gefvar TEST-TNTERVAL 0) (dorvar: TRALNING-TUPUTS ni) {defver TRAINING-OUTPUTS nit) (detvar TESTING-INPUTS nil) (defvar TESTING-OUTPUTS nil) (defvar TOTAL-TRATWING nit) (dafvar TORAL-TesTING nil) (detvar ToraL-1uPurs n11) (detvar ToraL-oureu7s nil) (defver ERA 0) (dofvar ALPHA 0) {eetvar NOISE 0) (Getvar TRAIN-FILE p11) (eefvar TEST-FILB nil) (Gofvar TOTAL-GUBSSED (noke~array 8 :eloment-type ' float)) (Getvar TOTAL-RIGHT (nake-array 8 :elenent-typs 'float)} soy Structures. {defotruct “enit) (weighted-rum ‘f10a0) Gctivation ‘float) (delta ‘fleat)) (eefstruct (net) ‘units connect lon# size next-layer prev-layer) (defstruct (connection) (weight 'float) (delte-wolght. 'fleat)) (dofun. output-layer? (Layer) (oul (nat-next-layor Layer))} (defun “snput-leyer? (ayer) (nal (net-prev-layer Layer} )} (@etun nidden-Layer? (ayer) {and (not-next~leyer layer) (net-prev-Layer layer))) a 345 Bullding the network. (@efun randon-reai (Lo hi} (4 Lo (random (= Ai 16)))) (Gofun rason-weight () (randow-real -0.8 0.9)) (@ofun randen-noise () (random-real 0,0 0.15) (eefun construct-unite 0) {ao ((o STRUCTURE (cdr n)) (ast-Layer pit) (temp-net nil) ((nuLln} (sett OUZPUT-LAYER last-Leyer}} (sot! temp-net. (wake-net, (e (ear mp eeay (Lt (ear n)) selemant-type ‘unit a (make-array (List (Lt (ear n)) (+ (ease n))} relament-type “connect Lon) (@etvar weTWoRE ail) nity a-star.lisp Mon Jun 10 18:04:03 1993 2 (pop sibly-chenge-paront: old-open bestnode ‘ open) ) i (let ({old-closed (find-node~in-nodelist suce ‘closed))) (cond {old-closed } step 24 (add-chitd bestnode old-closed) (poselbly-chenge-parent old-closed bestnede Yelosed)) te (et ((new-node (nakewsn rstate suce 'g (+ (an-g bostnoda) (cost-of-move (enstare bestnede) sues) rehildren ni}})) (est! (en-£ new-nods} (H (sn-g naw-nada) o (anh new-node) )) s (add-chila beatnode new-node) 3 (ada-tomnedoiist new-node opan)))))}))})) ghisct, 14 Buhctlon ADOWCHIED madisiea « node to have a nav successors (defun ada-ehild (pexent child) (Sete (on-childzen. parent) (cona child (sn-children parent)})) ne anne nnn —-. Function POSSIDLY-CHANGE-PARENT takes a now node thet Lo already on the ‘pen oF Slosed Lists, and changes the old node's gif, and parent folds Af the new path la-ahorter than the: old one. (Gefun possibly-change-parent (old bestnode nodel ist) (et ({old-cost (sng old) (cost-threugh-bestnede (+ (sng bestnode) (cost-of-nove (sn-state bestnede) (sn-state o1)))}) (when (> o1d-cost cost~thzough-bestnode) (sett {sn~parent ole) bestnoda) {eet (sn-g old) cost-through-hostnodo) ’ {act E (an-f old) (+ (sng old) (enh old))) (cond ((oq nodelist. topen) {change-priority-of-node ola ‘open)) {oq nedel ist ‘clowea) {propagate-cost. 01a)))))) 77 Function PROPAGATE-CoST takes & node.and ensures that the hourletic 7 estimates of its descendant nodes art (aefun propagate-cost (eld) (Go. ((chl1dron (sn-ehildren ola) (ede children))) {(9ell eniidzen) ni) Wet ((enila (ear chileren)) (when (or, (eq old (en-parent child)) eG (an-g ola) [cost-of-move (sn-atate old) (on-state child) )) {tang chi2a) )) (est (en-g chile) CF (sn-g old) 19) {set (an-f child) (4 (snag cnild) (en-n ehild))) (sot (an-parent child) ofa) (when (ember child open) (change-priority-of-node child *opent)) (propagata-coat chila)})}) iy Function EXTRACT-PATH xetrieves a solution path by tracing parent pointers 17 of 8 node. (detun extract~a-star=p: (ae Cigath nit) (h node (en-pazent n)?) (noid Ay path) (setg path (cons (an-etate:n) path) 2 (note) backprop. lisp Mon Jun 10 18:04:04 1991 “3 mm 311 Walttplication. (defun mate (crest arga) 7 (without~floating-underflewtraps (apply #"* args))) (apply ¥* args) 2 Feed Forward phase. {defun feod~rorvard (index set verbose) (when vorboss (format t "Feeding vactor ~d from ~d into network «..~1 Andex (Lf (eq set TRAINIMG-INPUrS) 0 1))) (@o ((w 1 (+ w))) (> a {nst-eize NETWORK)) nil) (ets ((nolseadded (Lz (= NOISE 1). (randon-noise) 0.0)) (enissinput (ares set index v)) Gnput-plus-noize (if (> thic-input 0.5) (© this-input. noise-aadea) (4 EMLarinpae notse-added) 19) (ott (unit-activation (aref (net-units NETWORK) v)) input-plus-noise)}) {do (ayer NETWORK (nat~next-Layer layer) )} ((output-Layer? layer) nil} (do ((ux 1 (24 u1))) (O al (netwsize (net-next-Layer Jayer))) nil) (let ((this-undt (aref (net-units (not-next-layer layer)) 91))) (etl (unit-waighted-sun thls-undt) 0.0) (@o ((c2 0 (1+ u2})} ((> u2 (netmslze layar)) nil} (sett (unst-woighted-sun this~unit) (+ (unlt-weighted-sum this-unity (ult "(unit-activation (aref (net-units layer) u2!) (connaction-weight (aref (net-connect one Leyes) ud 01)))))) (sett (unitactivation this-onit) (activatton-function (enit-veighted (hen verbose (fornst t "Rosult = =14,78 «4 (undt-activation {aref (neb-unkts OUSPUT-LAYER) 1))))) jam thigcunse)})) }) + Back Propagate piase. (defun back-propagate (index temp-alpha verbose) (when varbose (format t "Backpropagating errors ..,~4")) {ao ((a 1 (14,u))) (@ w (neteeize OUTPUT-LAYER) } nil) (et ({this-unit (aref (net-anits OUTPUT-LAYER) 0))) (sett (unit~delta this-unit) (molt (> (aref TRATHTNG-OUTPUTS Andex 0) fonit-activation thie-unit)) (unle-activation this-onit) (= 1.0 (uniteactivation thlerunit))}))) (@0 (Qayer (net=prev-Layar OUTPUT-LAYER) (net-prev-layer layer))) (Gnput-Layer? layer) nil) (do (a0 (14 u))) (> a (retmaize Leyer})) (lot “((thissunit (azef (net~unite Layez) y)} (som 0.09) (do ((u2 1 (4 u2})): ((> U2 (net-size (net-next—layer Layer)))) (sete sun (+ sum (matt (unit-deita . (are {net-units (net~ (connect Lon-waight (aref (net-connections Layer) u u2)))))) (sett (univ-doita thisunie Gault (= 1-0 (unit-activation this-unit)) vext-Layer ayer} 02) (unit-activation this-unit). oum)}))) {40 (ayer (nét-prov-layer OUTPUT-LAYER! (net-prev-layer layer}) ai ati) (mula Tayers nit) {@0 ((u 0 (1t u})) {(> u (net-size Leyer)) nS) (et ((low-unlt (aref (net-unite Leyer} u})) (do ((a2 1 (24 u2})) ((> u2 {netmeize (netwnext-Layer layer))> ntl) (lot* ((hi-unit (arof (natvunits (net-next-layer layer}) 2)) (the-connaction (aref (ret-connections Layer) w 12)) (newchange (+ (Bult B2R (unitcdelte hi-unit) fonit-activation Jow-untt)) (mult temp-aipha (connection-delta-weight the-connection)}})) (when verbose (format t “changing weight: (-d ~a -4) from ~14,76 to ~14, 76-4" 1 u dz (connactfon~weight the-connect ion) (+ (connection-weignt the-connect ion) newchange))) (Sete (connect fon-waight the-comnact isn) (+ (connection-weight tha-connectton) newshangs)) (sete (connect lon-delta-neight the-connection) newehange) ))))}) (defun learn {infile verbose) (dotines (epoch TOTAL-BFOCHS) (when verbose (Lomat t "Starting epoch ~d Get ((tenp-alpna (If [< epoch 10) 0.0 ALPHA})) ‘when (= 0 (mod epoch TEST-INTERVAL) + (evaluata-prograss epoch infile) ) Keo (OC 1 (14 X)))_ ((> x TOTAL-TRATING) » (feed-forward x TERINING=INPUTS verbose) {oack-propagate x temp-alpha verbose))))) s" epoch)? } Bvaluate Progress. (gefun evaluate-progress (epech Ant ile) (evaluate-training-data epoch infile) (evaluate-testing-data epoch infite)) (defun evatuate-training-data (epoch infite) {do ((4 0 (+ £)}) (f= 4 8)) (eett (aret TOTAL-cUESsED 1) 0.00) {do ((1 0 (1+ 199) (ls 4. 8)) (sate (axe TOTAL-RIGHT 1) 0.0)7 {do (44 1 G4 £09) (> 4 TorAL-TRAIMING): Get right (azef TRAINING-OOTPUTS. 1 3))) (feed-forward 1 TRAINING-INPOTS nil) (ee ((outpue-activation (unitvactivation (oref (net-units OUTPUT-LAYER).1)))) {40 (13 9 04-99) Ue 5-9) (hen Yor (> eutpur-activation (+ 0.5 (© 9.05 111) (< output-activation (= 0.5 (+ 0.05 9)11) (eeef (aze£ TORAL-GUESSED j) (- 1,0 (aref TOTAL~GUESSED 5))) (when (or (and (>, right 0.5) (© output-aetivation (+ 0.5 (* 0.05 91) (and (< Fight 0.5) {& output-activation (= 0.5 (+ 0.08 3)1))) (eets (pref TOTAL-RIGHT 3) (+ 1.0 (sreé TOTAU-RIGHT §)})))))1) (with-open-f{ile (ifile infile :direction zoutput (format file "EPOCH -d.Parformanca on Eesining dstat-- (format ifile “Confidence: ") Mon Jun 10 18:04:04 1991, 2 sprevelayer 1ast~Layer) (0 (( 0 (24 ¥))) (Ow (ear my) aL) (sett. (aref (net-units tempenat) v} (make-unit})} (when {ede n) (do ((ol 0 (te wth) (> ut (ear ny) nity {do ((u2 2 (24 92))) ((> ©2 (cade al) 92) (sett (axe£ (not~connectiona tenp-net) ul u2) (nake-connection)))}) feond (= (2ength n) (Length STRUCTURE) ) (sete REEWORK tenp-net) (sete (not-size tenp-net) (car 'n}) (setf Tast-layer venp-net) ) « (sot f (net-next-iayer Isst~layer) tonp-net) (ott (net-size temp-net) jcar n)} (este Jast-Layer temp-net))})) backprop. lisp (defen sot-inktial-weights () (do (ayer WETWORK (net-next-Layer Layer})) ({null Layer) nil) (et {isize (net-size Jayer)) (units (net-unite Layer) ) {connections (net-connections’ 1ayer))) feo ((u 0 Gt uy} (@ w size) nit) tivation (azef units uw) 0) ighted-aum (azef units u)) 0) (set (unitedeita (aref unite v}) 0)) (omen {not (output-layer? Layer)) (let ((nekt~layor-size (net~size,(net~next-layer layer)))) {ao (ul 0 (Qt uly}) (> ul size) nil) (do ((u2 1 (44 u2})) (> uz next-layer-size) nit) (set (connectlon-welght (aref connections ul u2)) (randem-weight)} (sete. (conhection=delta-waight (axef connections ui u2)) 9.0) 919 (do (Qayer WeTWORK’ (net~naxt-layer layer))) ({null layer) nit) (este (unit-activation. (arer (net-units layer) 0)) 1.0))) (@efun buitd-network () (sote TOPAL-INPUTS (car STRUCTURE) {set TOTAL-oUreUTS (car (last STRUCTURE) )) (construct-units) (set~initial-veighta) ) Butlding the test and train ests. {Gofon build-test~and-trath- (ast TOTAL~TRAINING 0} (with-open~tile (Atile PRALN-FILE sdicaction :input) (do ((x: (read~Line ifile ni1 ‘ezror) (read-line Af{1e nil 'erzox}) (tot 0)) (for (string-equal "* x) (oq x,"error)) (sett TOPAL-TRATHING tot) (hen (not (etring-equal "= x)) featf tot (14 eot)}})) (sett Torat~TESTING 0) (with-open-file (Lflle TEST-FILG sdfeection :input) (do ((x (xead~Line itile nil terror) (read-line Lftle nil ‘exzor}) (tot 0?) (or (steing-equal "* 3) aq x ‘error)) (eeté TOTAL-TEE7INe tot) } (men (uot (etring-equal "™ x)) (sett tot (14 eet)}))) (eotf TRAINING-TNPOTS (makewarray (List. (24 TOTAL-PRAIUING) (24 TOTAL-THPUTS)) ‘sinitial-elament 0.0)) LE TRAINING-OUTPUTS (wake~acrsy (List. (14 TOTAL-PRAINING) (14 TOTAL-oUrPUTS)) dinitlal-element 0.0)) (sete TESTING-INOTS 8 0 (naie-arcay (List (1+ TOTAL-PESTING) (14: TOMAL-IWPUTS)} Hinitial~elenent 0,0)) Oo (eee resting-ourpurs. (make-array (List (1+ SOTAL=TEsTING) (2+ TOTAL-OUTEUTS) } Hinitial-clenent 0.0)) (uitnsopeh-tile (if lle TRAIN-PILE :Airect ion (do (ix 2 Qs x))) (Ox TOTAL-TRAINENG) 41) (eo (ty 1 Gt yh) (Oy TOTALMAuPUTS) mil) (ett (arer TRAUNG-INPUTS yy; (zead Lfi18)1) (ao (iy 1 Gey). UG y TotatecurPuTs) nia) foot (aref TRAINING-OUTDUTS x y1 (bound-outputs {read 1641)))))) (withsopen-tile (ifile TEST-FILE +4iractien sinput) (do (x 1 (+ x))) (> x TOTAL-TESTING) nit) feo (y 1 G4 YI) (> y ToTAL-RPUTS) ALL) (setf (aret TESTING-INPUTS % y) (read LEile})) (20 Ug 1 GF yd} 1 y Torai-ouzeUrs) nil) (sett (aref TESTING-OUTPUTS, x.y) (bound-outpute (read 4¢41¢))})))) put) + Function ‘BouND-OUTPUTS takes a tazgat output from 4 testing or teaining 41 tile, and forces it to be at leat 3.1 and at most 0.9. (dofun Bound-outputs (x) (cond ((> 0.9) 0.9) (© 0.1) 0.2) ean {éefun init-natwork (verboae) (when verbose (fomat © "Building metwork ...-¥")) (buila-network) (when verbose (format t "Building tect and train sats ...-4")) (buila-test -and-erain-aats)) ae Reading the'Laput file, (cefun read~data (infile verbose) (when verbose (format t "Reading input data (with-open-flie (ifile intile :direstion input) {do ((x (sead~char itile) (xead-ehar Aeile))) ((equal x #\:) nit) tay (act SxROCTURE (reed {¥i20)) : (@o ((x (rend~char iftle) (read-chaz ifite))) ((equal x #\i) nil) nity (sett TOTAL-EDOCHS (road ifi16)) {do ((x (read=char ifile) (readechar {¢ie))) { (equal x #\:) ni2) nih) (set TEST-INTERVAL (read 1f438)> (a> (0 (wead~char ifite) (read-char {file))) ((egual x #\:) lay nia) (otf BTA (read LEt19)) (do (( (raad=char {file} (read-char Letie)}) ((equal x #\:) pil! nil} (sot ALPHA (raad Lf1le)) (do. ((% (read-ehar {file) (read-er. (sete NOISE (read irkle)) (do (x (read~char Irl1e) (xead-ckar if11e))) ((equal x #\:) na) nit) (sett TRAIN-FILE (delete #\tab (delote #\space (read-line Lfils)1)} {0 ((x (resduchar ifile) (readzchar i€ile)}) (equal x #\:) nil) il) (sett TESI-FIEE (dolete #\Zab (delete ¥\space (zead-line ifsJe))})}) my Ax116))) (equal x #\2) mA2) niay 7 Ativation Eunction. (detvar zeronfloat (coerce 0.0 *#Lost)? (defun activetion-function (eum) (/ 1.0 (1.0 (exp (© zaro-Eloot sumi)))) backprop2.lisp f meee Artificial Intelligence, Second Edition Blaine Rich and Kevin Knight Mecraw Wi21, 1991 This code may be fresly copied and used for educational ox zesearch purpos ALL software written by Kevin Knight. Comments, bugs, improvements to knightécs.cmu.edu BACKPROPAGATION ALGORITHM (eoLtIeLe oureUTS) "backprop2. L1ap" Backpropagation algorithm (multiple outputs) To vse this program, you must set up three input files: a *.Ext", a *.tzain%, anda *itest™. Here Le an exempl File xor-txt: Network structure: ary Epoch: 801 Test after every M epochs: 200 Learning rate (ata)! 0.35 Momentum (e1pha) + 0.90 Wolse?: 0 Training data: xor.traln ‘Testing data: xor.test File sor.train: File xor.teatt oo0 20d The main £ile contains settings for voriour paramsters, there are separate Tiles for training and teating data. ‘to run, call (backprop "xor-txt") (or whatever the filename 4a). The program will append periedically its results to the end of the *wor.ext" Eile. This version of backprop deale with multiple output value Instead of calculating statistics on how well the net predicte the output bit, this program prints out each Input sector with ite actual and target output vectors. (defvar NETWORK n11) {efvar OUTPU-IAYER nil) (defvar STRUCTURE nil) (defvar rorAL-sRocHs 0) (defvar TEST-INTERVAL 0) (detvar TRAINING-INPUTS ni1) (defver TRAIHING-oUTPUTS 11) {defvar TESTING-INFUTS All) {defvar TES7ING-OUTPUTS nil) {defvar TOTAL*TEAINING nil) (defvar TOTAL-TESTINS nil) (defvar TovAL-INeuTS aft) {defvar TOTAL-OUTPUTS nil) {defvar ETA 0) (defvar ALPHA 0) (defvar NOISE 0) (defvar TRAIN-PILE nil) (defvar TEST-ETLE ail) Fh Structures. a (dotsttuet (urit) (weighted=sum,“ float) (activation float) (delta ‘*flost)) (dorstruct (net) ‘unite connactioné elze next-layer prev-layer) (dafetruct. (eennection) (weight ‘float) (delta-weight float)? (derun output-Layor? (ayer! {null (net-rext~layer Layer))) (aefun input-layer? (Layers) (nul (net~prev-layer layer))) (aefun hidden-Layer? (layer) (and (net~next-layer layer) (net-prev-layer layer)}) bit Building the network. (aetun randonereal (1 ML) (+ lo (zandon (= 34 10999) (Aston randon-woight () (randon-real 0-1 0.2) {dofun randen-noise () (random-zeal 0.0 6.15)) {defun construct-nits () (do .((n STRUCTURE (cdr n)) (Last~leyer nil {tenp-net nil)) {(null n) (setf OUTPUT-LAYER last-Leyer)) (sat# camponee (wake-net tunite (mak ‘connect ions ‘it (ear a) (makenoreay ( seray {14° (ear n)) rolomont-type ‘unity a (14 (ear a) G+ (cade m)) fenest-type ‘coniection) pil backprop. lis Mon gun 10 18:04:04 1994 (do (18 0 (at 1017 (= 4 09) Closet dette ™6,28* (+ £ 0.0871) (fomat iftle *-4") (omat Lf!le *Gvavced: —*) {do (449 (G6 09) (le L 8)). (tormat Atte "6a * fron (aref TOIAI-GUESSED 1)))) (format (file *=3") (tomate ithe "corvects do (G9 04 99) Cet 0) eoumat Leite "6a » (round (azar aotaz-nscit {)}}) (format Lftle *=¥") (format ifte "Percent: m fe (8 0 G4 On) tte ka) (romat 1fite ™6,32 ° (SEG (atef zoraL-cussaep 1) 0.5) {7 (100.0 (acer ToTAy-RIGiT £)) (aref TOEAL-GUESsED 1)) oon) seat) (fomat Leite (Getun evaluate-tosting-data (epoch infite) (do ((2 © (1b 4))) Cle A 9)} (eete (axox TOTAL-GUESSED 4) 0.0)) feo (4 0 (a4 £))) (t~ £ 9)) (sete (aref TOTALARIGH? 4) 0.9)) feo (G4 2 G4 42) (> 1 somu-tesaine) ) (let (signe (azef Tesrinc-ouNpurs { 1)}) (eed-forverd 1 TESTENG-INOUTS ni) Got ((output-activat ion funit-activation (azet (net-unite OUTPUT-LAYER) 1)))) (ao (13 9 G+ 5)1) (Je) (nen for (> output-activation (+ 0.5 (#.0.08 4))) {< outpue~activation {- 015 (+ 0:03 3}1}) (sct£ (aref TOTAL-GUESSED 3} (+ 1.0 (axeg TOTAL-GUESSED 4))) (wien (or (and (> Fight 0.3) © output-activation (4 0.5 (* 0.08 4197) (and (< right 005) K< output~activation (~ 0.5 (* 0.05 4))))) {rete (ree TOTAL-RIGHT 3) (+ 1.0 (aret TOTAE-RIGHT 4}}))))))) (ishseponftla (ifile Infile :dizection soitpat sif-exises senpenty (fomat file TEPoc ~d. " Portormance en testing datart-¥" soso (format itite "confidence: =) (go (1 0 G4 AN) Ue i 0) (eomat Aetie "6,26 * (#4 0.05))) {fomat ifile *=3") (format ifile "Guessed: —*) (ee US 9 (6 i) (= £91) (roriat Seis *6a * (round {eref TOTAL-GUESSED 1)})) {tomat iftie *=8") (Comat iftle "Correct: ») (do (8 0 (14 4999 Ue 4 81) (format ttt “sa = {round (aret rOnaL-arcut i)})) (format. i2tie ta") (Comat iflle "Percent: (ao (10 4 1) Ulm k 8) {fomet iftie "6,25 * . (if © (ater ToraL-cursse: 4) 0,5) (7 (100.0 (arse TOTAL-RIGHT 1)) (eret TOTAL-GUESSED ‘1)) oy) (fomat ifite *24-5"))) 301 Prine weights: be {@etun print-weights (intiLe} (migucopenstite (4£tle intitle sdixection routput itt-extata soppend) (Eomat irile "¥atveightat-am) (G0 ((layer MEEWORK (nat-next-layer Layer) (ka (a+ k))) 4 (foxtput-taysr? layer) ni) (de ((u'o (at w)) (Ou (netesize layer)) af) Ge (402 2 (ds u2)1) (Guz (nernsize inot-next-Layer layer))) ni) (ek ({the-connection (axed, inet~connections layer) u48)}} (Comat ifile *(-d “dd ~24,72)-4" Xu ua (connection-voight ‘the-connectien)))))))) (Gefun print-weighte-to-screan () (format t *-$-tWelgnte:--¥"] {do (Qayer NETWORK (net-next~Lays: leyer)) (k 1 (14 419) (output~layer? layer) nil} (do (ty 9 (24 u)}) CO a {netesize tayer)) ni) . (do ((u2 1 (14 02))) (> 2 inet~aize (net~next-teyer Layer))) nil) (ter ((the-connection (atef (nat~connectione layee) a f2)4y (formate made" Q4at kw uz (connect ion-weight the-connect {on)})))))) Backprop. + (defun backprep (infile toptional veztase: (read~data intile verbose) Unit-network verbose) (learn intitle verbose) (print-weights Ingile)) backprop2. lisp 12 Wolesplication. as Mon Jun 10 18:0. (defun nuit (crest args) 1 (weithout~floating-andarflow-tnaps (apply #* azge))) (apply #7* arga)) 131 Feed Fosward ph: (Gatun feed-forward (index set {80 ((a 1 Gt w))) (Ou (net-size NETWORK) ) nit) (et* (Inoisevadded (if (= NOISE 1) (randon-neise) 0.0)) (ehlacinput (azef set index u)) (mput-plusenoise (if (> thiseinput. 0-5) (© this-input notse-sadeq) ( thiscinput nofse-added) ))) (sett (unit-activation (exef (net-units VETWORK) u)} inpot-plus-nolse))) {do ( (layer NETWORK (net-next-Layer Layer})) (foutput-layer? Layer} ni) (do ((at 1 G4 ui) (O Ul {noteatze (not-next-layer laysr))) nil? (Get ((this-unit (arer (net-units (net-next-layer Layer)) ul)}) (est. (unit-xoighted-sum. this~anit) 0.0) (de ((42 9 (+ u2))} ((@ u2 (net~size Layer)) nil) (otf {unit-woighted-sum this-unit) {4 (unit-weignted-cun this-onit) (molt (unit~sctivation (aref (net-unita Layer) v2)) (connect ion-weignt (arez (net-connections layer) w2 uh)))))) {etl (unit-activation this-unit) (activatlon-function (onkt-veighted-sum this-unit))})))) Back Propagate phase. (Gefun back~propagate’ (index tenp-alpha} {do ((0 1 (14 u)}) (0 w (net-size OUTPUT-LAYER)) nil) : (let {(uhis-unit (arof (net-unite OUTPUT-LAYER) w))) (sete (unit~delta this-unit) (mult (= (arof TRAINTNG-OUTPUTS. index u) (unit-activation thie-unit)) (unit-sctivation this-anit) (1.0 (anit-activation thiseunity))))) (do (ayer (net-peey-Layer OUTPUT-LAYER) (net-prev-Layer Layer))) ((input-Layer?. Layer) ni) {do ((10 (14'y)}) (> a (netmsize Layer?) (ist ((this-anit (aref {met-units Layer) u))) (aus 0.0)) (eo ({u2 1 (1+ u2))) (> u2 {netrsize (net-nex=-layar Laysr}))) (sete sum (4 oun (nuit (unit~derta (aref (nat-unite (not-next-Layer layer}) u2)) (connect ion-waight (arf (net-connactions Layer) wv u2)))})) (setf (unit~delta this~unit) (mult (> 1-0 (unit-activation this-unit)) (unit-activation this-anit) sum))))) (do (layer (net~prev-layer OUTPUT-EAVER) (nst=prev-layer layer) )) (null tayer) nit) (ao ((o 0 G+ Uy) (> u (oet~size Layer)) ni1) (let {(lowunlt (aref (net-units layer)’ w))) (do ((g2 2 Gt uay}) (> uz (het-size {net-next-Layer La; (lets ((hi-unit (rer (netwunits (net-next~layer Layer) v2}) (Ghe-connection (exef (wet-connections layer) a u2)) inowehange ( (malt BTA (unit-delte L-unie) (unit-activation ‘low-unit)) (ait temp-aipha (connection-Gelta-naight the-connection})))) {aetf (connection-weight the-connect lon} (h {connect ion-veight the-cannect ion) newchange) ) (soté (connection-delta-weight the-cosnect icp) newchange) }))))) ae ber Weakn. ber (Gofun learn (Anttie) {dotiings (epoch TOTAL EPICS) (et ((tempmatpha (iF (< epoch 19).0.8 ALPHA ]) (when (= 0 (nod epoch TEST-INTERVAL:) (avaluate-progresa epoch Infiley (do (Ge 1 (14 0) (> x TOTAL ARRAT (feed-forward % TAAINING-INPUTS) (oack-propagate x teap-alphad))): Fie twntuate Progress. a (dotun evaluste-progress (epoch inf ite) (with-open-file (ifile infile :éirection reutput :if-exists seppend) (fommat Lille *=4zpoch -d.-4-4" epoch: (format ifile *Training.-*")) (ovaluate-training-data epoch inf le) (ath-opsn-file (tile infile :éizection (fomat {file "testing.-¥")) (evaluate-testing-data epoch ingile1) put rit-exists (defun avalaate-training-data (epsch infil (eeciare (ignore epoch) ) (with-opon-file (file infile :éirection soutput :if-exists :append) (go (C+ 1)))_ (> £ TOTAL-TRATNIENG) ) (feed-forward 4 TRAINING-INpUTS) (ao (( 1 (1+ J0)) >} TORAL-TupiES) ntl) (format {file "4, LE" (aref TRAINENG-INDUTS 4 §))) (format file" =>'*) (do (1.1 G+ 39) > J TorAL-oUTETS) nL) (fomat issie "9/42 * wunitvactivation (aref (net-unt=é OUTPOT-ZAYER):3)))) (format ifile * (target =") (@o (3-2 G+ 3))) US 5 ToTAL 5 TomaL—epoTS)_ win) (eormat igile "4,1" (aref TESTING-INPUTS 1 3))) (format. i¢ile = ==>'™) (40 (G-1 G4 390} > 3 sotatroureater nin) (rormat tite 73,16 # (undt-activation Giref (not-unsxs OUTPOT-LAYER) 4910) tappend) backprop2.1isp Mon Jun 10 18:04:05 1991 2 tprev-layer leet-layer}) (eo (tu © (a4 wy CO o fear ay) nia) (sett (aref (nat-units tenprnet) a) (make-untt))) (shen (cdr mn) (do (ul ¢-@# oD) “(> ut feaz'n)) nin) . feo ((u2 1 @4u2))) (> ud (eadé n)) nL) (sett (aret (net~connections tamp-net) G2 id) {nake-connection))))} (cond ((~ (length n) (Length STRIGTURE) ) (sete NETWORK temp-net) (sot (nat~size tewp-net) (ear n)) (sctt last-layer tenp-net) ) tt (setf (not-next~layer last—Layer) tenp-net) (sett (net~size tazp-nat) (eat n}) (eet Aast-Layer temp-net)}))) (dofin sat-inittalvaights () {do ( (ayer NETWORK (not~next-Layer leyer))) ({nuli layer) ntl) (st (size (netwsize layer!) (units (net-untes Layer) {connections (net-connact ons: tayer})) (6e-((a 0 (4 u))) (> u size) nia) (set! (unit~activation (aref units u)) 0) (aete (unit-weighted-sun (azef units u)) 0) {setf (unit~dalea (aref units u)).0)) (when (net (output~Layer? Layer) (et ((nextLayer-size (net-size (not-next-layek 1a (do ((u1 0 (+ at))) (> ul size) nil) (de. ((u2 1 G4u2))) ((> ua next=layer-aize) ail) (sett (connection-weight (azef connections ul u2)) (randon-weight) } (sett (conection-deltanwoight. (aref connectiotis Wl. u2)) 9.091191) (Go ((layer REIWORR (net-next—Layer Layer))) ({mull Layee) nil) (sett (unit-ackivation (azef,(net-anits layer) 0) 1.9))) (@erun. bulié-network (acts TOTaL-Inpurs (car smUCTURE)) (sete ToraL-oureuTS (caz (last STRUCTURE))) (eonstructeanits) (set~initial-voights) } #41 Budding the test and train sets. a (Gofun paild-test-and-train-sets -() (sete TOTAL~PRADIING 0) (ith-opencrile (irtie TeATW-erLe setneetion stnputy (do (GG (deadline ittlo nil Terror) (eead=Line {file alk terror) (tot 9)) {for (ateingvequal ** x) (eq x-torror)) (setf TOTAL-TRATHING tot} fwnen (not. (string-equal "* x)) (sate tot (1+ tot})))) (sett TOTAL-TESTING 0) ° (with-open-tile (ifile TESt-FILE :direction :input) {ao (i (read-Line irtte ni ‘error): (read-line if{1e ail ‘error)) (tot 0)? (for (stzingwequal ** x) (eq x ‘ercor)) (set# TOTAL-TESTING tot)) (when (not {aézing-equal "™ xp) (eatf tot {1+ €ot)}})) (gate TeArNING-INpuTS. (akewareay (List (1+ TOTAL-FRAINING) (1+ TOTAE-ENFUTS) ) tnitdar-elenent 0.0)) (sete TRADIING-oorPuTE (wakeworeay (1Lst (1+ TOEAL-TRATWING) (24 ToraL-ourPuRi)) initial-element 0.0) (sett restiNc-rwUTS (moke-erray (List (1+ TOTAE-TESTING) (14 TOTAL-INPUTS)) tinttial-element 0,0) (sete. restinc-cureurs (eakevarray (List (1+°TorAL-tesrius) (14 TorAL-GurpuTS)) Hinitial~element 0.09) : (Hith-open-tite (eile TRAIN-FIEE :direct ion +input) (do (Ge 1 Ge x))) ((> 2 TORAL-TRAIMIS) nL) (do “(ty 4 y11) (> y TomaL-tupuTs) nil) (sete (aret TRAINTIG-IuPuTS “x y} (xead iftle))) fo (iy 1 Gt y))) (@ y TORAL-ouTDINS) Al) (sett (arer TRATHING-OUTPUTS x ¥) {hound-outputs (read S£426))))}) (with-open-flle (if ile TEST-FILE :direction :input) (do (oe 2 Ge a) (> x TOAL-THSTING) nil) feo Uy 2 (14 y))) (ey tonAb-INEUZSY iL) (eot# (aref TESTING-INPUTS x y) ieead {¢ile})) (do (y'T G4 y))) UG y Tomat-oompucey ntl) (sett {aref TESTING-OUTPUTS x y): ibound-outputs (xead if{16))))))) 42 Eunetion BOUND-OUTPUTS takes a target cutput from a testing or training 43 Hla, and forces it to be at least 0.1 sad at most 0.9. (dofan ‘bound~outpute (£) (cond ((> 20.9) O43) (ex 00a) 01) (ea) (dotun init-network.() {build-network) (boild-test-and-train-sets)) any 743 Reading the Lnput £116: aa (defun xead~data (infiie) (with-open-file (ifile infile :dlrectien sinput) (do (0 (xoad~char. igile) (xead-char (fi2e))) (equal x #\:), nity’ aii) (aet£ SmRUCTORE (read 1f4%0)) (do ((% (read~char $file) (read-char 1£218))) ((equal x #3) nil) nil) (set TOTAL-BeOcHS (read. ifile) ) (do (1x (read~cliar Iti1e) (read-char L¢hie))) (Ce: (eetf TEST-INTERVAL (read 4f120)) (ao (0 (road-sher S£128)) (réad-char 1£42¢))) (equal x #\2) ni). aii) (s0tf BIA (read 1fi16)) {a0 (Gx (r9) pun) nity char ifile) (read-chex ifi20)}) ((equal x #\2) nil) nfl) (sett ALPHA (xead Afile)) (G0 (Ge (rend=ehar fie) (road-char Lfkte))) ((equet x #\s) mit) n42) (sett NOISE (read 4Zile)}) {do ((x (read-char file) (xead-cher-{€i1e))) (equal x #\) nil) mit) {sock TRAIN-FILE (delete A\Teb (delete *\Space (read-line iftle)})} (do (tx (read-char iffle) (read-char ifite)}) ((equal X #\3) mil) nil) (sete TEST-FILE (delete A\Tab (delete #\Space (readline 4f41e)})))) Activation function, (astvar rero-float (coerce 0.0 ‘£1eat)) (dafun activetion-funct ion’ (sun) (L 1.0 (4 1.0 {exp (= zexo-float sun} ))}) Artificial Intelligence, Second Edition Blaine Rich and Kevin Kaight MeGraw Hill, 1891 This code may be freaiy copied and ueed for educational of reseerch purposes. ALL software written by Kevin Knight. Comments, bugs, inprovenante to knight@cs.cm.eay BREADTN-PIAS? SEARCH *bee-14ap" FP Structure BES-NODE stores nodes of the search tree, Every node in the Jr breadth-fizet search hae two componente: the state ond the parent of 32 the node. (defstruct pfs-node state parent) 7 Function BFS parfors a breadti+first search and returns a solution poth from start to goal. odes on: the frontier of the search space £¢ kept on RODE-LIST. When a node is expanded, its successors are appended to the back of NOBE-EEF, The vorsions of broadth-fizst searchin this file do not take edge costs into account ~~ i.e., thay expand the search space aa though 7 the cost of moving from one state to another were constant. (dofun BES (start coptional verbose) (Gat ((node-List (List (make-bfe-node retate start sparent 0i3)1)) {do". ({node (car node-List) {car node-list)) {solution~found (if (goal=state? start) nede ni))) (for (nul) node-1ist) solution-round) (4 (nail node-1ist) "Ne solution.” (oxtract-bfe-path solation-féund) )} (umen verbose (format t "Expanding node ~d-4" (be e-nods Gee ((saces (mapear 4” anda (a) (iake-bfs-node ratate © parent. node) ) (expand (bfs-node~state pode) )))} (satq solution-found (find-if # (Lanbda (=) (goal-state? (bfs-node-state #))) suces) ) (sotg node-list (append: (cdr node-1ist) succs)})))) 7 Function EXTRAGT-BFS-PATH takes @ goal node and follows its parent ty pointers back to the initial nede, returning the list of all states Slong the. path. (defun extrdct-bes-path (node) (do ({path nit) (n node (bfe-node-pazent n))) ({mulhn) path) (setg path (cons (bfe-node-state mn) path)))) 37 Function BFS-GRAPH 1s the sane as SPS except duplicate nodes are nat 42 added to the search space. that. 1s, the sama state will not be expanded Fs twice. Nodes already expanded are Kept in a list of closed nodes. (dofun Wfa-geaph: (start Goptional vazbose) (let ({node-List (ist {nake-bEs-node ratate start :parent nil)))) (dot ((node (ear noderList) (eax nods-List) ) (closed nit) (goal-found (46 (goal-state? start) node nil))) (Gor (null node-LL3e) geal-found) GE (moll nede-1tat) "Ho, solution.” (extract-pfs-path goal-found) )) (when verbose (format t "Expanding node ~d-4" (bes-node-state node) )) (ett (lali-succe: (mapcar #¥ (lambda (a) (make-bfs-node sstate rparant. node) ) (expand (bts~noderstate node) })) (suces (nodés-not-visited all-succs neds-list closed})) {eetg goal~found (find-if #” (Lambda (3) {goal-state? (bfs-node-state 9))) - ‘saces)} {e0tg node-List (append (cdr node-iist} suees)) satq closed (cons node closed)})})) 42 Function NODES-NOF-VISITED returns all nodes on ALL-SUCCS that are not. 22 present on NODE-LEST or CLOSED. That is, all nodea that have not been 17 proviously vielted. {@efun nodes-not-visited (all-siices nodeList closed) difference . (set-difference aii-suces node-list fest #! (lambda (a1 n2) closed rtest #/ (lambda (at p2) feqratates. (bfs-node-state nl) {hfs~node~state n2))})) backprop2. lisp Mon Jun 10 18:04:05 1991 (format i¢ile * (target =") (@o (3 2 Ge 309) (US 5 ToTAL-ouRPUTS) nia) (fommat {ri10 "4,12"" (aref TESTING-OUTPUTS 1 4))) (foxmat ifite *)-9"))}) 74 Print wotghes. Idofun print-weights (inesiey Withwopen-file. (¢fle intitle (tomat itile "-s-welghte:-a-¥") (do (ayer NETWORK {net=next-Layer Layer}) (k 1 (1+ x))) (output-layer? layer) il) {do (a 0 Gt UY (> u (net~aize teyex) nia) (oe, £02 2 4 0201) (0 a2 (netratze (net-aeet-Layer Layer) }) nit) (let “{(ehecconnection (acof (net-connectlons layer) ¥2)}) (format ifile tae" Gast kw u2 (connect ion-weight the-connection))?})))}) idiraction routpot :it-existe :apperd) 774 Backprop. : (detun backprop (inritey (raoa-data intile) (init-netwerk) (earn infile) {print—weights infi2e)) " Arvigicial Intelligence, Second Edition Elaine Rich and Kevin Knight MeGrav #411, 1991 “this code tay be freely copied and used for educational oF research purposes. All software written by Kevin Knight» Gomnents, bugs, inprovenents to knightees ons .edst ote DEPTH-FIRST. SEARCH ndze.akep" 22 Function DPS dees a depth-first search from the start state. If 4 903) Te Note is found, st returns a solution path, DFS can easily got stuck 12 SORES [neiiite loop, since it may enter the sene state twice along the Ty aume search path. (eofun dfs (start, boptional verbose) (let, {(resule (aes-1 start verbose) }) {ie (noht fepuie) "Me solution.” result))) (aetun dta-1 (start verbose) (aon verbose (format * "Expanding node ~&-4" start)) {cond ((goei-state? start) (List start)) e {do ((ovecs (expand start) (edr eucce)) (golution-found nil)) (lor solutien-found: {nail succa)) (ie selutien-found (eons start solution-found) 811)) Jig selution-found (df2-1 (car succe) vexbore))}))) fo 2 Tiactlon DES-AVOID-LOGES does ® depth-first: seaxch from the gtart statey 32 Eu never entere 9 state which it has already entered. along the current Fee ccn cath. Te avoids Loops by renenbering all of the aneéatory of & bp given stat 2 U2 punction DFS-NETH-CUTOFF takes an additional parancter, DEPTH-CUTOEF, yea factors the same search as DFS~AVOIO-LOOPS, but never searches Hi to a depth beyond the eutoel. {defon dfs-aveid-loops. (start koptional verbor0} (lets (iparents 11) (Gepen-cutof® sintinity*) (ete fare avoid-loope-I start parents depth-cutott vezboae))) (Ae (hull zesuit) "No solution." resu3t})) (aotun afs-with-catof! (staxt depth-cutoff coptional verbose) (let* ({parents nil) QREESIe (Gtevavold-Loops-1 start parents depth-cuteft verbose) )) (ae (nell Fesulty "We solutien.” sesutt))) (Getun dfs-avold-loops-1 (start parents depth-cutott verbose) nen verbose (format t "Expanding node ~d-f" stezt)) {eond ((goal-state? start) (List stert)) (@ depth-eutoff 0) nil) } decrease depth-cutofe until it is 0 te (et {(all-suces (expend stert})) (do ((aucee (removendts~parents ali-succs parent) (ear suees)) (solvtion=found nil) (lor solution-found (nel1 succs)) (iE solutlcncfoand (cons start solution-founa) ni1)) (aotg solution-found: (dfs-avold-loops-1 (car sucse) teons atart parents) GQ depth-cutess) ‘vezbose)})?1)) (dofun renove-dfx-poxents (all~suces parents) “wapoan #” (lambda (euce) Nit (member suce parents ttest #"aq-states) pal (det suce))) wwccs}) dfid.tisp Wed Jun 12 15:16:58. 1991 1 fo Axbiticlal Intelligence, Second Edition Blaine Rich and kevin Knight Mecraw Hii2, 1981 This code may be frosly copied and used for educational ox research purposes. ALL software written by Kovin Knight. Coninent's, bugs, inprovenants to krigit@es.cmavedu TTERATIVE-DEEPENTNG SEARCH rafia. lisp" Function DED does a depth-first iterative deepening search. hen it 31 reaches the goal state, it returns a solution path. DFID calls 22 DES-WITH-CUTOFF each tine With greater depth, which performs » depth 3) Limited, depth-first search avoiding loops along a zingle path. (dafun dfia (start ceptions) verbose) (do ((auecees nit) (Gepth 1 (1+ depen))) (suecess success) (shon verbose (format t "Beginning iteration nlinbex -€-*" depth) ) Get (leeoult (afswiehmcutose start depth verbose)!) (when (not (and (atringp resuie) (stringrequal result "No solution.*))) (eotg success reeult))))) {dofun dts~with-cutoft (start depth-cutett Loptional verbose (Get* (parents all) (result. (ats-avold-loops-1 start parents depth-cutef£ varbose) }) (4£-(mul1 result) "io solution.* ‘resuit})) etun dts-aveid-Locpe-1 (start parents depth-cutot® verbose) (hon ‘warboae (format € "Expanding node ~d-¥" start}) (cond ((gosl-state? start) (List stare)) (> depth-cutore 0) nil) 3 decrease depth-cutoff until it is 0 (te (ist ({att-succe (expand start))) (do ((svecs (renove-ida~parents all-suces pazents) (ear succs)) {solution=founa nil)) Uor selution-found (neil .egces) ) UE solution-found {cons start aelution-£ound) nil) (sotq solucion-found (dks-avola-loope-l (ear succs) ; {eons start parenta) (i= depth-cutots) verbose} }))})) 27 Function IDA-STAR performs a heuristic depth-first Iterative deepening 22 aearch. It oxplores the saarch epace deoper and deeper on cach iteretions during each iteration, it expands all nodes whose gsh values are less than sone thrashold. J} he thweshold 1a initialized to ‘intinity* before we call TDA~STAR-DFS ducing an iteration, and is sugnented by the minunum Enount Jt as exceeded during that iteration: (dotvar *enount~axcesded* 0) (defo Lde-star (start coptionsl verbore) {G0 ((solution-found nil) (iteration 1 (+ iteration) (tareshold (heuristic start) }) (solut ion-feuna. solution=tound) (when verbose (format t "Beginning iteration nuibar -d, thxeshoid = “a-4" iteration threshold)? (setq *amount-excosded* *intinitys) (et (parents nit) (depth 0)) (oatg solution-found (ida-star~ifs start parants threshold Septh varbose})) (eotq threshold (+ threshold *amount~szcesdad*)) , function IDA~STAR-DFS pexforms hourlstic threshold-Limited depth-first seach avalding loops aleng a single path. (dofun ida-star-dfa (start parents threshold cost-so-far verbose) (cond ({goal-state? start) (Iist start)) (& (4 cost-sorfar {heuristic start)) threshold) eq tamount-exceeded* (min tancunt-excooded* (= (4 cot-so-far (heuristic start)) : + threshota))) when verbose (format t "Expanding node ~d-€" atart)) (ist ((all-sveca (expand stare))) (do ((suces (rapcve-ida~parents all-succe parents) (cdr suécs)) {solutton-found, nil) ) (for salution-feahd (null “gucea)) (if solvtion-found (cons, start solution-found) nil) (sotq solution-found (ida-star-dfe (car succes) (cons start parents) threshold ( cost-so-tar (cost-of-nove start (ear auces))) verbose)}))})) (Cetun romove-ida-paronts (el1-suces parents) (mapesn @” (lanbde (sue3) (Af (manbor eoce parents theat #eq-stater) ab (ist sucer)) ali-aieea)) 4 Artificial Intelligence, Second Edition Bisine Rich and Kevin Khight MoGrew HLL, 1992 mais code may be frsely copied and used for educational ox research purposes hil software weitten by Kevin Knight. Bonmants, buge, improvenents te knightécs .cm. his file con be used as a tenplate to create nev gates. To create your 24m Tins, eit in the functions below, adding whatever auxiliary functions 252 Rocessary. the functions are: [éeep-enough pos depth) (etatic por player) t if the search has: proceaded deep encugh. Gvalwation of position pos from player's point ef view. generate all successor positions Eo pos. (opposite player) Zeturn the opposite player. {print-bosrd pos) print boasd position pos. cee eran player move) fetien new position basad on old position and player's move. ese player has won. © if pos is « drawn position. (movegen pos phaye:) (won? pos player) {aramn? pos) the important: variables are: setartt the initial board configuration. jnae fonctions and variables are a12 called from minimax-1iap- For an example gane, ae “tictactoe-1isp™. . jy Variable “START! Nalds the starting position for the gane- (detvar tstareh) {sotq tetart* nid 1 runction DEEe-ENOUGH returns & LE the search has procaeded deep enoogh. 11 eh i a tinction of (either oF porn the position and the dapth of «2ezan aeeprenooh (foe seen) ty function SHMTIC returns a number which {a the evaluation of perltten poe sence wlewsoine of the player. A positive sumber is favorable for the Uy players a negseive one is unfavorable. (dsfun static (poe player) nil) s4 Fonction WOVEGEN takes = position and a player snd generates 22) legal T eenetsuor positions, i.e-y a1] possible moves * player cold make, {gefun movegen (pos player) wind 1 Function OPPOSITE returns player one when given player two, and fh vice-versa (dofun opposite (player! ail) Function PRINT-BOARD prints the gane board. (detun print-board (pos) il) jz Eunctlon VAKE-}OVE roturns @ new posstion based on the eld position and bys player's move. (Gefun make-nove {pos player’nove) aid) fy Function ON? returns t Lf player has won 4n position po (defun von? (pos player! ily fy Function DRAIN? roturne € Lf pos Ls a dravn (tied) position. {eetun drawn? (pos) ail)

You might also like