Artificial Intelligence A Systems Approach
Artificial Intelligence A Systems Approach
ARTIFICIAL
INTELLIGENCE
ASystemsApproach
LICENSE,DISCLAIMEROFLIABILITY,ANDLIMITEDWARRANTY
TheCD-ROMthataccompaniesthisbookmayonlybeusedonasinglePC.Thislicensedoes
notpermititsuseontheInternetoronanetwork(ofanykind).Bypurchasingorusingthis
book/CD-ROMpackage(the“Work”),youagreethatthislicensegrantspermissiontousethe
productscontainedherein,butdoesnotgiveyoutherightofownershiptoanyofthetextual
contentinthebookorownershiptoanyoftheinformationorproductscontainedonthe
CD-ROM.Useofthirdpartysoftwarecontainedhereinislimitedtoandsubjecttolicensing
termsfortherespectiveproducts,andpermissionmustbeobtainedfromthepublisherorthe
ownerofthesoftwareinordertoreproduceornetworkanyportionofthetextualmaterial
orsoftware(inanymedia)thatiscontainedintheWork.
INFINITYSCIENCEPRESSLLC(“ISP”or“thePublisher”)andanyoneinvolvedinthe
creation,writingorproductionoftheaccompanyingalgorithms,code,orcomputerprograms
(“thesoftware”)oranyofthethirdpartysoftwarecontainedontheCD-ROMoranyofthe
textualmaterialinthebook,cannotanddonotwarranttheperformanceorresultsthatmight
beobtainedbyusingthesoftwareorcontentsofthebook.Theauthors,developers,andthe
publisherhaveusedtheirbesteffortstoinsuretheaccuracyandfunctionalityofthetextual
materialandprogramscontainedinthispackage;we,however,makenowarrantyofanykind,
expressorimplied,regardingtheperformanceofthesecontentsorprograms.TheWorkis
sold“asis”withoutwarranty(exceptfordefectivematerialsusedinmanufacturingthedisc
orduetofaultyworkmanship);
Theauthors,developers,andthepublisherofanythirdpartysoftware,andanyoneinvolved
inthecomposition,production,andmanufacturingofthisworkwillnotbeliablefordamages
ofanykindarisingoutoftheuseof(ortheinabilitytouse)thealgorithms,sourcecode,
computerprograms,ortextualmaterialcontainedinthispublication.Thisincludes,butisnot
limitedto,lossofrevenueorprofit,orotherincidental,physical,orconsequentialdamages
arisingoutoftheuseofthisWork.
Thesoleremedyintheeventofaclaimofanykindisexpresslylimitedtoreplacementof
thebookand/ortheCD-ROM,andonlyatthediscretionofthePublisher.
Theuseof“impliedwarranty”andcertain“exclusions”varyfromstatetostate,andmight
notapplytothepurchaserofthisproduct.
ARTIFICIAL
INTELLIGENCE
ASystemsApproach
M.TIMJONES
INFINITYSCIENCEPRESSLLC
Hingham,Massachusetts
NewDelhi
Copyright2008byINFINITYSCIENCEPRESSLLC
Allrightsreserved.
Thispublication,portionsofit,oranyaccompanyingsoftwaremaynotbereproducedinanyway,storedinaretrieval
systemofanytype,ortransmittedbyanymeansormedia,electronicormechanical,including,butnotlimitedto,
photocopy,recording,Internetpostingsorscanning,withoutpriorpermissioninwritingfromthepublisher.
Publisher:DAVIDPALLAI
INFINITYSCIENCEPRESSLLC
11LeavittStreet
Hingham,MA02043
Tel.877-266-5796(tollfree)
Fax781-740-1677
info@infinitysciencepress.com
www.infinitysciencepress.com
Thisbookisprintedonacid-freepaper.
M.TimJones.ArtificialIntelligence:ASystemsApproach
ISBN:978-0-9778582-3-1
Thepublisherrecognizesandrespectsallmarksusedbycompanies,manufacturers,anddevelopers
asameanstodistinguishtheirproducts.Allbrandnamesandproductnamesmentionedinthis
bookaretrademarksorservicemarksoftheirrespectivecompanies.Anyomissionormisuse(ofany
kind)ofservicemarksortrademarks,etc.isnotanattempttoinfringeonthepropertyofothers.
LibraryofCongressCataloging-in-PublicationData
JONES,M.TIM.
Artificialintelligence:asystemsapproach/M.TimJones.
p.cm.
Includesindex.
ISBN-13:978-0-9778582-3-1(hardcoverwithcd-rom:alk.paper)
1.Artificialintelligence--Dataprocessing.2.Artificialintelligence--Mathematicalmodels.I.Title.
Q336.J682008
006.3--dc22
2007045869
78904321
Ourtitlesareavailableforadoption,licenseorbulkpurchasebyinstitutions,corporations,etc.For
additionalinformation,pleasecontacttheCustomerServiceDept.at877-266-5796(tollfree).
RequestsforreplacementofadefectiveCD-ROMmustbeaccompaniedbytheoriginaldisc,your
mailingaddress,telephonenumber,dateofpurchaseandpurchaseprice.Pleasestatethenatureofthe
problem,andsendtheinformationtoINFINITYSCIENCEPRESS,11LeavittStreet,Hingham,MA02043.
ThesoleobligationofINFINITYSCIENCEPRESStothepurchaseristoreplacethedisc,basedondefective
materialsorfaultyworkmanship,butnotbasedontheoperationorfunctionalityoftheproduct.
DEDICATION
Thisbookisdedicatedtomywonderfulwife,Jill,withoutwhomthisbookwouldnotbe
possible.I’malsoindebtedtomyparentsMauryandCeleta,whoinstilledinmeadesireto
learnandwonder.
ACKNOWLEDGMENTS
Atthetimeofthiswriting,AIiscelebratingits50thanniversary.ItwasAugustof1956when
researchersmetattheDartmouthSummerResearchProjectonArtificialIntelligencewith
theagendaofcreatingintelligentmachines.Inthe50yearsthatfollowed,AIhasbecomea
genuinefieldofstudy,buttheroadhasnotbeenwithoutitsbumps.
Acknowledgingallthosewho’vecontributedtoAIwouldfillabookmuchlargerthan
this.ButI’dliketopersonallyrecognizeJohnMcCarthyforintroducingAIin1955(atthe
Dartmouth Summer Project) and for having created the wonderful Lisp programming
language.
TABLEOFCONTENTS
Chapter1 TheHistoryofAI 1-19
WhatisIntelligence? 1
TheSearchforMechanicalIntelligence 2
TheVeryEarlyDays(theearly1950’s) 3
AlanTuring 3
AI,ProblemSolvingandGames 4
ArtificialIntelligenceEmergesasaField 5
TheDartmouthAISummerResearchProject 5
BuildingToolsforAI 6
TheFocusonStrongAI 6
ConstrainedApplications 7
Bottom-UpApproachesEmerge 7
AI’sWinter 8
Results-OrientedApplications 8
AdditionalAIToolsEmerge 9
Neatvs.ScruffyApproaches 9
AIRemerges 10
TheSilentReturn 10
MessyandScruffyApproachesTakeHold 10
AgentSystems 12
AIInter-disciplinaryR&D 12
SystemsApproach 13
OverviewofthisBook 15
UninformedSearch 15
InformedSearch 15
AIandGames 15
KnowledgeRepresentation 16
MachineLearning 16
EvolutionaryComputation 16
NeuralNetworksPart1 16
NeuralNetworksPart2 17
IntelligentAgents 17
BiologicallyInspiredandHybridModels 17
LanguagesofAI 17
ChapterSummary 18
References 18
Resources 18
Exercises 19
AbouttheCD-ROM 485
Index 487-498
Chapter 1 THEHISTORYOFAI
T
hehistoryofAIisinterestingallbyitself.It’samodern-daydrama,
filledwithexcitementandanticipation,discovery,anddisappointment.
Fromover-promisesofearly(andlater)AIresearch,tofearsofthe
unknownfromthegeneralpublic,AI’shistoryisworthyofstudybyitself.
In this chapter, we’ll explore AI’s tumultuous history and also provide a
summaryintroductiontoeachofthechaptersofthisbook.
WHATISINTELLIGENCE?
To build software that is deemed intelligent, it’s helpful to begin with a
definition of intelligence. Intelligence can be simply defined as a set of
propertiesofthemind.Thesepropertiesincludetheabilitytoplan,solve
problems, and in general, reason. A simpler definition could be that
intelligenceistheabilitytomaketherightdecisiongivenasetofinputsand
avarietyofpossibleactions.
Usingthissimpledefinitionofintelligence(makingtherightdecision),
wecanapplythisnotonlytohumans,butalsotoanimalsthatexhibitrational
behavior.Buttheintelligencethatisexhibitedbyhumanbeingsismuch
morecomplexthanthatofanimals.Forexample,humanshavetheability
2 ArtificialIntelligence
tocommunicatewithlanguage,butsodosomeanimals.Humanscanalso
solveproblems,butthesamecanbesaidofsomeanimals.Onedifference
then is that humans embody many aspects of intelligence (the ability to
communicate, solve problems, learn and adapt) where animals typically
embodyasmallnumberofintelligentcharacteristics,andusuallyatamuch
lowerlevelthanhumans.
WecanusethesameanalogyonAIappliedtocomputersystems.For
example,it’spossibletobuildanapplicationthatplaysaworld-classgameof
Chess,butthisprogramknowsnothingofthegameofCheckers,norhowto
makeagoodcupoftea.Adataminingapplicationcanhelpidentifyfraud,
butcan’tnavigateacomplexenvironment.Fromthisperspective,themost
complex and intelligent applications can be deemed intelligent from one
perspective,butlackeventhesimplestintelligencethatcanbeseeninthe
leastintelligentofanimals.
NOTE Famed author Isaac Asimov once wrote about his experience with
aptitudetestsinthearmy.Inthearmy,hescoredwellabovethenorm.
But what he realized was that he could score well on tests that were
developedbyothersthatsharedhisacademicbents.Heopinedthatif
thetestsweredevelopedbypeopleinvolvedinautorepair,hewouldhave
scoredverypoorly.Theissuebeingthattestsaredevelopedarounda
coreofexpertise,andscoringpoorlyononedoesn’tnecessarilyindicate
alackofintelligence.
THESEARCHFORMECHANICALINTELLIGENCE
Historyisfilledwithstoriesofthecreationofintelligentmachines.Inthe
800sBC,theIliaddescribedthewingedTalos,abronzeautomatonforgedby
HephaestustoprotectCrete.TheinnerworkingsofTalosweren’tdescribed,
exceptthathewasbronze,andfilledwithichor(oraGreekgod’sblood).A
morerecentexampleisMaryShelley’sFrankenstein,inwhichthescientist
recreateslifefromold.In1921,KarelCapek’splay“Rossum’sUniversal
Robots”introducedtheconceptofcheaplaborthroughrobotics.
But one of the most interesting applications of artificial intelligence,
inanon-robiticform,wasthatoftheHAL9000introducedbyArthurC.
Clarkinhishisnovel“2001:ASpaceOdyssey.”HALwasasentientartificial
intelligence that occupied the Discovery spaceship (en route to Jupiter).
HALhadnophysicalform,butinsteadmanagedthespaceship’ssystems,
visuallywatchedthehumanoccupantsthroughanetworkofcameras,and
TheHistoryofAI 3
communicatedwiththeminanormalhumanvoice.Themoralbehindthe
storyofHALwasoneofmodern-dayprogramming.Softwaredoesexactly
whatonetellsittodo,andcanmakeincorrectdecisionstryingtofocuson
asingleimportantgoal.HALobviouslywasnotcreatedwithIsaacAsimov’s
threelawsofroboticsinmind.
THEVERYEARLYDAYS(THEEARLY1950s)
Whilethetermartificialintelligencehadnotyetbeenconceived,the1950s
weretheveryearlydaysofAI.Earlycomputersystemswerebeingbuilt,and
theideasofbuildingintelligentmachineswerebeginningtoform.
AlanTuring
In 1950 it was Alan Turing who asked whether a machine could think.
Turingnotlongbeforehadintroducedtheconceptofhisuniversalabstract
machine(calledtheTuringMachine)thatwassimpleandcouldsolveany
mathematicalproblem(albietwithsomecomplexity).Buildingonthisidea,
Turingwonderedthatifacomputer’sresponsewereindistinguishablefrom
ahuman,thenthecomputercouldbeconsideredathinkingmachine.The
resultofthisexperimentiscalledtheTuringTest.
IntheTuringtest,ifthemachinecouldfoolahumanintothinkingthat
itwasalsohuman,thenitpassedtheintelligencetest.Onewaytothinkof
theTuringtestisbycommunicatingtotheotheragentthroughakeyboard.
Questions are asked of the peer through written text, and responses are
provided through the terminal. This test provides a way to determine if
intelligencewascreated.Consideringthetaskathand,notonlymustthe
intelligent peer contain the necessary knowledge to have an intelligent
conversation,itmustbeabletoparseandunderstandnaturallanguageand
generatenaturallanguageresponses.Thequestionsmayinvolvereasoning
skills(suchasproblemsolving),somimickinghumanswouldbeafeat!
AnimportantrealizationofTuringduringthisperiodwastheneedto
start small and grow intelligence, rather than expecting it to materialize.
Turing proposed what he called the Child Machine in which a lesser
intelligent agent would be created and then subjected to a course of
education.Ratherthan assume that we could build an adult intelligence,
wewouldbuildachildintelligencefirstandtheninjectitwithknowledge.
Thisideaofstartingsmallandatlowerlevelscorrespondswithlaterideas
ofso-called“scruffy”thinkers.Thehumanbrainiscomplexandnotfully
4 ArtificialIntelligence
understood,insteadofstrivingtoimitatethis,whynotstartsmalleratthe
child(orevensmallerorganism)andworkourwayup?Turingcalledthisthe
blanksheetsargument.Achildislikeanotebookthat’sfullofblanksheets,
butisamechanismbywhichknowledgeisstored.
AlanTuring’slifeendedatayoungage,buthe’sconsideredthefounder
ofthefieldofAI(eventhoughthemonikerwouldnotbeappliedforanother
sixyears).
AI,ProblemSolving,andGames
Some of the earliest applications of AI focused on games and general
problemsolving.Atthistime,creatinganintelligentmachinewasbasedon
thebeliefthatthemachinewouldbeintelligentifitcoulddosomethingthat
peopledo(andperhapsfinddifficult).
NOTE In 1950, Claude Shannon proposed that the game of Chess was
fundamentalyasearchproblem.Infact,hewascorrect,butbruteforce
searchisn’ttrulypracticalforthesearchspacethatexistswithChess.
Search,heuristics,andacatalogofopeningandendingmovesprovides
afasterandmoreefficientwaytoplayChess.Shannon’sseminalpaper
on computer Chess produced what is called the Shannon number, or
10^120,whichrepresentsthelowerboundofthegametreecomplexity
ofChess.[Shannon1950]
The first AI program written for a computer was called “The Logic
Theorist.”Itwasdevelopedin1956byAllenNewell,HerbertSimon,andJ.
C.Shawtofindproofsforequations.[Newell1956]Whatwasmostunique
aboutthisprogramisthatitfoundabetterproofthanhadexistedbeforefor
agivenequation.In1957,SimonandNewellbuiltonthisworktodevelop
theGeneralProblemSolver(GPS).TheGPSusedmeans-endanalysisto
solveproblems,butingeneralwasrestrictedtotoyproblems.
Like complex math, early AI researchers believed that if a computer
couldsolveproblemsthattheythoughtwerecomplex,thentheycouldbuild
intelligentmachines.Similarly,gamesprovidedaninterestingtestbedforthe
developmentofalgorithmsandtechniquesforintelligentdecisionmaking.
IntheUKatOxfordUniversityintheearly1950s,researchersdeveloped
game-playing programs for two complex games. Christopher Strachey
developedaCheckersplayingprogramontheFerrantiMarkI.By1952,his
programcouldplayareasonablegameofCheckers.DietrichPrinzdeveloped
aprogram,againfortheFerrantiMarkI,thatcouldplayChess(mate-in-two
variety).Hisprogramcouldsearchathousandpossiblemoves,butonthis
TheHistoryofAI 5
earlycomputer,itrequiredsignificanttimeandplayedveryslowly.
In1952,ArthurSamuelraisedthebarforAIprograms.HisCheckers
playing program, which ran on the IBM 701, included learning and
generalization.WhatSamueldidwithhislearningCheckersprogramwas
uniqueinthatheallowedtwocopiesofhisprogramtoplayoneanother,
andthereforelearnfromeachother.Theresultwasaprogramthatcould
defeatitscreator.By1962,Samuel’sCheckersprogramdefeatedtheformer
ConnecticutCheckerschampion.
NOTE Samuel’s program, and his approach of playing copies against one
another,isoneofthefirstexamplesofcomputingsurvivalofthefittest
andthefieldwhichcametobecalledevolutionarycomputation.
ARTIFICIALINTELLIGENCEEMERGESASAFIELD
Bythemid1950s,AIbegantosolidifyasafieldofstudy.AtthispointinAI’s
life,muchofthefocuswasonwhatiscalledStrongAIStrongAIisfocused
on building AI that mimics the mind. The result is a sapient entity with
human-likeintelligence,self-awareness,andconsciousness.
TheDartmouthAISummerResearchProject
In 1956, the Dartmouth AI Conference brought about those involved in
research in AI: John McCarthy (Dartmouth), Marvin Minsky (Harvard),
Nathaniel Rochester (IBM), and Claude Shannon (Bell Telephone
Laboratories)broughttogetherresearchersincomputers,naturallanguage
processing,andneuronnetstoDartmouthCollegeforamonth-longsession
ofAIdiscussionsandresearch.TheSummerresearchprojectonAIbegan:
Weproposethata2month,10manstudyofartificialintelligence
becarriedoutduringthesummerof1956atDartmouthCollege
inHanover,NewHampshire.Thestudyistoproceedonthebasis
oftheconjecturethateveryaspectoflearningoranyotherfeature
ofintelligencecaninprinciplebesopreciselydescribedthata
machinecanbemadetosimulateit.Anattemptwillbemadeto
findhowtomakemachinesuselanguage,formabstractionsand
concepts,solvekindsofproblemsnowreservedforhumans,and
improvethemselves.Wethinkthatasignificantadvancecanbe
madeinoneormoreoftheseproblemsifacarefullyselected
groupofscientistsworkonittogetherforasummer.
6 ArtificialIntelligence
Since then, many AI conferences have been held around the world,
and on a variety of disciplines studied under the AI moniker. In 2006,
Dartmouthheldthe “Dartmouth Artificial Intelligence Conference: The
NextFiftyYears”(informallyknownasAI@50).Theconferencewaswell
attended(evenfromafewthatattendedthefirstconference50yearsprior),
andanalyzedAI’sprogressandhowitschallengesrelatetothoseofother
fieldsofstudy.
BuildingToolsforAI
Inadditiontocoiningthetermartificialintelligence,andbringingtogether
majorresearchersinAIinhis1956Dartmouthconference,JohnMcCarthy
designedthefirstAIprogramminglanguage.LISPwasfirstdescribedby
McCarthyinhispapertitled“RecursiveFunctionsofSymbolicExpressions
andtheirComputationbyMachine,PartI.”ThefirstLISPcompilerwas
alsoimplementedinLISP,byTimHartandMikeLevinatMITin1962for
theIBM704.
Thiscompilerintroducedmanyadvancedfeatures,suchasincremental
compilation.[LISP2007]McCarthy’sLISPalsopioneeredmanyadvanced
conceptsnowfamiliarincomputerscience,suchastrees(datastructures),
dynamictyping,object-orientedprogramming,andcompilerself-hosting.
LISP was used in a number of early AI systems, demonstrating its
usefulnessasanAIlanguage.Onesuchprogram,calledSHRDLU,provides
anaturallanguageinterfacetoatable-topworldofobjects.Theprogramcan
understandqueriesaboutthetable-top“world,”reasonaboutthestateof
thingsintheworld,planactions,andperformsomerudimentarylearning.
SHRDLUwasdesignedandimplementedbyTerryWinogradattheMIT
AILabonaPDP-6computer.
LISP, and the many dialects that evolved from it, are still in wide
use today. Chapter 13 provides an introduction to the languages of AI,
includingLISP.
TheFocusonStrongAI
Recall that the focus of early AI was in Strong AI. Solving math or logic
problems,orengagingindialogue,wasviewedasintelligent,whileactivities
suchaswalkingfreelyinunstableenvironments(whichwedoeveryday)
werenot.
In1966,JosephWeizenbaumofMITdevelopedaprogramthatparodied
apsychologistandcouldholdaninterestingdialoguewithapatient.The
design of Eliza would be considered simple by today’s standards, but its
TheHistoryofAI 7
pattern-matchingabilities,whichprovidedreasonableresponsestopatient
statements was real to many people. This quality of the program was
troublingtoWeizenbaumwholaterbecameacriticofAIbecauseofitslack
ofcompassion.
ConstrainedApplications
WhilemuchofearlyAIwasStrong-focused,therewerenumerousapplications
thatfocusedonsolvingpracticalproblems.Onesuchapplicationwascalled
the“DendralProject,”emergingin1965atStanfordUniversity.Dendralwas
developedtohelporganicchemistsunderstandtheorganizationofunknown
organic molecules. It used as its inputs mass spectrometry graphs and a
knowledgebaseofchemistry,makingitthefirstknownexpertsystem.
Other constrained applications in this era include Macsyma, a
computeralgebrasystemdevelopedatMITbyCarlEngelman,William
Martin, and Joel Moses. Macsyma was written in MacLisp, a dialect
of LISP developed at MIT. This early mathematical expert system
demonstrated solving integration problems with symbolic reasoning.
The ideas demonstrated in Macsyma eventually made their way into
commercialmathapplications.
Bottom-UpApproachesEmerge
EarlyAIfocusedonatop-downapproachtoAI,attemptingtosimulateor
mimicthehigherlevelconceptsofthebrain(planning,reasoning,language
understanding,etc.).Butbottom-upapproachesbegantogainfavorinthe
1960s,primarilymodelinglower-levelconcepts,suchasneuronsandlearning
at a much lower level. In 1949, Donald Hebb introduced his rule that
describeshowneuronscanassociatewithoneanotheriftheyarerepeatedly
activeatthesametime.Thecontributionofonecell’sfiringtoenableanother
willincreaseovertimewithpersistentfiring,leadingtoastrongrelationship
betweenthetwo(acausalrelationship).
But in 1957, the perceptron was created by Frank Rosenblatt at the
CornellAeronauticalLaboratory.Theperceptronisasimplelinearclassifier
that can classify data into two classes using an unsupervised learning
algorithm.Theperceptroncreatedconsiderableinterestinneuralnetwork
architectures,butchangewasnotfaraway.
AI’SWINTER
Prior to the 1970s, AI had generated considerable interest, and also
considerablehypefromtheresearchcommunity.Manyinterestingsystems
had been developed, but these fell quite short of the predictions made by
someinthecommunity.Butnewtechniquessuchasneuralnetworksbreathed
newlifeintothisevolvingfield,providingadditionalwaysforclassificationand
learning.Buttheexcitementofneuralnetworkscametoanendin1969with
thepublicationofthemongraphtitled“Perceptrons.”Thismonographwas
writtenbyMarvinMinskyandSeymourPapert,strongadvocatesofStrong(or
top-down)AI.Theauthorsrightlydemonstratedthatsingle-layerperceptrons
werelimited,particularlywhenconfrontedwithproblemsthatarenotlinearly
separable (such as the XOR problem). The result was a steep decline of
fundingintoneuralnetworkresearch,andingeneral,researchinAIasafield.
Subsequentresearchwouldfindthatthemulti-layernetworkssolvedthelinear
separationproblem,buttoolateforthedamagedonetoAI.
HardwarebuiltforAI,suchastheLISPmachines,alsosufferedaloss
of interest. While the machines gave way to more general systems (not
necessarily programmed in LISP), the functional languages like LISP
continuedtoattractattention.PopulareditorssuchasEMACS(developed
duringthisperiod)stillsupportalargeusercommunitywithascriptingshell
basedonLISP.
Results-OrientedApplications
WhiletherewasareductioninfocusandspendinginAIresearchinthe
1970s,AIdevelopmentcontinuedbutinamorefocusedarena.Applications
that showed promise, such as expert systems, rose as one of the key
developmentsinthisera.
Oneofthefirstexpertsystemstodemonstratethepowerofrules-based
architectures was called MYCIN, and was developed by Ted Shortliffe
following his dissertation on the subject while at Stanford (1974). MYCIN
operated in the field of medical diagnosis, and demonstrated knowledge
representation and inference. Later in this decade, another dissertation at
StanfordbyBillVanMellesbuiltontheMYCINarchitectureandservesasa
modelfortheexpertsystemshell(stillinusetoday).InChapter5we’llprovide
anintroductiontotherepresentationofknowledgeandinferencewithlogic.
Otherresults-orientedapplicationsincludedthosefocusedonnatural
language understanding. The goal of systems in this era was in the
development of intelligent question answering systems. To understand a
questionstatedinnaturallanguage,thequestionmustfirstbeparsedinto
TheHistoryofAI 9
its fundamental parts. Bill Woods introduced the idea of the Augmented
TransitionNetwork(orATN)thatrepresentsformallanguagesasaugmented
graphs.FromElizainthe1960stoATNsinthe1970s,NaturalLanguage
Processing(NLP)andNaturalLanguageUnderstanding(NLU)continues
todayintheformofchatterbots.
AdditionalAIToolsEmerge
JohnMcCarthyintroducedtheideaofAI-focusedtoolsinthe1950swiththe
developmentoftheLISPlanguage.Expertsystemsandtheirshellscontinued
the trend with tools for AI, but another interesting development that in a
waycombinedthetwoideasresultedfromtheProloglanguage.Prologwas
alanguagebuiltforAI,andwasalsoashell(forwhichexpertsystemscould
bedeveloped).Prologwascreatedin1972byAlainColmeraurandPhillipe
RousselbasedontheideaofHornclauses.Prologisadeclarativehigh-level
languagebasedonformallogic.ProgramswritteninPrologconsistoffactsand
rulesthatreasonoverthosefacts.YoucanfindmoreinformationonPrologin
Chapter5KnowledgeRepresentationandChapter13,TheLanguagesofAI.
NeatvsScruffyApproaches
A split in AI, its focus, and basic approaches was also seen during this
period. Traditional, or top-down AI (also called Good-Old-Fashioned-AI,
or GOFAI for short) continued during this period but new approaches
begantoemergethatlookedatAIfromthebottom-up.Theseapproaches
werealsolabeledNeatandScruffyapproachessegregatingthemintotheir
representativecamps.Thoseintheneatcampfavoredformalapproachesto
AIthatwerepureandprovable.Butthoseinthescruffycampusedmethods
lessprovablebutstillyieldingusefulandsignificantresults.Anumberof
scruffyapproachestoAIthatbecamepopularduringthisperiodincluded
geneticalgorithms(modelingnaturalselectionforoptimization)andneural
networks(modelingbrainbehaviorfromtheneuronup).
Geneticalgorithmsbecamepopularizedinthe1970sduetothework
ofJohnHollandandhisstudentsattheUniversityofMichigan.Holland’s
bookonthetopiccontinuestobeausefulresource.Neuralnetworks,while
stagnant for a time after the publication of “Perceptrons,” were revived
with Paul John Werbos’ creation of the backpropagation algorithm. This
algorithmremainsthemostwidelyusedsupervisedlearningalgorithmfor
training feedforward neural networks. You can learn more about genetic
algorithmsandevolutionarycomputationinChapter3andneuralnetworks
inChapters8,and9.
10 ArtificialIntelligence
AIRE-EMERGES
Justasspringalwaysfollowsthewinter,AI’swinterwouldeventuallyend
andbringnewlifeintothefield(startinginthemidtolate1980s).The
re-emergenceofAIhadsignificantdifferencesfromtheearlydays.Firstly,
thewildpredictionsofcreatingintelligentmachineswereforthemostpart
over.Instead,researchersandAIpractitionersfocusedonspecificgoals
primarilyintheweakaspectsofAI(asopposedtoStrongAI).WeakAI
focusedonsolvingspecificproblems,comparedtoStrongAI,whosegoal
wastoemulatethefullrangeofhumancognitivecapabilities.Secondly,
the field of AI broadened to include many new types of approaches,
for example, the biologically inspired approaches such as Ant Colony
Optimization(ACO).
TheSilentReturn
AninterestingaspectofAI’sreturnwasthatitoccurredsilently.Insteadof
thetypicalclaimsofStrongAI,weakalgorithmsfounduseinavarietyof
settings.Fuzzylogicandfuzzycontrolsystemswereusedinanumberof
settings, including camera auto-focus, antilock braking systems as well as
playingapartinmedicaldiagnosis.Collaborativefilteringalgorithmsfound
theirwayintoproductrecommendationatapopularonlinebookseller,and
popularInternetsearchenginesuseAIalgorithmstoclustersearchresults
tohelpmakefindingwhatyouneedeasier.
ThesilentreturnfollowswhatRodneyBrookscallsthe“AIeffect.”AI
algorithmsandmethodstransitionfrombeing“AI”tostandardalgorithms
andmethodsoncetheybecomepracticallyuseful.Themethodsdescribed
above are one example, another is speech recognition. The algorithms
behindrecognizingthesoundsofspeechandtranslatingthemintosymbols
wereoncedescribedwithintheconfinesofAI.Nowthesealgorithmsare
commonplace,andtheAImonikerhaslongsincepassed.Therefore,theAI
effecthasawayofdiminishingAIresearch,astheheritageofAIresearch
becomeslostinthepracticalapplicationofthemethods.
MessyandScruffyApproachesTakeHold
WithAI’sresurgencecamedifferentviewsandapproachestoAIandproblem
solving with AI algorithms. In particular, the scruffy approaches became
morewidespreadandthealgorithmsbecamemoreapplicabletoreal-world
problems.Neuralnetworkscontinuedtoberesearchedandapplied,andnew
algorithmsandarchitecturesresulted.Neuralnetworksandgeneticalgorithms
TheHistoryofAI 11
combinedtoprovidenewwaystocreateneuralnetworkarchitecturesthatnot
onlysolvedproblems,butdidsointhemostefficientways.Thisisbecausethe
survivalofthefittestfeaturesofthegeneticalgorithmdroveneuralnetwork
architecturestominimizeforthesmallestnetworktosolvethegivenproblem
athand.Theuseofgeneticalgorithmsalsogrewinanumberofotherareas
including optimization (symbolic and numerical), scheduling, modeling
andmanyothers.Geneticalgorithmsandneuralnetworks(supervisedand
unsupervised)arecoveredinChapters7,8,and9.
Otherbottom-upandbiologicallyinspiredapproachesfollowedinthe
1990sandbeyond.Inearly1992,forexample,MarcoDorigointroduced
theideaofusingstigmergy(indirectcommunicationinanenvironment,in
thiscase,pheromones).Dorigo’suseofstigmergywasappliedtoavariety
ofproblems.AntColonyOptimization(orACO)isdemonstratedwiththe
travelingsalesmanprobleminChapter12.
Also emerging out of the messy approaches to AI was a new field
called Artificial Life. Artificial Life research studies the processes of life
and systems related to life through a variety of simulations and models.
Inadditiontomodelingsingularlife,ALifealsosimulatespopulationsof
lifeformstohelpunderstandnotonlyevolution,butalsotheevolutionof
characteristicssuchaslanguage.Swarmintelligenceisanotheraspectof
thisthatgrewfromALiferesearch.ALifeisinterestinginthecontextofAI
becauseitcanuseanumberofAImethodssuchasneuralnetworks(asthe
neuro-controlleroftheindividualsinthepopulation)aswellasthegenetic
algorithmtoprovidethebasisforevolution.Thisbookprovidesanumber
ofdemonstrationsofALifebothinthecontextofgeneticalgorithmsand
neuralnetworks.
NOTE Oneoftheearliestsimulationenvironmentsthatdemonstratedartificial
lifewasthe“gameoflife”createdbyJohnConway.Thiswasanexample
ofacellularautomaton,andisexploredlater.
Anotherbottom-upapproachthatevolvedduringAI’sre-emergenceused
thehumanimmunesystemasinspiration.ArtificialImmuneSystems(orAIS)
useprinciplesoftheimmunesystemandthecharacteristicsthatitexhibits
forproblemsolvinginthedomainsofoptimization,patternrecognition,and
datamining.AverynovelapplicationofAISisincomputationalsecurity.
Thehumanbodyreactstothepresenceofinfectionsthroughthereleaseof
antibodieswhichdestroythoseinfectioussubstances.Networksofcomputers
can perform the same function, for example, in the domain of network
security.Ifasoftwarevirusisfoundonacomputerwithinagivennetwork,
12 ArtificialIntelligence
other“antibody”programscanbedispatchedtocontainanddestroythose
viruses.Biologycontinuestobeamajorsourceofinspirationforsolutions
tomanytypesofproblems.
AgentSystems
Agents,whicharealsoreferredtoasintelligentagentsorsoftwareagents,are
averyimportantelementofmodern-dayAI.Inmanyways,agentsarenotan
independentaspectofbutinsteadavehicleforAIapplications.Agentsare
applicationsthatexhibitcharacteristicsofintelligentbehavior(suchaslearning
orclassification),butarenotinthemselvesAItechniques.Therealsoexists
otheragent-basedmethodssuchasagent-orientedcomputingandmulti-agent
systems.Theseapplytheagentmetaphorforsolvingavarietyofproblems.
One of the most popular forms of intelligent agents is “agency”
applications.Thewordagencyisusedbecausetheagentrepresentsauser
forsometaskthatitperformsfortheuser.Anexampleincludesascheduling
application. Agents representing users intelligently negotiate with one
anothertoscheduleactivitiesgivenasetofconstraintsforeachuser.
The concept of agents has even been applied to the operation of a
deepspacespacecraft.In1999NASAintegratedwhatwascalledthe“Remote
Agent”intotheDeepSpace1spacecraft.DeepSpace1’sgoalwastotesta
numberofhigh-risktechnologies,oneofwhichwasanagentthatwasusedto
provideautonomytothespacecraftforlimiteddurationsoftime.TheRemote
Agentemployedplanningtechniquestoautonomouslyscheduleexperiments
basedongoalsdefinedbygroundoperators.Underconstrainedconditions,the
RemoteAgentsucceededinprovingthatanintelligentagentcouldbeusedto
autonomouslymanageacomplicatedprobeandsatisfypredefinedobjectives.
Todayyou’llfindagentsinanumberofareas,includingdistributedsystems.
Mobileagentsareindependentagentsthatincludeautonomyandtheability
totravelamongstnodesofanetworkinordertoperformtheirprocessing.
Insteadoftheagentcommunicatingwithanotheragentremotely,themobile
agentcantraveltotheotheragent’slocationandcommunicatewithitdirectly.
Indisconnectednetworksituations,thiscanbeverybeneficial.Youcanlearn
moreaboutintelligentagents(includingmobileagents)inChapter11.
AIINTER-DISCIPLINARYR&D
Inmanycases,AIresearchtendstobefringeresearch,particularlywhen
it’sfocusedonStrongAI.Butwhat’snotableaboutresearchinAIisthat
thealgorithmstendtofindusesinmanyotherdisciplinesbeyondthatof
TheHistoryofAI 13
AI.AIresearchisbynomeanspureresearch,butitsapplicationsgrowwell
beyondtheoriginalintentoftheresearch.Neuralnetworks,datamining,
fuzzylogic,andArtificialLife(forexample)havefoundusesinmanyother
fields.ArtificialLifeisaninterestingexamplebecausethealgorithmsand
techniquesthathaveresultedfromresearchanddevelopmenthavefound
their way into the entertainment industry (from the use of swarming in
animatedmotionpicturestotheuseofAIinvideogames).
Rodney Brook’s has called this the AI effect, suggesting that another
definition for AI is “almost implemented.” This is because once an AI
algorithmfindsamorecommonuse,it’snolongerviewedasanAIalgorithm
butinsteadjustanalgorithmthat’susefulinagivenproblemdomain.
SYSTEMSAPPROACH
In this book, the majority of the algorithms and techniques are studied
fromtheperspectiveofthesystemsapproach.Thissimplymeansthatthe
algorithmisexploredinthecontextofinputsandoutputs.Noalgorithmis
usefulinisolation,butinsteadfromtheperspectiveofhowitinteractswith
its environment (data sampling, filtering, and reduction) and also how it
manipulates or alters its environment. Therefore, the algorithm depends
onanunderstandingoftheenvironmentandalsoawaytomanipulatethe
environment.Thissystemsapproachillustratesthepracticalsideofartificial
intelligence algorithms and techniques and identifies how to ground the
methodintherealworld(seeFigure1.1).
Asanexample,oneofthemostinterestingusesofAItodaycanbefoundin
gamesystems.Strategygames,forexample,commonlyoccupyamapwithtwo
ormoreopponents.Eachopponentcompetesforresourcesintheenvironment
inordertogaintheupperhandovertheother.Whilecollectingresources,
eachopponentcanschedulethedevelopmentofassetstobeusedtodefeatthe
other.Whenmultipleassetsexistforanopponent(suchasamilitaryunit),they
canbeappliedinunison,orseparatelytolaysiegeonanotheropponent.
Wherestrategygamesdependonahigher-levelviewoftheenvironment
(such as would be viewed from a general), first-person shooter games
(FPS)takealower-levelview(fromthatofasoldier).AnagentinanFPS
dependsmostoftenonitsviewofthebattlefield.TheFPSagent’sviewofthe
environmentisatamuchlowerlevel,understandingcover,objectives,and
localenemypositions.TheenvironmentismanipulatedbytheFPSagent
throughitsownmovement,attackingordefendingfromenemies(through
findingcover),andpossiblycommunicatingwithotheragents.
14 ArtificialIntelligence
FIGURE1.1 ThesystemsapproachtoArtificialIntelligence.
Anobviousexampleofthesystemsapproachisinthefieldofrobotics.
Mobilerobots,forexample,utilizeanarrayofsensorsandeffectsthatmake
upthephysicalrobot.Atthecoreoftherobotisoneormorealgorithms
thatyieldrationalbehavior.
TheHistoryofAI 15
Ineachcase,theAIalgorithmthat’schosenisthecoreofanagent’s
sensors (inputs) and effectors (outputs). For this reason, the algorithm
can’ttrulybeusefulorunderstoodunlessit’sconsideredfromitsplacein
theenvironment.
OVERVIEWOFTHISBOOK
This book covers a wide range of AI techniques, each segmented
appropriatelyintotheirparticulargenre.Thefollowingchaptersummaries
presenttheideasandmethodsthatareexplored.
UninformedSearch
IntheearlydaysofAI,AIwasasearch,whethersearchinvolvedlookingfora
plan,orthroughthevariousmovesthatarepossible(andsubsequentmoves)
in a game of Checkers. In this chapter on uninformed (or blind) search,
the concept of search in various spaces is introduced, the representation
ofspacesforsearch,andthenthevariouspopularalgorithmsusedinblind
searchareexplored.Thisincludesdepth-first,breadth-first,uniform-cost-
search,andothers.
InformedSearch
Informedsearchisanevolutionofsearchthatappliesheuristicstothesearch
algorithm,giventheproblemspace,tomakethealgorithmmoreefficient.
Thischaptercoversbest-first,astar,hillclimbing,simulatedannealing,tabu
search,andconstraintsatisfaction.
AIandGames
Oneoftheearliestusesofblindandinformedsearchwasintheapplicationto
games.GamessuchasCheckersandChesswerebelievedtobeanintelligent
activity,andifacomputercouldbeendowedwiththeabilitytoplayagame
and win against a human opponent, it could be considered intelligent.
Samuel’sCheckersprogramdemonstratedaprogramthatcoulddefeatits
creator, and while a feat, this experiment did not produce an intelligent
computer except within the domain of Checkers. This chapter explores
two-playergamesandthecoreofmanygame-playingsystems,theminimax
algorithm.Avarietyofgamesarethendiscussed,fromtheclassicalgames
suchasChess,Checkers,andGotovideogameAI,exploringmovement,
behavior,team,andreal-timestrategyAI.
16 ArtificialIntelligence
KnowledgeRepresentation
Knowledge representation has a long history in AI, particularly in
Strong AI research. The goal behind knowledge representation is to find
abstractionsforknowledgethatresultinabaseofknowledgethat’suseful
to a given application. For example, knowledge must be represented in
awaythatmakesiteasyforacomputertoreasonwithitandunderstand
the relationships between elements of the knowledge base. This chapter
will provide an introduction to a number of fundamental knowledge
representationtechniquesaswellasintroducetheideasbehindpredicate
andfirst-orderlogictoreasonwithknowledge.
MachineLearning
Machine learning is best described as learning from example. Machine
learning incorporates a variety of methods such as supervised and
unsupervised learning. In supervised learning, a teacher is available to
definecorrectorincorrectresponses.Unsupervisedlearningdiffersinthat
noteacherispresent.(Instead,unsupervisedlearninglearnsfromthedata
itselfbyidentifyingits)relationships.Thischapterprovidesanintroduction
to machine learning, and then explores a number of machine learning
algorithmssuchasdecisiontreesandnearestneighborlearning.
EvolutionaryComputation
EvolutionarycomputationintroducedtheideaofscruffyapproachestoAI.
Insteadoffocusingonthehighlevel,tryingtoimitatethebehaviorofthe
human brain, scruffy approaches start at a lower level trying to recreate
the more fundamental concepts of life and intelligence using biological
metaphors. This chapter covers a number of the evolutionary methods
includinggeneticalgorithms,geneticprogramming,evolutionarystrategies,
differentialevolution,andparticleswarmoptimization.
NeuralNetworksI
While neural networks are one of the earliest (and more controversial)
techniques, they remain one of the most useful. The attack on neural
networks severely impacted AI funding and research, but neural
networksre-emergedfromAI’swinterasastandardforclassificationand
learning.Thischapterintroducesthebasicsofneuralnetworks,andthen
explores the supervised neural network algorithms (least-mean-squares,
backpropagation,probabilisticneuralnetworks,andothers).Thechapter
TheHistoryofAI 17
endswithadiscussionofneuralnetworkcharacteristicsandwaystotune
themgiventheproblemdomain.
NeuralNetworksII
Where the previous chapter explored supervised neural network
algorithms, this chapter provides an introduction to the unsupervised
variants. Unsupervised algorithms use the data itself to learn without
the need for a “teacher.” This chapter explores unsupervised learning
algorithms, including Hebbian learning, Simple Competitive Learning,
k-MeansClustering,AdaptiveResonanceTheory,andtheHopfieldauto-
associativemodel.
IntelligentAgents
Intelligent (or Software) Agents are one of newest techniques in the AI
arsenal. In one major definition, agents are applications that include the
conceptof“agency.”Thismeansthatthoseapplicationsrepresentauserand
satisfythegoalsofthetaskautonomouslywithoutfurtherdirectionfromthe
user.Thischapteronintelligentagentswillintroducethemajorconcepts
behindintelligentagents,theirarchitecturesandapplications.
BiologicallyInspiredandHybridModels
AIisfilledwithexamplesoftheuseofbiologicalmetaphors,fromearly
workinneuralnetworkstomodern-dayworkinartificialimmunesystems.
Nature has proven to be a very worthy teacher for complex problem
solving. This chapter presents a number of techniques that are both
biologicallyinspiredaswellashybrid(ormixed)modelsofAI.Methods
such as artificial immune systems, simulated evolution, Lindenmayer
systems,fuzzylogic,geneticallyevolvedneuralnetworks,andantcolony
optimizationareexplored,tonameafew.
LanguagesofAI
While most people think of LISP when considering the languages of AI,
therehavebeenalargenumberoflanguagesdevelopedspecificallyforAI
applicationdevelopment.Inthischapter,ataxonomyofcomputerlanguages
ispresentedfollowedbyshortexamples(andadvantages)ofeach.Thena
numberofAI-specificlanguagesareinvestigated,exploringtheirhistoryand
usethroughexamples.LanguagesexploredincludeLISP,Scheme,POP-11,
andProlog.
18 ArtificialIntelligence
CHAPTERSUMMARY
The history of AI is a modern-day drama. It’s filled with interesting
characters, cooperation, competition, and even deception. But outside of
the drama, there has been exceptional research and in recent history an
applicationofAI’sideasinanumberofdifferentsettings.AIhasfinallyleft
theperceptionoffringeresearchandenteredtherealmofacceptedresearch
andpracticaldevelopment.
REFERENCES
[LISP2007]Wikipedia“Lisp(programminglanguage)”,2007.
Available online at http://en.wikipedia.org/wiki/Lisp_%28programming_
language%29
[Newell1956]Newell,A.,Shaw,J.C.,Simon,H.A“EmpericalExplorationsof
theLogicTheoryMachine:ACaseStudyinHeuristics,”inProceedings
oftheWesternJointComputerConference,1956.
[Shannon1950]Shannon,Claude,“ProgrammingaComputerforPlaying
Chess,”PhilisophicalMagazine41,1950.
RESOURCES
Rayman, Marc D., et al “Results from the Deep Space 1 Technology
Validation Mission,” 50th International Astronomical Congress,
Amsterdam,TheNetherlands,1999.
decastr,LeandroN.,Timmis,JonathanArtificialImmuneSystems:ANew
ComputationalIntelligenceApproachSpringer,2002.
Holland,JohnAdaptationinNaturalandArtificialSystems.Universityof
MichiganPress,AnnArbor,1975.
McCarthy, John “Recursive Functions of Symbolic Expressions and their
ComputationbyMachine(PartI),”CommunicationsoftheACM,April
1960.
Shortliffe,E.H.“Rule-basedExperSystems:TheMycinExperimentsofthe
StanfordHeuristicProgrammingProject,”Addison-Wesley,1984.
Winograd,Terry“ProceduresasaRepresentationforDatainaComputer
Program for Understanding Natural Language,” MIT AI Technical
Report235,February1971.
Woods,WilliamA.“TransitionNetworkGrammarsforNaturalLanguage
Analysis,”CommunicationsoftheACM13:10,1970.
TheHistoryofAI 19
EXERCISES
1. Inyourownwords,defineintelligenceandwhyintelligencetestscan
hidetherealmeasureofintelligence.
2. WhatwastheTuringtest,andwhatwasitintendedtoaccomplish?
3. Whyweregamestheearlytest-bedforAImethods?Howdoyouthink
AIandgamesareviewedtoday?
4. HowdidArthurSamuelsetthebarforlearningprogramsinthe1950s?
5. WhatwasthefirstlanguagedevelopedspecificallyforAI?Whatlanguage
followedinthe1970s,developedalsoforAI?
6. DefineStrongAI.
7. WhateventismostcommonlyattributedtoleadingtoAI’swinter?
8. WhatismeantbyScruffyandNeatapproachestoAI?
9. AfterAI’swinter,whatwasmostuniqueaboutAI’sre-emergence?
10. ThisbookexploresAIfromthesystemsapproach.Definethesystems
approachandhowthisperspectiveisusedtoexploreAI.
Chapter 2 UNINFORMED
SEARCH
U
ninformed search, also called blind search and naïve search, is a
classofgeneralpurposesearchalgorithmsthatoperateinabrute-
forceway.Thesealgorithmscanbeappliedtoavarietyofsearch
problems, but since they don’t take into account the target problem, are
inefficient.Incontrast,informedsearchmethods(discussedinChapter3)
useaheuristictoguidethesearchfortheproblemathandandaretherefore
muchmoreefficient.Inthischapter,generalstatespacesearchisexplored
andthenavarietyofuninformedsearchalgorithmswillbediscussedand
comparedusingasetofcommonmetrics.
SEARCHANDAI
SearchisanimportantaspectofAIbecauseinmanyways,problemsolving
inAIisfundamentallyasearch.Searchcanbedefinedasaproblem-solving
techniquethatenumeratesaproblemspacefromaninitialpositioninsearch
ofagoalposition(orsolution).Themannerinwhichtheproblemspaceis
searchedisdefinedbythesearchalgorithmorstrategy.Assearchstrategies
offerdifferentwaystoenumeratethesearchspace,howwellastrategyworks
isbasedontheproblemathand.Ideally,thesearchalgorithmselectedisone
whosecharacteristicsmatchthatoftheproblemathand.
22 ArtificialIntelligence
CLASSESOFSEARCH
Fourclassesofsearchwillbeexploredhere.Inthischapter,we’llreview
uninformedsearch,andinChapter3,informedsearchwillbediscussed.
Chapter3willalsoreviewconstraintsatisfaction,whichtriestofindasetof
valuesforasetofvariables.Finally,inChapter4,we’lldiscussadversarial
search,whichisusedingamestofindeffectivestrategiestoplayandwin
two-playergames.
GENERALSTATESPACESEARCH
Let’sbeginourdiscussionofsearchbyfirstunderstandingwhatismeant
byasearchspace.Whensolvingaproblem,it’sconvenienttothinkabout
thesolutionspaceintermsofanumberofactionsthatwecantake,andthe
newstateoftheenvironmentasweperformthoseactions.Aswetakeone
ofmultiplepossibleactions(eachhavetheirowncost),ourenvironment
changes and opens up alternatives for new actions. As is the case with
manykindsofproblemsolving,somepathsleadtodead-endswhereothers
leadtosolutions.Andtheremayalsobemultiplesolutions,somebetter
thanothers.Theproblemofsearchistofindasequenceofoperatorsthat
transitionfromthestarttogoalstate.Thatsequenceofoperatorsisthe
solution.
How we avoid dead-ends and then select the best solution available
isaproductofourparticularsearchstrategy.Let’snowlookatstatespace
representationsforthreeproblemdomains.
SearchinaPhysicalSpace
Let’sconsiderasimplesearchprobleminphysicalspace(Figure2.1).Our
initialpositionis‘A’fromwhichtherearethreepossibleactionsthatleadto
position‘B,’‘C,’or‘D.’Places,orstates,aremarkedbyletters.Ateachplace,
there’sanopportunityforadecision,oraction.Theaction(alsocalledan
operator)issimplyalegalmovebetweenoneplaceandanother.Impliedin
thisexerciseisagoalstate,oraphysicallocationthatwe’reseeking.
This search space (shown in Figure 2.1) can be reduced to a tree
structureasillustratedinFigure2.2.Thesearchspacehasbeenminimized
heretothenecessaryplacesonthephysicalmap(states)andthetransitions
thatarepossiblebetweenthestates(applicationofoperators).Eachnodein
thetreeisaphysicallocationandthearcsbetweennodesarethelegalmoves.
Thedepthofthetreeisthedistancefromtheinitialposition.
UninformedSearch 23
FIGURE2.1: Asearchproblemrepresentedasaphysicalspace.
FIGURE2.2: RepresentingthephysicalspaceprobleminFigure2.1asatree.
SearchinaPuzzleSpace
The“TowersofHanoi”puzzleisaninterestingexampleofastatespacefor
solvingapuzzleproblem.Theobjectofthispuzzleistomoveanumberof
disksfromonepegtoanother(oneatatime),withanumberofconstraints
thatmustbemet.Eachdiskisofauniquesizeandit’snotlegalforalarger
disktositontopofasmallerdisk.Theinitialstateofthepuzzleissuchthat
alldisksbeginononepeginincreasingsizeorder(seeFigure2.2).Ourgoal
(thesolution)istomovealldiskstothelastpeg.
Asinmanystatespaces,therearepotentialtransitionsthatarenotlegal.
Forexample,wecanonlymoveapegthathasnoobjectaboveit.Further,
wecan’tmovealargediskontoasmallerdisk(thoughwecanmoveanydisk
24 ArtificialIntelligence
toanemptypeg).Thespaceofpossibleoperatorsisthereforeconstrained
onlytolegalmoves.Thestatespacecanalsobeconstrainedtomovesthat
havenotyetbeenperformedforagivensubtree.Forexample,ifwemovea
smalldiskfromPegAtoPegC,movingthesamediskbacktoPegAcould
bedefinedasaninvalidtransition.Notdoingsowouldresultinloopsand
aninfinitelydeeptree.
ConsiderourinitialpositionfromFigure2.3.Theonlydiskthatmay
moveisthesmalldiskatthetopofPegA.Forthisdisk,onlytwolegalmoves
arepossible,fromPegAtoPegBorC.Fromthisstate,therearethree
potentialmoves:
1. MovethesmalldiskfromPegCtoPegB.
2. MovethesmalldiskfromPegCtoPegA.
3. MovethemediumdiskfromPegAtoPegB.
Thefirstmove(smalldiskfromPegCtoPegB),whilevalidisnotapotential
move,aswejustmovedthisdisktoPegC(anemptypeg).Movingitasecond
timeservesnopurpose(asthismovecouldhavebeendoneduringtheprior
transition),sothere’snovalueindoingthisnow(aheuristic).Thesecond
moveisalsonotuseful(anotherheuristic),becauseit’sthereverseofthe
FIGURE2.3: Asearchspaceforthe“TowerofHanoi”puzzle.
UninformedSearch 25
previousmove.Thisleavesonevalidmove,themediumdiskfromPegAto
PegB.Thepossiblemovesfromthisstatebecomemorecomplicated,because
validmovesarepossiblethatmoveusfartherawayfromthesolution.
TIP
Aheuristicisasimpleorefficientruleforsolvingagivenproblemor
makingadecision.
Whenoursequenceofmovesbringsusfromtheinitialpositiontothegoal,
we have a solution. The goal state in itself is not interesting, but instead
what’sinterestingisthesequenceofmovesthatbroughtustothegoalstate.
Thecollectionofmoves(orsolution),doneintheproperorder,isinessence
aplanforreachingthegoal.Theplanforthisconfigurationofthepuzzle
canbeidentifiedbystartingfromthegoalpositionandbacktrackingtothe
initialposition.
SearchinanAdversarialGameSpace
Aninterestinguseofsearchspacesisingames.Alsoknownasgametrees,
these structures enumerate the possible moves by each player allowing
thesearchalgorithmtofindaneffectivestrategyforplayingandwinning
thegame.
NOTE ThetopicofadversarialsearchingametreesisexploredinChapter4.
ConsideragametreeforthegameofChess.Eachpossiblemoveisprovided
foreachpossibleconfiguration(placementofpieces)oftheChessboard.
Butsincethereare10120possibleconfigurationsofaChessboard,agame
treetodocumentthesearchspacewouldnotbefeasible.Heuristicsearch,
whichmustbeappliedhere,willbediscussedinChapter3.
Let’s now look at a much simpler game that can be more easily
representedinagametree.ThegameofNimisatwo-playergamewhere
eachplayertakesturnsremovingobjectsfromoneormorepiles.Theplayer
requiredtotakethelastobjectlosesthegame.
Nim has been studied mathematically and solved in many different
variations.Forthisreason,theplayerwhowillwincanbecalculatedbased
upon the number of objects, piles, and who plays first in an optimally
playedgame.
NOTE ThegameofNimissaidtohaveoriginatedinChina,butcanbetraced
to Germany as the word nimm can be translated as take. A complete
mathematical theory of Nim was created by Charles Bouton in 1901.
[Bouton1901]
26 ArtificialIntelligence
Let’swalkthroughanexampletoseehowNimisplayed.We’llbeginwith
asinglesmallpiletolimitthenumberofmovesthatarerequired.Figure
2.4illustratesashortgamewithapileofsixobjects.Eachplayermaytake
one, two, or three objects from the pile. In this example, Player-1 starts
thegame,butendsthegamewithaloss(isrequiredtotakethelastobject
whichresultsinalossinthemisèreformofthegame).HadPlayer-1taken
3initssecondmove,Player-2wouldhavebeenleftwithoneresultingina
winforPlayer-1.
Agametreemakesthisinformationvisible,asillustratedinFigure2.5.
NoteinthetreethatPlayer-1mustremoveonefromthepiletocontinue
thegame.IfPlayer-1removestwoorthreefromthepile,Player-2canwin
ifplayingoptimally.Theshadednodesinthetreeillustratelosingpositions
fortheplayerthatmustchoosenext(andinallcases,theonlychoiceleftis
totaketheonlyremainingobject).
Note that the depth of the tree determines the length of the game
(numberofmoves).It’simpliedinthetreethattheshadednodeisthefinal
movetobemade,andtheplayerthatmakesthismovelosesthegame.Also
notethesizeofthetree.Inthisexample,usingsixobjects,atotalof28nodes
isrequired.Ifweincreaseourtreetoillustrateapileofsevenobjects,the
treeincreasesto42nodes.Witheightobjects,threeballoonsto100nodes.
Fortunately, the tree can be optimized by removing duplicate subtrees,
resultinginamuchsmallertree.
FIGURE2.4: AsamplegameofNimwithapileofsixobjects.
UninformedSearch 27
TREES,GRAPHS,ANDREPRESENTATION
Ashorttouroftreesandgraphsandtheirterminologyisinorderbefore
exploringthevariousuninformedsearchmethods.
Agraphisafinitesetofvertices(ornodes)thatareconnectedbyedges
(orarcs).Aloop(orcycle)mayexistinagraph,whereanarc(oredge)may
lead back to the original node. Graphs may be undirected where arcs do
notimplyadirection,ortheymaybedirected(calledadigraph)wherea
directionisimplicitinthearc.Anarccanalsocarryaweight,whereacost
canbeassociatedwithapath.
Eachofthesegraphsalsodemonstratesthepropertyofconnectivity.Both
graphsareconnectedbecauseeverypairofnodesisconnectedbyapath.If
everynodeisconnectedtoeverynodebyanarc,thegraphiscomplete.One
specialconnectedgraphiscalledatree,butitmustcontainnocycles.
Building a representation of a graph is simple and one of the most
commonrepresentationsistheadjacencymatrix.Thisstructureissimply
FIGURE2.5: AcompleteNimgametreeforsixobjectsinonepile.
FIGURE2.8: Aconnectedgraphwithnocycles(otherwiseknownasatree).
an N by N matrix (where N is the number of nodes in the graph). Each
elementofthematrixdefinesaconnectivity(oradjacency)betweenthenode
referencedastherowandthenodereferencedasthecolumn.
RecalltheundirectedgraphinFigure2.6.Thisgraphcontainssixnodesand
eightarcs.TheadjacencymatrixforthisundirectedgraphisshowninFigure
2.9.Thetwodimensionsofthegraphidentifythesource(row)anddestination
nodes(column)ofthegraph.FromFigure2.6,weknowthatnodeAisadjacent
tonodesB,C,andD.Thisisnotedintheadjacencymatrixwithavalueofone
ineachoftheB,C,andDcolumnsforrowA.Sincethisisanundirectedgraph,
wenotesymmetryintheadjacencymatrix.NodeAconnectstonodeB(as
identifiedinrowA),butalsonodeBconnectstonodeA(asshowninrowB).
Foradirectedgraph(asshowninFigure2.7),theassociatedadjacency
matrixisillustratedinFigure2.10.Sincethegraphisdirected,nosymmetry
can be found. Instead, the direction of the arcs is noted in the matrix.
For example, node B connects to node A, but node A has no associated
connectiontonodeB.
Aninterestingpropertyoftheadjacencymatrixcanbefoundbyreviewing
therowsandcolumnsinisolation.Forexample,ifwereviewasinglerow,we
canidentifythenodestowhichitconnects.Forexample,rowCshowsonlya
connectiontonodeF(asindicatedbytheoneinthatcell).Butifwereview
thecolumnfornodeC,wefindthenodesthathavearcsconnectingtonode
C.Inthiscase,weseenodesA,D,andE(asillustratedgraphicallyinFigure
2.7).Wecanalsofindwhetheragraphiscomplete.Iftheentirematrixis
non-zero,thenthegraphiscomplete.It’salsosimpletofindadisconnected
graph(anodewhoserowandcolumncontainzerovalues).Loopsinagraph
canalsobealgorithmicallydiscoveredbyenumeratingthematrix(recursively
UninformedSearch 29
followingallpathslookingfortheinitialnode).
Inthesimplecase,thevaluesoftheadjacencymatrixsimplydefinethe
connectivityofnodesinthegraph.Inweightedgraphs,wherearcsmaynot
allbeequal,thevalueinacellcanidentifytheweight(cost,ordistance).
We’llexploreexamplesofthistechniqueinthereviewofneuralnetwork
construction(Chapter11).
Adjacencylistsarealsoapopularstructurewhereeachnodecontains
a list of the nodes to which it connects. If the graph is sparse, this
representationcanrequirelessspace.
UNINFORMEDSEARCH
The uninformed search methods offer a variety of techniques for graph
search,eachwithitsownadvantagesanddisadvantages.Thesemethodsare
exploredherewithdiscussionoftheircharacteristicsandcomplexities.
Big-Onotationwillbeusedtocomparethealgorithms.Thisnotation
definestheasymptoticupperboundofthealgorithmgiventhedepth(d)of
thetreeandthebranchingfactor,ortheaveragenumberofbranches(b)
fromeachnode.Thereareanumberofcommoncomplexitiesthatexistfor
searchalgorithms.TheseareshowninTable2.1.
Table2.1: Commonordersofsearchfunctions.
O-Notation Order
O(1) Constant(regardlessofthenumberofnodes)
30 ArtificialIntelligence
O(n) Linear(consistentwiththenumberofnodes)
O(logn) Logarithmic
O(n2) Quadratic
O(cn) Geometric
O(n!) Combinatorial
Big-Onotationprovidesaworst-casemeasureofthecomplexityofasearch
algorithmandisacommoncomparisontoolforalgorithms.We’llcompare
the search algorithms using space complexity (measure of the memory
requiredduringthesearch)andtimecomplexity(worst-casetimerequired
tofindasolution).We’llalsoreviewthealgorithmforcompleteness(canthe
algorithmfindapathtoagoalnodeifit’spresentinthegraph)andoptimality
(findsthelowestcostsolutionavailable).
HelperAPIs
AnumberofhelperAPIswillbeusedinthesourcecodeusedtodemonstrate
thesearchfunctions.TheseareshownbelowinListing2.1.
LISTING2.1: HelperAPIsforthesearchfunctions.
/*GraphAPI*/
graph_t *createGraph(intnodes);
void destroyGraph(graph_t*g_p);
void addEdge(graph_t*g_p,intfrom,intto,intvalue);
int getEdge(graph_t*g_p,intfrom,intto);
/*StackAPI*/
stack_t *createStack(intdepth);
void destroyStack(stack_t*s_p);
void pushStack(stack_t*s_p,intvalue);
int popStack(stack_t*s_p);
int isEmptyStack(stack_t*s_p);
/*QueueAPI*/
queue_t*createQueue(intdepth);
void destroyQueue(queue_t*q_p);
void enQueue(queue_t*q_p,intvalue);
int deQueue(queue_t*q_p);
int isEmptyQueue(queue_t*q_p);
/*PriorityQueueAPI*/
pqueue_t*createPQueue(intdepth);
UninformedSearch 31
void destroyPQueue(pqueue_t*q_p);
void enPQueue(pqueue_t*q_p,intvalue,intcost);
void dePQueue(pqueue_t*q_p,int*value,int*cost);
int isEmptyPQueue(pqueue_t*q_p);
int isFullPQueue(pqueue_t*q_p);
HE C
NT
D
O
The helper functions can be found on the CD-ROM at ./software/
common.
GeneralSearchParadigms
Beforewediscusssomeoftheuninformedsearchmethods,let’slookattwo
simplegeneraluninformedsearchmethods.
Thefirstiscalled‘GenerateandTest.’Inthismethod,wegeneratea
potential solution and then check it against the solution. If we’ve found
thesolution,we’redone,otherwise,werepeatbytryinganotherpotential
solution.Thisiscalled‘GenerateandTest’becausewegenerateapotential
solution,andthentestit.Withoutapropersolution,wetryagain.Notehere
thatwedon’tkeeptrackofwhatwe’vetriedbefore;wejustplowaheadwith
potentialsolutions,whichisatrueblindsearch.
Anotheroptioniscalled‘RandomSearch’whichrandomlyselectsanew
statefromthecurrentstate(byselectingagivenvalidoperatorandapplying
it). If we reach the goal state, then we’re done. Otherwise, we randomly
selectanotheroperator(leadingtoanewstate)andcontinue.
Random search and the ‘Generate and Test’ method are truly blind
methodsofsearch.Theycangetlost,getcaughtinloops,andpotentially
neverfindasolutioneventhoughoneexistswithinthesearchspace.
Let’s now look at some search methods that while blind, can find a
solution(ifoneexists)evenifittakesalongperiodoftime.
Depth-FirstSearch(DFS)
The Depth-First Search (DFS) algorithm is a technique for searching a
graphthatbeginsattherootnode,andexhaustivelysearcheseachbranch
toitsgreatestdepthbeforebacktrackingtopreviouslyunexploredbranches
(Figure 2.11 illustrates this search order). Nodes found but yet to be
reviewedarestoredinaLIFOqueue(alsoknownasastack).
NOTE AstackisaLIFO(Last-In-First-Out)containerofobjects.Similarto
astackofpaper,thelastitemplacedonthetopisthefirstitemtobe
removed.
32 ArtificialIntelligence
FIGURE2.11: SearchorderoftheDFSalgorithmoverasmalltree.
The space complexity for DFS is O(bd) where the time complexity is
geometric(O(bd)).Thiscanbeveryproblematicondeepbranchinggraphs,
asthealgorithmwillcontinuetothemaximumdepthofthegraph.Ifloops
are present in the graph, then DFS will follow these cycles indefinitely.
Forthisreason,theDFSalgorithmisnotcomplete,ascyclescanprohibit
thealgorithmfromfindingthegoal.Ifcyclesarenotpresentinthegraph,
thenthealgorithmiscomplete(willalwaysfindthegoalnode).TheDFS
algorithmisalsonotoptimal,butcanbemadeoptimalusingpathchecking
(toensuretheshortestpathtothegoalisfound).
HE C
NT
D
O
TheDFSimplementationcanbefoundontheCD-ROMat./software/
ch2/dfs.c.
Graphalgorithmscanbeimplementedeitherrecursivelyorusingastackto
maintainthelistofnodesthatmustbeenumerated.InListing2.2,theDFS
algorithmisimplementedusingaLIFOstack.
Listing2.2: Thedepth-firstsearchalgorithm.
#include<stdio.h>
#include“graph.h”
#include“stack.h”
#defineA 0
#defineB 1
UninformedSearch 33
#defineC 2
#defineD 3
#defineE 4
#defineF 5
#defineG 6
#defineH 7
intinit_graph(graph_t*g_p)
{
addEdge(g_p,A,B,1);
addEdge(g_p,A,C,1);
addEdge(g_p,B,D,1);
addEdge(g_p,C,E,1);
addEdge(g_p,C,F,1);
addEdge(g_p,D,G,1);
addEdge(g_p,D,H,1);
return0;
}
voiddfs(graph_t*g_p,introot,intgoal)
{
intnode;
intto;
stack_t*s_p;
s_p=createStack(10);
pushStack(s_p,root);
while(!isEmptyStack(s_p)){
node=popStack(s_p);
printf(“%d\n”,node);
if(node==goal)break;
for(to=g_p->nodes-1;to>0;to--){
if(getEdge(g_p,node,to)){
pushStack(s_p,to);
}
}
}
destroyStack(s_p);
return;
}
intmain()
{
graph_t*g_p;
34 ArtificialIntelligence
g_p=createGraph(8);
init_graph(g_p);
dfs(g_p,0,5);
destroyGraph(g_p);
return0;
}
TIP
A search algorithm is characterized as exhaustive when it can search
everynodeinthegraphinsearchofthegoal.Ifthegoalisnotpresentin
thegraph,thealgorithmwillterminate,butwillsearcheachandevery
nodeinasystematicway.
Depth-LimitedSearch(DLS)
Depth-Limited Search (DLS) is a modification of depth-first search that
minimizesthedepththatthesearchalgorithmmaygo.Inadditiontostarting
with a root and goal node, a depth is provided that the algorithm will not
descendbelow(seeListing2.3).Anynodesbelowthatdepthareomittedfrom
thesearch.Thismodificationkeepsthealgorithmfromindefinitelycycling
byhaltingthesearchafterthepre-imposeddepth.Figure2.12illustratesthis
searchwithadepthoftwo(nonodesdeeperthanleveltwoaresearched).
HE C
NT
D
O
TheDLSimplementationcanbefoundontheCD-ROMat./software/
ch2/dls.c.
Listing2.3: Thedepth-limitedsearchalgorithm.
#include<stdio.h>
#include“graph.h”
#include“stack.h”
#defineA 0
#defineB 1
#defineC 2
#defineD 3
#defineE 4
#defineF 5
#defineG 6
#defineH 7
intinit_graph(graph_t*g_p)
{
UninformedSearch 35
addEdge(g_p,A,B,1);
addEdge(g_p,A,C,1);
addEdge(g_p,B,D,1);
addEdge(g_p,C,E,1);
addEdge(g_p,C,F,1);
addEdge(g_p,D,G,1);
addEdge(g_p,D,H,1);
return0;
}
voiddls(graph_t*g_p,introot,intgoal,intlimit)
{
intnode,depth,to;
stack_t*s_p,*sd_p;
s_p=createStack(10);
sd_p=createStack(10);
pushStack(s_p,root);
pushStack(sd_p,0);
while(!isEmptyStack(s_p)){
node=popStack(s_p);
depth=popStack(sd_p);
printf(“%d(depth%d)\n”,node,depth);
if(node==goal)break;
if(depth<limit){
for(to=g_p->nodes-1;to>0;to--){
if(getEdge(g_p,node,to)){
pushStack(s_p,to);
pushStack(sd_p,depth+1);
}
}
}
}
destroyStack(s_p);
destroyStack(sd_p);
return;
}
intmain()
{
graph_t*g_p;
g_p=createGraph(8);
init_graph(g_p);
36 ArtificialIntelligence
dls(g_p,0,5,2);
destroyGraph(g_p);
return0;
}
Whilethealgorithmdoesremovethepossibilityofinfinitelyloopinginthe
graph,italsoreducesthescopeofthesearch.Ifthegoalnodehadbeenone
ofthenodesmarked‘X’,itwouldnothavebeenfound,makingthesearch
algorithmincomplete.Thealgorithmcanbecompleteifthesearchdepthis
thatofthetreeitself(inthiscasedisthree).Thetechniqueisalsonotoptimal
sincethefirstpathmaybefoundtothegoalinsteadoftheshortestpath.
Thetimeandspacecomplexityofdepth-limitedsearchissimilartoDFS,
fromwhichthisalgorithmisderived.SpacecomplexityisO(bd)andtime
complexityisO(bd),butdinthiscaseistheimposeddepthofthesearchand
notthemaximumdepthofthegraph.
IterativeDeepeningSearch(IDS)
IterativeDeepeningSearch(IDS)isaderivativeofDLSandcombinesthe
featuresofdepth-firstsearchwiththatofbreadth-firstsearch.IDSoperates
byperformingDLSsearcheswithincreaseddepthsuntilthegoalisfound.
FIGURE2.12: Searchorderforatreeusingdepth-limitedsearch(depth=two).
FIGURE2.13: IteratingincreaseddepthsearcheswithIDS.
UninformedSearch 37
Thedepthbeginsatone,andincreasesuntilthegoalisfound,ornofurther
nodescanbeenumerated(seeFigure2.13).
AsshowninFigure2.13,IDScombinesdepth-firstsearchwithbreadth-
firstsearch.Byminimizingthedepthofthesearch,weforcethealgorithmto
alsosearchthebreadthofthegraph.Ifthegoalisnotfound,thedepththat
thealgorithmispermittedtosearchisincreasedandthealgorithmisstarted
again.Thealgorithm,showninListing2.4,beginswithadepthofone.
LISTING2.4: Theiterativedeepening-searchalgorithm.
#include<stdio.h>
#include“graph.h”
#include“stack.h”
#defineA 0
#defineB 1
#defineC 2
#defineD 3
#defineE 4
#defineF 5
#defineG 6
#defineH 7
intinit_graph(graph_t*g_p)
{
addEdge(g_p,A,B,1);
addEdge(g_p,A,C,1);
addEdge(g_p,B,D,1);
addEdge(g_p,C,E,1);
addEdge(g_p,C,F,1);
addEdge(g_p,D,G,1);
addEdge(g_p,D,H,1);
return0;
}
intdls(graph_t*g_p,introot,intgoal,intlimit)
{
intnode,depth;
intto;
stack_t*s_p,*sd_p;
s_p=createStack(10);
sd_p=createStack(10);
pushStack(s_p,root);
38 ArtificialIntelligence
pushStack(sd_p,0);
while(!isEmptyStack(s_p)){
node=popStack(s_p);
depth=popStack(sd_p);
printf(“%d(depth%d)\n”,node,depth);
if(node==goal)return1;
if(depth<limit){
for(to=g_p->nodes-1;to>0;to--){
if(getEdge(g_p,node,to)){
pushStack(s_p,to);
pushStack(sd_p,depth+1);
}
}
}
}
destroyStack(s_p);
destroyStack(sd_p);
return0;
}
intmain()
{
graph_t*g_p;
intstatus,depth;
g_p=createGraph(8);
init_graph(g_p);
depth=1;
while(1){
status=dls(g_p,0,5,depth);
if(status==1)break;
elsedepth++;
}
destroyGraph(g_p);
return0;
}
HE C
NT
D
O
TheIDSimplementationcanbefoundontheCD-ROMat./software/
ch2/ids.c.
IDSisadvantageousbecauseit’snotsusceptibletocycles(acharacteristic
ofDLS,uponwhichit’sbased).Italsofindsthegoalnearesttotherootnode,
UninformedSearch 39
FIGURE2.14: Searchorderofthebreadth-firstsearchalgorithm.
asdoestheBFSalgorithm(whichwillbedetailednext).Forthisreason,it’s
apreferredalgorithmwhenthedepthofthesolutionisnotknown.
ThetimecomplexityforIDSisidenticaltothatofDFSandDLS,O(bd).
SpacecomplexityofIDSisO(bd).
Unlike DFS and DLS, IDS is will always find the best solution and
therefore,itisbothcompleteandoptimal.
Breadth-FirstSearch(BFS)
InBreadth-FirstSearch(BFS),wesearchthegraphfromtherootnodein
orderofthedistancefromtheroot.Becausetheordersearchisnearestthe
root,BFSisguaranteedtofindthebestpossiblesolution(shallowest)ina
non-weightedgraph,andisthereforealsocomplete.Ratherthandigging
deepdownintothegraph,progressingfurtherandfurtherfromtheroot
(asisthecasewithDFS),BFScheckseachnodenearesttherootbefore
descendingtothenextlevel(seeFigure2.14).
The implementation of BFS uses a FIFO (first-in-first-out) queue,
differing from the stack (LIFO) implementation for DFS. As new nodes
arefoundtobesearched,thesenodesarecheckedagainstthegoal,andif
thegoalisnotfound,thenewnodesareaddedtothequeue.Tocontinue
thesearch,theoldestnodeisdequeued(FIFOorder).UsingFIFOorder
for new node search, we always check the oldest nodes first, resulting in
breadth-firstreview(seeListing2.5).
LISTING2.5: Thebreadth-firstsearchalgorithm.
#include<stdio.h>
#include“graph.h”
#include“queue.h”
#defineA 0
40 ArtificialIntelligence
#defineB 1
#defineC 2
#defineD 3
#defineE 4
#defineF 5
#defineG 6
#defineH 7
intinit_graph(graph_t*g_p)
{
addEdge(g_p,A,B,1);
addEdge(g_p,A,C,1);
addEdge(g_p,B,D,1);
addEdge(g_p,C,E,1);
addEdge(g_p,C,F,1);
addEdge(g_p,D,G,1);
addEdge(g_p,D,H,1);
return0;
}
voidbfs(graph_t*g_p,introot,intgoal)
{
intnode;
intto;
queue_t*q_p;
q_p=createQueue(10);
enQueue(q_p,root);
while(!isEmptyQueue(q_p)){
node=deQueue(q_p);
printf(“%d\n”,node);
if(node==goal)break;
for(to=g_p->nodes-1;to>0;to--){
if(getEdge(g_p,node,to)){
enQueue(q_p,to);
}
}
}
destroyQueue(q_p);
return;
}
intmain()
{
UninformedSearch 41
graph_t*g_p;
g_p=createGraph(8);
init_graph(g_p);
bfs(g_p,0,7);
destroyGraph(g_p);
return0;
}
HE C
NT
D
O
TheBFSimplementationcanbefoundontheCD-ROMat./software/
ch2/bfs.c.
ThedisadvantageofBFSisthateachnodethatissearchedisrequired
tobestored(spacecomplexityisO(bd)).Theentiredepthofthetreedoes
nothavetobesearched,sodinthiscontextisthedepthofthesolution,and
notthemaximumdepthofthetree.TimecomplexityisalsoO(bd).
TIP
In practical implementations of BFS, and other search algorithms, a
closed list is maintained that contains those nodes in the graph that
have been visited. This allows the algorithm to efficiently search the
graphwithoutre-visitingnodes.Inimplementationswherethegraphis
weighted,keepingaclosedlistisnotpossible.
FIGURE2.15: BidirectionalsearchmeetinginthemiddleatnodeH.
42 ArtificialIntelligence
BidirectionalSearch
TheBidirectionalSearchalgorithmisaderivativeofBFSthatoperatesby
performingtwobreadth-firstsearchessimultaneously,onebeginningfrom
therootnodeandtheotherfromthegoalnode.Whenthetwosearches
meetinthemiddle,apathcanbereconstructedfromtheroottothegoal.
Thesearchesmeetingisdeterminedwhenacommonnodeisfound(anode
visitedbybothsearches,seeFigure2.15).Thisisaccomplishedbykeeping
aclosedlistofthenodesvisited.
Bidirectionalsearchisaninterestingidea,butrequiresthatweknowthe
goalthatwe’reseekinginthegraph.Thisisn’talwayspractical,whichlimits
theapplicationofthealgorithm.Whenitcanbedetermined,thealgorithm
hasusefulcharacteristics.Thetimeandspacecomplexityforbidirectional
searchisO(bd/2),sincewe’reonlyrequiredtosearchhalfofthedepthof
thetree.SinceitisbasedonBFS,bidirectionalsearchisbothcompleteand
optimal.
Uniform-CostSearch(UCS)
OneadvantageofBFSisthatitalwaysfindstheshallowestsolution.But
considertheedgehavingacostassociatedwithit.Theshallowestsolution
maynotbethebest,andadeepersolutionwithareducedpathcostwould
bebetter(forexample,seeFigure2.16).Uniform-CostSearch(UCS)can
be applied to find the least-cost path through a graph by maintaining an
orderedlistofnodesinorderofdescendingcost.Thisallowsustoevaluate
theleastcostpathfirst
FIGURE2.16: Anexamplegraphwherechoosingthelowestcostpathforthefirstnode(A->C)
maynotresultinthebestoverallpaththroughthegraph(A->B->E).
UninformedSearch 43
TIP
Uniform-costsearchisanuninformedsearchmethodbecausenoheuristic
is actually used. The algorithm measures the actual cost of the path
withoutattemptingtoestimateit.
ThealgorithmforUCSusestheaccumulatedpathcostandapriorityqueue
to determine the path to evaluate (see Listing 2.6). The priority queue
(sortedfromleastcosttogreatest)containsthenodestobeevaluated.As
nodechildrenareevaluated,weaddtheircosttothenodewiththeaggregate
sumofthecurrentpath.Thisnodeisthenaddedtothequeue,andwhen
allchildrenhavebeenevaluated,thequeueissortedinorderofascending
cost.Whenthefirstelementinthepriorityqueueisthegoalnode,thenthe
bestsolutionhasbeenfound.
LISTING2.6:Theuniform-costsearchalgorithm.
#include<stdio.h>
#include“graph.h”
#include“pqueue.h”
#defineA 0
#defineB 1
#defineC 2
#defineD 3
#defineE 4
intinit_graph(graph_t*g_p)
{
addEdge(g_p,A,B,5);
addEdge(g_p,A,C,1);
addEdge(g_p,A,D,2);
addEdge(g_p,B,E,1);
addEdge(g_p,C,E,7);
addEdge(g_p,D,E,5);
return0;
}
voiducs(graph_t*g_p,introot,intgoal)
{
intnode,cost,child_cost;
intto;
pqueue_t*q_p;
q_p=createPQueue(7);
enPQueue(q_p,root,0);
while(!isEmptyPQueue(q_p)){
44 ArtificialIntelligence
dePQueue(q_p,&node,&cost);
if(node==goal){
printf(“cost%d\n”,cost);
return;
}
for(to=g_p->nodes-1;to>0;to--){
child_cost=getEdge(g_p,node,to);
if(child_cost){
enPQueue(q_p,to,(child_cost+cost));
}
}
}
destroyPQueue(q_p);
return;
}
intmain()
{
graph_t*g_p;
g_p=createGraph(6);
init_graph(g_p);
ucs(g_p,A,E);
destroyGraph(g_p);
return0;
}
HE C
NT
D
O
TheUCSimplementationcanbefoundontheCD-ROMat./software/
ch2/ucs.c.
The UCS algorithm is easily demonstrated using our example graph in
Figure2.16.Figure2.17showsthestateofthepriorityqueueasthenodes
areevaluated.Atstepone,theinitialnodehasbeenaddedtothepriority
queue,withacostofzero.Atsteptwo,eachofthethreeconnectednodes
areevaluatedandaddedtothepriorityqueue.Whennofurtherchildrenare
availabletoevaluate,thepriorityqueueissortedtoplacetheminascending
costorder.
Atstepthree,childrenofnodeCareevaluated.Inthiscase,wefindthe
desiredgoal(E),butsinceitsaccumulatedpathcostiseight,itendsupat
theendofthequeue.Forstepfour,weevaluatenodeDandagainfindthe
goalnode.Thepathcostresultsinseven,whichisstillgreaterthanourB
nodeinthequeue.Finally,atstepfive,nodeBisevaluated.Thegoalnodeis
foundagain,witharesultingpathcostofsix.Thepriorityqueuenowcontains
thegoalnodeatthetop,whichmeansatthenextiterationoftheloop,the
algorithmwillexitwithapathofA->B->E(workingbackwardsfromthe
goalnodetotheinitialnode).
Tolimitthesizeofthepriorityqueue,it’spossibletopruneentriesthatare
redundant.Forexample,atstep4inFigure2.17,theentryforE(8)couldhave
beensafelyremoved,asanotherpathexiststhathasareducedcost(E(7)).
ThesearchofthegraphisshowninFigure2.18,whichidentifiesthepath
costateachedgeofthegraph.Thepathcostshownabovethegoalnode(E)
makesiteasytoseetheleast-costpaththroughthegraph,evenwhenit’snot
apparentfromtheinitialnode.
UCS is optimal and can be complete, but only if the edge costs are
non-negative (the summed path cost always increases). Time and space
complexityarethesameasBFS,O(bd)foreach,asit’spossiblefortheentire
treetobeevaluated.
IMPROVEMENTS
One of the basic problems with traditional DFS and BFS is that they
lackavisitedlist(alistofnodesthathavealreadybeenevaluated).This
modificationmakesthealgorithmscomplete,byignoringcyclesandonly
followingpathsthathavenotyetbeenfollowed.ForBFS,keepingavisited
listcanreducethesearchtime,butforDFS,thealgorithmcanbemade
complete.
46 ArtificialIntelligence
ALGORITHMADVANTAGES
Eachofthealgorithmshasadvantagesanddisadvantagesbasedonthegraph
tobesearched.Forexample,ifthebranchingfactorofthegraphissmall,
thenBFSisthebestchoice.Ifthetreeisdeep,butasolutionisknownto
beshallowinthegraph,thenIDSisagoodchoice.Ifthegraphisweighted,
thenUCSshouldbeusedasitwillalwaysfindthebestsolutionwhereDFS
andBFSwillnot.
CHAPTERSUMMARY
Uninformedsearchalgorithmsareaclassofgraphsearchalgorithmsthat
exhaustivelysearchforanodewithouttheuseofaheuristictoguidethe
search.SearchalgorithmsareofinterestinAIbecausemanyproblemscan
be reduced to simple search problems in a state space. The state space
consistsofstates(nodes)andoperators(edges),allowingthestatespaceto
berepresentedasagraph.Examplesrangefromgraphsofphysicalspaces
tomassivegametreessuchasarepossiblewiththegameofChess.
The depth-first search algorithm operates by evaluating branches to
theirmaximumdepth,andthenbacktrackingtofollowunvisitedbranches.
Depth-limitedsearch(DLS)isbasedonDFS,butrestrictsthedepthofthe
search.Iterative-deepeningsearch(IDS)usesDLS,butcontinuallyincreases
thesearchdepthuntilthesolutionisfound.
The breadth-first search (BFS) algorithm searches with increasing
depthfromtheroot(searchesallnodeswithdepthone,thenallnodeswith
depthtwo,etc.).AspecialderivativealgorithmofBFS,bidirectionalsearch
(BIDI),performstwosimultaneoussearches.Startingattherootnodeand
thegoalnode,BIDIperformstwoBFSsearchesinsearchofthemiddle.
Onceacommonnodeisfoundinthemiddle,apathexistsbetweentheroot
andgoalnodes.
The uniform-cost search (UCS) algorithm is ideal for weight graphs
(graphswhoseedgeshavecostsassociatedwiththem).UCSevaluatesagraph
usingapriorityqueuethatisorderedinpathcosttotheparticularnode.It’s
basedontheBFSalgorithmandisbothcompleteandoptimal.
ALGORITHMSSUMMARY
Table 2.2: Summary of the uninformed algorithms and their
characteristics.
UninformedSearch 47
REFERENCES
[Bouton1901]“Nim,agamewithacompletemathematicaltheory,”Ann,
Math,Princeton3,35-39,1901-1902.
EXERCISES
1. What is uninformed (or blind) search and how does it differ from
informed(orheuristic)search?
2. The graph structure is ideal for general state space representation.
Explainwhyanddefinetheindividualcomponents.
3. DefinethequeuingstructuresusedinDFSandBFSandexplainwhy
eachusestheirparticularstyle.
4. Whatisthedefinitionoftreedepth?
5. Whatisthedefinitionofthebranchingfactor?
6. Whataretimeandspacecomplexityandwhyaretheyusefulasmetrics
forgraphsearch?
7. Ifanalgorithmalwaysfindsasolutioninagraph,whatisthisproperty
called?Ifitalwaysfindsthebestsolution,whatisthischaracteristic?
8. ConsideringDFSandBFS,whichalgorithmwillalwaysfindthebest
solutionforanon-weightedgraph?
9. UsetheDFSandBFSalgorithmstosolvetheTowersofHanoiproblem.
Whichperformsbetterandwhy?
10. ProvidethesearchorderforthenodesshowninFigure2.19forDFS,BFS,
DLS(d=2),IDS(startdepth=1),andBIDI(startnodeA,goalnodeI).
48 ArtificialIntelligence
11.Ingeneral,IDSisbetterthanDFS.Drawagraphwherethisisnotthe
case.
12. Ingeneral,IDSisnotcomplete.Why?
13. Identifyamajordisadvantageofbidirectionalsearch.
14. UsingtheUCSalgorithm,findtheshortestpathfromAtoFinFigure2.20.
I
n Chapter 2, we explored the uninformed search methods such as
depth-firstandbreadth-firstsearch.Thesemethodsoperateinabrute-
forcefashionandaresubsequentlyinefficient.Incontrast,thischapter
will present the informed search methods. These methods incorporate a
heuristic,whichisusedtodeterminethequalityofanystateinthesearch
space.Inagraphsearch,thisresultsinastrategyfornodeexpansion(which
nodeshouldbeevaluatednext).Avarietyofinformedsearchmethodswillbe
investigatedand,aswithuninformedmethods,comparedusingacommon
setofmetrics.
NOTE A heuristic is a rule of thumb that may help solve a given problem.
Heuristicstakeproblemknowledgeintoconsiderationtohelpguidethe
searchwithinthedomain.
INFORMEDSEARCH
Inthischapter,we’llexploreanumberofinformedsearchmethods,including
best-firstsearch,a-starsearch,iterativeimprovementalgorithmssuchashill
climbingandsimulatedannealing,andfinally,constraintsatisfaction.We’ll
demonstrateeachwithasampleproblemandillustratetheheuristicsused.
50 ArtificialIntelligence
BEST-FIRSTSEARCH(BEST-FS)
InBest-Firstsearch,thesearchspaceisevaluatedaccordingtoaheuristic
function.NodesyettobeevaluatedarekeptonanOPENlistandthosethat
havealreadybeenevaluatedarestoredonaCLOSEDlist.TheOPENlistis
representedasapriorityqueue,suchthatunvisitednodescanbedequeued
inorderoftheirevaluationfunction(recallthepriorityqueuefromChapter
2fortheUniform-CostSearch).
The evaluation function f(n) is made up of two parts. These are the
heuristicfunction(h(n))andtheestimatedcost(g(n)),where(seeEq3.1):
f(n)=g(n)+h(n) (Eq3.1)
Wecanthinkoftheestimatedcostasavaluemeasurablefromoursearch
space,andtheheuristicfunctionasaneducatedguess.TheOPENlistis
thenbuiltinorderoff(n).Thismakesbest-firstsearchfundamentallygreedy
becauseitalwayschoosesthebestlocalopportunityinthesearchfrontier.
NOTE Thesearchfrontierisdefinedasthesetofnodeopportunitiesthatcanbe
searchednext.InBest-Firstsearch,thefrontierisapriorityqueuesorted
inf(n)order.Giventhestrictorderoff(n),theselectionofthenodeto
evaluatefromthepriorityqueueisgreedy.
The complexity of best-first is O(bm) for both time and space (all nodes
aresavedinmemory).BymaintainingaCLOSEDlist(toavoidrevisiting
nodesandthereforeavoidingloops)best-firstsearchiscomplete,butitis
notoptimal,asasolutioncanbefoundinalongerpath(higherh(n)witha
lowerg(n)value.
TIP
Best-Firstsearchisacombinationofevaluationfunctions,h(n)andg(n).
NotethatBreadth-FirstsearchisaspecialcaseofBest-Firstsearchwhere
f(n)=h(n),andUniform-CostsearchisaspecialcaseofBest-Firstsearch
wheref(n)=g(n).
Best-FirstSearchandtheN-QueensProblem
Let’s now discuss the best-first search algorithm in the context of a large
searchspace.TheN-queensproblemisasearchproblemwherethedesired
result is an N by N board with N queens such that no queen threatens
another(seeFigure3.1).Forthisboard,ineachofthehorizontal,vertical,
anddiagonalrows,noqueenisabletocaptureanother.
InformedSearch 51
FIGURE3.1: SampleN-Queensboard(whereN=4).
FIGURE3.2: BoardrepresentationfortheN-Queensproblem(whereN=4).
Animportantaspectofproblemsolvingandsearchisrepresentation.For
thisexample,we’llchooseasimplerepresentationthatfitsthesolutionspace
wellandmakesitsimpletoenumerate.Eachboardpositionisrepresented
byasinglebitandifthebitiszero,thenthepositionisvacant,otherwise,
itisoccupiedbyaqueen.We’llsimplifytheproblembyassigningaqueen
toeachrowontheboard.Enumeratingthesearchspaceisthendefinedas
lookingatthepossiblemovesofqueenshorizontally.Forexample,thequeen
atthetopofFigure3.1canmoveleftorright,butthequeeninthesecond
rowcanonlymoveright(seeFigure3.1).Figure3.2alsoshowstheboard
representationasa16-bitvalue(unsignedshort,inthecaseofC).
Givenastate(theboardconfiguration),wecanidentifythechildstates
for this board by creating a new board for each of the possible queen
52 ArtificialIntelligence
positionchanges,givenhorizontalmovementonly.ForFigure3.2,thisboard
configurationcanresultinsixnewchildstates(asinglequeenchangeposition
ineach).Notethatsincewemaintainaclosedlist,boardconfigurationsthat
havealreadybeenevaluatedarenotgenerated,resultinginasmalltreeand
moreefficientsearch.
Fortheheuristic,we’llusethenode’sdepthinthetreeforh(n),and
thenumberofconflicts (number of queens that could capture another)
forg(n).
Best-FirstSearchImplementation
Let’s now look at a simple implementation of Best-First search in the C
language.We’llpresentthetwomajorfunctionsthatmakeupthissearch
algorithm;thefirstisbest_fs,whichisthemainloopofthealgorithm.The
secondfunction,generateChildNodes,buildsoutthepossiblestates(board
configurations)giventhecurrentstate.
Ourmainfunction(best_fs)istheOPENlistenumeratorandsolution
tester. Prior to calling this function, our OPEN list (priority queue)
and CLOSED list have been created. The root node, our initial board
configuration,hasbeenplacedontheOPENlist.Thebest_fsfunction(see
Listing3.1)thendequeuesthenextnodefromtheopenlist(bestf(n))If
thisnodehasag(n)(numberofconflicts)ofzero,thenasolutionhasbeen
found,andweexit.
LISTING3.1: TheBest-Searchfirstmainfunction.
voidbest_fs(pqueue_t*open_pq_p,queue_t*closed_q_p)
{
node_t*node_p;
intcost;
/*EnumeratetheOpenlist*/
while(!isEmptyPQueue(open_pq_p)){
dePQueue(open_pq_p,(int*)&node_p,&cost);
/*Solutionfound?*/
if(node_p->g==0){
printf(“FoundSolution(depth%d):\n”,node_p->h);
emitBoard(node_p);
break;
}
generateChildNodes(open_pq_p,closed_q_p,node_p);
InformedSearch 53
}
return;
}
NoteinListing3.1thatwhilecostisthef(n),wecheckg(n)todetermine
whetherasolutionisfound.Thisisbecausef(n)maybenon-zerosinceit
includesthedepthofthesolution(h(n)).
HE C
NT
D
O
TheBestFSimplementationcanbefoundontheCD-ROMat./software/
ch3/bestfs.c.
Thenextfunction,generateChildNodes,takesthecurrentboardconfiguration
andenumeratesallpossiblechildconfigurationsbypotentiallymovingeach
queenoneposition.Themovesarraydefinesthepossiblemovesforeach
positionontheboard(-1meansonlyright,2meansbothleftandright,and
1meansonlyleft).Theboardisthenenumerated,andwheneveraqueenis
found,themovesarrayischeckedforthelegalmoves,andnewchildnodes
arecreatedandloadedontotheOPENlist.
Note that we check the CLOSED list here to avoid creating a board
configurationthatwe’veseenbefore.Onceallpositionsonthecurrentboard
havebeenchecked,andnewchildnodesarecreated,thefunctionreturns
tobest_fs.
When a new board configuration is found, the createNode function
iscalledtoallocateanewnodestructureandplacesthisnewnodeonthe
OPENlist(andCLOSEDlist).Noteherethattheoneplusthedepth(h(n))
ispassedintoidentifythelevelofthesolutioninthetree.
LISTING3.2: ThegenerateChildNodesfunctiontoenumeratethechildnodes.
voidgenerateChildNodes(pqueue_t*pq_p,
queue_t*closed_q_p,node_t*node_p)
{
inti;
unsignedshortcboard1,cboard2;
constintmoves[16]={-1,2,2,1,
-1,2,2,1,
-1,2,2,1,
-1,2,2,1};
/*Generatethechildnodesforthecurrentnodeby
*shufflingthepiecesontheboard.
54 ArtificialIntelligence
*/
for(i=0;i<16;i++){
/*Isthereaqueenatthisposition?*/
if(checkPiece(node_p->board,i)){
/*Removecurrentqueenfromtheboard*/
cboard1=cboard2=(node_p->board&~(1<<(15-i)));
if(moves[i]==-1){
/*Canonlymoveright*/
cboard1|=(1<<(15-(i+1)));
if(!searchQueue(closed_q_p,cboard1)){
(void)createNode(pq_p,closed_q_p,cboard1,node_p->h+1);
}
}elseif(moves[i]==2){
/*Canmoveleftorright*/
cboard1|=(1<<(15-(i+1)));
if(!searchQueue(closed_q_p,cboard1)){
(void)createNode(pq_p,closed_q_p,cboard1,node_p->h+1);
}
cboard2|=(1<<(15-(i-1)));
if(!searchQueue(closed_q_p,cboard2)){
(void)createNode(pq_p,closed_q_p,cboard2,node_p->h+1);
}
}elseif(moves[i]==1){
/*Canonlymoveleft*/
cboard2|=(1<<(15-(i-1)));
if(!searchQueue(closed_q_p,cboard2)){
(void)createNode(pq_p,closed_q_p,cboard2,node_p->h+1);
}
}
}
}
return;
}
Let’snowwatchthealgorithminaction.Onceinvoked,arandomrootnode
isenqueuedandthenthepossiblechildconfigurationsareenumeratedand
loadedontotheOPENlist(seeListing3.3).Thedemonstrationhereshows
ashallowtreeofthreeconfigurationschecked,therootnode,oneatlevel
one,andthesolutionfoundatdepthtwo.Acondensedversionofthisrun
isshowninFigure3.3.
InformedSearch 55
FIGURE3.3: Graphical(condensed)viewofthesearchtreeinListing3.3.
LISTING3.3: Best-FirstSearchfortheN-Queensproblem(N=4).
Newnode:evaluateBoard4824=(h0,g3)
InitialBoard:
boardis0x4824
0100
1000
56 ArtificialIntelligence
0010
0100
Checkingboard0x4824(h0g3)
Newnode:evaluateBoard2824=(h1,g2)
Newnode:evaluateBoard8824=(h1,g3)
Newnode:evaluateBoard4424=(h1,g4)
Newnode:evaluateBoard4814=(h1,g3)
Newnode:evaluateBoard4844=(h1,g4)
Newnode:evaluateBoard4822=(h1,g3)
Newnode:evaluateBoard4828=(h1,g2)
Checkingboard0x2824(h1g2)
Newnode:evaluateBoard1824=(h2,g1)
Newnode:evaluateBoard2424=(h2,g5)
Newnode:evaluateBoard2814=(h2,g0)
Newnode:evaluateBoard2844=(h2,g2)
Newnode:evaluateBoard2822=(h2,g3)
Newnode:evaluateBoard2828=(h2,g2)
Checkingboard0x2814(h2g0)
FoundSolution(h2g0):
boardis0x2814
0010
1000
0001
0100
VariantsofBest-FirstSearch
Oneinterestingvariantofbest-firstsearchiscalledgreedybest-firstsearch.
Inthisvariant,f(n)=h(n),andtheOPENlistisorderedinforder.Since
histheonlyfactorusedtodeterminewhichnodetoselectnext(identified
astheclosenesstothegoal),it’sdefinedasgreedy.Becauseofthis,greedy
best-firstisnotcompleteastheheuristicisnotadmissible(becauseitcan
overestimatethepathtothegoal).We’lldiscussadmissibilityinmoredetail
inthediscussionofA-starsearch.
Anothervariantofbest-firstsearchisbeam-search,likegreedybest-first
search,itusestheheuristicf(n)=h(n).Thedifferencewithbeam-searchis
thatitkeepsonlyasetofthebestcandidatenodesforexpansionandsimply
throwstherestway.Thismakesbeam-searchmuchmorememoryefficient
than greedy best-first search, but suffers in that nodes can be discarded
which could result in the optimal path. For this reason, beam-search is
neitheroptimalorcomplete.
InformedSearch 57
A*SEARCH
A* search, like best-first search, evaluates a search space using a heuristic
function. But A* uses both the cost of getting from the initial state to the
currentstate(g(n)),aswellasanestimatedcost(heuristic)ofthepathfrom
thecurrentnodetothegoal(h(n)).Thesearesummedtothecostfunctionf(n)
(SeeEq3.1).TheA*search,unlikebest-first,isbothoptimalandcomplete.
TheOPENandCLOSEDlistsareusedagaintoidentifythefrontierfor
search(OPENlist)andthenodesevaluatedthusfar(CLOSED).TheOPEN
listisimplementedasapriorityqueueorderedinlowestf(n)order.What
makesA*interestingisthatitcontinuallyre-evaluatesthecostfunctionfor
nodesasitre-encountersthem.ThisallowsA*toefficientlyfindtheminimal
pathfromtheinitialstatetothegoalstate.
Let’snowlookatA*atahighlevelandthenwe’lldigfurtherandapply
ittoawell-knownproblem.Listing3.4providesthehighlevelflowforA*.
LISTING3.4:High-levelflowfortheA*searchalgorithm.
InitializeOPENlist(priorityqueue)
InitializeCLOSEDlist
PlacestartnodeontheOPENlist
LoopwhiletheOPENlistisnotempty
Getbestnode(parent)fromOPENlist(leastf(n))
ifparentisthegoalnode,done
PlaceparentontheCLOSEDlist
Expandparenttoalladjacentnodes(adj_node)
ifadj_nodeisontheCLOSEDlist
discardadj_nodeandcontinue
elseifadj_nodeisontheOPENlist
ifadj_node’sgvalueisbetterthan
theOPEN.adj_node’sgvalue
discardOPEN.cur_node
calculateadj_node’sg,handfvalues
setadj_nodepredecessortoparent
addadj_nodetoOPENlist
continue
end
else
calculateadj_node’sg,handfvalues
setadj_nodepredecessortoparent
58 ArtificialIntelligence
addadj_nodetoOPENlist
end
end
endloop
NoteintheflowfromListing3.4thatoncewefindthebestnodefromthe
OPENlist,weexpandallofthechildnodes(legalstatespossiblefromthe
best node). If the new legal states are not found on either the OPEN or
CLOSEDlists,theyareaddedasnewnodes(settingthepredecessortothe
bestnode,orparent).IfthenewnodeisontheCLOSEDlist,wediscard
itandcontinue.Finally,ifthenewnodeisontheOPENlist,butthenew
nodehasabettergvalue,wediscardthenodeontheOPENlistandadd
thenewnodetotheOPENlist(otherwise,thenewnodeisdiscarded,ifitsg
valueisworse).Byre-evaluatingthenodesontheOPENlist,andreplacing
themwhencostfunctionspermit,weallowbetterpathstoemergefromthe
statespace.
Aswe’vedefinedalready,A*iscomplete,aslongasthememorysupports
the depth and branching factor of the tree. A* is also optimal, but this
characteristic depends on the use of an admissible heuristic. Because A*
mustkeeptrackofthenodesevaluatedsofar(andalsothediscoverednodes
tobeevaluated),thetimeandspacecomplexityarebothO(bd).
NOTE The heuristic is defined as admissible if it accurately estimates the
path cost to the goal, or underestimates it (remains optimistic). This
requires that the heuristic be monotonic, which means that the cost
neverdecreasesoverthepath,andinsteadmonotonicallyincreases.This
means that g(n) (path cost from the initial node to the current node)
monotonicallyincreases,whileh(n)(pathcostfromthecurrentnodeto
thegoalnode)monotonicallydecreases.
FIGURE3.4: TheEightPuzzleandademonstrationofmovingfromaninitialconfigurationto
thegoalconfiguration(doesnotincludeallsteps).
InformedSearch 59
A*SearchandtheEightPuzzle
WhileA*hasbeenappliedsuccessfullytoproblemdomainssuchaspath-
finding, we’ll apply it here to what’s called the Eight Puzzle (also known
astheNbyM,orn2-1tilepuzzle).Thisparticularvariationofthepuzzle
consistsofeighttilesina3by3grid.Onelocationcontainsnotile,which
canbeusedtomoveothertilestomigratefromoneconfigurationtoanother
(seeFigure3.4).
Note in Figure 3.4 that there are two legal moves that are possible.
The‘1’tilecanmoveleft,andthe‘6’tilecanmovedown.Thefinalgoal
configurationisshownattheright.Notethatthisisonevariationofthegoal,
andtheonethatwe’llusehere.
TheEightPuzzleisinterestingbecauseit’sadifficultproblemtosolve,
butonethat’sbeenstudiedatlengthandisthereforeverywellunderstood.
[Archer1999]Forexample,thenumberofpossibleboardconfigurationsof
theEightPuzzleis(n*n)!,butonlyhalfofthesearelegalconfigurations.
TIP
During the 1870s, the Fifteen Puzzle (4 by 4 variant of the N by M
puzzle)becameapuzzlecrazemuchliketheRubik’scubeofthe1970s
and1980s.
Onaverage,22movesarerequiredtosolvethe3by3variantofthepuzzle.
Butconsidering22astheaveragedepthofthetree,withanaveragebranching
factorof2.67,2.4trillionnon-uniquetileconfigurationscanbeevaluated.
Eight-PuzzleRepresentation
We’ll use a common representation for the Eight Puzzle, a linear vector
containingthetileplacementfromlefttoright,toptobottom(seeFigure
3.5).Thisparticularfigureshowsthemovespossiblefromtheinitialpuzzle
configurationtodepthtwoofthisparticularstatespacetree.
FIGURE3.5: EightPuzzleconfigurationusingasimplevector.
60 ArtificialIntelligence
Forourheuristic,we’llusethedepthofthetreeasthecostfromtherootto
thecurrentnode(otherwiseknownasg(n)),andthenumberofmisplaced
tiles(h(n))astheestimatedcosttothegoalnode(excludingtheblank).The
pathcost(f(n))thenbecomesthecostofthepathtothecurrentnode(g(n))
plustheestimatedcosttothegoalnode(h(n)).Youcanseetheseheuristics
inthetreeinFigure3.6.Fromtherootnode,onlytwomovesarepossible,
butfromthesetwomoves,threenewmoves(states)openup.Atthebottom
ofthistree,youcanseethatthecostfunctionhasdecreased,indicatingthat
theseboardconfigurationsarelikelycandidatestoexplorenext.
NOTE TherearetwopopularheuristicsfortheN-puzzleproblem.Thefirstis
simplythenumberoftilesoutofplace,whichingeneraldecreasesas
thegoalisapproached.TheotherheuristicistheManhattandistanceof
FIGURE3.6: EightPuzzletreeendingatdepthtwo,illustratingthecostfunctions.
InformedSearch 61
tileswhichsumsthetiledistanceofeachoutofplacetiletoitscorrect
location. For this implementation, we’ll demonstrate the simple, but
effective,tiles-out-of-placeheuristic.
TIP
While there are (3*3)! board configurations possible, there are only
(3*3)!/2 valid configurations. The other half of the configurations are
unsolvable.We’llnotdwellonthishere,butinthesourceimplementation
you’llseethetestininitPuzzleusingtheconceptofinversionstovalidate
theconfigurationoftheboard.Thisconceptcanbefurtherexploredin
[KGong2005].
A*SearchImplementation
ThecoretheofA-staralgorithmisimplementedinthefunctionastar().This
functionimplementsA-starasshowninListing3.4.We’llalsopresentthe
evaluationfunction,whichimplementsthe‘tiles-out-of-place’metric.The
listandothersupportfunctionsarenotpresentedhere,butareavailableon
theCD-ROMforreview.
NOTE TheA*implementationcanbefoundontheCD-ROMat./software/ch3/
astar.c.
Let’sstartwiththeevaluationfunctionwhichcalculatestheestimatedcost
fromthecurrentnodetothegoal(asthenumberoftilesoutofplace),see
Listing 3.6. The function simply enumerates the 3 by 3 board as a one-
dimensionalvector,incrementingascorevaluewheneveratileispresentin
apositionitshouldnotbein.Thisscoreisthenreturnedtothecaller.
LISTING3.6: TheEightPuzzleh(n)estimatedcostmetric.
doubleevaluateBoard(board_t*board_p)
{
inti;
constinttest[MAX_BOARD-1]={1,2,3,4,5,6,7,8};
intscore=0;
for(i=0;i<MAX_BOARD-1;i++){
score+=(board_p->array[i]!=test[i]);
}
return(double)score;
}
62 ArtificialIntelligence
TheastarfunctionisshowninListing3.7.Priortocallingthisfunction,we’ve
selectedarandomboardconfigurationandplaceditontotheOPENlist.We
thenworkthroughtheOPENlist,retrievingthebestnode(withtheleastf
valueusinggetListBest)andimmediatelyplaceitontheCLOSEDlist.We
checktoseeifthisnodeisthesolution,andifso,weemitthepathfrom
theinitialnodetothegoal(whichillustratesthemovesthatweremade).To
minimizesearchingtoodeeplyinthetree,wehaltenumeratingnodespast
agivendepth(wesearchthemnofurther).
Thenextstepistoenumeratethepossiblemovesfromthisstate,which
willbeamaximumoffour.ThegetChildBoardfunctionisusedtoreturn
anadjacentnode(usingtheindexpassedintodeterminewhichpossible
movetomake).Ifamoveisn’tpossible,thenaNULLisreturnedandit’s
ignored.
Withanewchildnode,wefirstchecktoseeifit’salreadybeenevaluated
(if it’s on the CLOSED list). If it is, then we’re to destroy this node and
continue (to get the child node for the current board configuration). If
we’venotseenthisparticularboardconfigurationbefore,wecalculatethe
heuristicsforthenode.First,weinitializethenode’sdepthinthetreeasthe
parent’sdepthplusone.Next,wecallevaluateBoardtogetthetiles-out-of-
placemetric,whichwillactasourhvalue(costfromtherootnodetothis
node).Thegvalueissettothecurrentdepth,andthefvalueisinitialized
withEq3.1.
(Eq3.1)
Weincludeanalphaandbetaparameterheretogivedifferentweightsto
thegandhvalues.Inthisimplementation,alphais1.0andbetais2.0.This
meansthatmoreweightisgiventothehvalue,andsubsequentlytheclosera
nodeistothegoalisweighedhigherthanitsdepthinthestatespacetree.
Withthefvaluecalculated,wechecktoseeifthenodeisontheOPEN
list.Ifitis,wecomparetheirfvalues.IfthenodeontheOPENlisthasa
worsefvalue,thenodeontheOPENlistisdiscardedandthenewchild
nodetakesitsplace(settingthepredecessorlinktotheparent,soweknow
howwegottothisnode).IfthenodeontheOPENlisthasabetterfvalue,
thenthenodeontheOPENlistremainsontheopenlistandthenewchild
isdiscarded.
Finally,ifthenewchildnodeexistsonneithertheCLOSEDorOPEN
list,it’sanewnodethatwe’veyettosee.It’ssimplyaddedtotheOPENlist,
andtheprocesscontinues.
This algorithm continues until either one of two events occur. If the
OPENlistbecomesempty,thennosolutionwasfoundandthealgorithm
InformedSearch 63
exits.Ifthesolutionisfound,thenshowSolutioniscalled,andthenodes
linkedtogetherviathepredecessorlinksareenumeratedtoshowthesolution
fromtheinitialnodetothegoalnode.
LISTING3.7: TheA*algorithm.
voidastar(void)
{
board_t*cur_board_p,*child_p,*temp;
inti;
/*Whileitemsareontheopenlist*/
while(listCount(&openList_p)){
/*Getthecurrentbestboardontheopenlist*/
cur_board_p=getListBest(&openList_p);
putList(&closedList_p,cur_board_p);
/*Dowehaveasolution?*/
if(cur_board_p->h==(double)0.0){
showSolution(cur_board_p);
return;
}else{
/*Heuristic-averagenumberofstepsis22fora3x3,so
*don’tgotoodeep.
*/
if(cur_board_p->depth>MAX_DEPTH)continue;
/*Enumerateadjacentstates*/
for(i=0;i<4;i++){
child_p=getChildBoard(cur_board_p,i);
if(child_p!=(board_t*)0){
if(onList(&closedList_p,child_p->array,NULL)){
nodeFree(child_p);
continue;
}
child_p->depth=cur_board_p->depth+1;
child_p->h=evaluateBoard(child_p);
child_p->g=(double)child_p->depth;
child_p->f=(child_p->g*ALPHA)+(child_p->h*BETA);
/*Newchildboardontheopenlist?*/
if(onList(&openList_p,child_p->array,NULL)){
temp=getList(&openList_p,child_p->array);
if(temp->g<child_p->g){
64 ArtificialIntelligence
nodeFree(child_p);
putList(&openList_p,temp);
continue;
}
nodeFree(temp);
}else{
/*Childboardeitherdoesn’texist,orisbetterthana
*previousboard.Hookittotheparentandplaceonthe
*openlist.
*/
child_p->pred=cur_board_p;
putList(&openList_p,child_p);
}
}
}
}
}
return;
}
EightPuzzleDemonstrationwithA*
Intheimplementation,thetilesarelabeledA-Hwithaspaceusedtodenote
theblanktile.Uponexecution,oncethesolutionisfound,thepathtaken
from the initial board to the goal is enumerated. This is shown below in
Listing3.8,minimizedforspace.
LISTING3.8: AsamplerunoftheA*programtosolvetheEightPuzzle.
$./astar
GBD
FCH
EA
BGD
FCH
EA
BGD
FCH
EA
GBD
FC
InformedSearch 65
EAH
...
ABC
DF
GEH
ABC
DF
GEH
ABC
DEF
GH
ABC
DEF
GH
A*Variants
ThepopularityofA*hasspawnedanumberofvariantsthatofferdifferent
characteristics.TheIterative-DeepeningA*algorithmbacktrackstoother
nodeswhenthecostofthecurrentbranchexceedsathreshold.Tominimize
the memory requirements of A*, the Simplified Memory-Bounded A*
algorithm(SMA*)wascreated.SMA*usesthememorymadeavailableto
it,andwhenitrunsoutofmemory,thealgorithmdropstheleastpromising
nodetomakeroomfornewsearchnodesfromthefrontier.
ApplicationsofA*Search
A*searchisapopulartechniqueandhasseenuseasapath-findingalgorithm
forcomputerstrategygames.Forbetterperformance,manygamesemploy
simpler shortcut methods for path-finding by limiting the space of their
movement (using a much sparser graph over the landscape), or by pre-
calculatingroutesforin-gameuse.
HILL-CLIMBINGSEARCH
Hillclimbingisaniterativeimprovementalgorithmthatissimilartogreedy
best-firstsearch,exceptthatbacktrackingisnotpermitted.Ateachstepin
thesearch,asinglenodeischosentofollow.Thecriterionforthenodeto
followisthatit’sthebeststateforthecurrentstate.Sincethefrontierforthe
searchisasinglenode,thealgorithmisalsosimilartobeamsearchusinga
beamwidthofone(ourOPENlistcancontainexactlyonenode).
66 ArtificialIntelligence
FIGURE3.7: Statespaceillustratingtheproblemwithhillclimbing.
Theproblemwithhillclimbingisthatthebestnodetoenumeratelocally
maynotbethebestnodeglobally.Forthisreason,hillclimbingcanleadto
localoptimums,butnotnecessarilytheglobaloptimum(thebestsolution
available).ConsiderthefunctioninFigure3.7.Thereexistsalocaloptimum
andaglobaloptimum.Thegoalshouldbetomaximizethefunction,butif
webeginatthefarleftandworkourwaytowardtheglobaloptimum,we
getstuckatthelocaloptimum.
SIMULATEDANNEALING(SA)
Simulated Annealing (SA) is another iterative improvement algorithm in
which randomness is incorporated to expand the search space and avoid
becomingtrappedinlocalminimum.Asthenameimplies,thealgorithm
simulatestheprocessofannealing.
Annealingisatechniqueinmetal-castingwheremoltenmetalisheated
andthencooledinagradualmannertoevenlydistributethemoleculesintoa
crystallinestructure.Ifthemetaliscooledtooquickly,acrystallinestructure
doesnotresult,andthemetalsolidisweakandbrittle(havingbeenfilledwith
bubblesandcracks).Ifcooledinagradualandcontrolledway,acrystalline
structureformsatamolecularlevelresultingingreatstructuralintegrity.
ThebasicalgorithmforsimulatedannealingisshowninListing3.9.We
startwithaninitialsolutioncandidateandtheloopwhilethetemperatureis
greaterthanzero.Inthisloop,wecreateanadjacentcandidatesolutionby
perturbingourcurrentsolution.Thischangesthesolutiontoaneighboring
solution,butatrandom.Wethencalculatethedeltaenergybetweenthe
new(adjacent)solution,andourcurrentsolution.Ifthisdeltaenergyisless
InformedSearch 67
thanzero,thenournewsolutionisbetterthantheold,andweacceptit(we
movethenewadjacentsolutiontoourcurrentsolution).
LISTING3.9: Simulatedannealingalgorithm.
simulated_annealing()
{
cur_solution=random()
computeE(cur_solution)
while(Temperature>0)
adj_solution=perturb_solution(cur_solution)
computeE(adj_solution)
deltaE=adj_solution.energy–cur_solution.energy
/*Isnewsolutionbetter,thentakeit*/
if(deltaE<0)
cur_solution=adj_solution
else
p=exp(-deltaE/Temperature)
/*Randomlyacceptworsesolution*/
if(p>RANDOM(0..1))
cur_solution=adj_solution
end
end
reduceTemperature
end
endsimulated_annealing
Ifournewsolutionwasnotbetterthantheold,thenweacceptitwitha
probabilityproportionaltothecurrenttemperatureandthedeltaenergy.
Thelowerthetemperature,thelesslikelywe’llacceptaworsesolution.But
thebetterthedeltaenergy,themorelikelywe’llacceptit.Thisprobability
iscalculatedasshowninEq3.2.
(Eq3.2)
Sinceourtemperaturedecreasesovertime,it’slesslikelythataworse
solution will be accepted. Early on when the temperature is high, worse
solutions can be accepted allowing the search to move away from local
maximuminsearchoftheglobalmaximum.Asthetemperaturedecreases,
itbecomesmoredifficulttoacceptaworsesolution,whichmeansthatthe
algorithmsettlesonasolutionandsimplyfine-tunesit(ifpossible).
68 ArtificialIntelligence
FIGURE3.8: ASampleTSPtourthroughasmallgraph.
Theclassicalsimulatedannealingalgorithmalsoincludesmontecarlo
cycles where a number of trials are performed before decreasing the
temperature.
TheTravelingSalesmanProblem(TSP)
To demonstrate the simulated annealing algorithm, we’ll use the classic
TravelingSalesmanProblem(orTSP).IntheTSP,we’regivenasetofcities
andarelativecostfortravelingbetweeneachcitytoeachother.Thegoal
istofindapaththroughallcitieswherewevisitallcitiesonce,andfindthe
shortestoveralltour.We’llstartatonecity,visiteachothercity,andthen
endattheinitialcity.
ConsiderthegraphshowninFigure3.8.Manycitiesareconnectedto
oneanother,butanoptimalpathexiststhattourseachcityonlyonce.
The TSP is both interesting and important because it has practical
implications.Considertransportationproblemswheredeliveriesarerequired
andfuelandtimearetobeminimized.Anotherinterestingapplicationis
thatofdrillingholesinacircuitboard.Anumberofholesmustbedrilled
quicklyonasingleboard,andinordertodothis,anoptimalpathisneeded
tominimizethemovementofthedrill(whichwillbeslow).Solutionstothe
TSPcanthereforebeveryuseful.
TSPTourRepresentation
Torepresentasetofcitiesandthetourbetweenthem,we’lluseanimplicit
adjacencylist.Eachcitywillbecontainedinthelist,andcitiesthatarenext
tooneanotherareimpliedasconnectedinthetour.RecalloursampleTSP
inFigure3.8wheresevencitiesmakeuptheworld.Thiswillberepresented
asshowninFigure3.9.
InformedSearch 69
FIGURE3.9: AdjacencylistfortheTSPtourshowninFigure3.8.
FIGURE3.10: Demonstrationofrowswappingtoperturbthetour.
70 ArtificialIntelligence
NotethatthelistshowninFigure3.9isasinglelistintourorder.When
wereachtheendofthelist,wewraptothefirstelement,completingthe
tour.Toperturbthetourwetaketworandomrowsfromthelistandswap
them.ThisisdemonstratedinFigure3.10.Notehowbysimplyswapping
two elements, the tour is greatly perturbed and results in a worse tour
length.
SimulatedAnnealingImplementation
Theimplementationofsimulatedannealingisactuallyquitesimpleinthe
Clanguage.We’llreviewthreeofthefunctionsthatmakeupthesimulated
annealing implementation, the main simulated annealing algorithm,
perturbing a tour, and computing the length of the tour. The remaining
functionsareavailableontheCD-ROM.
LISTING3.10: StructuresfortheTSPsolution.
typedefstruct{
intx,y;
}city_t;
typedefstruct{
city_tcities[MAX_CITIES];
doubletour_length;
}solution_t;
TheEuclideandistanceofthetouriscalculatedwithcompute_tour.This
functionwalksthroughthetour,accumulatingthesegmentsbetweeneach
city(seeListing3.11).Itendsbywrappingaroundthelist,andaddinginthe
distancefromthelastcitybacktothefirst.
LISTING3.11: CalculatingtheEuclideantourwithcompute_tour.
voidcompute_tour(solution_t*sol)
{
inti;
doubletour_length=(double)0.0;
for(i=0;i<MAX_CITIES-1;i++){
tour_length+=
euclidean_distance(
sol->cities[i].x,sol->cities[i].y,
sol->cities[i+1].x,sol->cities[i+1].y);
InformedSearch 71
}
tour_length+=
euclidean_distance(
sol->cities[MAX_CITIES-1].x,
sol->cities[MAX_CITIES-1].y,
sol->cities[0].x,sol->cities[0].y);
sol->tour_length=tour_length;
return;
}
Given a solution, we can create an adjacent solution using the function
perturb_tour.Inthisfunction,werandomlyselecttwocitiesinthetour,and
swapthem.Aloopexiststoensurethatwe’veselectedtwouniquerandom
points(sothatwedon’tswapasinglecitywithitself).Onceselected,thex
andycoordinatesareswappedandthefunctioniscomplete.
LISTING3.12: Perturbingthetourbycreatinganadjacentsolution.
voidperturb_tour(solution_t*sol)
{
intp1,p2,x,y;
do{
p1=RANDMAX(MAX_CITIES);
p2=RANDMAX(MAX_CITIES);
}while(p1==p2);
x=sol->cities[p1].x;
y=sol->cities[p1].y;
sol->cities[p1].x=sol->cities[p2].x;
sol->cities[p1].y=sol->cities[p2].y;
sol->cities[p2].x=x;
sol->cities[p2].y=y;
return;
}
iterations(theMonteCarlostep).Ifthenewsolutionisbetter,weaccept
it by copying it into the current solution. If the new solution is worse,
thenweacceptitwithaprobabilitydefinedbyEq3.2.Theworsethenew
solutionandthelowerthetemperature,thelesslikelywearetoacceptthe
newsolution.WhentheMonteCarlostepiscomplete,thetemperature
isreducedandtheprocesscontinues.Whenthealgorithmcompletes,we
emitthecitytour.
LISTING3.13: Thesimulatedannealingmainfunctionimplementation.
intsimulated_annealing(void)
{
doubletemperature=INITIAL_TEMP,delta_e;
solution_ttempSolution;
intiteration;
while(temperature>0.0001){
/*Copythecurrentsolutiontoatemp*/
memcpy((char*)&tempSolution,
(char*)&curSolution,sizeof(solution_t));
/*MonteCarloIterations*/
for(iteration=0;
iteration<NUM_ITERATIONS;iteration++){
perturb_tour(&tempSolution);
compute_tour(&tempSolution);
delta_e=tempSolution.tour_length–
curSolution.tour_length;
/*Isthenewsolutionbetterthantheold?*/
if(delta_e<0.0){
/*Acceptthenew,better,solution*/
memcpy((char*)&curSolution,
(char*)&tempSolution,sizeof(solution_t));
}else{
/*Probabilisticallyacceptaworsesolution*/
if(exp((-delta_e/temperature))>RANDOM()){
memcpy((char*)&curSolution,
(char*)&tempSolution,sizeof(solution_t));
}
}
}
/*Decreasethetemperature*/
InformedSearch 73
temperature*=ALPHA;
}
return0;
}
Simulatedannealingpermitsarandomwalkthroughastatespace,greedily
followingthebestpath.Butsimulatedannealingalsoprobabilisticallyallows
followingworsepathsinanefforttoescapelocalmaximumsinsearchof
the global maximum. This makes simulated annealing a random search,
but heuristically driven. For all of its advantages, simulated annealing is
incompleteandsuboptimal.
SimulatedAnnealingDemonstration
Let’snowlookatthesimulatedannealingalgorithminaction.We’lllookat
thealgorithmfromavarietyofperspectives,fromthetemperatureschedule,
toasamplesolutiontoTSPfor25cities.
FIGURE3.11: ThetemperaturedecaycurveusingEq3.3.
74 ArtificialIntelligence
Thetemperaturescheduleisafactorintheprobabilityforacceptinga
worsesolution.Inthisimplementation,we’lluseageometricdecayforthe
temperature,asshowninEq3.3.
T = aT (Eq 3.3)
Inthiscase,weuseanalphaof0.999.Thetemperaturedecayusingthis
equationisshowninFigure3.11.
TherelativefitnessofthesolutionoverarunisshowninFigure3.12.
Thisgraphshowsthelengthofthetourduringthedecreaseintemperature.
Noteattheleft-handsideofthegraphthattherelativefitnessisveryerratic.
Thisisduetothehightemperatureacceptinganumberofpoorersolutions.
As the temperature decreases (moving to the right of the graph), poorer
solutionsarenotacceptedasreadily.Attheleft-handsideofthegraph,the
algorithmpermitsexplorationofthestatespace,whereattheright-handof
thegraph,thesolutionisfine-tuned.
A sample TSP tour is shown finally in Figure 3.13. This particular
solutionwasfora25citytour.
FIGURE3.12: Therelativefitness.
InformedSearch 75
FIGURE3.13: SampleTSPtouroptimizedbysimulatedannealing.
TABUSEARCH
Tabusearchisaverysimplesearchalgorithmthatiseasytoimplementand
canbeveryeffective.ThebasicideabehindTabusearchisneighborhood
searchwithaTabulistofnodesthatismadeupofnodespreviouslyevaluated.
Therefore,thesearchmaydeteriorate,butthisallowsthealgorithmtowiden
thesearchtoavoidbecomingstuckinlocalmaxima.Duringeachiteration
ofthealgorithm,thecurrentsearchcandidateiscomparedagainstthebest
solution found so far so that the best node is saved for later. After some
searchcriteriahasbeenmet(asolutionfound,oramaximumnumberof
iterations)thealgorithmexits.
TheTabulistcanbeoffinitesizesothattheoldestnodescanbedropped
makingroomfornewTabunodes.ThenodesontheTabulistcanalsobe
timed,suchthatanodecanonlybeTabuforsomeperiodoftime.Either
caseallowsthealgorithmtoreusetheTabulistandminimizetheamount
ofmemoryneeded.
76 ArtificialIntelligence
FIGURE3.14: The4-QueensproblemsolvedbyTabusearch.
MonitoringTabusearchthroughthestatespaceofthe4-Queensproblem
isshowninFigure3.14.Theinitialpositionistheroot,whichhasascore
ofthree(threeconflicts).Thegoalistominimizethescore,wherezeroisa
solution(goalnode).Atthefirstiteration,theneighbornodesareevaluated,
andthebestselected.Notealsoherethatourinitialnodehasbeenplaced
ontheTabulist.Atiterationtwo,theneighborsareevaluatedforthecurrent
nodeandthebestischosentomoveforward.TheTabulistnowcontainsthe
previoustwobestnodes.Inthisiteration,we’vefoundanodewithascoreof
zero,whichindicatesagoalnodeandthealgorithmterminates.
ThebasicflowforTabusearchisshowninListing3.14.Givenaninitial
position (shown here as a initial random position), the search space is
enumeratedbytakingthebestneighbornodethatisnotTabu.Ifit’sbetter
thanourbestsavedsolution,itbecomesthebestsolution.Theprocessthen
continueswiththelastsolutionuntilaterminationcriteriaismet.
InformedSearch 77
LISTING3.14: ThebasicflowoftheTabusearchalgorithm.
tabu_search()
{
cur_solution=random()
evaluate_position(cur_solution)
best=cur_solution
tabu(cur_solution)
while(!termination_critera){
/*Getthebestneighbor,notonthetabulist*/
cur_solution=best_non_tabu_neighbor(cur_solution)
evaluate_position(cur_solution)
tabu(cur_solution)
if(cur_solution.f<best.f){
best=cur_solution
}
}
returnbest
}
ToillustratetheTabusearchalgorithm,we’llusetheN-Queensproblem
asdemonstratedwiththebest-firstsearchalgorithm.(SeeFigure3.1fora
recapoftheproblemanddesiredsolution.)AfterdiscussingthebasicTabu
searchimplementation,we’llexploresomeofthevariantsthatimprovethe
algorithm.
TabuSearchImplementation
TheTabusearchalgorithmisverysimpleandcanbeillustratedinasingle
function(seeListing3.15,functiontabu_s).Thisfunctionisthecoreof
theTabusearchalgorithm.Thesupportingfunctionsarenotshownhere,
butareavailableontheCD-ROM.
HE C
NT
D
O
TheCsourcelanguageimplementationofTabusearchcanbefoundon
theCD-ROMat./software/ch3/tabus.c.
The implementation begins with a seeding of the random function
(RANDINIT) followed by the creation of the Tabu queue. This queue
representsourTabulist,orthoseelementsthatwillnotbeevaluatedfurther
78 ArtificialIntelligence
ifrediscovered.Theinitialsolutionisthencreated,andcopiedtothebest
solution(viainitBoardtocreatethesolution,andevaluateBoardtoevaluate
thevalueofthesolution).ThecurrentsolutionisthenloadedontotheTabu
listsothatit’snotevaluatedagain.
Theloopthenbegins,forwhichwe’lloperateforever,oruntilasolution
is found. For this simple problem, we’ll always find a solution in some
numberofiterations.ThecalltoreviewChildNodesevaluatestheneighbor
solutions,andpicksthebestonethatisnotontheTabulist.Thissolutionis
returned(byreference)andthenloadedontotheTabulist.Noteherethat
wefirstchecktoseeifit’salreadyontheTabulist.Ifnot,wecheckthestate
oftheTabulist.Iffull,weneedtheoldestelementtomakeroomforthe
newnode,andthenaddittothequeue.
TIP
RecallthatqueuesareFIFOinnature.Therefore,removinganodefrom
thequeueautomaticallyremovestheoldestnode,satisfyingthepolicy
forthealgorithm(removetheoldestnodefirst,iftheTabulistisfull).
Finally,wecheckthevalueofthesolution,andifzero,wehavethegoalnode.
ThiscannowbeemittedusingtheemitBoardfunction.
LISTING3.15: BasicTabusearchalgorithmimplementationinC.
voidtabu_s()
{
unsignedshortbest_sol,cur_sol;
intbest_f,cur_f;
RANDINIT();
tabu_q=createQueue(MAX_ELEMENTS);
/*Getinitialboard*/
cur_sol=best_sol=initBoard();
cur_f=best_f=evaluateBoard(best_sol);
enQueue(tabu_q,best_sol);
while(1){
printf(“Iterationfor%x\n”,cur_sol);
/*Return(byreference)thebestnon-tabuneighbor*/
reviewChildNodes(&cur_sol,&cur_f);
/*Addthecurrentbestsolutiontothetabulist(remove
*theoldestifneeded).
*/
if(!searchQueue(tabu_q,cur_sol)){
if(isFullQueue(tabu_q)){
InformedSearch 79
(void)deQueue(tabu_q);
}
enQueue(tabu_q,cur_sol);
}
/*Savethebestsolutionsofar*/
if(cur_f<=best_f){
best_sol=cur_sol;
best_f=cur_f;
}
/*Solutionfound?*/
if(best_f==0){
emitBoard(best_sol);
break;
}
}
destroyQueue(tabu_q);
return;
}
TabuSearchDemonstration
TheTabusearchapplicationefficientlyfindsthesolutiontothisproblem(a
statespaceof256uniquenodes).ThefirstevaluateBoardistheinitialnode
(seeListing3.16),followedbyfouriterationsofthealgorithm.Notethatwhile
theinitialnodehadacostoftwo,subsequentnodesevaluatedwereworse,
buteventuallyledtothegoal.Tabusearchpermitstheevaluationawayfrom
localminimumtofindtheglobalminimum,asdemonstratedhere.
LISTING3.16: SampleexecutionofTabusearchforthe4-Queensproblem.
evaluateBoard1281=(f2)
Iterationfor1281
evaluateBoard2281=(f2)
evaluateBoard2181=(f3)
evaluateBoard2481=(f3)
evaluateBoard2241=(f2)
evaluateBoard2221=(f3)
evaluateBoard2281=(f2)
evaluateBoard2282=(f3)
Iterationfor2281
evaluateBoard4281=(f1)
80 ArtificialIntelligence
evaluateBoard4181=(f1)
evaluateBoard4481=(f3)
evaluateBoard4281=(f1)
evaluateBoard4241=(f3)
evaluateBoard4282=(f2)
Iterationfor4281
evaluateBoard8281=(f2)
evaluateBoard8181=(f3)
evaluateBoard8481=(f4)
evaluateBoard8241=(f2)
evaluateBoard8221=(f3)
evaluateBoard8281=(f2)
evaluateBoard8282=(f2)
Iterationfor8282
evaluateBoard4282=(f2)
evaluateBoard2282=(f3)
evaluateBoard4182=(f0)
evaluateBoard4482=(f2)
evaluateBoard4282=(f2)
evaluateBoard4142=(f2)
evaluateBoard4181=(f1)
evaluateBoard4184=(f3)
solutionis0x4182
0100
0001
1000
0010
TabuSearchVariants
InordertomakeTabusearchmoreeffectiveforverydifficultsearchproblems,
anumberofmodificationsexist.Thefirstoftheseiscalledintensificationand
essentiallyintensifiesthesearcharoundagivenpoint(suchasthebestknown
solution).Theideaisthatwetakeapromisingnode,andintensifythesearch
aroundthispoint.Thisisimplementedusinganintermediatememory,which
containstheneighbornodestodigintofurther.
Oneissuethatcomesupinlocalsearchalgorithmsisthattheycanget
stuckinlocaloptimums.Tabusearchintroducestheconceptofdiversification
toallowthealgorithmtosearchnodesthathavebeenpreviouslyunexplored
toexpandthespaceofsearch.
InformedSearch 81
When we follow the best solutions, it’s very possible to get stuck in
localoptimumsfordifficultproblems.Anotherinterestingvariantiscalled
constraintrelaxation,whichrelaxestheneighborhoodselectionalgorithmto
acceptlowerqualitynodesinthesearchspace.Thispermitsthealgorithmto
expanditssearchtodescendintolowerqualitysolutionsinsearchofhigher
qualitysolutions.[Gendreau2002]
CONSTRAINTSATISFACTIONPROBLEMS(CSP)
Inmanysearchproblems,we’reinterestednotjustinthegoal,buthowwe
gotfromtheinitialstatetothegoalstate(takeforexample,theeightpuzzle).
Aswe’lllearnlater,planningsystemsrelyonthisaspectofsearchasaplan
isnothingmorethanasequenceofstepstogetfromagivenstatetoagoal
state.Forsomeproblems,we’renotinterestedinthepathtothegoal,but
insteadjustthegoalstate(forexample,theN-Queensproblem).Problems
ofthistypearecalledConstraintSatisfactionProblems(CSP).
Formally,wecanthinkaboutCSPintermsofasetofvariableswitha
domainofpossiblevalues,andasetofconstraintsthatspecifytheallowable
combinationsofvariablevalues.Considerthefollowingsimpleexample.We
wishtofindvaluesforthesetofvariablesxandyeachhavingadomainof
{1-9},suchthatEq3.4(theconstraint)issatisfied.
x+y=x*y (Eq3.4)
Withoutmuchwork,weknowthatassigningthevalueoftwoforbothx
andysatisfiestheconstraintdefinedbytheequation.
GraphColoringasaCSP
One of the more popular CSPs is called Graph Coloring. Given a graph,
andasetofcolors,theproblemistocolorthenodessuchthatanedgedoes
notdirectlyconnecttoanodeofthesamecolor.Considerthemapshown
inFigure3.15.Wecanseeasetofobjectsthatareadjacenttooneanother.
ObjectAisadjacenttoobjectsBandC,whileobjectDisadjacentonlyto
objectB.ThegraphportionofFigure3.14illustratesthegraphofthemap.
Fromthisgraph,wecanseeedgeswhichdefinetheadjacencyoftheobjects
(nodes)ofthegraph.
Nowconsidertheproblemofcoloringthemapgiventheconstraintsthat
eachobjectcanbeonecolorandnocoloredobjectsshouldbeadjacenttoone
another.Canthegraphbecoloredusingthreecolors(red,green,andblue)?
82 ArtificialIntelligence
FIGURE3.15: TheclassicalgraphcoloringCSP.
Using trial and error, we can very easily color this simple graph by
iteratingthroughthenodesofthegraphandassigningacolorsothatour
constraintremainssatisfied.Let’sstartwithnodeA,colorit,andthenstepto
eachremainingnode,coloringaswego.InListing3.17,weseethecoloring
process.Eachnodeisfollowedbyaconstraint,withthecolorfinallychosen
inparentheses.
NodeA–Pickanycolor(Red)
NodeB–PickacolorotherthanRed(Blue)
NodeC–PickacolorotherthanRedandBlue(Green)
NodeD–PickacolorotherthanBlue(Red)
NodeE–PickacolorotherthanGreen(Red)
NodeF–PickacolorotherthanGreenorRed(Blue)
NodeG–PickacolorotherthanRedorBlue(Green)
LISTING3.17: Graphcoloringthroughtrialanderror.
Throughasimpleprocessofelimination,we’vebeenabletocolorthegraph
usingtheconstraintsofpreviouslycolorednodestodeterminethecolorto
paintadjacentnodes(finalresultshowninFigure3.17).
InformedSearch 83
FIGURE3.16: ResultofGraphColoringfromListing3.17.
TIP
The famous Four Color Theorem (to prove that only four colors are
requiredtocoloranyplanarmap)datesbacktothemid1800s.There
were numerous failed attempts to prove the theorem and it remained
conjectureuntil1976,whenAppelandHakencreatedaformalproof.
SchedulingasaCSP
Oneofthemostpracticalapplicationsofconstraintsatisfactionistotheproblem
of scheduling. Numerous types of scheduling problems exist, from airline
timetables to the scheduling of manufacturing processes. The scheduling
problemcomesdowntoallocatingresourcestoactivitiesintimepreservinga
setofconstraints.Forexample,inamanufacturingproblem,aresourcecan
beprocessedthroughavarietyofactivitieseachrequiringaspecificamount
of time. In addition, activities have precedence that must be maintained (a
resourcemustbeprocessedbyActivityAbeforeActivityB).Thegoalofthe
CSPinthisproblemistoidentifyanoptimalscheduleofresourcesthroughthe
activitiessuchthattheendproductcanbeoptimallyproduced.
84 ArtificialIntelligence
CONSTRAINT-SATISFACTIONALGORITHMS
AlargenumberofalgorithmsexisttosolveCSPsfromthesimpleGenerate
andTestalgorithmtoconstraint-propagationandconsistency.We’llexplore
afewoftheavailablealgorithmsthatcanbeused.Notethatsomeofthe
algorithmswe’vediscussedthusfar(suchasdepth-firstsearchinvestigated
inChapter2,andsimulatedannealingandTabusearchfromChapter3)can
beusedeffectivelytosolveCSPs.
GenerateandTest
GenerateandTestisthesimplestofthealgorithmstoidentifyasolutionfor
aCSP.Inthisalgorithm,eachofthepossiblesolutionsisattempted(each
valueenumeratedforeachvariable)andthentestedforthesolution.Since
testingeachcombinationofvariablewithinthedomainofeachvaluecan
beextremelyslowandinefficient,heuristicscanbeappliedtoavoidthose
combinationsthatareoutsideofthesolutionspace.
Backtracking
The backtracking algorithm operates with a simple uninformed search
algorithm,suchasdepth-firstsearch.Ateachnode,avariableisinstantiated
withavalueandtheconstraintviolationsarechecked.Ifthevaluesarelegal,
searchispermittedtocontinue,otherwise,thecurrentbranchisabandoned
andthenextnodefromtheOPENlistisevaluated.
To increase the efficiency of the backtracking algorithm, the most
constrained variable is instantiated first. Take for example, node C from
Figure3.14.Inthiscase,thenodehasfourneighbors(themostofanynode
inthegraph).
TIP
BothGenerateandTestandbacktrackingarecommonsystematicsearch
algorithms,astheysystematicallyassignvaluestovariablesinsearchof
asolution.GenerateandTestishighlyinefficient,whilebacktracking
suffersfromtrashing,ortherepeatedfailuretofindasolutionforthe
samereason(forexample,asinglevariableremainingconstant).
ForwardCheckingandLookAhead
Forwardcheckingissimilartobacktracking,exceptthatwhenaparticular
nodeisevaluatedforthecurrentvariable(calledarcconsistency),onlythose
validnodesareconsideredfortheOPENlisttoevaluateinthefuture.Nodes
thatcanbedetectedasinvalidareimmediatelyignored,resultingonlyinthe
InformedSearch 85
mostpromisingbranchesforfurthersearch.Thiscanminimizeanumberof
nodesgeneratedforsearch,buttendstoinvolvemoreworkwhenevaluating
asinglenode(forforwardcheckingofconstraints).
Avariationonforwardcheckingiscalledlookahead.Inthisalgorithm,
insteadofsimplyevaluatingchildnodesbasedonthecurrentlyinstantiated
value,allsubsequenttobeinstantiatedvariablesareinstantiatedgiventhe
currently instantiated values. This results in very flat search trees. (See
Figure 3.17 for a graphical view of backtracking, forward checking, and
lookahead.)
TIP
Forwardcheckingiscommonlymoreeffectivethanbacktrackingasthe
numberofnodestobeevaluatedisreduced.
FIGURE3.17: Comparisonofbacktracking,forwardchecking,andlookaheadconstraint
propagationalgorithms.
86 ArtificialIntelligence
Min-ConflictsSearch
AninterestingheuristicrepairalgorithmistheMin-Conflictsheuristic.This
algorithmbeginsbychoosingthevariablethatviolatesthefewestnumber
ofconstraintsandthensystematicallyimprovesthesolutionthroughusing
analgorithmsuchashillclimbingorA*withtheheuristicfunctionh(n)
definedasthetotalnumberofconstraintsviolated.
CHAPTERSUMMARY
Informedsearchmethods,unliketheuninformed(orblind)methods,usea
heuristictodeterminethequalityofanystateinthesearchspace.Thisallows
thealgorithmtoguidethesearchandchoosethenextnodetoexpand.In
thischapter,anumberofinformedsearchmethodswereexplored,including
best-firstsearch,A*search,hill-climbingsearch,simulatedannealing,Tabu
search,andfinallyalgorithmsforconstraintsatisfaction.Understandingthe
informedmethodsofsearchisimportantbecausemuchofproblemsolving
inAIcanberefinedtosearchmethods.Thekeyischoosingthealgorithm
thatisbestsuitedtotheparticularproblemathand.
ALGORITHMSSUMMARY
Table3.1: Summaryoftheuninformedalgorithmsandtheircharacteristics.
Algorithm Time Space Optimal Complete Derivative
Best-FirstSearch O(bm) O(bm) No Yes BFS/UCS
A*Search O(2N) O(bd) Yes Yes BestFS
IDA* O(2N) O(d) Yes Yes A*
SMA*
SimAnneal - - No No
Tabu - - No No
b,branchingfactor
d,treedepthofthesolution
m,treedepth
REFERENCES
[Archer 1999] Archer, A.F. “A Modern Treatment of the 15 Puzzle,”
AmericanMath.Monthly106,793-799,1999.
InformedSearch 87
[KGong2005]Gong,Kevin.“AMathematicalAnalysisoftheSixteenPuzzle.”
Lastupdated12/2005.
Availableonlineathttp://www.kevingong.com/Math/SixteenPuzzle.html
[Gendreau 2002] Gendreau, Michel. “An Introduction to Tabu Search,”
UniversitedeMontreal.July2002.Availableonlineathttp://www.ifi.uio.
no/infheur/Bakgrunn/Intro_to_TS_Gendreau.htm
RESOURCES
[Glover1990]Glover,Fred.“TabuSearch:ATutorial,”Interfaces,20(4):
74-94,1990.
EXERCISES
1. Best-first search uses a combined heuristic to choose the best path to
followinthestatespace.Definethetwoheuristicsused(h(n)andg(n)).
2. Best-firstsearchusesbothanOPENlistandaCLOSEDlist.Describe
thepurposeofeachforthebest-firstalgorithm.
3. Describethedifferencesbetweenbest-firstsearchandgreedybest-first
search.
4. Describethedifferencesbetweenbest-firstsearchandbeamsearch.
5. Whataretheadvantagesofbeamsearchoverbest-firstsearch?
6. A* search uses a combined heurstic to select the best path to follow
throughthestatespacetowardthegoal.Definethetwoheuristicsused
(h(n)andg(n)).
7. Briefly describe A* search and the problems to which it can be
applied.
8. Whatismeantbyanadmissibleheuristic?
9. How do the alpha and beta parameters tune the heuristics for A*
search?
10. Briefly explain the difference between A* search and SMA*. What
advantagedoesSMAhaveoverA*?
11. Hill climbing is a standard iterative improvement algorithm similar
to greedy best-first search. What are the primary problems with hill
climbing?
12. DescribeSimulatedannealingandifitcombinesiterativeimprovement
withstochasticsearch.
13. Describethealgorithmandhowitdiffersfromrandomsearch.
88 ArtificialIntelligence
14. WhatisthepurposeoftheMonteCarlostepinthesimulatedannealing
algorithm?
15. BrieflydescribetheTabusearchalgorithm.
16. TheTabulistcanbesizedfortheproblemathand.Whateffectdoes
changingthesizeoftheTabulisthaveonthesearchalgorithm?
17. Describe the intensification and diversification modifications of Tabu
search.
18. Describetheessenceofaconstraintsatisfactionproblem.
19. What are some of the major applications of constraint satisfaction
search?
20. Compare and contrast the CSP algorithms of backtracking, forward
checking,andlookahead.
Chapter 4 AIANDGAMES
A
Ihasalonghistoryinthegenreofgames.Fromthefirstintelligent
Checkersplayer,totheteamAIdevelopedforfirst-person-shooters,
AIisatthecore.ThischapterwillcoveraspectsofgameAIfrom
traditionalgameplayingofCheckers,Chess,Othello,andGotomorerecent
video games including first-person-shooters, strategy games, and others.
We’llintroducetheminimaxalgorithmandalpha-betapruning,whichare
centraltotraditionaltwo-playergames.We’llthenexploreotheralgorithms
thatcanbefoundinmoderngamesystems.
TWO-PLAYERGAMES
Two-player games are games in which two players compete against each
other.Thesearealsoknownaszero-sumgames.Thegoaltheninplayinga
two-playergameischoosingamovethatmaximizesthescoreoftheplayer
and/orminimizesthescoreofthecompetingplayer.
NOTE A zero-sum game is one in which the gain of one player is balanced
exactlybythelossoftheotherplayer.Zerosumgameshavebeenstudied
extensively by John von Neumann and Oskar Morgenstern and then
90 ArtificialIntelligence
laterbyJohnNash.Chessisanexampleofazero-sumgame.Incontrast,
non-zero-sum games are those in which two players attempt to get
rewardsfromabankerbycooperatingorbetrayingtheotherplayer.The
prisoner’s dilemma is a classic example of a non-zero-sum game. Both
zeroandnon-zero-sumgamesaretypesofgameswithinthefieldofgame
theory.GametheoryhasarangeofusesfromparlorgamessuchasPoker,
tothestudyofeconomicphenomenafromauctionstosocialnetworks.
Considerthetwo-playergameTic-Tac-Toe.Playersalternatemoves,and
aseachmoveismade,thepossiblemovesareconstrained(seethepartial
Tic-Tac-ToegametreeinFigure4.1).Inthissimplegame,amovecanbe
selectedbasedonthemoveleadingtoawinbytraversingallmovesthatare
constrainedbythismove.Also,bytraversingthetreeforagivenmove,we
canchoosethemovethatleadstothewinintheshallowestdepth(minimal
numberofmoves).
Tic-Tac-Toe is an interesting case because the maximum number of
movesistinywhencomparedtomorecomplexgamessuchasCheckersor
FIGURE4.1: Partialgametreeforthetwo-playergameofTic-Tac-Toe.
AIandGames 91
Chess.Tic-Tac-Toeisalsoopentonumerousoptimizations.Consider,for
example,thefirstXmoveinFigure4.1.Iftheboardisrotated,onlythree
uniquemovesareactuallypossible.Withoutoptimization,thereexist362,880
nodeswithinthecompletegametree.
NOTE Two-player games are useful as a test-bed for validating competitive
algorithms.Alsoofinterestareone-playergames(alsoknownaspuzzles).
Examplesofusefulone-playergamesincludethen-diskTowersofHanoi
puzzleandtheN-puzzle(seeChapters2and3).
Ateachnodeinthetree(apossiblemove)avaluedefiningthegoodness
ofthemovetowardtheplayerwinningthegamecanbeprovided.Soata
givennode,thechildnodes(possiblemovesfromthisstateinthegame)
eachhaveanattributedefiningtherelativegoodnessofthemove.It’san
easytaskthentochoosethebestmovegiventhecurrentstate.Butgiven
thealternatingnatureoftwo-playergames,thenextplayermakesamove
that benefits himself (and in zero-sum games, results in a deficit for the
alternateplayer).
Astaticevaluationfunction(thatmeasurethegoodnessofamove)is
usedtodeterminethevalueofagivenmovefromagivengamestate.The
evaluationfunctionidentifiestherelativevalueofasuccessormovefromthe
listofpossiblemovesasameasureofthemovequalitytowardwinningthe
game.ConsiderthepartialgametreeinFigure4.2.
Thestaticevaluationfunctionisdefinedasthenumberofpossiblewin
positionsnotblockedbytheopponentminusthenumberofpossiblewin
positions(row,column,anddiagonal)fortheopponentnotblockedbythe
currentplayer:
FIGURE4.2: Tic-Tac-Toegametreewithstaticevaluationfunction.
92 ArtificialIntelligence
THEMINIMAXALGORITHM
Insimplegames,algorithmsexistthatcansearchthegametreestodetermine
thebestmovetomakefromthecurrentstate.Themostwellknowniscalled
the Minimax algorithm. The minimax algorithm is a useful method for
simpletwo-playergames.Itisamethodforselectingthebestmovegiven
analternatinggamewhereeachplayeropposestheotherworkingtoward
amutuallyexclusivegoal.Eachplayerknowsthemovesthatarepossible
givenacurrentgamestate,soforeachmove,allsubsequentmovescanbe
discovered.
Ateachnodeinthetree(possiblemove)avaluedefiningthegoodnessof
themovetowardtheplayerwinningthegamecanbeprovided.Soatagiven
node,thechildnodes(possiblemovesfromthisstateinthegame)eachhave
anattributedefiningtherelativegoodnessofthemove.It’saneasytaskthen
tochoosethebestmovegiventhecurrentstate.Butgiventhealternating
natureoftwo-playergames,thenextplayermakesamovethatbenefitsthem
(andinzero-sumgames,resultsinadeficitforthealternateplayer).
NOTE Theplyofanodeisdefinedasthenumberofmovesneededtoreachthe
currentstate(gameconfiguration).Theplyofagametreeisthenthe
maximumofthepliesofallnodes.
Minimaxcanuseoneoftwobasicstrategies.Inthefirst,theentiregame
treeissearchedtotheleafnodes(end-games),andinthesecond,thetreeis
searchedonlytoapredefineddepthandthenevaluated.Let’snowexplore
theminimaxalgorithmingreaterdetail.
AIandGames 93
NOTE Whenweemployastrategytorestrictthesearchdepthtoamaximum
numberofnodes(donotsearchbeyondNlevelsofthetree),thelook
aheadisrestrictedandwesufferfromwhatiscalledthehorizoneffect.
Whenwecan’tseebeyondthehorizon,itbecomeseasiertomakeamove
thatlooksgoodnow,butleadstoproblemslateraswemovefurtherinto
thissubtree.
Minimaxisadepth-firstsearchalgorithmthatmaintainsaminimumor
amaximumvalueforsuccessornodesateachnodethathaschildren.Upon
reachingaleafnode(orthemaxdepthsupported),thevalueofthenode
is calculated using an evaluation (or utility) function. Upon calculating a
node’s utility, we propagate these values up to the parent node based on
whosemoveistotakeplace.Forourmove,we’llusethemaximumvalueas
ourdeterminerforthebestmovetomake.Forouropponent,theminimum
valueisused.Ateachlayerofthetree,thechildnodesareaisscannedand
dependingonwhosemoveistocome,themaximumvalueiskept(inthecase
ofourmove),ortheminimumvalueiskept(inthecaseoftheopponent’s
move).Sincethesevaluesarepropagatedupinanalternatingfashion,we
maximize the minimum, or minimize the maximum. In other words, we
assumethateachplayermakesthemovenextthatbenefitsthemthemost.
ThebasicalgorithmforminimaxisshowninListing4.1.
LISTING4.1: Basicalgorithmforminimaxgametreesearch.
minimax(player,board)
ifgame_won(player,board)returnwin
foreachsuccessorboard
if(player==X)returnmaximumofsuccessorboards
if(player==O)returnminimumofsuccessorboards
end
end
To demonstrate this approach, Figure 4.3 shows the end-game for a
particular Tic-Tac-Toe board configuration. Both X and O have played
threeturns,andnowit’sX’sturn.Wetraversethistreeindepth-firstorder,
anduponreachingeitherawin/loseordrawposition,wesetthescorefor
theboard.We’lluseasimplerepresentationhere,-1representingaloss,0
foradraw,and1forawin.Theboardswithboldlinesdefinethewin/loss/
drawboardswherethescoreisevaluated.Whenallleafnodeshavebeen
evaluated,thenodevaluescanbepropagatedupbasedonthecurrentplayer.
94 ArtificialIntelligence
FIGURE4.3: End-gametreeforagameofTic-Tac-Toe.
Atlayer2inthegametree,it’sO’sturn,soweminimizethechildrenand
scoretheparentwiththesmallestvalue.Atthefarleftportionofthegame
tree,thevalues0and1arepresent,so0iskept(theminimum)andstored
intheparent.Atlayer1inthetree,we’relookingatthemaximum,soout
ofnodescores0,-1,and-1,wekeep0andstorethisattheparent(theroot
nodeofourgametree).
Withthescoreshavingbeenpropagatedtotheroot,wecannowmake
thebestmovepossible.Sinceit’sourmove,we’remaximizing,sowelookfor
thenodewiththelargestscore(theleft-mostnodewithavalueof0),and
wetakethisposition.Ouropponent(whoisminimizing)thenchoosesthe
minimumnodevalue(left-mostnodeintreedepth2).Thisleavesuswith
ourfinalmove,resultinginadraw.
Note that in a game where perfect information is available to each
player,andnomistakesaremade,theend-resultwillalwaysbeadraw.We’ll
buildaprogramtoplayTic-Tac-Toetoillustratehowthisalgorithmcanbe
constructed.Likeanytreealgorithm,itcanbebuiltsimplyandefficiently
usingrecursion.
TIP
Analternativetobuildingtheentiresearchtreeistoreducethedepth
ofthesearch,whichimpliesthatwemaynotencounterleafnodes.This
AIandGames 95
isalsoknownasanimperfectinformationgameandcanresultinsub-
optimalstrategiesofplay.Theadvantageofreducingthesearchtreeis
thatgameplaycanoccurmuchmorequicklyandminimaxcanbeused
forgamesofhighercomplexity(suchasChessorCheckers).
Recursively searching an entire game tree can be a time (and space)
consumingprocess.Thismeansthatminimaxcanbeusedonsimplegames
suchasTic-Tac-Toe,butgamessuchasChessarefartoocomplextobuild
anentiresearchtree.ThenumberofboardconfigurationsforTic-Tac-Toe
is around 24,683. Chess is estimated to have on the order of 10100 board
configurations–atrulymassivenumber.
MinimaxandTic-Tac-Toe
Let’s now look at an implementation of the minimax algorithm that uses
recursionbetweentwofunctions.We’llfirstexploretherepresentationof
theproblem,whichcanbeveryimportanttobeabletostorelargenumbers
ofboardconfigurationsforgametreesearch.
TheTic-Tac-Toeboardrequiresninepositionswhereeachpositioncan
takeoneofthreevalues(an‘X,’‘O,’orempty).Bitwise,wecanrepresentour
valuesintwobits,whichgivesusfouruniqueidentifies.Withninepositions
ontheTic-Tac-Toeboard,requiringtwobitseach,wecanusea32-bitvalue
torepresenttheentireboardwithnumerousbitsleftover(seeFigure4.4
fortheTic-Tac-Toeboardrepresentation).
FIGURE4.4: RepresentingaTic-Tac-Toeboardinapacked32-bitvalue.
96 ArtificialIntelligence
TIP
FromFigure4.4,wecanseethattheTic-Tac-Toeboardfitseasilywithin
a 32-bit type, including room to spare. Using a 32-bit value is also
importantforefficiencyasmostmoderncomputersystemsusethistype
internallyforatomicmemoryaccessregisterstorage.
Theminimaxalgorithmiseasilyimplementedasarecursivealgorithm.Forthis
implementation,we’llusetwofunctionsthatrecursivelycalleachother.Each
functionplaysthegameinthecontextofaspecificplayer(seeListing4.2).
LISTING4.2: Recursivealgorithmforminimaxgametreesearch.
play_O(board)
ifend_game(board)returneval(board)
foreachemptyslotinboard
new_board=board
markemptycellwithOinnew_board
value=play_X(new_board)
ifvalue<min
value=min
end
returnvalue
end
play_X(board)
ifend_game(board)returneval(board)
foreachemptyslotinboard
new_board=board
markemptycellwithXinnew_board
value=play_O(new_board)
ifvalue>max
value=max
end
end
returnvalue
end
A call to play_X begins the construction of the game tree with a specific
board.Eachfunctionbeginswithacalltoend-game,whichdeterminesif
thegamehasended(nocellsavailabletoplaceapiece,orthegamehasbeen
won).Iftheend-gamehasbeendetected,theboardisevaluatedandascore
AIandGames 97
returned.We’lluseaverysimplescoringmethodfortheTic-Tac-Toeboard.
IfthegamehasbeenwonbytheXplayer,a‘1’isreturned.Ifthegamehas
beenwonbytheOplayer,a‘-1’isreturned.Finally,ifadrawresults,the
value0isreturned.
Iftheend-gamehasnotoccurred,thecurrentboardisiterated,looking
foremptylocations.Foreachemptylocation,thefunctionplacesitsvalue(in
anewboard,toavoidcorruptingthecurrentboardthatwillbeusedmultiple
times).Thenewboardisthenpassedtothealternatefunctiontocontinue
FIGURE4.5: Demonstratingthetworecursivefunctionsinapartialgametree.
98 ArtificialIntelligence
buildingthegametreefortheopposingplayer.Astheplay_functionreturns
itsvalue,it’scomparedtothecurrentminormaxvaluedependingonthe
currentroleoftheplayer(Xismaximizing,Oisminimizing).
Wecouldhaveimplementedthisusingonefunction,buttwofunctions
makes it a bit easier to understand as we know when we’re in play_X,
that we’re maximizing and when we’re in play_Y, we’re minimizing. The
relationshipsofthetwofunctionsandtheirrolesareshowninFigure4.5.
MinimaxImplementationforTic-Tac-Toe
AsshowninListing4.3,we’llusetwofunctionstoimplementtherecursive
game-treesearch.We’lldiscussthesetwofunctionsinparticular,butnotthe
entireimplementation,whichcanbefoundontheCD-ROM.
HE C
NT
D
O
ThesourcecodefortheminimaxTic-Tac-Toegameplayercanbefound
ontheCD-ROMat./software/ch4/minimax.c.
Oncethehumanplayer’smovehasbeenacceptedandplacedonthe
board,acallismadetoevaluateComputerMove(withthenewgameboard,
and a depth of 0, since we’re at the root of the tree). We’ll discuss the
implementationforbothfunctionsnow,astheyfundamentallyarethesame,
exceptfortheminimumandmaximumchecking.
Upon entry to the function, we immediately check for a win by the
opposingplayer(sincethiscallisdonewhentheopposingplayerhasmade
amove).Ifthepriorplayerexecutedaplaythatwonthegame,thescoreis
returned(MIN_INFINITYforevaluateComputerMove,MAX_INFINITY
forevaluateHumanMove).Thismayseemopposite,butwe’remaximizing
for the computer player, and we check for this in the opposing function,
evaluateHumanMove. We then walk through all available open board
positions,placeourtokeninthespace,andthencalltheopposingfunction
for evaluation. Upon return, we store the min or max, depending on the
function’srole(maxforcomputerplayerthatismaximizing,minforhuman
playerthatisminimizing).Ifnoemptyspaceswerefound,thenbydefault
thegameisadrawandwereturnthisscore.
OneimportantpointtonoteinevaluateComputerMoveisthataswe
storeanewmaxvalue,whichidentifiesthecurrentbestmove,wecheck
the depth of this particular board configuration. As computer_move is a
global(identifyingthebestmovesofar),weonlywanttostorethisforthe
boardconfigurationcontainingourpossiblenextmove,noteveryboardin
thetreetothedepthsoftheultimatesolution.Thiscanbeidentifiedasthe
treedepth,whichwillbe0.
AIandGames 99
LISTING4.3: RecursivealgorithmImplementationforminimaxgametreesearch.
shortevaluateHumanMove(unsignedintboard,intdepth)
{
inti,value;
unsignedintnew_board;
shortmin=MAX_INFINITY+1;
shortevaluateComputerMove(unsignedint,int);
/*Thecomputer(max)justmadeamove,soweevaluatethatmovehere
*/
if(checkPlayerWin(O_PLAYER,board))returnMAX_INFINITY;
for(i=0;i<MAX_CHILD_NODES;i++){
if(getCell(i,board)==EMPTY){
new_board=board;
putCell(X_PLAYER,i,&new_board);
value=evaluateComputerMove(new_board,depth+1);
if(value<=min){
min=value;
}
}
}
/*Nomoveispossible--draw*/
if(min==MAX_INFINITY+1){
returnDRAW;
}
returnmin;
}
intcomputer_move;
shortevaluateComputerMove(unsignedintboard,intdepth)
{
inti,value;
unsignedintnew_board;
shortmax=MIN_INFINITY-1;
/*Thehuman(min)justmadeamove,soweevaluatethatmovehere*/
if(checkPlayerWin(X_PLAYER,board))returnMIN_INFINITY;
for(i=0;i<MAX_CHILD_NODES;i++){
if(getCell(i,board)==EMPTY){
new_board=board;
putCell(O_PLAYER,i,&new_board);
value=evaluateHumanMove(new_board,depth+1);
100 ArtificialIntelligence
if(value>=max){
max=value;
if(depth==0)computer_move=i;
}
}
}
/*Nomoveispossible--draw*/
if(max==MIN_INFINITY-1){
returnDRAW;
}
returnmax;
}
FIGURE4.6: AsamplegameofTic-Tac-Toeshowingthenumberofboardsanalyzedwithminimax.
AIandGames 101
Minimaxisagreatalgorithmforsmalldepthandbranchingfactors,butit
canconsumequiteabitofstorageformorecomplexproblems.Figure4.6
showsasamplegameplayedusingtheminimaxalgorithm.Thecomputer
playsfourmoves,chosenbyminimax.Foreachofthosefourmoves,atotal
of60,810configurationsareevaluated.
TIP
Whilethereare39uniqueTic-Tac-Toeboards,thereareactuallymany
fewervalidboardsasanearlywin(priortofillinguptheboard)makes
allsuccessorboardsinvalid.
What’sneededisawaytoavoidsearchingbranchesofthegametreethat
areobviouslybad.Onewaytoachievethisisthroughapruningalgorithm
calledAlpha-Betathat’susedinconjunctionwiththeminimaxalgorithm.
MinimaxwithAlpha-BetaPruning
Alpha-beta pruning is a simple algorithm that minimizes the game-tree
searchformovesthatareobviouslybad.ConsideraTic-Tac-Toeboardwhere
theopposingplayerwouldwinonthenextmove.Ratherthangoingonthe
offensivewithanothermove,thebestmoveistheonethatdefendstheboard
fromawinonthenextmove.
Chessisaclassicexampleofthisproblem.Considermovingthekingso
thatit’sinimmediatedanger.It’saninvalidmove,andthereforethegame
treethatfollowedthismovecouldbepruned(notevaluated)toreducethe
searchspace.
Thisisthebasicideaofalpha-betapruning.Identifymovesthatarenot
beneficial,andremovethemfromthegametree.Thehigherinthegametree
thatbranchesareprunedthegreatereffectinminimizingthesearchspaceof
thetree.Let’snowexplorethealgorithmbehindalpha-betapruning.
Duringthedepth-firstsearchofthegametree,wecalculateandmaintain
twovariablescalledalphaandbeta.Thealphavariabledefinesthebestmove
thatcanbemadetomaximize(ourbestmove)andthebetavariabledefines
thebestmovethatcanbemadetominimize(theopposingbestmove).While
wetraversethegametree,ifalphaisevergreaterthanorequaltobeta,then
theopponent’smoveforcesusintoaworseposition(thanourcurrentbest
move).Inthiscase,weavoidevaluatingthisbranchanyfurther.
Let’s look at an example game tree to demonstrate the operation of
alpha-betapruning.We’llusethesimplegametreeshowninFigure4.7.The
algorithmbeginsbysettingalphato-INFINITYandbetato+INFINITY,
and then makes a call to the minimizing routine. The minimizer iterates
through the successor nodes and finds the smallest utility of three. This
102 ArtificialIntelligence
FIGURE4.7: Initialgametreeforalpha-betapruning.
FIGURE4.8: Prunedgametreeattheminimizerlevel.
becomesthebetavariableintheminimizer,butisreturnedtothemaximizer
functiontobecomethealphavariable.
Thecurrentalphaandbetavariablesarethenpassedtotheminimizer
againforcheckoftheright-handsubtree(seeFigure4.8).Oncethefirst
nodeisevaluated(fromlefttoright),wefinditsutilitytobetwo.Sincethis
valueislessthanourbeta(currently+INFINITY),betabecomestwo.We
AIandGames 103
thenchecktoseeifalpha>=beta.Itis,andthereforewecanconcludethe
remainingnodeswillbeminimizedtotwoorless(sincetheparentnode
isaminimizer),whichislessthantheutilityoftheleft-handsubtree,and
availableforpruning.
Theideabehindalpha-betapruningisthatifwe’reevaluatingmoves,and
findamovethat’sworsethanthemovewe’vediscoveredsofar,weignoreit
andmoveon(don’tdiganydeeperintothatsubtree).
HE C
NT
D
O
Thesourcecodeforminimaxwithalpha-betapruningcanbefoundon
theCD-ROMat./software/ch4/alphabeta.c.
Theimplementationforalpha-betapruningisquitesimpleastheonly
necessityistomaintainthealphaandbetavariablesanddeterminewhen
pruningshouldoccur.Listing4.4providesthealpha-betaimplementation,
asamendedfromouroriginalminimaxfunctionsfromListing4.3.
Listing4.4: Updatedminimaximplementationforalpha-betapruning.
shortevaluateHumanMove(unsignedintboard,intdepth,
intalpha,intbeta)
{
inti,value;
unsignedintnew_board;
shortmin=MAX_INFINITY+1;
shortevaluateComputerMove(unsignedint,int,int,int);
/*Thecomputer(max)justmadeamove,soweevaluatethatmovehere
*/
if(checkPlayerWin(O_PLAYER,board))returnMAX_INFINITY;
for(i=0;i<MAX_CHILD_NODES;i++){
if(getCell(i,board)==EMPTY){
new_board=board;
putCell(X_PLAYER,i,&new_board);
value=evaluateComputerMove(new_board,depth+1,alpha,beta);
if(value<min){
min=value;
}
if(value<beta)beta=value;
/*Prunethissubtreebynotcheckinganyfurthersuccessors*/
if(alpha>=beta)returnbeta;
}
104 ArtificialIntelligence
}
/*Nomoveispossible--draw*/
if(min==MAX_INFINITY+1){
returnDRAW;
}
returnmin;
}
shortevaluateComputerMove(unsignedintboard,intdepth,
intalpha,intbeta)
{
inti,value;
unsignedintnew_board;
shortmax=MIN_INFINITY-1;
/*Thehuman(min)justmadeamove,soweevaluatethatmovehere*/
if(checkPlayerWin(X_PLAYER,board))returnMIN_INFINITY;
for(i=0;i<MAX_CHILD_NODES;i++){
if(getCell(i,board)==EMPTY){
new_board=board;
putCell(O_PLAYER,i,&new_board);
value=evaluateHumanMove(new_board,depth+1,alpha,beta);
if(value>max){
max=value;
if(depth==0)computer_move=i;
}
if(value>alpha)alpha=value;
/*Prunethissubtreebynotcheckinganyfurthersuccessors*/
if(alpha>=beta)returnalpha;
}
}
/*Nomoveispossible--draw*/
if(max==MIN_INFINITY-1){
returnDRAW;
}
returnmax;
}
FIGURE4.9: AsamplegameofTic-Tac-Toeshowingthenumberofboardsanalyzedwith
alpha-betapruning.
successor nodes are pruned (by simply returning at this point, with the
currentalphavalue).
So how does alpha-beta pruning help to optimize the basic minimax
algorithm? Let’s review a sample game played using alpha-beta, and the
numberofboardsevaluatedateachstep(seeFigure4.9).Thecomputer’s
firstmovewithalpha-betascannedatotalof2,338Tic-Tac-Toeboards.Recall
fromFigure4.6thatthefirstmovefortheminimaxalgorithmscanned59,705
Tic-Tac-Toeboards.Quiteadifference,makingitpossibletodogame-tree
searchformorecomplexgames,suchasChessorCheckers.
106 ArtificialIntelligence
Whenalpha-betapruningisused,thenumberofnodesonaveragethat
needtobescannedisO(bd/2).Thiscomparedtominimax,whichwillscan
onaverageO(bd)nodes.Thebranchingfactorcanalsobereducedinthe
best case from b, for minimax, to b1/2 for alpha-beta pruning. When the
effective branching factor of a game tree is reduced, the possibilities for
search can extend deeper into a game tree or make more complex game
treessearchable.
TIP
TheaveragebranchingfactorforChessis38,whiletheaveragebranching
factorforCheckersis8(fornon-capturepositions).[Lu1993]
CLASSICALGAMEAI
Let’snowexploretheapplicationofAIandsearchthat’susedinclassical
gamessuchasChess,Go,Backgammon,andevenBridgeandPoker.The
application of AI to games is one of search, knowledge, and heuristics.
Understanding AI and games is important because it provides a sandbox
totesttheefficacyofsearchalgorithms.It’salsoameanstounderstandthe
complexityofgames.Forexample,whilebuildingaworthyAIalgorithmfor
thegameofGoiselusive,Bridge-playingAIalgorithmsregularlywinatthe
highestlevelofBridgechampionships.
Inthissection,we’llreviewsomeofthemorepopulargamesthathave
founduseofAIandthetechnologiestheyemploy.Aswe’llsoondiscover,
minimaxwithalpha-betapruningisapopulartechniqueamongintelligent
game-playingprograms,butheuristicsalsoplayabigpartinbuildingfaster
andmoreefficientplayers.
Checkers
We’llbeginourexplorationofAIinclassicalgameswithaquickreviewof
AI’sapplicationinCheckers.ArthurSamuel,anearlypioneerinAIand
machinelearning,didsomeoftheearliestworkingivingcomputersthe
abilitytolearnfromexperience.Inadditiontoprogrammingacomputer
to play Checkers on the IBM 701 computer, he pioneered the idea of
letting the program learn by competing against itself. The resulting
Checkers program competed and defeated the fourth ranked player in
thenation.[Samuel1959]ArthurSamuel’sworkonthecheckersprogram
wassoimportantinnon-numericalcomputation,thatheinfluencedthe
designersofIBM’searlycomputerstoincludenewlogicalinstructions.
[McCarthy1990]
AIandGames 107
NOTE TheCheckersprogrambuiltattheUniversityofAlberta,Canada,isthe
firstprogramtowinvsahumaninthemachineworldchampionship
competition.[Chinnok]
Samuel’sworkremainsofinterestintheworldofCheckersAI,butmore
recently,neuralnetworkshavebeenemployed.IntheAnacondaCheckers
player,enoughknowledgewasprovidedforanunderstandingofthelegal
movesinthegame,andthenthe“player”wasadaptedusinganevolutionary
strategy(geneticalgorithmsevolvingtheweightsoftheneuralnetwork).The
resultwasaCheckersplayerthatbeatacommerciallyavailableCheckers
program6-0.[Chellapilla,Fogel2000].
TIP
ThetopicofgeneticallyevolvedneuralnetworksiscoveredinChapter8.
CheckersismuchsimplerthanChessinboththetypesofpiecesatplay
(twoforCheckers,sixforChess)andtherulesthatareusedduringplay.
Further, in Checkers, each player has half the board to play (32 squares
insteadofthefull64).ButwhilesimplerthanChess,Checkersiscomplex
initsownright.Let’snowreviewhowChecker’sAIrepresentstheboard
andplaysanintelligentgame.
Checker-BoardRepresentation
ThedatastructurerepresentationofaCheckersboardisimportantbecause
itisastrongdeterminerintheefficiencyofthesearchandevaluationaspects
oftheprogram(aswellastheamountofoverallmemoryusedbythesearch
gametree,openingbook,andendgamedatabase).
Acommonrepresentationisasimple8by8matrixthatcontainsoneof
sixvalues(empty,red,black,red-king,andblack-king).Anoptimizationof
the8by8isthe10by10model,whichincludesaborderofonecellaround
theentireboard(withthesecellscontainingthestaticvalueoffboard).This
simplifiesthemovegeneratorinidentifyingillegalmoves.
Otherrepresentationsexisttopacktheboardintoasmallerspace,but
commonly rely on a particular CPU architecture and the instructions to
interrogateandmanipulatetheindividualbits.
TechniquesEmployedbyCheckersPrograms
CheckersprogramshavesomesimilaritieswithothertypesofAIplayers
suchasChessinthattheyhaveuniqueopeningandendinggamephases.
For this reason, we’ll see similarities between Checkers and Chess
programs.
108 ArtificialIntelligence
OpeningBooks
SinceCheckershasbeenwidelystudiedfromatheoreticalperspective,there
isalargeamountofworkininvestigatingopeningmovesthatcanleadto
beneficial board configurations. Commonly, a database of opening moves
foragivenstrategyisinterrogatedtoselectthefirstsetofmovesbeforethe
evaluationandsearchfunctionsareusedtodrivepiecemovement.
StaticEvaluationFunction
The board evaluator is commonly a weighted numeric feature vector.
Particularfeaturesincludethenumberofredpieces,numberofblackpieces,
disparityofpieces(numberofredpiecesminusthenumberofblackpieces),
thenumberofredkings,etc.Theweightsarecommonlytunedbyahuman
expert,butcanalsobetunedautomatically(meaningtheevaluationfunction
isnolongerstatic).
NOTE Chinookincludes22weightedfeaturesthatdefinetheevaluationfunction.
Thefeatureweightsaretypicallyhand-tuned(insomecasesforaspecific
competition).
SearchAlgorithm
Aswithmostclassicalgames,minimaxwithalpha-betapruningisusedasthe
meanstosearchthegametree.Checkershasanaveragebranchingfactorof
10,whichislessthanChess,butlargeenoughtomakesearchingtheentire
treeinfeasible.
Whilealpha-betapruningdoesagreatjobofminimizingthesearchtree,
thereareothertechniquesthatcanbeappliedheuristicallytofurtherreduce
thegametree’ssearchspace.
Anumberofsearchenhancementsexistsuchaswindowing,wherethe
alphaandbetaboundsareawindowofthepreviouslycomputedvaluesand
canresultinfastersearchingofthegametree.Othermodificationsinclude
PrincipalVariationSearch(PVS),whichapplieswindowingtoeachnodein
thegametree.
MoveHistory
Tospeedupthesearch,ahash-tableisconstructedduringplaythatmaintains
boardconfigurationsandtheircharacteristics.Sinceparticularboardscan
showupfrequentlyduringagame,theycanbestored(withtheirassociated
alphaandbetaparametersfromtheminimaxtree)tominimizesearching
the particular subtree. The hashtable permits a fast lookup of the board
configuration to see if it has been seen before. If so, its alpha and beta
parametersarereturned,whicharethenusedbythealpha-betasearch.
AIandGames 109
TIP
AcommonhashfunctionusedinCheckershash-tablesiscalledZobrist
hashing. This hash function creates an XOR of the checker board,
which results in uniquely different hash results for different board
configurations (which is necessary for fast hash store and lookup to
ensureahit).
End-gameDatabase
An end-game database contains a relation between board configurations
whereafewpiecesremain,andthestrategythatwillleadtoawin.These
(typicallycompressed)databasesencodetheboardinacompactwayand
thenuseanindexfunctiontoquicklyidentifythestrategytobeused.
NOTE The Chinook end-game database includes all eight piece board
configurations (almost 444 billion configurations). The database
is compressed using a run-length encoding of the end-game
representation.
Chess
Chessisaninterestingtest-bedforintelligentapplicationsbecausethegameis
richandcomplexwithamassivesearchspace.Forthisreason,traditionalsearch
algorithmsarewoefullyinadequatetoplayareasonablyintelligentgame.
Chess is a game of perfect information, unlike games such as Poker,
wherenotalloftheinformationisknowntoeachplayer.Bothplayersseethe
sameChessboardandknowallmovesthatarepossibleforbothplayers.
EarlyChesscomputersoperatedinabrute-forceway,asthespeedofthe
computerwasviewedasitsgreatestasset.Understandingthecomplexityof
thegameofChess,it’snowknownthatmoreisrequired(butcomputational
speeddoesn’thurt).
NOTE EarlyinthedaysofChessautomation,limiteddepthsearchminimaxwas
usedtodeterminethebestmovetomake.WithlimitedCPUpowerand
memory,minimaxoperatedinveryshallowtrees,sothehorizoneffect
minimized the intelligence of the moves. With the advent of minimax
variations, you’ll still find minimax as the core algorithm in modern
Chesssystemstoday.
Chessprogramsarecommonlymadeupofthreemodules.Thefirstis
amovegeneratorwhichanalyzesthecurrentboardandidentifiesthelegal
moves that can be made. The second module is the evaluation function,
110 ArtificialIntelligence
whichcomputesarelativeutilityforagivenboardconfiguration(howgood
agivenboardiscomparedtoothersforagivenboardconfiguration).The
finalmoduleisthesearchalgorithmwhichmustefficientlyiteratethrough
theavailableboardconfigurations,givenamove,anddecidewhichpathto
takethroughthetreetoselectthenextmovetomake.
TIP
Inthe1990sIBM’sDeepBluesuccessfullydefeatedGaryKasparov,who
atthetimewastheworldChesschampion.
Chess-BoardRepresentation
A simple representation for a Chess board is an 8 by 8 two Dimensional
array with each cell containing an identifier representing the state of the
cell.Forexample,0wouldrepresentanemptycell,1forawhitepawn,-1
forablackpawn,etc.
Animprovedrepresentationaddedatwocellboardtotheentireboard,
which was filled with a known character signifying an illegal move. This
madeiteasiertoidentifyillegalmovesaswouldbepossiblewithknights,
andoptimizedtheboundscheckingaspectoftheChessprogram.
Today, a common representation for Chess programs is the bitboard.
Usinga64-bitword(availableonmanycomputers),eachcellontheChess
board can be represented by a bit in the 64-bit word. Each bit simply
determinesifapieceisinthecell(1)orifthecellisempty(0).Butinstead
ofhavingasinglebitboardfortheentireChessboard,there’sabitboardfor
everytypeofpieceontheChessboard(oneeachforthewhiteandblack
piecetypes).Apawnbitboardwouldrepresenttheplacementofwhitepawns
ontheboard.Abishopbitboardwouldcontainbitsfortheblackbishops.
Theadvantageofthebitboardisthatthecomputersthatsupportthe64-bit
typecanveryeasilyrepresentandquerythestateoftheboardsthroughbit
maskingoperations,whichareextremelyefficient.
Anotheradvantageofthebitboardistheefficiencyinselectingalegal
move.Bybitwiseor-ing(combining)thebitboards,youcanseewhichcells
aretakenontheboardandtherefore,whichmovesarelegal.It’sasimple
operationthat’sefficientformovegeneration.
TechniquesUsedinChessprograms
Let’s now look at some of the major techniques and algorithms that are
employedbyChessprograms.
OpeningBookDatabase
ThefirstfewmovesinaChessgameareimportanttohelpestablishgood
AIandGames 111
boardposition.Forthisreason,manyChesssystemsemployadatabaseof
openingmovesforagivenstrategythatcanbelinearlysearched.
MinimaxSearchwithAlpha-BetaPruning
Chess systems typically use a modified version of game-tree search by
performingonlyashallowsearchofthegametreeusingminimaxwithalpha-
betapruning.Whilenotintuitive,movesthatresultinsmallerscores(gain
orloss)aresometimeschosenthatcanimprovetheoverallboardposition
ratherthanashort-termgain.
TIP
ThetypicalbranchingfactorforChessisaround35,butwithalpha-beta
pruning,thiscanbereducedtoaneffectivebranchingfactorof25.Still
large,butthisreductioncanhelpgreatlytoallowdeepersearchesinto
thegametree.
OthersearchalgorithmshavebeendevisedforChesssuchasaspiration
search, which sets the bound of the alpha and beta parameters to some
heuristically defined value instead of +INFINITY and -INFINITY. This
narrows the search to nodes with a particular characteristic. There’s also
quiescence search, which tries to evaluate positions that are “relatively
quiescent,”ordead.[Shannon1950]
NOTE Another mechanism to minimize the search space is called null move
forwardpruning.Thebasicideahereisifyoudonothing(nomove),
cantheopponentdoanythingtochangetheboardconfigurationtotheir
benefit?Iftheanswerisno,thenopponentmovescouldbesafelypruned
fromthetree.Hashedtranspositiontablesarealsoemployedtoidentify
subtreesthathavealreadybeenevaluatedtoavoidrepetitivesearch.The
hashisusedtoquicklyidentifytheidenticalboardconfiguration.
StaticBoardEvaluation
Itshouldbeclearthatunlesswe’reneartheend-game,oursearchofthe
gametreewillnotencounteranyleafnodes.Therefore,we’llneedtohave
agoodutilityfunctionthathelpsusdecidewhichmovetomakegivenour
nearbyhorizon.TheutilityfunctionforChessdefineswhetheragivenboard
configurationisgoodorbadfortheplayer,anditcandecidethisbasedon
alargenumberoffactors.Forexample,isourkingoranimportantpiecein
jeopardy,oristheopponentinjeopardyforthecurrentboard?Isapiecelost
intheboard,andifitis,what’sthecostofthepiece(pawnbeingtheleast,
followedbythebishop,knight,rook,andqueeninincreasingvalue).Some
112 ArtificialIntelligence
oftheotherevaluationsthatcantakeplaceincludethespaceavailablefor
piecestomovetoandthenthenumberofcurrentthreats(fororagainst).
[AIChess]
While the minimax algorithm with alpha-beta pruning provides the
meanstosearchthegametree,theevaluationfunctionhelpsustodecide
whichpathisbestbasedonanumberofindependentutilityfunctions.
Othello
ThegameofOthello(alsoknownasReversi)isanothergameforwhichmany
algorithmsofAIhavebeenapplied.ExamplesofAIalgorithmsthathave
beenimplementedinOthelloplayersincludebasicheuristics,minimaxwith
alpha-betapruning,neuralnetworks,geneticalgorithms,andothers.
Othellohassomecommonaspectsthataresimilartoothertwo-playerzero-
sumgames.Forexample,likeChess,Othellohasanopeninggame,amid-game,
andanend-game.Duringthesephasesofthegame,thealgorithmsmaydiffer
forhowmovesaresearchedandselected(we’llexploresomeoftheseshortly).
AI for Othello programs, like Chess and Checkers, regularly beat
humanchampionsofthegame.Forexample,in1997,theOthelloprogram
LogistellodefeatedtheworldchampionTakeshiMurakamisixgamestozero.
Thisprogramretiredthefollowingyearfromtournamentplay.
TechniquesUsedinOthelloPrograms
OpeningKnowledge
Likemanygame-playingsystems,openingmovesareagoodindicatorfor
later strong board positions. For this reason, knowledge of strong initial
boardpositionsiscollectedandusedbymanyOthello-playingprograms.In
somecases,thedataiscollectedautomaticallythroughself-play.
StaticEvaluationFunction
ThemostimportantaspectofallOthelloprogramsistheevaluationfunction.
It’salsooneofthemostdifficultpartstoprogram,asthereareanumberof
variationsthatcanbeapplied.Threeparticularvariationsaredisk-squaretables,
mobility-basedevaluation,andpattern-basedevaluation.[Anderson2005]
Disk-squaretablesevaluatetheboardfromtheperspectivethatdifferent
cellshavedifferentvalues.Forexample,thecornercellevaluatestoahigh
score,wherethecellsnexttocornersevaluatetolowscores.
Inmobility-basedevaluation,movesthatmaximizemobility(definedas
thenumberofmovesavailable)arescoredhigherthanthosethatminimize
mobility.
AIandGames 113
Finally,pattern-basedevaluationfunctionsattempttopatternmatchlocal
configurationstodeterminetheutilityofamove.Thisiscommonlydoneby
evaluatingeachrow,column,diagonal,andcornerconfigurationindependently
andthensummingthem,potentiallywithweightsfordifferentfeatures.
Additionally, there are a number of heuristics that can be used in
evaluating potential moves. This includes avoiding edge moves (as they
cancreateopportunitiesfortheopponent),aswellasmaintainingaccessto
regionsoftheboard(ascanbethecasewhenalledgedisksinaregionare
forthecurrentplayer).
SearchAlgorithm
Inthemid-andend-gamephasesofOthello,minimaxgame-treesearchwith
alpha-beta pruning is used. Alpha-beta pruning is especially significant in
Othelloassearchingnineplieswithminimaxaloneevaluatesaboutabillion
nodes.Usingalpha-betapruning,thenumberofnodestoevaluatereducesto
amillionnodesandsometimesless.TheLogistelloOthelloplayermakesuse
ofminimaxwithalpha-beta,amongotherspecializedalgorithmsforOthello.
In addition to minimax with alpha-beta pruning, selective search can
beusedeffectively.Selectivesearchissimilartoiterativedeepening(see
Chapter2).Inselectivesearch,wesearchtoashallowdepth,andthentake
pathsthatleadtothebestmoves,andsearchthemtoamuchdeeperdepth.
Thisallowsustosearchdeeperintothegametreebyfocusingoursearch
tomovesthatprovidethegreatestbenefit.Usingthismethodwithstatistics
tounderstandtherelationshipbetweentheshallowsearchandthedeeper
searchiscalledMulti-Prob-Cut,orMPC.ThiswascreatedbyMichaelBuro
andisformalizedtocut-pairs(forexample,theshallowsearchoffourlevels
tothedeepsearchof12levelsiscalledacutpairof4/12).
End-games
Asthegamenearstheend,thenumberofavailablemovesdecreasesand
allowsamuchdeepersearchofthegametree.Asinthemid-game,minimax
searchwithalpha-betapruningworkswell,butvariationssuchasMPCare
alsoused.
OtherAlgorithms
Whileminimaxandalpha-betaarecommon,andusedinvaryingstagesofthe
game,otheralgorithmshavealsofoundapplicationinOthello.TheOthello
program Hannibal makes use of multi-layer neural networks for move
determination.Inordertotraintheneuralnetwork,arandommulti-layer
neuralnetworkiscreatedandthenplaysagainstitself.Themovesthatled
toawinarereinforcedinthenetworkthroughabackpropagationalgorithm.
114 ArtificialIntelligence
Conversely,themovesthatresultedinalossarenegativelyreinforcedto
weaken their selection in the future. After many games and associated
backpropagationareasonableOthelloplayerresults.
Go
We’llendourdiscussionofperfectinformationgameswiththegameofGo.
GoisoneofthecurrentAIchallengesasthescopeandcomplexityofthe
gameisquitelarge.Consider,forexample,thegametreebranchingfactor
whichisagreatdeterminerofgamecomplexity.InthegameofChess,the
averagebranchingfactoris35.ButinGo,thetypicalbranchingfactoris
300.Thisorderofmagnitudedifferencemeansthatgame-treesearchwith
minimax,evenwhenapplyingalpha-betapruning,resultsinshallowtree
searches.Eventhen,considerablememoryandtimearerequired.
TIP
Togiveaconcreteexample,fourmovesinChesscanevaluate35^4board
configurations(orroughly1.5million).InGo,fourmoveswouldevaluate
200^4boardconfigurations(or1.6trillion).
Whileconsiderabledevelopmentcontinuesforbuildinganintelligent
Goplayer,theresultshavenotbeenpromising.Chess,evenwithitsmassive
gametreesisnowviewedassimpleincomparisontoGo.Forthisreason,
somebelievethatwhenaGoprogramisabletoplayatthechampionship
level,AIwillhavematuredtoanewlevel.[Johnson1997]
Go-BoardRepresentation
RepresentingaGoboardcanbealittledifferentthaninothergameboard’s
representation. In addition to the simple board representation (19 by 19
board with values indicating empty, black stone, or white stone), other
attributesaremaintainedtosupportanalysisoftheboardandsubsequent
move generation. Attributes include groups of connected stones, eye
information(patternsofstones),andlife-and-deathstatus(whichstonesare
indangerofcapture,andwhichcanbekeptalive).
TechniquesUsedinGoPrograms
Go has some similarities with other game playing AI, and also some
differences.It’sinterestingtonotethatbuildingalgorithmsforcomputer
Go has ranged from game-tree search, rules systems, evolutionary
algorithms, and cognitive science. But given the complexity of Go, new
algorithmsarelikelynecessarytodealwiththebreadthanddepthfound
inthegame.
AIandGames 115
OpeningMoves
LikemostothergameAI,theopeningmovesareimportanttoestablisha
goodboardconfiguration.Forthisreason,almostallGoprogramsusewhat’s
knownasJosekilibraries.Theselibrariescontainsequencesofmovesthat
canbeusedpriortousingtheevaluationfunctionformovegeneration.
MoveGeneration
GiventhelargebranchingfactorofGo,simplytakingeachlegalmoveand
generatingagametreetoacertaindepthisnotfeasible.Instead,heuristics
areappliedtoidentifywhichlegalmovesaregoodcandidatesforreview.
For this set of moves, evaluation is invoked to determine which to take.
ThisdiffersfromChess,forexample,wheresearchdeterminesthemoveto
take.InGo,movecandidatesaregenerated,andthentheseareevaluated
todeterminewhichisbest.
SomeoftheheuristicsthatcanbeappliedtoGomovegenerationinclude
shapeorgroupgeneration(attemptingtomatchpatternsinapatternlibrary),
keepinggroupsalive,ortryingtokillopponentsgroups,andalsoexpanding
or defending territories on the board. Heuristics can also be applied for
globalmoves,orthosethatfocusonlocalregionsoftheboard.
NOTE Relating to move generation, some Go programs implement Goal
Generation.Thisprovidesahigher-levelviewofthestrategyandtactics
tobeusedforlower-levelmovegeneration.
Evaluation
Once a set of candidate moves are identified, the moves are ordered to
evaluate them based on their relative importance. The importance could
bebasedonthecurrentgoal(asdictatedbyahigher-levelgoalgenerator),
capturingastring,ormakinganeye,amongothers.
Evaluatingthecandidatemovescannowbeperformedusinggame-tree
search(suchasminimaxwithalpha-betapruning).ButGodiffersquitea
bitinhowthesearchisperformed.Forexample,theGoIntellectprogram
usesheuristicswithinthesearchtodeterminewhentocutoffasearchbased
onapre-determinedtargetvalue,ortoassociateanurgencyvaluewitha
moveandtoconsiderthiswhenevaluatingtheposition.Otherprogramscan
restrictmovestoaregionoftheboardandperformlocaltreesearch.
OtheralgorithmshavebeenusedinevaluatingGoboardconfigurations,
suchasAnd-Orgraphs,neuralnetworks,TemporalDifference(TD)learning,
andProof-NumberSearch.ByfarthemostimportantcharacteristicofGo
programs lies in heuristics and rules developed by strong Go players or
throughretrogradeanalysis.
116 ArtificialIntelligence
End-game
The use of the end-game database finds successful use in Go, like many
other complex games. These databases can be automatically generated
fromboardconfigurationsandapplyingrulestodeterminethebestmove
tomake,andthenafterdeterminingthefinaloutcome,attributingthemove
withwin/loss/draw.
Backgammon
Let’s now leave the world of perfect information games and explore a
number of imperfect information games (or stochastic games). Recall
thatinaperfectinformationgame,eachplayerhasaccesstoallavailable
informationaboutthegame(nothingishidden).Backgammonintroduces
theelementofchance,wheredicearerolledtodeterminethelegalmoves
thatarepossible.
WhatmakesBackgammonsointerestinginthedomainofAIisthatitis
extremelycomplex.RecallthatinChess,theaveragebranchingfactoris35,
andinGo,thebranchingfactorcanbeashighas300.InBackgammon,the
branchingfactorreaches400,makingitinfeasibleasacandidateforgame-
treesearch.Simplelookupofmovesbasedonagivenboardconfiguration
is also infeasible due to the enormous number of states in Backgammon
(estimatedatover10^20).
Luckily,therearesomeotherAItechniquesthathavebeensuccessfully
appliedtoBackgammon,andplayatthesamelevelashumanchampions.
TechniquesUsedinBackgammonPrograms
While other complex games such as Chess, Checkers, and Othello have
successfullyusedgame-treesearchformoveevaluationandgeneration,the
highbranchingratioofBackgammonmakesitinfeasible.Twoapproaches
thathavebeenappliedtoBackgammonarebothbasedonmulti-layerneural
networkswithself-learning.
Neurogammon
TheNeurogammonplayer(createdbyGeraldTesauro)usedamulti-layer
neural network and the backpropagation algorithm for training. The raw
boardconfigurationwasusedastheMLPnetworkinput,andtheoutput
defined the move to make. Neurogammon was trained using supervised
learning(backpropagation)andadatabaseofgamesrecordedfromexpert
Backgammonplayers.TheresultwasastrongBackgammonplayer,butdue
toitsdependenceonafinitedatabaseofrecordedgames,playedbelowthe
levelofexpertplayers.
AIandGames 117
TD-Gammon
ThesubsequentBackgammonprogram,alsobuiltbyTesauro,calledTD-
Gammon, used a multi-layer neural network knowledge structure, but a
differenttechniquefortrainingaswellasanimprovedlearningalgorithm.
First, TD-Gammon makes use of the Temporal Difference, or TD,
learningalgorithm(coveredinChapter8).Foreachplay,thevirtualdice
wererolled,andeachofthepotential20moveswereevaluated.Themove
withthehighestestimatedvaluewasusedastheexpectedvalue.Theboard
configurationwasthenappliedtotheneuralnetworkandthenfed-forward
FIGURE4.10: TD-GammonMLPneuralnetworkrepresentation.
118 ArtificialIntelligence
throughthenetwork.Adeltaerrorwasthencomputedgiventheresulting
valueandthemovewiththehighestexpectedvalue,andtheerrorapplied
totheweightsofthenetwork.
Interestingly,noexpertgameswereappliedtothenetwork,butinstead
theneuralnetworkwasusedtoselectmovesfortheplayerandtheopponent.
Inthisway,theneuralnetworkplayedagainstitself,andaftermanygames,
aneuralnetworkresultedthatcouldplay(andwin)attheexpertlevel.
TIP
The ability for TD-Gammon to learn the game of Backgammon with
zeroinitialknowledgeofthegameisatestamenttotheworkofArther
Samuel’sCheckersplayerthatintroducedtheideaofself-playasameans
tobuildaprogramthatlearnedwithouttheneedforsupervision.
Additionally, Tesauro updated the inputs to the neural network to
includenotonlytheconfigurationoftheBackgammonboard,butalsosome
specializedinputsthatcharacterizedfeaturesofthecurrentboard.
Whennotlearning,theTD-Gammonprogramusedthegeneratedneural
networkasthemeansformovegeneration.Foreachofthepossiblemoves
fromagivenboardconfiguration,eachpossiblenewboard(afterthemove
ismade)isappliedtotheneuralnetwork.Theresultofthenetworkisthe
probabilityofawin,giventhelastmove(seeFigure4.10).Theprocessthen
issimplytoevaluateeachofthepossiblemoves(usingamovegenerator)
andselectthenewboardconfiguration,whichyieldsthehighestprobability
forwin(theoutputcelloftheneuralnetwork).
Tesauro’sTD-Gammonhasevolvedsincetheinitialversion.Theinitial
TD-Gammon(0.0)utilized40hiddennodeswithintheneuralnetworkand
300KtraininggamestotieforbestusingotherBackgammonprograms(such
as Neurogammon). Later versions of TD-Gammon increased the hidden
nodesto160andalsoincreasedthenumberoftraininggamestowellabove
onemillion.TheresultwasastrongBackgammonplayerthatoperatesatthe
samelevelasthebesthumanplayersintheworld.[Sutton/Barto1998]
Inadditiontohavingzeroknowledgeofthegame,theneuralnetwork
haslearnedthebestopeningpositionsthatdifferedfromthosethoughtto
bethebestatthetimebyhumanplayers.Infact,humanplayersnowuse
theopeningpositionsfoundbyTD-Gammon.
Poker
Poker is a very interesting game that doubles as an ideal test-bed for AI
algorithms. It’s also a game of many aspects that can utilize different AI
AIandGames 119
techniques.AsPokerisagameofimperfectinformation(notallinformation
is available to all players in terms of their cards), a Poker program must
includetheabilitytomodelthelikelycardtheopponenthas.
WhatmakesPokermostinterestingisthatPokerisagameofdeception.In
additiontomodelingtheopponentandtheirstrategy,thePokerprogrammust
beabletoseethroughdeceptivetacticsoftheopponentintermsofbluffing
(attemptingtomakeanotherplayerbelievethattheirhandisbetterthanit
actuallyis).Ofequalimportanceistherequirementtoavoidpredictableplay.
ProfessionalPokerplayerscaneasilyexploitanypredictablefeatureorpattern
ofaPokerplayer,andthereforesomeelementofrandomnessisnecessary.
Fromapracticalperspective,anumberofcharacteristicsarenecessary
tobuildastrongPokerprogram.Thefirstistheabilitytoevaluatethegiven
hand(ascomparedtotheinvisiblehandsoftheopponents)anddetermine
whether the hand could win. The likelihood would be measured as a
probability,butgiventheneedtoavoidpredictabilityoftheplayer,bluffing
mustbeincorporatedtomakeuseofweakhands.Thebettingstrategyis
alsoanimportantcharacteristic.Whilethiscouldbebasedsolelyonhand
strength,itshouldalsoincludedatafromtheopponentmodel,payofffrom
thepot,etc.Theopponentmodelisanotherimportantelementwhichis
usedtounderstandtheplayer’shiddencardsbasedontheirbehavior(betting
strategy,pastexperience,etc.).
Loki-ALearningPokerPlayer
The Loki Poker player is a learning program that incorporates opponent
modeling with play that can be difficult to predict. The simplified basic
architectureofLokiisshowninFigure4.11.Theprimaryelementsofthis
architecturearetheTriplerGenerator,HandEvaluator,OpponentModeler,
andfinallytheActionSelector.
Thefundamentalelementofthearchitectureiswhat’scalledthetriple.A
tripleisasetofthreeprobabilitiesthatrepresenttheprobabilityoffolding,
raising,andcalling.Usingthepublicgamestate,theopponent’sbehavioris
modeledbymaintainingtriplesforthevariousgamestates.Astheopponent
makesdecisions,thetriplesareupdatedtomaintainaconstantpictureof
whattheopponentdoesgivenacertainscenario.
Usingthemodeloftheopponent,wecanthenidentifywhatweshould
dogivenourcurrenthandandtheprobabilityoftheopponent’smove.From
this,wegenerateatriple.Theactionselectorcanthenrandomlypickone
oftheactionsbasedontheprobabilitiesinthetriple.Usingprobabilitiesin
thiswaymakesitmuchmoredifficultfortheopponenttopredictwhatwe
willdo,sincefundamentallyitcanberandom.
120 ArtificialIntelligence
FIGURE4.11: BasicarchitectureoftheLokiPokerplayer(Adaptedfrom[Loki2003]).
Thebettingstrategyisbasedonaninternalsimulationgivenascenario
andtheexpectedpayout.Thesimulationmakesuseofgame-treesearchto
identifythepossibleoutcomes,butthesearchisselectivetoavoidthelarge
statespacethatcouldbeenumerated.
Lokihasalsomadeuseofself-playasameanstolearnthegameofPoker
and self-optimization. The variance in play with Loki (using probability
triples)allowedittoplaynumerousversionsofitself,whileadaptingtothe
play.TheopponentmodelingandsimulationallowLokitoplayreasonably
strongPoker.
Scrabble
For our final classical game, we’ll explore the game of Scrabble. The
interestingideasbehindbuildingaScrabbleplayerarealsoonesthatwe’ve
seenthusfarinotherclassicalgameplayers.
The Maven Scrabble player (created by Brian Sheppard) divides its
playintothreeseparatephases.Thesearethemid-game(whichlastsuntil
therearenineorfewertilesleftinthebag),pre-end-gamephase(which
beginswhenninetilesareleft)andfinally,theend-gamephase(whenno
tilesremain).
AIandGames 121
VIDEOGAMEAI
Whileclassicalgameshaveconcentratedonbuildingoptimalplayersusing
AI,videogamessuchasfirst-person-shooters(FPS)orstrategygamesfocus
122 ArtificialIntelligence
more on building AI that is both challenging and enjoyable to play. As
challengingisrelativetotheplayer’sability,theAIideallyshouldbeadaptive
andincreaseindifficultyastheplayer’sabilityincreases.
MuchofthedevelopmentofAIforclassicalgamesfocusedonbrute-
forcesearchrelyingonhigh-performancecomputing.VideogameAIdiffers
greatlyinthatlittleoftheCPUisavailablefortheAI(aslittleas10%,since
themajorityoftheCPUistiedupwiththephysicsandgraphicsengines).
Therefore,novelalgorithmsarenecessarytosynthesizebelievablecharacters
andbehaviorsinvideogamesthatconsumelittleoftheCPU.
TIP
WhileAIisthetermcommonlyusedtodescribetheopponent’sbehavior
in a variety of video games, this is a misnomer. Most video game AI
is simplistic in nature and rarely rises above the level of finite state
machines.
ApplicationsofAIAlgorithmsinVideoGames
Let’snowreviewsomeofthetechniquesusedinvideogameAI.We’lltake
across-sectionofthedomainsinwhichAIcanbeapplied,andthenexplore
someofthealgorithmsthathavebeenusedthere.
NOTE Thefollowingsectionsdefinesomeofthecommonlyusedelementsfor
AI,butnotnecessarilythestateoftheart.Seethereferencessectionfor
moreinformationonwherevideogameAIisgoingtoday.
TheapplicationofAIintovideogamesisarichareaforresearchona
numberoflevels.Videogameenvironments(suchascanbefoundinreal-
timestrategygamesorfirst-person-shooters)provideausefultest-bedfor
theapplicationandvisualizationofAItechniques.Gamesthemselveshave
becomeahugeindustry(it’sestimatedthatgamesgrossmorethanmovies),
sothedevelopmentofAItechniqueswiththeassociatedconstraintsthatcan
befoundingames(suchasminimalCPUallotment)canbeverybeneficial.
It’salsopossibletoallowdifferentalgorithmsandtechniquestocompete
against one another in these environments to understand their subtle
differencesandadvantages.
Inadditiontotheentertainmentvalueofvideogames,thetechniques
for building believable characters also finds value (and research funding)
inmilitaryapplications.Forexample,flightcombatsimulatorsthatmimic
thestrategyandtacticsofveteranpilots,orthehierarchicalanddisciplined
behavioroftroopsonthegroundinbattlemanagementsimulators.Each
AIandGames 123
of these applications requires intelligent algorithms that may differ in
embodiment,butevolvefromthesamesetoftechniques.
NOTE Thephysicalembodimentofmanyoftheseideasandalgorithmsinthe
fieldofroboticswillbeexploredinChapter10.
MovementandPath-finding
Theobjectofpath-findinginmanygamesfromfirstorthird-person-shooters,
toreal-timestrategygamesisidentifyingapathfrompointAtopointB.In
mostcases,multiplepathsexistfrompointAtopointB,soconstraintsmay
existsuchasshortestpath,orleastcost.Consider,forexample,twopoints
separatedbyahill.Itmayinfactbefastertogoaroundthehill,thangoing
overit,butgoingupthehillcouldgivesomeadvantagetotheplayer(sayan
archerwithanenemyopponentontheoppositedown-slope).
Insomecases,path-findingissearch.Thelandscapeuponwhichwe’re
toplotarouteisagraphofnodesrepresentingwaypoints.Eachedgeof
thegraphhasagivencost(forexample,plainscouldhaveanedgecost
ofone,whereinclinescouldhaveanedgecostconsistentwithitsslope).
Consideringpath-findingassearchthroughagraphofnodeswithweighted
edges, the A* search algorithm (explored in Chapter 3) is ideal for this
application.ItisoptimalcomparedtoDFSandBFS,andcangiveusthe
optimalpath.
The problem with A* is that it’s a very compute-intensive algorithm.
Consideringthenumberofagentsinareal-timestrategygamethatneedto
movearoundthemap,theamountoftimetakenbyA*wouldbemultiplied.
AstheAIinareal-timestrategygamewouldalsoneedtosupporthigh-level
goalplanningandeconomicstrategy,path-findingisbutoneelementthat
shouldbeoptimized.
Luckily, there are some other options to simplify the operation of
determiningwhichmovetomakeinamap.We’llstartwithasimpleexample
that demonstrates offensive and defensive movement using a graph and
lookuptable,andthenexploresomeoftheothertechniquesused.
TableLookupwithOffensiveandDefensiveStrategy
Anymapcanbereducedtoagraph,wherethenodesaretheplacesthatcan
bevisited,andtheedgesarethepathsbetweenthenodes.Reducingamap
inthiswaydoesacoupleofthings.First,itpotentiallyreducesamapwith
aninfinitenumberofpointsintoagraphwithfewerpoints.Theedges,or
thepathsbetweennodesinthegraph,definethevariouswaysthatwecan
travelaroundourgraph(andourmap).
124 ArtificialIntelligence
ConsiderthesimplemapofaroominFigure4.12.Inthismap,thehuman
playerentersatthebottom.OurNon-Player-Character(or,NPC)entersat
thetop.TherearemanylocationsonthemapthattheplayerandNPCcould
go,butveryfewofthemareimportant.Notethatthisisasimplisticexample,
andarealsystemwouldincludemanymoremappoints.
Consider that we’re developing an AI for an FPS. The object is for
the player and NPC to fight one another, but this implies that we must
instillinourNPCtheabilitytoattacktheplayerbymovingtohislocation.
Additionally,ifourNPCisinjured,wewanttoavoidtheplayertoregain
ourstrength.TomakeitsimplerforourNPCagent,we’llencodethemap
asasimplegraph.Thisgraphcontainsthedefensivepositionsthatwillbe
importanttoourNPC(seeFigure4.13).
InFigure4.13,oursimplemaphasbeenreducedtoanevensimpler
graph. This contains the seven positions that our NPC agent may exist.
Additionally, the edges of the graph show the legal moves that our NPC
FIGURE4.12: SimplemapforaNPCAI.
AIandGames 125
agentmaymake.Notethatsincetheplayermayexistatanylocationinthe
map(notrestrictedtothegraph),wedefinerectanglesfortheplayer.Ifthe
playerisintherectangle,we’llsimplifyhispositiontothenodecontained
intherectangle.Thepurposeofthiswillbecomeclearshortly.
NOTE We’llassumehere,forthesakeofsimplicity,thattheNPCalwayssees
theplayer.Inamorecomplicatedsystem,theplayerwouldneedtobe
intheNPC’sfield-of-view(FOV)inorderfortheNPCtoidentifythe
player’spresence.
WenowhaveasimplegraphforourNPC.Thenextstepistodefine
what our NPC agent should do depending on its strategy. For simplicity,
we’llimplementtwobasicstrategies.IfourNPCishealthy,we’lltakean
offensivestrategytoattacktheplayer.IftheNPCisnothealthy,thenwe’ll
takeadefensivestrategy.
FIGURE4.13: Simplemapreducedtoanevensimplergraph.
126 ArtificialIntelligence
Let’s begin with the offensive strategy (shown in Figure 4.14). The
strategy is implemented as a simple graph connectivity matrix with two
dimensions.TherowsrepresentthecurrentlocationofourNPC,whilethe
columnsrepresentthecurrentlocationoftheplayer.Recallthatourplayer
canexistanywhereinthemap.Iftheplayerisinarectangle,we’llusethe
nodecontainedintherectangletoidentifytheplayer’scurrentposition.If
theplayerisnotinarectangle,we’llsimplydefinethisasunknown.
ThestrategydefinedinTable4.14isoneoffollowingtheplayertohis
positioninthemap.Forexample,iftheNPCisatnodeE,andtheplayeris
aroundnodeA,thentheNPCwillusetheoffensivestrategytableandmove
tonodeD.IftheplayerthenmovesfromnodeAtonodeC,weusethetable
againfortheNPCatnodeD,andtheplayeratnodeC,whichresultsinthe
NPCmovingtonodeC(ontheattack).
FIGURE4.14: LookuptablefortheNPCagentoffensivestrategy.
AIandGames 127
TIP
The lookup table provides for reactive behaviors, as our NPC simply
reactstothemovementoftheplayer.Notethatit’salsostateless,nostate
iskeptbetweenlookups,andtheNPCsimplyusesitspositionandthe
player’spositiontodeterminethenextmove.
ThedefensivestrategyisshowninTable4.15.Thisstrategyisoneof
takingsometimetohealbyavoidingtheplayer.Take,forexample,theNPC
atnodeDandtheplayeragainaroundnodeA.Thelookuptablereturnsa
movefromnodeDtonodeE,essentiallyputtingdistancebetweenusand
the player. If the player then moved to node D, the lookup table would
returnnodeF,movingawayfromaneventualmovebytheplayertonode
E.Notethatinsomecases,thebestmoveistonotmoveatall.IftheNPC
wasatnodeGandtheplayeratnodeB,thereturnvaluefromthetableis
‘-’indicatingtosimplystayput.
FIGURE4.15: LookuptablefortheNPCagentdefensivestrategy.
128 ArtificialIntelligence
Whilesimple,thismethodgivesusanefficientwaytobuildoffensiveand
defensivestrategiesforanNPC.Nosearchisinvolved,simplytablelookup.
To add some level of unpredictability in the offensive strategy, the move
couldbeselectedrandomlywithsomeprobability,ratherthansimplytaking
thelookupvalue.
NOTE ConsideralsotheoffensivestrategyinFigure4.14asapath-findertoa
givennode.IfweneedtogetfromnodeGtonodeB,thelookuptable
takesusfromnodeGtonodeCandthenfinallytonodeB.Thelookup
tableinthiswaygivesusasimpleandefficientalgorithmtogetfrom
pointAtopointB.Thisworkswellinstaticenvironments,buttendsto
fallapartiftheenvironmentchangesovertime.
Inlargeenvironments,itcanalsobepossibletosegregateamapinto
multipleconnectedmaps,eachhavingfunnelpointsforwhichanNPCmay
travel.Figure4.16showsamapofthreezones.Ratherthanincludingasingle
lookuptableforallpoints,threeseparatelookuptableswouldexist.Ifour
NPCagentwasintheleftroom,itcouldusethelookuptabletodetermine
whichpathtotaketoagivennode.Ifthedestinationwereoutsideofthe
room, it would by default move to node A, and from there, the external
lookuptablewouldtakeoverforroutingtheNPCtoitsdestination.Ifthe
FIGURE4.16: Segregatingamapintoseparatezonestosimplifypath-finding.
AIandGames 129
destination were to the room on the right-hand side, the NPC would be
directedtonodeB,anduponreachingnodeB,useitslookuptabletoget
therestoftheway.
It’salsoverycommonforthesealgorithmstotime-slicetheirprocessing.
Thispermitstheworktobedoneoveranumberofiterations,withouttying
uptheCPUonasingleoperation.
NPCBehavior
In the last section, we gave our NPC the ability to traverse a map using
strategies based on the NPC’s health (using intentional offensive and
defensivestrategies).Let’snowexploresomeoptionsforgivingourNPC
theabilitytobehaveintelligentlyinitsenvironment.
The behavior of an NPC can’t be considered in isolation, because
behavior is ultimately grounded in the environment. The NPC must be
able to perceive its environment (see, hear, etc.). With the information
perceivedfromtheenvironment(aswellasinternalstateinformationsuch
asmotivation),theNPCcanreasonaboutwhatshouldbedone.Theresult
ofreasoningispotentiallyanintentionalact,whichisperformedasanaction
(seeFigure4.17).Thisaction(andsubsequentactions)iswhatweexternally
viewastheagent’sbehavior.
FIGURE4.17: TheclosedLoopofreasoning.
130 ArtificialIntelligence
TheNPCwillbeequippedwithasetofsensorsthatallowittosense,or
perceive,itsenvironment.Thesesensorscouldindicateobjects(suchasthe
player)initsfield-of-viewintermsofsight,ordirectionofanobjectusing
localizationofsound.Withasetofsensorsasinput,theNPCcannowreason
aboutwhattodo.Theinternalstateisalsokepttoindicateahigher-level
setofgoalsthataretobeachieved,ortoindicatethehealthoftheNPC
(whichcouldchangeitsstrategy).Giventhestateoftheenvironmentand
theinternalstateoftheNPC,anactioncanbeselected.Thiscanalterthe
environment,changetheNPC’sinternalstate,andresultinachangeinwhat
theNPCwilldonext.AnNPC’sactioncouldbemovingtoanewlocation(as
describedintheprevioussection),communicatingwithotherNPCs(possibly
aboutthelocationoftheplayer),changingitsweapon,orchangingitsstance
(goingfromanuprighttoaproneposition).
Let’snowreviewafewoftheoptionsthatcangiveourNPCsomebasic
reasoningabilities.
StaticStateMachines
Oneofthesimplestmethods,andalsooneofthemostcommon,isthestate
machine. State machines consist of a set of states, and arcs, between the
statesthatdefinetheconditionsnecessaryfortransition.Considerthesimple
gameAIstatemachineinFigure4.18.
OurNPCsentrydefinedbythestatemachine(betterknownasaFinite
StateMachine,orFSM)hastwobasicfunctionsinlife.TheNPCmarches
between two locations, guarding some entry from the player. When the
playerisinsight,theNPCfightstothedeath.Thestatemachineimplements
thisasthreesimplestates(onecanthinkofthemasmentalstates).Inthe
firststate,theNPCmarchestothelocationidentifiedasX.Itcontinuesto
marchtoXunlessoneoftwothingshappen.IftheNPCreacheslocationX,
it’satitsdestinationforthestate,andwetransitiontothealternatemarch
FIGURE4.18: StatemachineforasimplegameAI.
AIandGames 131
state.IftheNPCseestheplayer,itattacks.Ifneitheroftheseeventsoccurs,
theNPCcontinuesmarching.
WhentheNPCseestheplayer,byenteringitsfield-of-view,theFSM
transitionstotheattackstate.Inthisstate,theNPCfightstothedeath.Ifthe
NPCdies,thenthestatemachineisnolongeractive(theNPClieslifeless
intheenvironment).IftheNPCdefeatstheplayer,itbeginsmarchingagain
towardlocationX.
There’snotmuchtotheFSM,buttheyaresimpleandeasytodebug.
Theyarealsoverypredictable,butit’spossibletoaddtransitionprobabilities
togivetheNPCasmallelementofrandomness.
LayeredBehaviorArchitectures
OurpreviousFSMdefinedaverysimpleagentthathadtwothingsonits
mind,marchingandattacking.WhatwasmostimportanttotheNPCwas
attacking,butwhilenoplayerwasinitsfield-of-view,itwasquitehappyto
marchbackandforth.ButwhathappensifourNPChasmorethingstoworry
about.IftheNPC’shealthislow,itshouldheadtotheinfirmary.IftheNPCis
lowonammo,itshouldheadtothearmorytoreload.Ifmorethanoneplayer
appearstoattack,shoulditfight,orrushtotheguardhousetopullthealarm?
Thesearen’tentirelycomplex,buttheNPCnowneedstoengageinsome
thoughttodeterminethemostappropriateactionforthegivenscenario.
OnewaytohandlethisconflictinactionselectionisRodneyBrooks’
subsumption architecture. This architecture confines responsibilities to
isolated layers, but allows the layers to subsume one another if the need
arises.Let’slookatoursimpleNPCagaintoseehowwemightmapthenew
refinedbehaviorsintosubsumption(seeFigure4.19).
FIGURE4.19: BehaviorlayersforthesimpleNPC.
132 ArtificialIntelligence
Figure 4.19 illustrates one example of our NPC’s requirements to a
layeredarchitecture.Theselevelsshouldbeviewedinrelativeimportance.
Atlevelone,ourNPCperformshisguardduty,dutifullymarchingalonghis
route.Iftheplayercomesintoview,theNPCswitchestotheattacklayer
toridtheenvironmentofthepeskyplayer.FromBrookes’architecture,the
Attack layer subsumed (took priority over) the March layer. If no players
areinthefield-of-view,andtheNPCneedstobehealedorreplenishhis
ammunition,thesustainlayertakesovertoperformthoseactions.Finally,the
NPCwillturntothesurvivelayerifmorethanoneplayerisseeninthefield-
of-view.Astheconstraintsaremetwithinthelayers,theNPCwilldefaultto
thelowestlayer.Therefore,oncetheplayerisdisposedof,andtheammoand
healtharereturned,theNPCwillreturntothemarchinlevelone.
Withineachoftheselayers,individualFSMscanbeusedtoimplement
therelevantbehavior.Inthisway,entirelynewstatemachinesareconsulted
basedonthecurrentbehaviorlayerfortheNPC.
NOTE AninterestinganalogytoNPCsinvideogamesisthefieldofIntelligent
Agents. We’ll explore the field of Intelligent Software Agents in
Chapter11.
OtherAction-SelectionMechanisms
We’lldiscussotherrelevantalgorithmsthatcouldbeusedtoselectanaction
foranNPCinthefollowingchapters.Neuralnetworksareaninteresting
example of behavior selection (classification), as are planning algorithms
andnumerousothermachine-learningalgorithms(reinforcementlearning,
tonameone).Themachine-learningalgorithmsareofparticularinterestas
theycaninstilltheabilityfortheNPCtolearnandbehaveinnewwaysgiven
previousencounterswiththeplayer.
NOTE Inatypicalgame,agameengineprovidesthebaseplatformforgraphics
and fundamental game-play. The NPC behaviors are commonly
implemented using high-level scripts, with a script interpreter
implementedwithinthegameengine.Thisprovidesagreatamountof
flexibility,wherethegameengineisimplementedinahigh-levellanguage
suchasCorC++forefficiency,andtheNPCbehaviorsareimplemented
inascriptinglanguageforflexibility.
TeamAI
In many games, there’s not just a single NPC soldier that we’re fighting
against,butanentirearmythatmustworktogetherinharmonywithasingle
AIandGames 133
orhandfulofgoalsinmind.Thecontrolcanexistatanumberoflevelswithin
ahierarchy(seeFigure4.20),fromthesquadleaderdirectingtroopstoflank
anenemyinthefieldusingreal-timebattlefieldinformation,tothegeneral
managinghisentirearmytoimplementthehigher-levelmilitarystrategies.
Managinganoverallarmycanentailalargenumberofproblems,from
schedulingandproduction,toresourceallocation,totheoverallstrategyand
tacticsoftheforceinthefield.
Theproblemherecanbesimplifiedgreatlybyminimizingthenumber
oflevelsororganization.First,theindividualsoldierssimplyfolloworders,
andunlesstheyarerouted,canfighttothedeath.Theseindividualunits
canbeprogrammedusingfinitestatemachines,orothersimplemechanisms
forbehavior.
TIP
Followingordersisarequirement,butthere’salsosomethingtobesaid
forautonomyatthesoldierlevel,takingadvantageofalocalsituation
toimprovethechancesofaglobalwin.
Theproblemtosolvethenisthehigher-levelcontroloftheentireforce.This
canbesubdividedaswell,especiallywhenforcesaresplitforindependent
goals.AtthehighestlevelistheStrategicAI.ThislayeroftheAIhasthe
FIGURE4.20: Organizationalstructureofasimplifiedarmy.
134 ArtificialIntelligence
globalviewofthebattlefield,troopstrengths,availableresources,etc.Nextis
theTacticalAIwhosejobistoimplementthestrategyprovidedfromabove.
Atthelowestrungistheindividualsoldier,onwhosecollectiveshoulders
thatoverallstrategyrelies.
NOTE Forthisdiscussion,we’llfocusonTeamAIthatopposesahumanplayer,
but much of this can also be applied to Team AI that cooperatively
supportstheplayer,thoughthiscanbeconsiderablymoredifficult.For
example,workingcooperativelywiththeplayerrequiresthattheNPC
help,butalsostayoutoftheplayer’sway.
GoalsandPlans
Aninterestingmechanismforhigh-levelcontroloverahierarchyofmilitary
unitsisindefiningagoal,andthencreatingaplantoreachthatgoal.Also
necessaryistheneedtoreformulateaplan,wheniteventuallyfails.
Let’sfirstdiscusstheplanningvernacularandthenexplorehowitcan
be applied to team AI. First, there’s the goal. A goal can be an end-goal
(fortheendofthegame)oranintermediategoalthatmovesuscloserto
anend-gamesituation.Formally,agoalisaconditionthatisdesiredtobe
satisfied. Example goals include taking an enemy position, destroying a
bridge,flankinganenemy,etc.Toreachagoal,wemustperformaseriesof
FIGURE4.21: Theplananditselements.
AIandGames 135
actions,whichareindependentstepswithinaplan.Eachactionpotentially
hasaprerequisitethatmustbemetinorderfortheactiontobeperformed.
ExampleactionsincludemovingasetofNPCunitstoalocation,attacking
theenemy,plantingacharge,etc.Theplan,then,isasetofactionsthatwhen
performedinthegivenorder,achieveagoal(seeFigure4.21).Themethod
bywhichaplanisformulatedisthroughtheuseofaplanner.
Asanexample,let’ssaywehavetwoNPCsthatdesiretoattackaplayer
that is firing from a covered position. An effective attack is for one NPC
to provide covering fire at the player, while the other NPC moves to a
flankingpositiontobeinabetterpositiontoattacktheplayer.Thiscould
berepresentedsimplyasshowninFigure4.22.
Thegoaloftheplanistoeliminatetheplayerthat’sinacoveredposition
(meaningtheplayerisshieldedfromfirefromtheNPC’scurrentposition).
Toeliminatetheplayer,oneNPCprovidescoveringfireontheplayerso
thatanotherNPCcanmovetoaflankingpositiontoattacktheplayer.Once
thisNPCisinapositiontofire,theotherNPCrushestheplayer’sposition.
Implicit in the plan is cooperation, and through a strict ordering of the
FIGURE4.22: SampleplantoeliminateaplayerinacoveredPosition.
136 ArtificialIntelligence
actions, coordination. Later, we’ll see how planning is achieved (and like
muchofAI,it’sfundamentallyasearchproblem).
NOTE Implicitinplanningarepreconditions(orprerequisites)thatmustbe
metinorderfortheplantobevalid.ConsidertheplaninFigure4.22if
onlyoneNPCispresent.WhathappensifNPC_1iseliminatedwhileon
themovetoitsflankingposition?Preconditionsmustbemetforaplan
tobevalid,andifatanytimeactionsinaplancannotbeperformed,the
plancannotcontinue.Therefore,indynamicenvironments,planningand
replanninggohand-in-hand.
Real-TimeStrategyAI
A final interesting use of AI is in the development of real-time strategy
games.Real-TimeStrategyAIdiffersfromTeamAIinthatwewilldealnot
onlywiththemilitaristicaspectsofthegame,butalsotheeconomicaspects.
Forexample,inareal-timestrategygame,elementsofthecivilizationmust
engageinresourcegatheringaspartofahigher-levelgoalofbuildingan
armytodefeatanopponent.
Forexample,inthebeginningofareal-timestrategygame,thefocus
issocietalandmilitarybuildup.Thisinvolvescreatingnewcitizenstobuild
theeconomy.Oncetheeconomyreachesacertainlevel,amilitarybuildup
canoccurtoattackanddefeatanenemy.
Theconditionsdescribedcouldbeembeddedwithinthegameengine
itself,butforflexibility,couldbeimplementedseparatelypermittingease
ofmodificationwithouthavingtorebuildthegameengine.Onepossibility
that’sbeenusedinthepastisRule-BasedSystems(orRBS).
Rule-BasedProgramming
Rule-basedsystemsareaneffectivewaytoencodeexpertknowledgeabout
game play into a strategy game. Rule-based systems are so interesting
thatastandardscommitteehasbeenformedtostudytheiruseingames.
[IGDA2005]
NOTE We’llexplorerule-basedprogrammingaspartofthereviewofknowledge
representationinChapter6.
Arule-basedsystemismadeupoftwomemories,onethatholdsfacts,
andanotherthatholdsasetofrulesthatexisttodeterminethebehavior.A
rule-matchingalgorithmisappliedtothefacts,andthoserulesthatmatch
factsaresavedintotheconflict-set.Thissetofrulesisthenreviewedand
AIandGames 137
onerulepickedtofireinaprocesscalledconflictresolution.Theruleisthen
appliedandtheworkingmemoryisupdated.
NOTE Selectingaruletofirefromalistofrulesthathavematchedcanusea
numberofalgorithms.Onecouldselectatrandom,thelastruletomatch,
ortherulethat’smostspecific(hadthelargestnumberofconditions).
FIGURE4.23: Typicalflowofarule-basedsystem.
138 ArtificialIntelligence
Nowthatwe’veexploredtheprocess,let’slookattheanatomyoffacts
and rules. A fact is an atomic element of knowledge that can be used in
conjunction with other facts to reason using rules. Facts can take on a
varietyofforms;oneofthemostcommonistheS-expression(orSymbolic
Expression), which simply is a way of structuring the data. Consider the
followingfourfactsinoursimpleknowledgebase:
(opponent-1sizelarge)
(opponent-2sizesmall)
(armysizelarge)
Regardingourgameworld,thesefactstellusthatthefirstopponentis
largeinsize,thesecondopponentissmall,andourarmyisalsoconsidered
large. Before looking at some rules given our small knowledge base, let’s
reviewthestructureofarule.
Rulesaremadeupoftwothings,antecedentsandconsequents.Ifyou
thinkaboutaruleasanif/thenconstruct,thenthe‘if’istheantecedent,and
the‘then’istheconsequent.Considerthefollowingsimplecomparison:
if((opponent.size<=SMALL_SIZE)&&
(army.size>=LARGE_SIZE)){
attack_opponent();
}
Thisconditionalsimplysaysthatiftheopponent’smilitarysizeissmall,
andoursislarge,we’llattackthem.Asimplerulethatencodesthisbehavior
couldbeimplementedasfollows:
(rule“AttackOpponentBasedUponSize”
(?opponentsizesmall)
(armysizelarge)
==>
(armyattack?opponent))
Notethatthe‘?opponent’elementwillmatchanyopponentinthe
workingmemory,andwhenitmatchesone,we’llusethissameopponent
whenattacking.Oncethisrulefires,ourworkingmemorywillexistas:
(opponent-1sizelarge)
(opponent-2sizesmall)
(armysizelarge)
(armyattackopponent-1)
AIandGames 139
Thisnewfactwoulddrivebehaviorwithinthegameenginetoformulate
aplantoattacktheenemy.TheRBScouldalsoremovefactsasdrivenby
consequentstoreflectthedynamicenvironmentofthegame.
TIP
Todealwithsizes,theRBScan‘fuzzify’valuesfromrangestodistinct
sets. For example, the size used in the previous examples could
identify the range [0..4999] as small, [5000..9999] as medium, and
[10000..50000]aslarge.Thisreducesthecomplexityoftheinputsand
simplifiestherulesbase.
AnRBSisausefulmethodforencodingknowledgeintoagamethat
canbeusedtodrivebehaviors,especiallyhigh-levelbehaviorsinmatters
ofstrategy.It’salsoadvantageousbecauseitallowsthestrategyelementto
bedecoupledfromthegameengineandpermitslatertweakingbygame
developers.
CHAPTERSUMMARY
ThischapterprovidedabroadspectrumoftechniquesusedingameAI,from
classicalgames,tomodernvideogames.InclassicalgamessuchasChessand
Checkers,andeveninformationimperfectgamessuchasPoker,theminimax
game-treesearchalgorithmwithalpha-betapruningcanbefound.Inthese
games,thegoalofdevelopersistobuildanopponentthatcandefeatworld-
classplayers.Incontrast,thegoalinvideogamesistobuildopponentsthat
arechallengingtoplay,butnotperfect.Evenwiththislimitationinmind,
variousAItechnologiescanbefoundinmoderngames,fromneuralnetworks,
torule-basedsystemsanddecisiontrees,toembedgameopponentsthatare
bothchallengingandadaptivetothehuman’slevelofplay.
REFERENCES
[AIHorizon]Availableonlineat:
http://www.aihorizon.com/essays/chessai/boardrep.htm
[AIChess]Availableonlineat:
http://www.cs.cornell.edu/boom/2004sp/ProjectArch/Chess/algorithms.
html
[Archer 1999] Archer, A.F. “A Modern Treatment of the 15 Puzzle,”
AmericanMath.106:,793-799,1999.
140 ArtificialIntelligence
[Anderson2005]Anderson,Gunnar.“WritinganOthelloprogram,”Available
onlineat:
http://www.radagast.se/othello.
[Chellapilla,Fogel2000]Chellapilla,KumarandFogel,DavidB.“Anaconda
Defeats Hoyle 6-0: A Case Study Competing an Evolved Checkers
ProgramagainstCommerciallyAvailableSoftware(2000).”Proceedings
ofthe2000CongressonEvolutionaryComputationCEC00.
[IGDA 2005] “Working Group on Rule-based Systems,” AI Interface
Standards Committee, The 2005 AIISC Report, 2005. Available
onlineat:
http://www.igda.org/ai/report-2005/rbs.html
[Johnson1997]Johnson,George.“ToTestaPowerfulComputer,Playan
AncientGame.”IntroductiontoComputationandCognition.Available
onlineat:
http://www.rci.rutgers.edu/%7Ecfs/472_html/Intro/NYT_Intro/ChessMatch/
ToTest.html
[Loki 2003] Jonathan Schaeffer, Darse Billings, Lourdes Pena, Duane
Szafron.“LearningtoPlayStrongPoker.”2003.
[Lu1993]Lu,Chien-PingPaul.“ParallelSearchofNarrowGameTrees.”
Master’s thesis, University of Alberta, Department of Computing
Science, Edmonton, Canada, 1993.
[McCarthy1990]Availableonlineat:
http://www-db.stanford.edu/pub/voy/museum/samuel.html
[Samuel 1959] Samuel, A. L. “Some Studies in Machine Learning using
theGameofCheckers,”IBMJournalofResearchandDevelopment,
1959.
[Shannon1950]J.Schaeffer“1989WorldComputerChessChampionship,”
Computers,ChessandCognition,Springer-Verlag,NewYork,1990.
[Sutton/Barto1998]Sutton,RichardS.,andBarto,AndrewG.“Reinforcement
Learning:AnIntroduction.”MITPress,1998.
RESOURCES
BruceMoreland’sChessProgrammingTopics.Availableonlineat:
http://www.seanet.com/~brucemo/topics/topics.htm
[Chinook]Availableonlineat:
http://www.cs.ualberta.ca/%7Echinook/
TheIntelligentGoFoundation.Availableonlineat:
http://intelligentgo.org/en/computer-go/overview.html
AIandGames 141
Bouzy, Bruno and Cazenave, Tristan. “Computer Go: an AI Oriented
Survey.”UniversiteParis.
Brooks,Rodney.“ARobustLayeredControlSystemforAMobileRobot,”
IEEEJournalofRoboticsandAutomationRA-2,April1986.
Tesauro, Gerald. “Temporal Difference Learning and TD-Gammon.”
Availableonlineat:
http://www.research.ibm.com/massive/tdl.html
TD-learning,NeuralNetworks,andBackgammon.Availableonlineat:
http://www.cs.cornell.edu/boom/2001sp/Tsinteris/gammon.htm
[Sheppard 2002] Sheppard, Brian. “World-championship-caliber
Scrabble,”
ArtificialIntelligence134:(2002),241-275.
EXERCISES
1. What is meant by adversarial search, and how does it differ from
traditionaltreesearch?
2. Whatisplyingame-treesearch?
3. GiventhegametreeshowninFigure4.24,whatisthevalueattheroot
node?
4. Minimaxcansearchtotheleavesofthetree,ortoapredefineddepth.
Whataretheconsequencesofendingasearchatapredefineddepth?
FIGURE4.24: Samplegametreeforminimaxsearch.
142 ArtificialIntelligence
5. GiventhegametreeshowninFigure4.25,whatisthevalueattheroot
nodeandwhichnodesareprunedfromthesearch?
6. Whatwasthefirstsuccessfulgame-playingprogramthatusedself-play
tolearnaneffectivestrategy?
7. Explainwhatismeantbyperfect-informationandimperfect-information
games.Giveafewexamplesofeach,anddefinewhichtypetheyare.
8. Define some of the similarities and differences for building a game-
playingAIforCheckersandChess.
9. WhataresomeofthemajordifferencesbetweenbuildingAIforclassical
gamesandvideogames?
FIGURE4.25: Samplegametreeforminimaxsearchwithalpha-betapruning.
Chapter 5 KNOWLEDGE
REPRESENTATION
K
nowledgeRepresentation(KR),asthenameimplies,isthetheory
andpracticeofrepresentingknowledgeforcomputersystems.By
thatwemeanconciserepresentationsforknowledgeinaformthat’s
directlymanipulatablebysoftware.Thisisanimportantdistinctionbecause
representingknowledgeisonlyusefulifthere’ssomewaytomanipulatethe
knowledgeandinferfromit.
INTRODUCTION
FromtheperspectiveofStrongAI,KRisconcernedwiththecognitive
science behind representing knowledge. How, for example, do people
store and manipulate information? Many of the early representation
schemes resulted from this research, such as frames and semantic
networks.
Thischapterwillexplorethevariousschemesfortherepresentationof
knowledge,fromtheearlyrepresentationmethodstopresent-daymethods
such as the Semantic Web. We’ll also explore some of the mechanisms
for the communication of knowledge, as would be used in multi-agent
systems.
144 ArtificialIntelligence
TYPESOFKNOWLEDGE
Whilealargetaxonomyofthevaryingtypesofknowledge,couldbecreated
we’llfocusontwoofthemostimportant,declarativeandprocedural.
Declarative (or descriptive) knowledge is the type of knowledge that
isexpressedasdeclarationsofpropositions(orfactualknowledge).Onthe
otherhand,proceduralknowledgeisexpressedastheknowledgeofachieving
somegoal(forexample,howtoperformagiventask).Proceduralknowledge
is commonly represented using productions, and is very easy to use but
difficulttomanipulate.Declarativeknowledgecanberepresentedaslogic,
andissimplertomanipulate,butismoreflexibleandhasthepotentialtobe
usedinwaysbeyondtheoriginalintent.
NOTE In Chapter 11, we’ll investigate intelligent agent architectures, some
of which operate on declarative knowledge (such as Prodigy), where
othersutilizeproceduralknowledge(suchastheProceduralReasoning
System).
THEROLEOFKNOWLEDGE
FromthecontextofAI,representingknowledgeisfocusedonusingthat
knowledgetosolveproblems,andtheimplicationthatknowledgeismore
thanjustfactualinformation.Therefore,themannerinwhichtheknowledge
is stored is important. For example, we can store knowledge in a human
readableformanduseit(suchasthisbook),butknowledgestoredinthis
formisnotreadilyusefulbyAI.Therefore,theknowledgemustbestored
inawaythatmakesitpossibleforAItosearchit,andifnecessary,infernew
knowledgefromit.
Theprimarygoalofknowledgerepresentationistoenableanintelligent
entity (program) with a knowledge base to allow it to make intelligent
decisions about its environment. This could embody an embodied agent
toknowthatfireishot(anditshouldbeavoided),orthatwaterincertain
casescanbeusedtodouseafiretomakeitpassable.Itcouldalsobeused
toreasonthatrepeatedattemptstologintoasecureaddressispotentially
anattempttohackadevice,andthatthepeeraddressassociatedwiththis
activitycouldbemonitoredonotheractivities.
KnowledgeRepresentation 145
SEMANTICNETWORKS
Semantic networks are a useful way to describe relationships between a
numbersofobjects.Thisissimilartoanimportantfeatureofhumanmemory,
where there exists a large number of relations. Consider the concept of
freeassociation.ThistechniquewasdevelopedbySigmundFreud,where
thepatientcontinuallyrelatesconceptsgivenastartingseedconcept.The
techniqueassumedthatmemoriesarearrangedinanassociativenetwork,
which is why thinking of one concept can lead to many others. The idea
behindfreeassociationisthatduringtheprocess,thepatientwilleventually
stumbleacrossanimportantmemory.
ConsidertheexampleshowninFigure5.1.Thissimplesemanticnetwork
containsanumberoffactsandrelationshipsbetweenthatknowledge.Typical
semanticnetworksusethe“IS_A”and“AKO”(AKindOf)relationtolink
knowledge.Asshownhere,we’veupdatedtherelationshipstoprovidemore
meaningtothenetwork.Therectanglesinthenetworkrepresentobjects,
andthearcsrepresentrelationships.Herewecanseethattwocapitalcities
areshown,andarecapitalsonthesamecontinent.Onecapitalisofastateof
theUnitedStates,whileanotherisofVenezuela.Simplerelationsalsoshow
thattwocitiesofNewMexicoareAlbuquerqueandSantaFe.
FIGURE5.1: Asimplesemanticnetworkwithasmallnumberoffactsandrelations.
146 ArtificialIntelligence
Theinterestingcharacteristicofsemanticnetworksisthattheyhavethe
abilitytorepresentalargenumberofrelationshipsbetweenalargenumbers
ofobjects.Theycanalsobeformedinavarietyofways,withvaryingtypes
ofrelationships.Theconstructionofthesemanticnetworkisdrivenbythe
particularapplication.
TIP
AninterestingexampleofasemanticnetworkistheUnifiedModeling
Language, or UML. UML is a specification for object modeling in a
graphicalnotation.It’sausefulmechanismtovisualizerelationshipsof
largeandcomplexsystems,andincludestheabilitytotranslatefroma
graphicalform(abstractmodel)toasoftwareform.
FRAMES
Frames, as introduced by Marvin Minsky, are another representation
techniquethatevolvedfromsemanticnetworks(framescanbethoughtof
as an implementation of semantic networks). But compared to semantic
networks,framesarestructuredandfollowamoreobject-orientedabstraction
withgreaterstructure.Theframe-basedknowledgerepresentationisbased
aroundtheconceptofaframe,whichrepresentsacollectionofslotsthatcan
befilledbyvaluesorlinkstootherframes(seeFigure5.2).
AnexampleuseofframesisshowninFigure5.3.Thisexampleincludes
anumberofdifferentframes,anddifferenttypesofframes.Theframesthat
are colored gray in Figure 5.3 are what are called generic frames. These
framesareframesthatdescribeaclassofobjects.Thesingleframewhichis
notcolorediscalledaninstanceframe.Thisframeisaninstanceofageneric
frame.Notealsotheuseofinheritanceinthisexample.TheArchergeneric
framedefinesanumberofslotsthatareinheritedbygenericframesofits
class.Forexample,theLongbowmangenericframeinheritstheslotsofthe
Archergenericframe.Therefore,whiletheweaponslotisnotdefinedinthe
Longbowmanframe,itinheritsthisslotandvaluefromtheArcherframe.
Similarly,‘john’isaninstanceoftheLongbowmanframe,andinherits
the‘weapon’slotandvalueas
well.Notealsotheredefinition
fo the ‘defense’ slot value.
While a frame may define
a default value for this slot,
it may be overridden by the
instanceframe.
FIGURE5.2: Thestructureofaframe.
KnowledgeRepresentation 147
FIGURE5.3: Asetofframesforastrategygame.
Finally, in this example, we also relate generic frames through
comparison.WedefinethataninstanceofanArcherisstrongagainstan
instanceofthePikemanframe.
What makes frames different than semantic networks are the active
components that cause side effects to occur when frames are created,
manipulated,orremoved.Thereareevenelementsthatcontrolactionsfor
slot-levelmanipulations.Thesecanbethoughtofastriggers,butarealso
referredtoasDemons.Addingtriggerstoaframerepresentationlanguageis
calledproceduralattachementandisawaytoincludeinferentialcapabilities
intotheframerepresentation(seeTable5.1).
TABLE5.1: Proceduralattachmentsforuseinframes.
Demon Action
if_new Triggeredwhenanewframeisadded.
148 ArtificialIntelligence
if_added Triggeredwhenanewvalueisplacedintoaslot.
if_removed Triggeredwhenavalueisremovedfromaslot.
if_replaced Triggeredwhenavalueisreplacedinaslot.
if_needed Triggeredwhenavaluemustbepresentinaninstance
frame.
AnexampleofthisisshowninListing5.1.Inthisexample,wedefine
someoftheframesshowninFigure5.3.Withframes(iniProlog),wecan
definerangesforsomeoftheneededparameters(suchasdefense),andif
thevaluefallsoutsideofthisrange,indicatethisissuetoconsole.Thisallows
theframestonotonlyincludetheirownmetadata,butalsotheirownself-
checkingtoensurethattheframesarecorrectandconsistent.
LISTING5.1: FrameexampleswithiProlog.
Archerakoobjectwith
weapon:bow;
armor:light;
strong-against:Pikeman;
weak-against:Heavy-cavalry;
LongbowmanakoArcherwith
origin:britain;
attack:6;
range 0..9
defense:2;
range 0..9
help ifnewvalue>9then
printf(newvalue,“defensetoolarge.”
johnisaLongbowmanwith
if_removed print(“Nolonger...”)!
age:28;
range 1..70
defense3;
Anextensionoftheframeconceptiswhatarecalledscripts.Ascriptis
atypeofframethatisusedtodescribeatimeline.Forexample,ascriptcan
beusedtodescribetheelementsofataskthatrequiremultiplestepstobe
performedinacertainorder.
KnowledgeRepresentation 149
PROPOSITIONALLOGIC
Propositional Logic, also known as sentential logic, is a formal system in
whichknowledgeisrepresentedaspropositions.Further,thesepropositions
canbejoinedinvariouswaysusinglogicaloperators.Theseexpressionscan
thenbeinterpretedastruth-preservinginferencerulesthatcanbeusedto
derivenewknowledgefromtheold,ortesttheexistingknowledge.
First, let’s introduce the proposition. A proposition is a statement,
or a simple declarative sentence. For example, “lobster is expensive” is a
proposition.Notethatadefinitionoftruthisnotassignedtothisproposition;
itcanbeeithertrueorfalse.Intermsofbinarylogic,this
propositioncouldbefalseinMassachusetts,buttrueinColorado.Buta
propositionalwayshasatruthvalue.So,foranyproposition,wecandefine
thetrue-valuebasedonatruthtable(seeFigure5.4).Thissimplysaysthat
foranygivenproposition,itcanbeeithertrueorfalse.
Wecanalsonegateourpropositiontotransformitintotheoppositetruth
value.Forexample,ifP(ourproposition)is“lobsterisexpensive,”then~P
is“lobsterisnotexpensive.”Thisisrepresentedinatruthtableasshown
inFigure5.5.
Propositionscanalsobecombinedtocreatecompoundpropositions.The
first,calledaconjunction,istrueonlyifbothoftheconjunctsaretrue(Pand
Q).Thesecondcalledadisjunction,istrueifatleastoneofthedisjunctsare
true(PorQ).ThetruthtablesfortheseareshowninFigure5.6.Theseare
obviouslytheANDandORtruthtablesfromBooleanlogic.
Thepowerofpropositionallogiccomesintoplayusingtheconditionalforms.
The two most basic forms are called Modus Ponens and Modus Tollens.
ModusPonensisdefinedas:
FIGURE5.6: Truetableforconjunctionanddisjunctionoftwopropositions.
P,(P->Q),inferQ
whichsimplymeansthatgiventwopropositions(PandQ),ifPistrue
thenQistrue.InEnglish,let’ssaythatPistheproposition“thelightison”
and Q is the proposition “the switch is on.” The conditional here can be
definedas:
if“thelightison”then“theswitchison”
So,if“thelightison”istrue,theimplicationisthat“thelightison.”Note
herethattheinverseisnottrue.Justbecause“theswitchison,”doesn’tmean
that“thelightison.”Thisisapieceofknowledgethatgivesussomeinsight
intothestateoftheswitchofwhichweknowthestateofthelight.
Inotherwords,usingthisrule,wehaveawaytosyntacticallyobtainnew
knowledgefromtheold.
NOTE In these examples, we can think of P as the antecedent, and Q as the
consequent.Usingtheif/thenform,theconditionalportionoftheclaim
istheantecedentandtheclaimfollowingthe‘then’istheconsequent.
KnowledgeRepresentation 151
ThetruthtableforModusPonensisshowninFigure5.7.
ModusTollenstakesthecontradictoryapproachofModusPonens.With
ModusTollens,weassumethatQisfalseandtheninferthatthePmustbe
false.ModusTollensisdefinedas:
P,(P->Q),notQ,thereforenotP.
Returningtoourswitchandlightexample,wecansay“theswitchisnot
on,”therefore“thelightisnoton.”Theformalnameforthismethodisproofby
contrapositive.ThetruthtableforModusTollensisprovidedinFigure5.8.
TIP
Tohelpmakesenseofthenames,ModusPonensisLatinfor“modethat
affirms,”whileModusTollensisLatinforthe“modethatdenies.”
DeductiveReasoningwithPropositionalLogic
Indeductivereasoning,theconclusionisreachedfromapreviouslyknownset
ofpremises.Ifthepremisesaretrue,thentheconclusionmustalsobetrue.
Let’s now explore a couple of examples of deductive reasoning using
propositional logic. As deductive reasoning is dependent on the set of
premises,let’sinvestigatethesefirst.
1)Ifit’sraining,thegroundiswet.
2)Ifthegroundiswet,thegroundisslippery.
Thetwofacts(knowledgeabouttheenvironment)arePremise1and
Premise2.Thesearealsoinferencerulesthatwillbeusedindeduction.
Nowweintroduceanotherpremisethatitisraining.
152 ArtificialIntelligence
3)It’sraining.
Now, let’s prove that it’s slippery. First, using Modus Ponens with
Premise1andPremise3,wecandeducethatthegroundiswet:
4)Thegroundiswet. (ModusPonens:Premise1,Premise3)
Again,usingModusPonenswithPremise3and4,wecanprovethat
it’sslippery:
5)Thegroundisslippery.(ModusPonens:Premise3,Premise4)
Notethatinthisexample,thehypotheticalsyllogismwouldworkaswell,
provingthatthegroundisslipperyinasinglestep.
LimitationsofPropositionalLogic
Whilepropositionallogicisuseful,itcannotrepresentgeneral-purposelogic
in a compact and succinct way. For example, a formula with N variables
has 2**N different interpretations. It also doesn’t support changes in the
knowledgebaseeasily.
Truthvaluesofpropositionscanalsobeproblematic,forexample;consider
thecompoundpropositionbelow.Thisisconsideredtrue(usingModusPonens
whereP->QistruewhenPisfalseandQisfalse,seeFigure5.7).
Ifdogscanfly,thencatscanfly.
Bothstatementsareobviouslyfalse,andfurther,there’snoconnection
betweenthetwo.Butfromthestandpointofpropositionallogic,theyare
syntacticallycorrect.Amajorproblemwithpropositionallogicisthatentire
propositionsarerepresentedasasinglesymbol.Inthenextsection,we’ll
lookatanotherlogicrepresentationthatpermitsfinercontrolandamore
accuraterepresentationofanenvironment.
FIRST-ORDERLOGIC(PREDICATELOGIC)
Intheprevioussection,propositionallogicwasexplored.Oneissuewiththis
typeoflogicisthatit’snotveryexpressive.Forexample,whenwedeclare
apropositionsuchas:
Thegroundiswet.
it’snotclearwhichgroundwe’rereferringto.Norcanwedetermine
whatliquidismakingthegroundwet.Propositionallogiclackstheability
totalkaboutspecifics.
Inthissection,we’llexplorepredicatecalculus(otherwiseknownasFirst-
OrderLogic,orFOL).UsingFOL,wecanusebothpredicatesandvariablesto
addgreaterexpressivenessaswellasmoregeneralizationtoourknowledge.
KnowledgeRepresentation 153
In FOL, knowledge is built up from constants (the objects of the
knowledge),asetofpredicates(relationshipsbetweentheknowledge),and
somenumberoffunctions(indirectreferencestootherknowledge).
AtomicSentences
Aconstantreferstoasingleobjectinourdomain.Asamplesetofconstants
include:
marc,elise,bicycle,scooter,the-stranger,colorado
A predicate expresses a relationship between objects, or defines
propertiesofthoseobjects.Afewexamplesofrelationshipsandproperties
aredefinedbelow:
owns,rides,knows,
person,sunny,book,two-wheeled
Withourconstantsandpredicatesdefined,wecannowusethepredicates
todefinerelationshipsandpropertiesoftheconstants(alsocalledAtomic
sentences). First, we define that both Marc and Elise are ‘Persons.’ The
‘Person’isapropertyfortheobjects(MarcandElise).
Person(marc)
Person(elise)
Theabovemayappearasafunction,withPersonasthefunctionand
Marc or Elise as the argument. But in this context, Person(x) is a unary
relationthatsimplymeansthatMarcandElisefallunderthecategoryof
Person.NowwedefinethatMarcandElisebothknoweachother.Weuse
theknowspredicatetodefinethisrelationship.Notethatpredicateshave
arity,whichreferstothenumberofarguments.The‘Person’predicatehas
anarityifonewherethepredicate‘knows’hasanarityoftwo.
Knows(marc,elise)
Knows(elise,marc)
We can then extend our domain with a number of other atomic
sentences,shownanddefinedbelow:
Rides(marc,bicycle) -Marcridesabicycle.
Rides(elise,scooter) -Eliseridesascooter.
Two-Wheeled(bicycle) -ABicycleistwo-wheeled.
Book(the-stranger) -The-Strangerisabook.
Owns(elise,Book(the-stranger)) -E lise owns a book called The
Stranger.
Finally,afunctionallowsustotransformaconstantintoanotherconstant.
Forexample,thesister_offunctionisdemonstratedbelow:
Knows(marc,sister_of(sean)) -MarcknowsSean’ssister.
154 ArtificialIntelligence
CompoundSentences
Recall from propositional logic that we can apply Boolean operators to
buildmorecomplexsentences.Inthisway,wecantaketwoormoreatomic
sentencesandwithconnectives,buildacompoundsentence.Asampleset
ofconnectivesisshownbelow:
∧ AND
∨ OR
¬ NOT
⇒ LogicalConditional(then)
⇔ LogicalBiconditional
Examplesofcompoundsentencesareshownbelow:
Knows(marc,elise)∧Knows(elise,marc)
- MarcandEliseknowoneanother.
Knows(marc,elise)∧¬Knows(elise,marc)
- MarcknowsElise,andElisedoesnotknowMarc.
Rides(marc,scooter)∨Rides(marc,bicycle)
- MarcridesascooterorMarcridesabicycle.
Wecanalsobuildconditionalsusingthelogicalconditionalconnective,
forexample:
Knows(marc,elise)⇒Knows(elise,marc)
- IfMarcknowsElise,thenEliseknowsMarc.
Thiscanalsobewrittenasabiconditional,whichchangesthemeaning
slightly. The biconditional, a ⇔ b simply means “b if a and a if b,” or “b
impliesaandaimpliesb.”
Knows(marc,elise)⇔Knows(elise,marc)
- MarcknowsEliseifEliseknowsMarc.
NOTE Anotherwaytothinkaboutthebiconditionalisfromtheconstructionof
twoconditionalsintheformofaconjunction,or:
(a⇒b)∧(b⇒a)
Thisimpliesthatbotharetrueorbotharefalse.
Variables
Sofar,we’veexploredsentenceswherealloftheinformationwaspresent,
buttobeuseful,weneedtheabilitytoconstructabstractsentencesthatdon’t
specifyspecificobjects.Thiscanbedoneusingvariables.Forexample:
Knows(x,elise)⇒Person(x)
- IfxKnowsElise,thenxisaPerson.
KnowledgeRepresentation 155
Ifwealsoknewthat:‘Knows(marc,elise)’thenwecoulddeducethat
Marcisaperson(Person(marc)).
Quantifiers
Let’snowbringittogetherwithquantifiers.Aquantifierisusedtodetermine
thequantityofavariable.Infirst-orderlogic,therearetwoquantifiers,the
universal quantifier (∀) and the existential quantifier (∃). The universal
quantifierisusedtoindicatethatasentenceshouldholdwhenanythingis
substitutedforthevariable.Theexistentialquantifierindicatesthatthere
issomethingthatcanbesubstitutedforthevariablesuchthatthesentence
holds.Let’slookatanexampleofeach.
∃x.Person(x)
-Thereexistsx,thatisaPerson.
∀x.∃x.Person(x)∧(Knows(x,elise)∨Knows(x,marc))
-Forallpeople,thereexistssomeonethatKnowsMarcorElise.
∀x.∃x.Knows(x,elise)⇒Person(x)
-Foranyx,ifthereissomeonexthatKnowsElise,thenxisaPerson.
Nowthatwehaveabasicunderstandingoffirst-orderlogic,let’sexplore
FOLfurtherwithProlog.
TIP
Chapter13providesanintroductiontothelanguagesofAI,including
Prolog.
First-OrderLogicandProlog
PrologisactuallybasedonaversionofFOLwithsomesubtledifferences
thatwe’llexplore.Prologisinessencealanguagefortherepresentationof
knowledge.Itallowsthedefinitionoffacts(orclauses)andalsorules(which
arealsoclauses,buthavebodies).Let’sbeginwithaquickintroductionto
Prologandfollowwithafewexamplestodemonstratebothitsknowledge
representationandreasoningabilities.
TIP
A Prolog environment is made up of a number of elements, but three
of the important items are the knowledge base (rules and facts), the
inferenceengine,andtheinterpreter(theinterfacetotheinferenceengine
andknowledgebase).
SimpleExample
Let’sstartwithanexamplethatwasfirstexploredwithpropositionallogic.
Considertheargument:
156 ArtificialIntelligence
Allmenaremortal.
Socratesisaman.
Therefore,Socratesismortal.
Wecantranslateeverythingbuttheconclusionintopredicatelogicas
follows:
∀xMan(x)⇒Mortal(x)
Man(Socrates)
SoforallXwhereXisaMan,thenXisalsoaMortal.We’veprovidedthe
predicateindicatingthatSocratesisaMan,thereforeSocratesisalsoMortal.
Nowlet’sseehowthisistranslatedintoProlog.First,we’lldefinetherule:
mortal(X):-
man(X).
NotethatinProlog,theruleisdefineddifferentlythantheFOLargument
(asaHornclause).TothePrologtheoremprover,thiscanbereadas“toshow
mortal(X),solveman(X).”Therulecanalsoberead“mortal(X)ifman(X).”
Notetheperiodattheendoftheruleindicatestheendofthesentence.
Next,weprovideourfact(Socratesisaman):
man(socrates).
Notethatthisisidenticaltodefiningaruleforafact,whichcouldbe
specifiedas:
man(Socrates):-true.
Alloftheinformationhasnowbeendefined.Aquerycannowbeissued
toPrologtotesttheconclusion.Twomethodsareshownbelow.Thefirst
simplyteststheconclusion,andthesecondisaqueryforthoseobjectsthat
satisfythepredicate:
|?-mortal(Socrates).
yes
|?-mortal(X)
X=socrates
yes
KnowledgeRepresentation 157
Noteinthesecondexamplethatwe’verequestedthatPrologmatch
forthepredicate(forwhatobjectsdoestheexpressionevaluatetotrue?).
It returns with the X variable instantiated to Socrates (the expected
result).
NOTE In the early 1980s, Japan began the Fifth Generation Computer
Systems project. The goal of this project was the development of a
“fifth generation” super-computer built for massive parallelism with
AIasitsprimaryapplication.Thecorelanguageforthecomputerwas
Prolog, but as Prolog at the time did not support concurrency, other
languages were developed in its place. While the project was under
development,technologyaroundtheworldevolvedindifferentdirections
(microprocessorsandsoftware).Theprojectdidnotmeetwithsuccess,
but did produce five parallel machines and a variety of applications.
[Feigenbaum1983]
InformationRetrievalandKR
Prologisoftenviewedasawaytointelligentlystoreandretrieveinformation,
butcandosoinawaythatmakesitmoreusefulthanwhatiscommonly
available in a traditional relational database system. Let’s begin with an
exampleofusingPrologtorepresentasmallamountofknowledgeandthen
exploreafewrulestomakesenseoftheknowledge.
The first step is to define the domain of knowledge, or the facts for
whichthelaterruleswillutilize.Thisexamplewilluseasetofsimplefacts
abouttheSolarSystem,beginningwiththoseplanetsthatorbitthesun.A
commonpredicatewillbeusedcalledorbits,whichwillbeusednotonly
to describe the planets (objects that orbit the sun) but also to define the
satellitesthatorbittheplanets(seeListing5.1).NoteinListing5.1thatthe
planetsarefirstdefined(thosethatorbitthesun).Thisisfollowedbyeach
oftheplanets.Eachplanetthathassatellitesusestheorbitspredicateto
identifythesatellitesthatorbiteachoftheplanets(onlyasampleareshown
inListing5.1).Finally,thegaseous_planetpredicateisusedtodefinethe
gaseousgiantsinthesolarsystem.
LISTING5.1: AknowledgebaseoffactsabouttheSolarSystem.
orbits(sun,mercury).
orbits(sun,venus).
orbits(sun,earth).
orbits(sun,mars).
158 ArtificialIntelligence
orbits(sun,jupiter).
orbits(sun,saturn).
orbits(sun,uranus).
orbits(sun,neptune).
orbits(earth,the_moon).
orbits(mars,phobos).
orbits(mars,deimos).
orbits(jupiter,metis).
orbits(jupiter,adrastea).
…
orbits(saturn,pan).
orbits(saturn,atlas).
…
orbits(uranus,cordelia).
orbits(uranus,ophelia).
…
orbits(neptune,naiad).
orbits(neptune,thalassa).
…
gaseous_planet(jupiter).
gaseous_planet(saturn).
gaseous_planet(uranus).
gaseous_planet(neptune).
With this set of facts, a set of rules can now be defined to embed
additionalknowledgeintothedatabaseaswellassupportasmallamountof
reasoning.First,recallthataplanetisdefinedasamajorobjectthatorbits
thesun.Thisrulecanusetheorbitspredicatewiththesuntoidentifythose
itemsthatareplanets(andnotsatellites).
planet(P):-
orbits(sun,P).
Asshown,anyobject(fromourknowledgebase)thatorbitsthesunis
a planet. With this definition, we can easily define a rule for satellites. A
satelliteissimplyanobjectthatorbitsaplanet.
satellite(S):-
orbits(P,S),planet(P).
Thispredicateusesaconjunctionwhichfirstusesthesatelliteobject(S)
todeterminewhichobjects(P)withtheorbitspredicatearesatisfied.Ifthis
predicateproducesaPobject,andit’saplanet(usingtheplanetpredicate),
thentheSisasatellite.
KnowledgeRepresentation 159
Thenextruleseparatesthegasgiantplanets(gaseousplanets)fromthe
terrestrialplanets.NotethatintheendofListing5.1,thegaseousplanetsare
definedusingthegaseous_planetpredicate.Thisisusedasshownbelowin
thenegatedform(\+)inconjunctionwiththeplanetpredicate,todetermine
ifanobjectisaterrestrialplanet.
terrestrial_planet(P):-
planet(P),\+gaseous_planet(P).
Finally, with the satellite data that’s provided, it’s very simple to
determine whether the object has no satellites. Given the planet object
(whichisvalidatedwiththeplanetpredicate),thisisusedinconjunction
withtheorbitspredicatetodetermineifnoresultsarereturnedwiththe
negation(\+).
no_moons(P):-
planet(P),\+orbits(P,S).
Having a predicate that identifies that a planet has no satellites (no_
moons), it’s very simple to negate this to identify whether a planet has
satellites(has_moons).
has_moons(P):-
\+no_moons(P).
This example illustrates an interesting property of knowledge
representation and that of human accumulation of knowledge as well.
Knowledgecanbebuiltonotherknowledge.Givenaset,andadistinction
between a portion of that set, classifications can be further defined and
refinedfromaninitiallysmallsetofknowledge.Forexample,knowingthata
planetorbitsthesun,weknowthatallotherobjectsthatorbitanobjectother
thanthesunaresatellites.Asatelliteisamoonofanobject,soit’ssimpleto
determinethoseobjectsthathavemoonsandthosethatdon’t.Thisproperty
canalsobeacursebecauseastherelationshipsgrowbetweenthedata,any
errorcanhavewideeffectsovertheentireknowledgebase.
RepresentingandReasoningaboutanEnvironment.
In addition to simple reasoning with a database, Prolog can be used to
representanenvironmentandtoreasonaboutit.Let’snowexploreavery
simplemapthatidentifiesasetoflocationsandthenlookatafewfunctions
thatprovidePrologwiththeabilitytotraversethemap.NoteinFigure5.9
thatourmapisreallynothingmorethanasimplegraph.Theconnections
inthemaparebidirectional(sothatyoucangetfromLyonstoLongmont,
andalsofromLongmonttoLyons).
RecallfromChapter2thatinagraph,thereexistsasetofedges.Anedge
isaconnectionbetweentwoverticesonthegraph.Sothefirstiteminour
160 ArtificialIntelligence
FIGURE5.9: Maprepresentedasasimplebidirectionalgraph.
knowledgebasearetheedgesinthegraph(whichcitiesconnecttowhich
othercities).Forthis,we’lluseapredicatecallededge,whichindicates
anedgebetweenthetwoobjects(seeListing5.2).Noteherethatonlyone
directionisshown,aswe’llletPrologknowaboutthebidirectionalusage
whenwedefinetherules.
LISTING5.2: DefiningtheedgesofoursimplemapforProlog.
edge(lyons,fort_collins).
edge(lyons,longmont).
edge(lyons,boulder).
edge(boulder,longmont).
edge(boulder,denver).
edge(longmont,lyons).
edge(longmont,boulder).
edge(longmont,fort_collins).
edge(longmont,denver).
edge(fort_collins,denver).
With this set of facts defined, we can perform some queries with
Prolog.Forexample,wecanprovideacity,andthenquerywhichcitiesare
connectedwiththepredicate:
|?-edge(longmont,Y).
Y=lyons?;
KnowledgeRepresentation 161
Y=boulder?;
Y=fort_collins?;
Y=denver
yes
|?-
Wecouldalsousethepredicateintheformedge(X, longmont),but
sincewedon’tdetailthebidirectionalnatureofourgraph,thisisn’tasuseful.
Next,aruleisdefinedtodeterminetheconnectednessoftwocities.We’ll
callthisruleconnect,anditwillreturntrueifthecitiesareconnectedonthe
mapbyanedge,orfalseiftheyarenotconnected(seeListing5.3).Notehow
thisruleisconstructed.Weusethepredicateedgetoquerywhetheranedge
hasbeendefined,butsincewedon’tencodeeachdirectioninthefacts,we
cantestbothcasesheretoensurethatwecatchafactthatindicatesthatthe
edgeexists.RecallthatthesemicolonisaBooleanORoperation,soifoneor
theotheristrue,thenwereturntrue(thatanedge,andconnectionexists).
LISTING5.3: Definingaruletotestwhetheranedgeexists.
connected(X,Y):-
edge(X,Y);edge(Y,X).
Nowlet’sexploreasetofrulesthatcanbeusedtodetermineoneor
morepathsbetweencitiesinoursimplemap.We’llcalltherulepath,andit
usestwootherrules;traverseandreverse.Ruletraverseisarecursiverule
thatsearchesforapathbetweenastartingandendingcity(labeledXand
Y).ItalsomaintainsaPathvariable,whichisthequerybywhichtheruleis
defined.Oncetraversehasfoundapath,thereverseruleisusedtoreverse
theorderofthepathfoundtoreturnittotheuser(seeListing5.4).Notethe
useof[X]inthetraverserule.Thebracketsindicatethatalistisbeingbuilt,
andthecontentsofthelistisdefinedasX,orthefirstcityinthepath.
LISTING5.4: Rulestorecursivelydeterminepaths.
path(X,Y,Path):-
traverse(X,Y,[X],Q),
reverse(Q,Path).
Twohigh-levelruleswillbecreatedtotraversethegraph.Thefirst(shown
inListing5.5)isthespecialcaseofapathbetweenjusttwocities.Inthiscase,
162 ArtificialIntelligence
theconnectedpredicateisusedtodeterminethisconditionandnorecursive
callsareperformed.Notethatinthetraversecall,thepathisconstructedwith
Y(endcity)astheheadandthepathasthetail.Thisisdonebecausethelist
isbuiltintheorderofthemostrecentlyvisitedcity.Theinitialcityisthelast
elementinthelist,andthemostrecentlyvisitedisthefirstitem.Whenthe
reverseisused,thelistwillappearnormallyinoutput(firsttolastcity).
LISTING5.5: Firstrulefortraverse.
traverse(X,Y,P,[Y|P]):-
connected(X,Y).
Thesecondmorecomplexrulecreatesarecursivecallafteranumberoftests
(preconditions).First,theconnectedpredicateisusedtoreturnthecities
connectedtothecurrentcity(X).Thisstepdeterminesthenextpossible
stepsinthepath,forwhichtheremaybenumerous.Eachisreturnedasa
listconstructedseparately(separatecallstotraverseforseparatepaths).The
nextcitytovisitiscalledZ,andthenexttestistoensurethatthenextcity
isnotourdestination(Z\==Y).
Finally,weusethememberfunctiontotestwhetherthecurrentcityis
partofthecurrentpath(sincewewon’tvisitacitymorethanonce).Ifthese
testspass,thenwemakearecursivecalltotraversewithZasthecurrentcity
(nexttovisit),andYasthedestination.Thepathisupdated,addingZtothe
citypath.Thecallsthencontinue(foreachpath),untiloneormoreofthe
testsfail,uponwhichtherecursivecallsstopandthecallchainisunwound
(returnedtothemaincaller).(SeeListing5.6.)
LISTING5.6: Secondrecursiverulefortraverse.
traverse(X,Y,V,Path):-
connected(X,Z),Z\==Y,\+member(Z,V),
traverse(Z,Y,[Z|V],Path).
Aquerycannowbeperformedtodeterminetheavailablepathsfrom
acitytoanothercity.AnexampleofthisprocessusingPrologisshownin
Listing5.7.Notethatallpermutationsofthepathsarereturned.
LISTING5.7: Atestqueryofthemapknowledgebase.
|?-path(lyons,denver,P).
P=[lyons,fort_collins,denver]?;
P=[lyons,fort_collins,longmont,denver]?;
KnowledgeRepresentation 163
P=[lyons,fort_collins,longmont,boulder,denver]?;
P=[lyons,fort_collins,longmont,boulder,denver]?;
P=[lyons,longmont,denver]?;
P=[lyons,longmont,boulder,denver]?;
P=[lyons,longmont,fort_collins,denver]?;
P=[lyons,longmont,boulder,denver]?;
P=[lyons,boulder,denver]?;
P=[lyons,boulder,longmont,denver]?;
P=[lyons,boulder,longmont,fort_collins,denver]?;
P=[lyons,boulder,longmont,denver]?;
P=[lyons,boulder,longmont,fort_collins,denver]?;
P=[lyons,longmont,denver]?;
P=[lyons,longmont,boulder,denver]?;
P=[lyons,longmont,fort_collins,denver]?;
P=[lyons,longmont,boulder,denver]?;
(10ms)no
|?-
SEMANTICWEB
The Semantic Web is the name behind an effort to change the way that
thewebisdefinedandinterpreted.Today,theInternetismadeupofhosts
andcontentthatispredominantlydefinedthroughtheHyper-TextMarkup
Language(orHTML).Thislanguageprovidesawaytoaddtexturetoweb
pages, so that instead of simply being text documents, other information
canbeincludedsuchasaudioorgraphicalinformation.ButHTMLsimply
providesawaytobeautifycontentsothatitcanberenderedinadevicein
anindependentway.
A simple example of HTML is shown in Listing 5.8. This example
defines a simple web page that includes a title (that is placed on the
header of the browser) and a body that simply emits two paragraphs.
Eachparagraphinthisexampledefinesanauthorandatitlethatthey’ve
authored.UsingtheparagraphHTMLtag(<p>),theselinesareseparated
byasingleline.
LISTING5.8: AsimpleexampleofHTML.
<html>
<head>
164 ArtificialIntelligence
<title>AuthorList</title>
</head>
<body>
<p>JohnDoeArticleA</p>
<p>JaneDoeArticleB</p>
</body>
</html>
SofromListing5.8,weseethatit’srelativelyeasytocreateawebpage
thatcanbeviewedbyanystandardwebbrowser.Whilethisisuseful,it’s
onlyusefulfromahumanviewingperspective.Thebrowserunderstands
howtorendertheinformationgiventhemarkup,butitknowsnothingof
thecontent.Ifwe’retobuildsoftwareagentsthatcanreadwebpagesand
understandtheircontent,somethingelseisrequired.ThisiswhereRDF,
orResourceDescriptionFramework,canbeapplied.
RDF allows the definition of metadata within the markup, called
statements.EachstatementinRDFcontainsthreestructuralparts,asubject,
predicate,andobject(seeListing5.9).Alsoprovidedinthisexampleisthe
namespace(codedxmlns),whichprovidesadisambiguousspaceofnames
defined by a Uniform Resource Locator (or URL, a web address). The
namespaceprovidesaclarificationofthetermsusedwithintheRDF.
LISTING5.9: AsimpleexampleofRDF.
<rdf:RDF
xmlns:rdf=”http://www.w3.org/2006/09/test-rdf-syntax-ns#”
xmlns=”http://schemas.mtjones.com/rdftest/”>
<rdf:Description=”http://mtjones.com/rdf”>
<publications>
<rdf:DescriptionID=”mtjones.com”>
<article>
<author>JohnDoe</author>
<title>ArticleA</article>
</article>
<article>
<author>JaneDoe</author>
<title>ArticleB</article>
</article>
</rdf:Description>
</publications>
KnowledgeRepresentation 165
</rdf:Description>
</rdf:RDF>
What’s important from this example is that the data is provided in a
formatthatmakesitsimpletoparseandunderstandtheelementsofthedata.
Thisisbecausethedataismarkedwiththemeaningofthedata(metadata).
Notethatarticledefinesanauthorandtitleofanarticle(asdefinedbythe
author tag and the title tag). In this way, an RDF parser can embody an
application(orsoftwareagent)withtheabilitytounderstandthedatathat’s
beenprovided.
COMPUTATIONALKNOWLEDGEDISCOVERY
Computational Knowledge Discovery systems provide an interesting
perspective on creativity and human knowledge creation. Many of these
systems rely on the use of heuristics to constrain the search as well as
the recursive application of a small set of discovery methods. They also
commonlyrelyontheuseofsimpleforms(oftheinitialknowledge)toaid
inthediscoveryprocess.[Wagman2000]Twoofthemostfamoussystems
areBACONandtheAutomatic
Mathematician(AM).
Thetopicathanddiffersfromothermachinelearningtechniquesbased
on their methods for discovering knowledge. Machine-learning or data
miningmethods,produceknowledgeinavarietyofforms,suchasdecision
trees.Knowledge(orScientific)Discoverymethodsuseformalismssuchas
equationsproducingotherequationstorepresentthediscoveredknowledge.
Buteachmethodlooksattheinitialdataandtherelationshipsofthatdata
usingaheuristicsearchtoproducenewknowledge.
One problem with computational knowledge discovery is that its use
requires post-processing by a human to filter and interpret the resulting
knowledge. From this perspective, computational knowledge discovery is
betterdescribedasassistedknowledgediscovery,asahumaniscommonly
requiredtointerpretandapplytheresults.Butevenwiththisdrawback,
computationalknowledgediscoveryhasproducednewknowledgeinmany
scientificfields(leadingtopublicationsinrefereedliterature).
TheBACONSystem
BACONisactuallyaseriesofdiscoverysystemsdevelopedoveranumber
ofyears.OneaspectofBACONisitsabilitytodiscoversimplephysicallaws
166 ArtificialIntelligence
giventherelationshipsbetweentwovariables.Forexample,usingthesimple
generalmethodsprovidedbyBACON,twovariablescanbereviewedfortheir
relationship.Ifonevariable(X)increasesandtheothervariable(Y)increases,
thentheINCREASINGheuristiccanbeapplied,causingBACONtoanalyze
theirratio(X/Y).Ifonevariableincreaseswhiletheotherdecreases,thenthe
DECREASINGheursticcanbeemployed(causingBACONtoanalyzetheir
product,XY).Finally,ifthevalueXremainsconstant(ornearlyconstant),
BACONcanhypothesizethatXmaintainsthisvalue.
NotethatwhileBACONisprovidedaseriesofdata(oftwovariables),it
hasthecapacitytocreatenewdatabasedonthepredefinedrules(providing
ratios,products,andconstants).BACONusesthesenewseriesaswellin
itsattempttoanalyzethedata.Whiletheserulesareverysimple,BACON
usedthemtosuccessfullyrediscoverKepler’sthirdlawofplanetarymotion
(seeEq5.1,wheredistheplanet’sdistancefromtheSun,pisitsperiod,
andkisaconstant).
(Eq5.1)
AutomaticMathematician(AM)
TheAutomaticMathematician(orAM)isamathematicaldiscoverysystem
withsignificantdifferencesfromBACON.AMwasdesignedbyDougLenat
to discover new concepts in elementary mathematics and set theory. But
ratherthansearchingforasolutiontoaproblem,orapredefinedgoal,AM
simplyfollows“interesting”heuristicsandgeneratesnewexampleslooking
forregularities.Whenregularitiesarefound,conjecturescanbecreated.
WhentheAMisstarted,it’sprovidedwithafewfactsaboutitsdomain
(simplemathconcepts)usingtheframeknowledgerepresentationtechnique.
Forexample,AMcouldunderstandtheconceptofprimenumbers.Given
theseconceptsandrulesofthumb,AMthenappliestheseconceptsbasedon
theirworth(themoreinterestingarule,themorelikelyitistobeused).
AMusesasetofgeneralmethodstocreatenewmathematicalconcepts
(such as creating the inverse of an existing relation, or specializing by
KnowledgeRepresentation 167
restricting the domain of a concept). The underlying model used by AM
was small LISP programs, which represented a variety of mathematical
concepts.NewLISPprograms,weregeneratedandmodifiedtorepresent
newconcepts(basedontheirinterpretationwithaLISPinterpreter).
AMdidnotdiscovertheconceptsonitsown,butreliedonahuman
interpretertoreviewthedataproducedtounderstandwhatitfound.AM
didrediscovernumerous mathematical concepts, such as prime numbers
andGoldbach’sconjecturebutinsteadofidentifyingthesecharacteristics,
itinsteadhasbeencriticizedasprovidingthedatatomakethediscovery
withhumanreview.
NOTE Doug Lenat followed the development of AM with another discovery
system called EURISKO. This development, instead of searching
for mathematical concepts, focused instead on the search for useful
heuristics.
ONTOLOGY
AnOntologyisacoreconceptinmodernknowledgerepresentation,though
the ideas behind it have been in existence since the beginning of KR. An
ontologyfromtheperspectiveofAIisamodelthatrepresentsasetofconcepts
withinaspecificdomainaswellastherelationshipsbetweenthoseconcepts.
RecalltheexampleofthesemanticnetworkinFigure5.1.Thisisan
ontologyforthedomainofplacesandcapitals.The‘is-a’typeofrelationship
definesahierarchicaltaxonomythatdefineshowtheobjectsrelatetoone
another.
TIP
NotethatFigure5.1canalsobedefinedasasetofMeronymyrelations,
asitdefineshowtheobjectscombinetoformcompositeobjects(citiesto
statestocountriestocontinents).
Aninterestinguseofanontologyisintheformofalanguageasameans
to encode an ontology for the purposes of communicating knowledge
betweentwoentities(agents).Thisisexploredinthenextsection.
COMMUNICATIONOFKNOWLEDGE
Havingavastrepositoryofknowledgeismostusefulifitcanbesharedand
usedfromavarietyofperspectives.Beingabletoshareknowledgeallows
168 ArtificialIntelligence
multipledisparateagentstocooperativelyusetheavailableknowledge,and
modifytheknowledge(suchasisdoneinblackboardarchitectures).
NOTE Youcanlearnmoreaboutknowledgecommunicationandsharingwith
agentsinChapter14“AgentArchitectures.”Thesearecommonlycalled
“AgentCommunicationLanguages,”orACLs.
Examplesofprotocolsthatenablesharingofknowledgebetweenagents
includetheWebOntologyLanguage(OWL)usedintheSemanticWeb,the
Knowledge Query and Manipulation Language (KQML), and the Agent
CommunicationLanguage(ACL).
COMMONSENSE
ComputersystemsthatincludesomeformofAIforagivenproblemdomain
canstillrightlybeconsideredunintelligentbecausetheylacksomeofthe
mostfundamentalknowledgecalledcommonsense.Forexample:
• Objects fall toward Earth (due to gravity) and not up away from the
Earth.
• Ifyougetnearafire,youcouldbeburned.
• It’sdangeroustoflyakiteinalightningstorm.
To enable reasoning at a common-sense level, a common-sense
knowledge base is proposed that contains common-sense knowledge that
mostpeoplepossess.Thisknowledgebaseisconstructedinawaythatan
applicationcanuseittocreateinferences(toreason).Examplesofthetype
ofknowledgethatisrepresentedincludesbehaviorofitems,effectsofactions
(yellingatsomeonemaymakethemangry),andpreconditionsofactions(one
putstheirsocksonbeforetheirshoes).Manyothertopicscouldbecovered
such as the properties of objects (fire is hot), and descriptions of human
behaviors(ifsomeoneiscrying,thentheymaybesad).
An interesting example of a common-sense project is called Cyc.
ThisprojectwascreatedbyDougLenat(whoalsocreatedtheAutomatic
Mathematician).TheCycknowledgebaseincludesoveramillionconcepts
and rules, which are defined in a language based on predicate calculus
(similartotheLISPprogramminglanguage).Examplesofbasicknowledge
encodedinCycinclude:
(#$capitalCity#$Colorado#$Denver)
whichencodes“DenveristhecapitalcityofColorado.”
(#$genls#$Men#$Mortal)
KnowledgeRepresentation 169
which represents “All men are mortal.” The Cyc knowledge base is
partitionedintoanumberofcollectionsofknowledgecalledmicrotheories.
A microtheory is a contradiction-free of concepts and facts about a
particular domain of knowledge (like an ontology). Microtheories can
berelatedtooneanotherandareorganizedintoahierarchy,supporting
inheritance.
The development of Cyc and its inference engine continues, but the
majorworkfocusisprimarilyinknowledgeengineeringorhand-encoding
knowledgeandrulestorepresentbasiccommon-senseknowledge.
CHAPTERSUMMARY
Knowledgerepresentationfocusesonthemechanismsbywhichinformation
can be stored and processed in a way to make the information useful as
knowledge.Thischapterpresentedsomeofthemoreimportantmechanisms
forknowledgerepresentation,suchassemanticnetworksandframes,andalso
methodsthatenablenotonlystorageofknowledge,butalsoprocessing,such
aspropositionallogicandfirst-order(orpredicate)logic.Finally,applications
ofknowledgerepresentationwereexploredincludingcomputationalscientific
discovery,andcommon-sensereasoningwiththeCycproject.
REFERENCES
[Feigenbaum 1983] Feigenbaum, Edward A., McCorduck, Pamela “The
FifthGeneration:ArtificialIntelligenceandJapan’sComputerChallenge
totheWorld.”1983.
[Wagman 2000] Wagman, Morton. “Scientific Discovery Processes in
Humans and Computers: Theory and Research in Psychology and
ArtificialIntelligence,”PraegerPublishers,2000.
RESOURCES
Brachman,RonaldJ.,“WhatIS-Aisandisn’t.AnAnalysisofTaxonomicLinks
inSemanticNetworks.”IEEEComputer,16:(10),October,1983.
Langley,P.“BACON.1:Ageneraldiscoverysystem.”InProceedingsofthe
secondbiennialconferenceoftheCanadianSocietyforComputational
StudiesofIntelligence,”173-180,1978.
170 ArtificialIntelligence
Lenat, D.B. “AM: An artificial intelligence approach to discovery in
mathematicsasheuristicsearch,”Ph.D.Theses,AIM-286,STAN-CS-
76-570,StanfordUniversity,AILab,Stanford,1976.
Lenat,D.B.,andBrown,J.S.“WhyAMandEURISKOappeartowork.”
ArtificialIntelligence23(3):269-294,1984.
Post,Emil“IntroductiontoaGeneralTheoryofPropositions,”American
JournalofMathematics43:163-185.
Freud,Sigmund“TheInterpretationofDreams,”Macmillan,1913.
ObjectManagementGroup,“UMLSpecification,”2007.
Available online at: http://www.omg.org/technology/documents/modeling_
spec_catalog.htm
Woods,W.“What’sinaLink:FoundationsforSemanticNets,”Representation
andUnderstanding:StudiesinCognitiveScience,AcademicPress,1975.
EXERCISES
1. Defineknowledgerepresentationinyourownwords.Whatareitsmost
importantcharacteristics?
2. Definetwotypesofknowledgeandtheirdifferences.
3. Considerafamilytree,andrepresentthisusingasemanticnetwork.
4. Frames include what is called a procedural attachment that can be
used to trigger functions for various types of events. Describe these
attachmentsanddemonstratehowtheycanbeused.
5. DefineModusPonensandModusTollensandprovideanexampleofeach.
6. Definetheuniversalandexistentialquantifierusedinpredicatelogic.
7. Representeachofthefollowingsentencesinfirst-orderlogic:
a.Awhaleisamammal.
b.JanelovesJohn.
c.JohnknowsJane’sfather.
d.Ifit’sraining,thenthegroundiswet.
e.Iftheswitchisonandthelightisoffthenthelight-bulbisbroken.
f.Allcomputershaveaprocessor.
8. Describetheadvantagesofpredicatelogicoverpropositionallogic.
9. Representthesentencein7.dinProlog.
10. Describe the purpose behind the Semantic Web. What is its
representationandhowdoesithelp?
Chapter 6 MACHINE
LEARNING
M
achinelearning,asthenameimplies,focusesonalgorithmsand
methods that allow a computer to learn. Another way to think
aboutthisisfromtheperspectiveoflearningbyexample.Givena
setofdata,machinelearningcanlearnaboutthedataandtheirrelationships,
producinginformation.Inthischapter,machinelearningwillbeinvestigated
including a number of machine-learning algorithms. Algorithms to be
exploredincludedecisiontrees,Markovmodels,nearestneighborlearning,
andothers.
MACHINE-LEARNINGALGORITHMS
There exist numerous types of machine-learning algorithms. Some of
the more popular approaches include supervised learning, unsupervised
learning,andprobabilisticlearning.
Supervisedlearningalgorithmsimplythatateacherispresenttoidentify
whenaresultisrightorwrong.Theinputdatacontainsbothapredictor
(independentvariable)andtarget(dependentvariable)whosevalueistobe
estimated.Throughtheprocessofsupervisedlearning,thealgorithmpredicts
thevalueofthetargetvariablebasedonthepredictorvariables.Examples
172 ArtificialIntelligence
SupervisedLearning
Supervised learning algorithms use training data that has been classified
(hasatargetvalueforeachtrainingvector).Thepurposeofthesupervised
learningalgorithmistocreateapredictionfunctionusingthetrainingdata
thatwillgeneralizeforunseentrainingvectorstoclassifythemcorrectly.
Inthissection,decision-treelearningisexploredtoconstructadecision
treefromobservedbehavior.
LearningwithDecisionTrees
Oneofthemostintuitiveandpracticalmethodsforsupervisedlearningis
thedecisiontree.Adecisiontreeistheresultofaclassificationprocessin
whichthesourcedataisreducedintoapredictortreethatrepresentsaset
of if/then/else rules. As an example, consider an observer that watched a
FPSplayerandrecordedtheiractionswhenconfrontedwithanenemy.The
resultisshowninTable6.1
Table6.1: Observedactionsofaplayerinafirst-person-shootergame.
Weapon Ammo Health Behavior
Gun Full Low Fight
Gun Low Full Evade
Knife Low Full Fight
Knife Low Low Evade
MachineLearning 173
FIGURE6.1: AsimpledecisiontreeforanFPSplayer.
FIGURE6.2: Conditionalexpressionresultingfromthesimpledecisiontree.
Asimpleexampleofadecisiontree,createdfromtheobserveddatain
Table6.1,isshowninFigure6.1.
AsshowninFigure6.1(andTable6.1),thisplayerhasthreepredictor
variables.TheseareWeapon(theweaponcurrentlycarriedbytheplayer),
Ammo(theamountofammunitioncarriedbytheplayer),andfinally,Health
(thelevelofhealthoftheplayer).Theinteriornodesofthedecisiontreeare
thefeatures,andthearcsoutofthefeaturenodesarefeaturevalues.Each
leafinthetreeisacategory(orclass).Thebreakdownofthisdecisiontree
resultsinasimpleconditionalexpressionshowninFigure6.2.
Thisconditionexpression(representingthedecisiontree)definesthat
iftheplayerhasagunwithfullammo,itwillfight.Iftheplayerhasaknife
andfullhealth,itwillfight.Otherwise,theplayerwillevade.Thisisclearly
showninthesimpleconditionalinFigure6.2.
It’seasytoseehowthedecisiontreecanbeconstructedfromthedata
inTable6.1,butwithmuchlargerdatasetswithmanypredictorvariables,
thetaskisconsiderablymoredifficult.TheID3(IterativeDichotomizer3)
algorithm automates this task through a form of top-down greedy search
throughthedecision-treespace.
TIP
Decision-treelearningisacommontechniqueusedindatamining.Data
miningisalargerfieldthatstudiestheextractionofusefulinformation
fromdatasets.Dataminingisapopulartechniqueinfinancialanalysis
andalsofindsuseinidentifyingfraud.
174 ArtificialIntelligence
Usingadecisiontreeissimplydefinedasstartingattherootandtakingthe
paththroughthetreewhosefeaturevaluesrepresentthoseofagivenexample.
Whentheleafofthetreeisreached,theexamplehasbeenclassified.
CreatingaDecisionTree
Creatingadecisiontreefromasetofexamplesisastraightforwardprocess.
Butthegoalistocreateasimpledecisiontreethatisconsistentwiththe
trainingdata.Simplecanbeambiguous,butsimplerdecisiontreestendto
bemorereliableandgeneralizebetter.
Creatingadecisiontreeisarecursiveprocessoverthefeaturesofthe
dataset,butitalwaysbeginswiththedeterminationoftherootofthetree.
Tochoosewhichfeaturetobeginbuildingthetree,thestatisticalproperty
ofinformationgainisused.Thefeaturewiththehighestinformationgainis
selectedwhichspecifiesthefeaturemostusefulforclassification.
Informationgainisbasedonanotherideafrominformationtheorycalled
entropy, which measures the amount of information behind the feature.
EntropyisdefinedinEq6.1.
Eq6.1
Givenasampleset(S),thesumistakenoftheproportionsofSthat
belongtoclassI.IfallsamplesofSbelongtothesameclassI,thenentropy
is0.Entropyof1.0definesthatthesampleset(forclassI)iscompletely
random.
TocalculateentropyfortheBehaviorlistedinTable6.1,wewouldapply
Eq6.1asfollows:
E(S)=-(2/5)Log2(2/5)+-(3/5)Log2(3/5)
E(S)=0.528771+0.442179=0.970951
Withtheentropycalculatedforthetarget(Behavior),thenextstepis
to choose the feature with the highest information gain. To calculate the
informationgainforafeature,Eq6.2isused.
Eq6.2
Informationgainidentifieshowmuchinfluencethegivenfeaturehas
overpredictingtheresultingcategory(inthiscase,Behavior).Anexample
ofcalculatingtheinformationgainoffeatureAmmoisdemonstratedas:
Gain(Sweapon)=E(S)-(2/4)*Entropy(Sgun)-(2/4)*Entropy(Sknife)
MachineLearning 175
Theentropyofavalueofagivenattributeiscalculatedbasedonthe
target feature. For example, the Weapon is value(Gun) in two of four
examples.FortheexampleswhereWeaponhasavalueofGun,thetargets
aresplitbetweenFightandEvade(1/2foreach).Therefore,theentropy
forvalueofGunis:
Entropy(Sgun)=-(1/2)Log2(1/2)-(1/2)Log2(1/2)
Entropy(Sgun)=0.5-0.5=0.0
ForexampleswheretheWeaponhasthevalueKnife,it’ssplitaswell
(halfhaveKnife,andwhentheexampleisKnife,thetargetBehaviorissplit
betweenFightandEvade,or1/2).
Entropy(Sknife)=-(1/2)Log2(1/2)-(1/2)Log2(1/2)
Entropy(Sknife)=0.5-0.5=0.0
SoreturningtothegaincalculationforWeapon,theresultis:
Gain(Sweapon)=0.970951-(2/4)*(0.0)-(2/4)*(0.0)
Gain(Sweapon)=0.970951-0.0-0.0=0.970951
Next,theinformationgainforbothAmmoandHealthiscalculated:
Gain(Sammo)=E(S)-(1/4)*Entropy(Sfull)-(3/4)Entropy(Slow)
Entropy(Sfull)=-(1/1)Log2(1/1)-(0/1)Log2(0/1)
Entropy(Sfull)=0
Entropy(Slow)=-(1/3)Log2(1/3)-(2/3)Log2(2/3)
Entropy(Slow)=0.528321-0.389975=0.138346
Gain(Sammo)=0.970951-(1/4)*0-(3/4)*0.138346
Gain(Sammo)=0.970951-0-0.103759=0.867192
Gain(SHealth)=E(S)-(2/4)*Entropy(Sfull)-(2/4)Entropy(Slow)
Entropy(Sfull)=-(1/2)Log2(1/2)-(1/2)Log2(1/2)
Entropy(Sfull)=0.5-0.5=0.0
Entropy(Slow)=-(1/2)Log2(1/2)-(1/2)Log2(1/2)
Entropy(Slow)=0.5-0.5=0.0
Gain(Shealth)=0.970951-(2/4)*0.0-(2/4)*.0.
Gain(Shealth)=0.970951-0.0-0.0=0.970951
Theresultinggainsarethen:
Gain(Sweapon)=0.970951-0.0-0.0=0.970951
Gain(Sammo)=0.970951-0-0.103759=0.867192
Gain(Shealth)=0.970951-0.0-0.0=0.970951
Foratie-breaker,wesimplyselectthefirstlargestinformationgainand
usethatasthelargest.ThisyieldsWeaponasthefeaturetorepresentthe
176 ArtificialIntelligence
FIGURE6.3: Bifurcationofthedatasetgivenarootfeaturedecision.
rootofthetree.Thealgorithmthencontinues,withthebifurcationofthe
datasetintotwosets,splitbytheselectedfeatureandfeaturevalues,and
theprocesscontinuestobuildtheremainderofthetree(seeFigure6.3).
Theprocesscontinuesasaboveonthetwolegsofthetree.Theentropy
is calculated, and the information gain on each remaining feature to
determinewhichtousetocontinuetodevelopthetree.Onepossibleresult
wasoriginallyshowninFigure6.1.
CharacteristicsofDecision-TreeLearning
Asexploredearlier,theID3algorithmusesagreedyformofsearch.This
meansthatthealgorithmpicksthebestattributeateachstep,anddoesnot
returntore-evaluateearlierattributechoices.Thiscanbeproblematic,as
greedydoesn’tnecessarilymeanoptimal.Theresultcanbeasub-optimalor
unbalancedtreethatdoesnotyieldthebestsegregationoffeatures.
Decisiontreeesthatarelargeorarecreatedfromansmallamountof
training data tend to overfit (or over-generalize). In general, while larger
decisiontreesaremoreconsistentwiththetrainingdata,smallertreestend
togeneralizebetter.Onewaytomanagethisproblemistoprunethetraining
datatoasmallersubsettoavoidover-fitting.
The advantages of decision trees is that they are very fast (tend to
classifyinfewerdecisionsthanthefeaturesofthedataset)andarealsoeasy
tointerpret.Mostimportantly,sinceadecisiontreeisinherentlyhuman-
readable,it’sveryeasytounderstandhowtheclassificationworks.
UnsupervisedLearning
Recallthatsupervisedlearninglearnsfromdatathatispreclassified(each
trainingvectorincludesthetargetclass).Unsupervisedlearningdiffersin
thatnotargetvariableexists.Allvariablesaretreatedasinputs,andtherefore
unsupervisedlearningisusedtofindpatternsinthedata.Thisallowsthe
datatobereducedandsegmentedintoitsrepresentativeclasses.
MachineLearning 177
MarkovModels
AusefulexampleofaMarkovmodelistheMarkovchain,namedafterits
creator, Andrey Markov. A Markov chain is a kind of probabilistic state
machinethatthatcanbeeasilytrainedgivenasetoftrainingdata.Eachstate
canprobabilisticallyleadtootherstates,butpriorstateshavenorelevance
tosubsequentstatetransitions(onlythecurrentstate).
WhatmakesMarkovchainsinterestingisthattheycanbeveryeasily
createdfromobservations.Take,forexample,theideaofthesmarthome.
Thehomemonitorstheactionsoftheoccupantduringthetimethatthe
occupant is home. Every evening, the occupant sets the alarm for 6 am
(SundaythroughThursday).At6am,afterthealarmsounds,themonitor
notices that the bathroom light is turned on. Through observation, we
capturethefollowingoveraweek’straining:
Weekday,6amalarm,bathroom-light-on
Weekend,noalarm,bathroom-light-off
With this data (five samples of weekday, two samples of weekend),
it’svisiblethatonweekdays,thealarmresultsintheobservationthatthe
bathroomlightcomesonwithaprobabilityof1.0.Withthisdata,theSmart
Homecouldturnonthebathroomlightwhenitseestheprecursoreventof
thealarmgoingoff.
Thisideacanbeappliedtoalargevarietyofproblemsinthedomainof
prediction,clusteringandclassification.Inthenextsection,theMarkovchain
is used to train character probability state machines to generate random
words.Thesewordsaregeneratedfromaprobabilisticstatemachineusing
regularwordsfortraining.
Word-FormLearningwithMarkovChains
AsimpleMarkovchaincanbeusedtogeneratereasonablesyntactically
correct words using known words for training. To build random, but
reasonablewordstheprobabilityoflettersfollowingotherlettersisused.
Forexample,iftheword“the”wereusedasatrainingexamplefourpoints
ofdatacanbecollected.Theseare:
thewordbeginswiththeletter‘t’
theletter‘h’followstheletter‘t’
theletter‘e’followstheletter‘h’
theletter‘e’endstheword.
Ifweanalyzeanumberofwordsthisway,wecanidentifythefrequencythat
lettersstartwords,endwords,andfollowotherletters.Atablerepresentation
178 ArtificialIntelligence
FIGURE6.4: Theresultinglettertransitionmatrix.
isusedtoidentifythebeginningletter(rows)followedbythesubsequentletter
(columns).Asanexample,considertheword‘as’beinganalyzed.Tostart,the
cellindicatedbytransitiona->sisincremented(row‘a’,column‘s’).Thenthe
transitions->‘end’isincremented(shownasrow‘s’,column‘\0’).
Considerthetrainingsampleconsistingofthewords:
mast,tame,same,teams,
team,meat,steam,andstem.
TheproductofanalyzingthesewordsisshowninFigure6.4.Foreach
word,thelettertransitionsarecountedtoarriveatthematrixrepresentation.
Notealsothatthesumoftherowsiscalculated(thisisusedlaterforword
generation).Thenumberoftimesagivenletteroccurredfirstisalsocounted
(again,usedinwordgeneration).
Now that the matrix exists, how would a random word be created
from this representation? For this, the counts are used as probabilities
to determine which letter to use given a previously selected letter. To
accomplishthis,arandomnumberisgeneratedusingthesumvalueasthe
bounds(themaximumrandomnumberthatcanbeused).Withthisrandom
value,roulettewheelselectionisusedtodeterminewhichletterfromarow
touse(depictedgraphicallyinFigure6.5forrow‘m’).
TIP
Roulette wheel selection views the available options as a roulette wheel.
Elementswithzeroprobabilityofselectiondonotappear.Thosewithnon-
zeroprobabilityoccupyaspaceonthewheelproportionaltotheirfrequency.
Forexample,withasumof9,letteraisselected2/9ofthetime,orP(0.22).
MachineLearning 179
Forroulettewheelselection,arandomvalueisselectedwhichservesasthe
limit.Arandomstartingpointisselected(inthisexample,thefirstcell)anda
countisincrementedgiventhecell’scontents(seeFigure6.6).Ifthecountis
greaterthanorequaltorandomvalue,thentheletterisselected.Otherwise,
thewheelcontinuesandthenextslotisaccumulated.Thisprocesscontinues
untilthecountvalueisthesameorgreaterthantherandomvalue.When
thistestissatisfied,theletterisselected(inthiscase,‘e’isselectedtofollow
theletter‘m’).Theprocesswouldthencontinuefortheletter‘e,’untilthe
terminalcharacterwasselected(‘\0’)indicatingthatthewordiscomplete.
Thisfollowsthesampledata,whichindicatesthattheletter‘e’followsthe
letter‘m’3/8’softhetimeorP(0.375)).
What’susefulaboutthisalgorithmisthatletterswithhigherfrequencies
(meaning higher numbers of occurrences in the training samples) are
selected with a greater probability than letters with lower occurrences.
Thisallowswordgeneration(inthiscase)toprobabilisticallyfollowthe
trainingset.
WordGenerationwithMarkovChains
Togenerateanewrandomword,thefirststepistodefinewhichlettertouse
first.Forthis,thevector(called‘countoffirstletter’inFigure6.4)isused.
Aletterisselectedrandomly(pertheearlierdiscussionusingroulettewheel
selection).Withthefirstletterselected,weemitthisletter,andthenselect
thenextletterusingthecurrentletter’srowinthematrix(seeFigure6.4).
NextletterselectionwasexploredinFigure6.6.Whentheletterselectedis
theNULL(or\0)character,thewordiscomplete.
ThiscanbedemonstratedbyhandusingtherepresentationmatrixinFigure
6.4.InTable6.2,therandomvalueandresultingletterselectedisshown.
180 ArtificialIntelligence
Table6.2: Sampleiterationofwordgeneration.
RandomValue Action
7 Iterate‘firstlettervector,’select‘m’
1 Iteratematrixrow‘m,’select‘a’
2 Iteratematrixrow‘a,’select‘t’
5 Iteratematrixrow‘t,’select‘e’
6 Iteratematrixrow‘e,’select\0(NULL)
Theresultisthegenerationoftherandomword‘mate’(notpartofthetraining
set).Inthenextsection,thecodetoimplementthiscapabilityisexplored.
MarkovChainImplementation
The implementation of the Markov chain for random word construction
requirestwobasiccomponents.Thefirstisthetrainingelementwhichreads
the training file (called the corpus) and builds the matrix representation
of the letter ‘state machine.’ The second element is the word generation
elementwhichusesthematrixrepresentationtogeneraterandomwords.
HE C
NT
D
O
ThecompleteimplementationoftheMarkov-Chaindemonstrationcan
befoundontheCD-ROMat./software/ch6/markov.c.
Thematrixisrepresentedusingasimpletwo-dimensionalarrayas:
#defineROWS 28
#defineCOLS 28
unsignedintmatrix[ROWS][COLS];
Indexes(rowandcolumn)0through25representtheletters‘a’through
‘z.’Index26(rowandcolumn)representsthenewline(endofword).Column
27representsthesumoftheparticularrow.Finally,row27representsthe
countofthegivenletterappearingasthefirstletter(seeFigure6.4).
Building the letter matrix (as illustrated in Figure 6.4) is a simple
process that relies on a very simple state machine. The first state, called
START_LTR_STATE,existstoidentifythatasingleletterhasbeenreceived
andthatanotherletterisrequiredtoincrementacell.Inthisstate,when
acharacterisreceived,thecountofthefirst-letter-vectorisincremented
(whichidentifieshowmanytimesthischaracteristhefirstinaword).After
aninitialcharacterisreceived,thenextstateisenteredcalledtheNEXT_
LTR_STATE. In this state, the transition counts are incremented (how
oftenonecharacterleadstoanother).Whenthenextcharacterreceivedis
MachineLearning 181
FIGURE6.7: Recordingaword’sdatawiththeparsingstatemachine.
aterminalcharacter(suchasspace,punctuation,etc.),thisdataisrecorded
(characterattheendofaword),andthenthestatemachinetransitionsback
totheFIRST_LTR_STATE.ThisprocessisshowninFigure6.7.
The state machine is implemented in function read_corpus shown in
Listing6.1.Thefunctionsimplyreadseachlinefromthedefinedfile,and
usesthestatemachinetomaintainlettertransitioncountsinthematrix.
LISTING6.1: Creatingthematrixrepresentationfromthetrainingdata.
intread_corpus(char*filename)
{
FILE*fp;
charline[MAX_LINE+1];
inti,state=START_LTR_STATE;
charch,prev_char;
/*Openandtestthecorpusfile*/
fp=fopen(filename,“r”);
if(fp==NULL)return-1;
/*Loopthrougheachlineofthefile*/
while(fgets(line,MAX_LINE,fp)!=(char*)0){
/*Loopthroughtheline*/
for(i=0;((i<MAX_LINE)&&(line[i]!=0));i++){
ch=tolower(line[i]);
/*Statemachineforcharacterhandling*/
switch(state){
caseSTART_LTR_STATE:
/*We’reontheinitialletter,saveitifnon-terminal*/
if(!is_terminal(ch)){
182 ArtificialIntelligence
prev_char=ch;
matrix[FIRST_LETTER][to_idx(ch)]++;
state=NEXT_LTR_STATE;
}
break;
caseNEXT_LTR_STATE:
if(is_terminal(ch)){
/*Ifthenextletterisaterminal,transitionback*/
matrix[to_idx(prev_char)][26]++;
state=START_LTR_STATE;
}else{
/*Ifthenextletterisanon-terminal,incrementthecount*/
matrix[to_idx(prev_char)][to_idx(ch)]++;
prev_char=ch;
}
break;
}
}
}
/*Fillinsumcolumnsinthematrix*/
calculate_maximums();
return0;
}
Whenread_corpushasfinished,thematrixisupdatedandrepresentsthe
trainingdata.Thematrixcannowbeusedtogeneraterandomwordsthat
mimicthestructureofthewordsinthetrainingdata.Twofunctionsareused
forwordgeneration,thesearegenerate_wordandselect_letter.
Function generate_word (see Listing 6.2) is the higher-level function
thatselectsletterstobuildaword.Itbeginsbyselectingaletter,butfrom
the vector representing the frequency of letters appearing first (FIRST_
LETTER). Each new letter (row in the matrix) is requested using the
currentletter(representingtherowfromwhichtoselect).Oncetheterminal
characterisselected,thewordiscompleteandthefunctionreturns.
LISTING6.2: Functiongenerate_wordtocreateanewrandomword.
voidgenerate_word(void)
{
intsel;
MachineLearning 183
/*Selectthefirstlettertouse*/
sel=select_letter(FIRST_LETTER);
/*Continue,selectingadditionallettersbasedupon
*thelastletter.
*/
while(sel!=END_LETTER){
printf(“%c”,(‘a’+sel));
sel=select_letter(sel);
}
printf(“\n”);
return;
}
The core of the word creation algorithm is the select_letter function
(seeFunction6.3).Thisfunctionimplementstheroulettewheelselection
algorithm.Thealgorithmbeginswiththerow,whichrepresentstheprevious
letter.Thisprovidestherowthatisusedtodeterminewhichletterwillfollow.
Arandomvalueiscreated(max_val),whichrepresentsthelimit(wherethe
ballwillstopintheroulettewheel).Therowisthenaccumulated(starting
at the first cell) until the max_val is reached or exceeded. At this point,
theletterisreturned.Ifthemax_valisnotreached,thenthenextcellis
accumulatedandtheprocesscontinues.
heselect_letterfunctionthatprobabilisticallyselectsletters
Listing6.3: T
basedonthecurrentletter.
charselect_letter(introw)
{
intmax_val;
inti=0,sum=0;
/*Pickthemaximumvalue(forrouletteselection)*/
max_val=RANDMAX(matrix[row][MAX_COUNTS])+1;
/*Performtheroulettewheel*/
while(1){
/*Addinthecurrentslotvalue*/
sum+=matrix[row][i];
/*If>=max_val,thenselectthisletter*/
if(sum>=max_val)returni;
/*Otherwise,skiptothenextletterintherow*/
if(++i>=MAX_COUNTS)i=0;
184 ArtificialIntelligence
}
exit(0);
}
Generatingrandomwordsfromaprogrambuiltfromthissourcecan
resultininterestingwords,andalsonotsointerestingwords.Forexample,
thesewordsweregeneratedfromasamplecorpus:
antinsubaized
sutosermed
eroconated
axpoged
porantide
arouded
anvilured
arketized
Butmoreoftenthannot,wordslikethiscanresult:
rolacunficonged
phteroobund
From this perspective, extracting the more useful words from the
nonsensicalwordscanbedonewithhumaninvolvement.Inotherwords,the
applicationgeneratessolutionsetswhicharethenreviewedbyahumanto
identifythosethathavevalue(forexample,ifanewcompanyordrugname
werebeingsearched).Thisiscalledhuman-assistedunsupervisedlearning
and results in the best approach (software for search, human review for
filtering).
OtherApplicationsofMarkovChains
Markovchainsandtheirvariantscanbefoundinanumberofareas,including
speechrecognition,speechunderstanding,musiccomposition,andanumber
ofotherareas.Thesimplicityofthealgorithmmakesitveryefficient.The
systemhasnoideaoftheeventsforwhichprobabilitiesarebeingdefined,all
that’slearnedisprobabilitybetweeneventswithinasystem.Butevenwithout
anunderstandingofthesemanticsbehindtherelationships,thesystemcan
stillreacttothem(suchasthealarm/bathroom-lightexample).Theapproach
is also useful from the human-assisted learning perspective (with human
operatorsconsideringtheimportanceoftherelationshipsfound).
MachineLearning 185
FIGURE6.8: Samplefeaturespacefor1NNclassification.
NearestNeighborClassification
Oneoftheearliestunsupervisedlearningalgorithmsthatisalsooneofthe
simplestiscallednearestneighborclassification.Thisalgorithmclassifiesan
unseenpatternorconceptbyfindingtheclosestpatternintheknowndataset.
Thisiswhatismeantbynearestneighbor.Theclassofthenearestneighborin
themulti-dimensionalfeaturespaceistheclassofthe(unseen)testvector.
AnexampleofthisisshowninFigure6.8.Atthecenterofthefeature
space is a new sample that is yet to be classified. The distance between
thenewsampleandotherexamplesinthespaceiscalculated.Theclosest
exampleisusedastheclasstowhichthisnewsampleshouldbeclustered.
Inthisexample,theclosestexampleisoneunitaway,andthereforetheclass
ofthisexampleisused.Declaringtheclassbasedontheclosestsampleis
calledonenearestneighbor(1NN).
Calculatingthedistancebetweentwofeaturevectorscanbeaccomplished
inanumberofways,butthemostpopularistheEuclideanmeasure.Another
popularfunctionusestheManhattandistance.TheEuclideandistanceis
calculatedusingEq6.3forfeaturevectorspandq.
Eq6.3
1NNExample
Thealgorithmfor1NNiseasilydescribedandimplemented.With1NN,the
sample(unclassified)featurevectorischeckedagainstallknownexamples.
Theclassfortheclosestexampleisthenusedastheclassforthesample
featurevector.
Fortheexamplevectors,thetableinFigure6.9isused.Thiscontains
anumberoffeaturesthatincertaincombinationsdefineatypeofanimal.
Fivetypesofanimalsareclassifiedhereusing14examplevectorsand10
attributes.
FIGURE6.9: Examplefeaturevectorsforanimalclassification.
FIGURE6.10: Distancecalculationforthemysteryanimaltotheknownexamples.
MachineLearning 187
Asanexample,consideranewlydiscoveredanimalwiththeattributes
shownbyitsfeaturevector(seeFigure6.10).What’sobservedisthatthe
mystery animal lays eggs and lives in both the water and on land. The
distanceofthefeaturevectortoeachknownexampleiscalculatedandthe
closestexampleusedfortheclass(seeFigure6.11).Inthiscase,themystery
animalisclassifiedasanamphibian.
Theimplementationfor1NNisverysimpleandshowninListing6.4.As
shownthecalc_distancesimplycalculatesthedistanceusingeachfeatureof
thevector.The1NNalgorithmsimplywalksthrougheachexamplevector
andcalculatesthedistancefromthesamplevector(seemain).Theclosest
vectoristhenusedastheclass.
HE C
NT
D
O
Thecompleteimplementationofthe1NNdemonstrationcanbefound
ontheCD-ROMat./software/ch6/one_nn.c.
LISTING6.4: Sampleimplementationofthe1NNclusteringalgorithm.
#defineMAX_FEATURES 10
#defineMAMMAL 0
#defineBIRD 1
#defineFISH 2
#defineAMPHIBIAN 3
#defineREPTILE 4
typedefstruct{
intfeatures[MAX_FEATURES];
intclass;
}sample_t;
#defineMAX_SAMPLES 14
sample_tsamples[MAX_SAMPLES]={
/*LYLEFBMCWHWBCBHFSSLWLBWG*/
{{1,0,1,1,1,0,0,0,0,0},MAMMAL},/*Squirrel*/
{{1,0,1,1,1,0,0,0,0,0},MAMMAL},/*Cat*/
{{0,1,0,0,0,1,0,0,1,0},AMPHIBIAN},/*Frog*/
{{0,1,0,0,1,0,1,0,1,0},BIRD},/*Duck*/
{{1,0,1,1,0,0,0,0,0,0},MAMMAL},/*Bat*/
{{1,0,1,1,1,0,0,0,0,0},MAMMAL},/*Elephant*/
{{0,1,0,0,0,1,0,1,0,0},REPTILE},/*Alligator*/
{{0,1,0,0,1,0,1,0,0,0},BIRD},/*Owl*/
{{0,1,0,0,0,1,0,1,0,1},FISH},/*Trout*/
{{0,1,0,0,0,1,0,1,0,0},REPTILE},/*Turtle*/
188 ArtificialIntelligence
{{0,1,0,0,0,1,0,0,1,0},AMPHIBIAN},/*WtrDragn*/
{{1,0,1,1,1,0,0,0,0,0},MAMMAL},/*Elk*/
{{0,1,0,0,0,1,0,1,0,0},REPTILE},/*Snake*/
{{0,1,0,0,0,1,0,1,0,1},FISH}/*Salmon*/
};
char*names[5]={“Mammal”,“Bird”,“Fish”,“Amphibian”,“Reptile”};
doublecalc_distance(int*feature_vector,intexample)
{
doubledistance=0.0;
inti;
/*Computethedistanceforeachfeatureofthevector*/
for(i=0;i<MAX_FEATURES;i++){
distance+=sqr((samples[example].features[i]-feature_vector[i]));
}
returnsqrt(distance);
}
intmain(void)
{
inti,class=0;
doubledistance,min=100.0;
intfv[MAX_FEATURES]={0,1,0,0,0,0,0,0,1,0};
for(i=0;i<MAX_SAMPLES;i++){
/*Calculatedistancebetweenthesampleandexample_ivector*/
distance=calc_distance(fv,i);
/*Ifthisistheclosestvector,saveit*/
if(distance<min){
min=distance;
class=samples[i].class;
}
}
printf(“Classis%s\n”,names[class]);
return0;
}
k-NNExample
Aproblemwith1NNclassificationisthatitcanbesusceptibletonoisydata.
Onesolutiontothisproblemisinsteadofsimplyclassifyingbasedonthe
closestneighbor,taketheclosestkneighborsandusethemajorityvoteto
determinethecorrectclass(seeFigure6.11).
MachineLearning 189
FIGURE6.11: Usingtheclosestkexamplestoclassifyanewsample.
Theadvantageofk-NN(wherek>1)isthattheprobabilityofmisclassifying
asampleisreducedbecausemoreexamplescanweighinontheresult.The
kportioncanbetakentoofar,andifthekvalueistoolarge,itcanalsoresult
inmisclassification.Thevalueofkshouldthereforebesmall,butnottoo
large.Unrelatedexamplesinfluencethevote.
Theimplementationfork-NNisavariationof1-NNinthatkexamples
areusedforclassificationinsteadofsimplyone.
Thedatarepresentationfork-NNisidenticaltothatone1-NN,asshown
inListing6.4.WhatdiffersishowtheclassificationoccursoncetheEuclidean
distances are calculated. Listing 6.5 provides the main function for k-NN
classification.Asshown,eachdistanceiscalculatedforthefeaturevectorfc
(withcalc_distance)andthensavedinthedistancearray.Functioncount_votes
istheninvoked(withthekvalue)tofindandcountthevotingexamples.
HE C
NT
D
O
Thecompleteimplementationofthek-NNdemonstrationcanbefound
ontheCD-ROMat./software/ch6/k_nn.c.
LISTING6.5: Mainfunctionforthek-NNimplementation.
intmain(void)
{
inti,class=0;
intfv[MAX_FEATURES]={0,1,0,1,0,0,0,0,1,0};
190 ArtificialIntelligence
doubledistance[MAX_SAMPLES];
intk=3;
/*Walkthrougheachexamplevector*/
for(i=0;i<MAX_SAMPLES;i++){
distance[i]=calc_distance(fv,i);
}
/*Count,SortandReturnWinningClass*/
class=count_votes(distance,k);
printf(“Classis%s\n”,names[class]);
return0;
}
Thecalc_distancefunctionisthesameasshowninListing6.4.Thenext
function,count_votes,isusedtofindtheclosestkexamplestothesample
vectorandthentofindtheclassrepresentedbythemajorityoftheexamples.
Thefunctionbeginsbymovingtheclassmembersfromtheexamplevectors
intoanewlistvector.Thelististhensortedusingthedistancevectorpassed
intothefunction.Theclassvotesarrayisthenzeroedandthetopkmembersof
thelist(thoseclosesttotheunclassifiedsample)arecounted.Finally,theclass
withthemostvotesisreturnedtothemainfunctiontoemittheclasstowhich
thesamplebelongs(basedonthemajorityvoteoftheclosestkexamples).
LISTING6.6: Functiontofindandcounttheclosestkexamplevectors.
intcount_votes(double*dist,intk)
{
inti,list[MAX_SAMPLES];
intvotes[MAX_CLASSES];
intsorted;
intmax,class;
/*Moveclassestothenewtemporarylistarray*/
for(i=0;i<MAX_SAMPLES;i++)list[i]=samples[i].class;
/*Sortthelistinascendingorderofdistance*/
sorted=0;
while(!sorted){
sorted=1;
for(i=0;i<MAX_SAMPLES-1;i++){
if(dist[i]>dist[i+1]){
inttemp=list[i];list[i]=list[i+1];list[i+1]=temp;
doubletdist=dist[i];dist[i]=dist[i+1];dist[i+1]=tdist;
MachineLearning 191
sorted=0;
}
}
}
/*Countthevotes*/
for(i=0;i<MAX_CLASSES;i++)votes[i]=0;
/*Addthevotetotheparticularclass*/
for(i=0;i<k;i++){
votes[list[i]]++;
}
/*Countthevotesandreturnthelargestclass*/
max=votes[0];
class=0;
for(i=1;i<MAX_CLASSES;i++){
if(votes[i]>max){
max=votes[i];
class=i;
}
}
returnclass;
}
FIGURE6.12: k-NNExampleforClassification(k=3).
192 ArtificialIntelligence
Thek-NNalgorithmismuchlesssusceptibletonoisethanthe1-NNand
thereforecanprovideamuchbetterclassificationthan1-NN.Thevalueof
kshouldbelargeenoughtoyieldarepresentativesetofvotingexamples,
butsmallenoughtoavoidtoosmallofasample.
Theprocessofk-NNisshowninFigure6.12.Thedistancefromeach
example to the sample vector (mystery animal) is performed and the top
through chosen. In this case, the examples aren’t sorted, but instead just
selectedfromthelist(showninbold).Inthisexample,twooftheclosest
examplesarefromtheamphibianclassandonefromthebirdclass.
Thenearestneighboralgorithmk-NNisagreatalgorithmforclassifying
feature vectors using a set of known classified examples. Drawbacks can
includetheprocessingtimerequirediftheexamplesetandfeaturevector
arelarge.Itsbiggestadvantageisitssimplicity.
CHAPTERSUMMARY
WhilemachinelearningisoneoftheoldertechniquesofAI,itremainsuseful
andeffectiveforgenerallearning.Inthischapter,theideasbehindsupervised
andunsupervisedlearningwereexploredandacollectionofalgorithmsthat
demonstratetheseapproachesweregiven.Decisiontreeswereintroducedfrom
theperspectiveofsupervisedlearning,andalsoMarkovchainsandnearest
neighboralgorithmsfromtheunsupervisedlearningperspective.Allareuseful
forlearningandclassificationinawidevarietyofproblemdomains.
RESOURCES
Anzai,Y.PatternRecognitionandMachineLearningNewYork,Academic
Press,1992.
Carbonell,J.(Ed.)MachineLearningParadigmsandMethodsBoston,MA
MITPress,1990.
Dasarthy, B. (Ed.). “Nearest Neighbor (NN) Norms: NN Pattern
ClassificationTechniques,”1991.
Hastie,T.etal“TheElementsofStatisticalLearning,”Springer,2001.
Mitchell,T.M.“MachineLearning,”McGraw-Hill,1997.
EXERCISES
1. Inyourownwords,definethecoredifferencesbetweensupervisedand
unsupervisedlearning.
MachineLearning 193
2. Whatisacommonapplicationofdecision-treelearning?
3. Defineentropyanditsapplicationtodecision-treelearning.Whatcan
beinferredifentropyiszeroorone?
4. Whatissuescanresultfromcreatingdecisiontreesfromtrainingsets
thataretoosmallortoolarge?
5. WhatotherapplicationsareusefulforMarkovchains?
6. In the Markov example presented in this book, only two letters are
consideredtobuildaprobabilitymatrix.Updatethesampleprogramto
considerthreelettersinprobabilityconstruction.Howdoesthisaffect
theproductionofrandomwords?
7. Nearestneighborclassificationusesafeaturevectortorepresentknown
concepts.Howcouldafirst-person-shootercharacter(NPC)bedefined
suchaction-selectionwasperformedwith1NN?
8. Describethedifferencebetween1NNandk-NNclassification.
9. Whatistheprimaryissuewith1NNclassification?
10. How does k-NN classification improve on the capabilities of 1NN
classification?
11. Whatistheprimaryissuewithk-NNclassification?
FIGURE6.13: k-NNExampleforClassification(k=3).
Chapter 7 EVOLUTIONARY
COMPUTATION
E
volutionaryComputationreferstoaclassofalgorithmsthatutilize
simulated evolution to some degree as a means to solve a variety
of problems, from numerical optimization to symbolic logic. By
simulatedevolution,wemeanthatthealgorithmshavetheabilitytoevolve
apopulationofpotentialsolutionssuchthatweakersolutionsareremoved
and replaced with incrementally stronger (better) solutions. In other
words, the algorithms follow the principle of natural selection. Each of
thealgorithmshassomeamountofbiologicalplausibility,andisbasedon
evolutionorthesimulationofnaturalsystems.Inthischapter,we’llexplore
geneticalgorithms,geneticprogramming,evolutionarystrategies,differential
evolution,andanotherbiologicallyinspiredalgorithm:swarmintelligence.
SHORTHISTORYOFEVOLUTIONARYCOMPUTATION
It’snosurprisethatevolutionhasbeenusedasametaphorforsolvingvery
difficultproblems.Evolutioninitselfisamechanismofincrementalsearch,
whereby more fit solutions to problems propagate to future generations,
andlessfitsolutionsgraduallyfadeaway.Thisprocessofnaturalselection
providesawonderfulvehicleforfindingsolutionstodifficultmultivariate
optimizationproblems.
196 ArtificialIntelligence
Whileevolutionaryalgorithmshaveexistedforquitesometime,theiruse
hasincreasedasmoderncomputingsystemspermittheevolutionoflarger
populationsofsolutionstomuchmorecomplexproblems.
EvolutionaryStrategies
Oneoftheearliestusesofevolutionoccurredinthe1960sbyRechenberg.
Rechenbergintroducedevolutionstrategiesasameanstooptimizevectorsof
real-values to optimize physical systems such as airfoils. [Rechenberg 1965]
In this early evolutionary strategy, the population size was restricted to two
members,theparentandchild.Thechildmemberwasmodifiedinarandom
way(aformofmutation),andwhichevermemberwasmorefit(parentorchild)
wasthenallowedtopropagatetothenextgenerationastheparent.Forexample,
asshowninFigure7.1,thechildmemberismorefitthantheparentinthefirst
generation,whichresultsinitbeingtheparentinthenextgeneration.
FIGURE7.1: Demonstratingthesimpletwomemberevolutionarystrategy.
FIGURE7.2: Evolvingfinitestatemachinesforasimpleparsingtask.
EvolutionaryComputation 197
EvolutionaryProgramming
EvolutionaryProgrammingwasalsointroducedandadvancedinthe1960sby
Fogel.Withevolutionaryprogramming,Fogelevolvedpopulationsoffinite
statemachines(orautomata)thatsolvedvariousproblems.[Fogel1966]A
finite-statemachineisagraphwithstatetransitionsbasedonaninputsymbol
andthecurrentstate(forexample,manyparsersaredesignedasfinite-state
machines).Fogel’smethodincrementallyimprovedthepopulationthrough
randommutationofthestatetransitions.
TheexampleshowninFigure7.2demonstratesoneoftheencodings
thatcouldbeusedforevolvingfinite-statemachines.Thegoalistoevolve
afinite-statemachinethatcanrecognizepatternssuchasaabb,aabbaabb,
aabbaabbaabb,etc.
TheupperleftofFigure7.2isthefinite-statemachinerawencodingthat
canbemutatedduringevolution.Thisparticularfinite-statemachineresultsin
thestatetransitiondiagramshowninthemiddleright.Thiscanbediagrammed
asshowninthelowerrightofFigure7.2,thestate-machinediagram.
GeneticAlgorithms
JohnHollandintroducedtheideaofgeneticalgorithmsinthe1960sasa
population-basedalgorithmwithgreaterbiologicalplausibilitythanprevious
approaches.Whereevolutionarystrategiesandevolutionaryprogramming
used mutation as a way to search the solution space, Holland’s genetic
algorithm extended this with additional operators straight from biology.
Potential solutions (or chromosomes) are represented as strings of bits
insteadofrealvalues.Inadditiontomutation,Hollandalsousedcrossover
andinversiontonavigatethesolutionspace(seeFigure7.3).
FIGURE7.3: Holland’sbit-stringgeneticalgorithm.
198 ArtificialIntelligence
Hollandalsostudiedthemathematicalfoundationofhisalgorithms,looking
moretounderstandthemfromatheoreticalperspectivethanusingthem
tosolveproblems.
NOTE All living organisms consist of cells, where each cell contains a set
of chromosomes (strings of DNA). Each chromosome is made up of
genes,eachofwhichcanencodeatrait(behavioralorphysical).These
chromosomesserveasthebasisforgeneticalgorithms,whereapotential
solutionisdefinedasachromosome,andtheindividualelementsofthe
solutionarethegenes.
GeneticProgramming
In1990s,JohnKozaintroducedthesubfieldcalledGeneticProgramming.
This is considered a subfield because it fundamentally relies on the core
genetic algorithm created by Holland, and differs in the underlying
representation of the solutions to be evolved. Instead of using bit-strings
(aswithgeneticalgorithms)orreal-values(asisthecaseforevolutionary
programming or evolutionary strategies), genetic programming relies on
S-expressions(programtrees)astheencodingscheme.
ConsidertheexampleshowninFigure7.4.Thepopulationconsistsof
twomembers,AandB.Usingthecrossoveroperator,aportionofAisgrafted
ontoB,resultinginanewexpression.Geneticprogrammingalsoutilizesthe
mutationoperatorasawayofextendingthepopulationtothesearchspace.
FIGURE7.4: UsingthecrossoveroperatortocreatenewS-expressions.
EvolutionaryComputation 199
TIP
While Koza’s representation focused on S-expressions, linear genetic
programming has been introduced to evolve programs in standard
machine(assembly)languages.
BIOLOGICALMOTIVATION
The evolutionary algorithms covered in this chapter are all biologically
plausible. In each case, the algorithms that we’ll explore are population-
based. Each of the algorithms operates on a population of entities,
parallelizingtheabilitytosolveaproblem.
Thefirstsetofalgorithmsthatwe’llreview(geneticalgorithms,genetic
programming,andevolutionarystrategies)istrulyevolutionaryinnature.
Thesealgorithmsinvolvenaturalselectionacrossapopulationofpotential
solutions.Membersofthepopulationarebornandeventuallydie,butpass
ontheirgeneticmaterialsthroughthepopulationsinsearchofasatisfactory
solution.Atthecoreofthesealgorithmsiswhat’scalledrecombination,or
thecombiningandmutatingofsolutionsthatcanchangethematerialinthe
population.Asthemembersofthepoolchange,onlythosethatarefitcan
moveontothenextpopulation(potentiallyinmorefitform).Thisprocess
isillustratedinFigure7.5.
FIGURE7.5: Fundamentalprocessofpopulation-basedevolutionaryalgorithms.
FIGURE7.6: Optimizationbynaturalsystem(particleswarmandAntColonyOptimization).
200 ArtificialIntelligence
Thesecondsetofalgorithmsthatwe’llexploreiscapableofsolvinga
varietyofproblemsusingnatureasaguide.Thefirstalgorithm,inthefield
ofswarmintelligence,usesapopulationofparticlesthatswarmwitheach
otheroverafitnesslandscapetosolvevariousproblemssuchasoptimization.
Thebestparticleisusedasthecenteroftheswarm,withotherparticles
swarmingaroundit(lookingforbettersolutions).
Thesecondalgorithm,AntColonyOptimization,simulatesacolonyof
antsandusessimulatedpheromonestofindsolutionstovariousgraph-related
problems(asshowninFigure7.6,wherethepheromonetrailidentifiesthe
shortestpaththroughthegraph).
GENETICALGORITHMS(GA)
Let’sbeginourdiscussionofevolutionaryalgorithmswiththemostpopular
andmostflexiblealgorithm,theGeneticAlgorithm.Thegeneticalgorithm
isn’treallyasinglealgorithm,butacollectionofalgorithmsandtechniques
that can be used to solve a variety of problems in a number of different
problem domains. For example, many consider genetic algorithms a
techniquefornumericaloptimization,butgeneticalgorithmscanbeused
formuchmore(aswe’llseelaterinthesampleapplication).
The ability of the genetic algorithm to solve wide-ranging problems
is derived from the method by which the solution is represented in the
population.Aswesawintheintroduction,solutionscanberepresentedas
bit-strings(withanunderlyingrepresentation),real-values,aswellasmore
abstractentitiessuchspecialencodingsofLISPS-expressions.Thegenetic
algorithmcanbeappliedtomanyproblems,andislimitedprimarilytothe
developer’sabilitytoefficientlyrepresentasolution.We’llseeanumberof
possiblesolutionencodingsforGeneticAlgorithms,GeneticProgramming,
andEvolutionaryStrategies.
GeneticAlgorithmOverview
The GA is a collection of algorithm recipes that can be used to evolve
solutionstoarangeofdifferenttypesofproblems.Wecallitarecipebecause
therearealargenumberofvariationsoftheGAthatcanbeused.These
variationsaretypicallyselectedbasedonthetypeofproblemtobesolved.
Let’sstartwithadiscussionofthebasicflowoftheGA,andthenwe’ll
digintothedetailsandexplorewhichvariantsaremostuseful.Notethat
theGAiscalledapopulation-basedtechniquebecauseinsteadofoperating
on a single potential solution, it uses a population of potential solutions.
EvolutionaryComputation 201
Thelargerthepopulation,thegreaterthediversityofthemembersofthe
population,andthelargertheareasearchedbythepopulation.
Oneattempttounderstandwhygeneticalgorithmsworkiscalledthe
Building-BlockHypothesis(BBH).Thisspecifies,forbinaryGA,thatthe
crossoveroperation(splittingtwochromosomesandthenswappingthetails)
improvesthesolutionbyexploitingpartialsolutions(orbuildingblocks)in
theoriginalchromosome.Onecanthinkofthisasgeneticrepair,wherefit
buildingblocksarecombinedtogethertoproducehigherfitnesssolutions.
Additionally,usingfitness-proportionateselection(higherfitmembersare
selected more often), less fit members and their corresponding building
blocks,dieoutthusincreasingtheoverallfitnessofthepopulation.
The overall genetic algorithm can be defined by the simple process
showninFigure7.7.First,apoolofrandompotentialsolutionsiscreated
that serves as the first generation. For the best results, this pool should
haveadequatediversity(filledwithmembersthatdiffermorethantheyare
similar).Next,thefitnessofeachmemberiscomputed.Thefitnesshereis
ameasureofhowwelleachpotentialsolutionsolvestheproblemathand.
Thehigherthefitness,thebetterthesolutioninrelationtoothers.
FIGURE7.7: Simpleflowofthegeneticalgorithm.
202 ArtificialIntelligence
FIGURE7.8: TwoofthesimplerGAselectionmodels.
FIGURE7.9: Illustratingthecrossoveroperatorsingeneticrecombination.
Next,membersofthepopulationareselectedbasedonsomealgorithm.
Thetwosimplestapproachesareroulettewheelselection,andelitistselection
(seeFigure7.8).Roulettewheelselectionisaprobabilisticalgorithmthat
selects members of the population proportionate with their fitness (the
higherfitthemember,themorelikelyitwillbeselected).Inelitistselection,
thehigherfitnessmembersofthepopulationareselected,forcingthelesser
fitmemberstodieoff.
Using roulette wheel selection (using the data from Figure 7.8), one
likelyselectionresultwouldbethattwoofMemberAwouldbeselected,
andoneeachofMembersCandD.SinceMemberAisofhigherfitnessthat
theothermembers,ithastheprivilegeofpropagatingmorechromosomes
tothenextpopulation.Elitistselectioninthismodel(showninFigure7.8)
simplytakestheupper50%ofthepopulation’smembers(mostfit),andthen
distributesthesetothenextgeneration.
ReturningnowtoourGAflow,fromtheselectionprocess,wehavea
numberofmembersthathavetherighttopropagatetheirgeneticmaterial
to the next population. The next step is to recombine these members’
materialtoformthemembersofthenextgeneration.Commonly,parents
areselectedtwoatatimefromthesetofindividualsthatarepermittedto
propagate (from the selection process). Given two parents, two children
EvolutionaryComputation 203
FIGURE7.10: Illustratingthemutationandinversiongeneticoperators.
arecreatedinthenewgenerationwithslightalternationscourtesyofthe
recombinationprocess(withagivenprobabilitythatthegeneticoperatorcan
occur).Figures7.9and7.10illustratefourofthegeneticoperators.
Figure7.9illustratesthecrossovergeneticoperators.Usingcrossover,
theparentsarecombinedbypickingacrossoverpoint,andthenswapping
thetailsofthetwoparentstoproducethetwochildren.Anothervariantof
crossover creates two crossover points, swapping the genetic materials in
twoplaces.
Figure7.10coversthemutationoperatorandalsotheinversionoperator.
EachoftheseoperatorsweretheoriginalgeneticoperatorsfromHolland’s
originalwork.Themutationoperatorsimplymutates(orflips)abit.Note
thatinreal-valuedchromosomes,aslightchangetothevaluecanalsobe
performedasmutation(smallincrementordecrementofthevalue).The
inversionoperatortakesapieceofthechromosome,andinvertsit.Inthis
case,therangeofbitsareflipped.
Finally,we’vediscussedtheprocessoftheGA,butnothowitterminates.
Thereareanumberofwaysthatwecanterminatetheprocess.Themost
obviousistoendwhenasolutionisfound,oronethatmeetsthedesigner’s
criteria.Butfromthealgorithm’sperspective,wealsoneedtoaccountfor
thepopulation,anditsabilitytofindasolution.
Another termination criterion, potentially returning a suboptimal
solution,iswhenthepopulationlacksdiversity,andthereforetheinabilityto
adequatelysearchthesolutionspace.Whenthemembersofthepopulation
become similar, there’s a loss in the ability to search. To combat this, we
terminate the algorithm early by detecting if the average fitness of the
populationisnearthemaximumfitnessofanymemberofthepopulation
(forexample,iftheaveragefitnessisgreaterthan0.99timesthemaximum
fitness).Oncethepopulationbecomestoosimilar,themembershavefocused
onsimilarareasofthesearchspace,andarethereforeincapableofbranching
outtonewareasinsearchofmorefitindividuals.
204 ArtificialIntelligence
FIGURE7.11: LegalmovesfortheTowersofHanoiwithinitialandgoalstates.
FIGURE7.12: MovesequencerepresentationfortheTowersofHanoiproblem.
TIP
Theissueoflackofdiversityingeneticalgorithmsresultsinpremature
convergence,asthemembersconvergeonalocalmaximum,nothaving
foundtheglobalmaximum.Earlyterminationisonesolution,butothers
includealgorithmrestartifthissituationisdetected.
GeneticAlgorithmImplementation
Let’s now explore an implementation of a genetic algorithm to solve the
Towers of Hanoi problem. This problem involves three pegs, with three
unique-sizeddiscs,andthegoalofmovingthediscsfromonepegtoanother.
Theconstraintexiststhatadisccanbemovedtoapegonlyifthepegis
empty,orifthedisccurrentlyonthepegislargerthanthepegtobemoved
(seeFigure7.11).
Thefirstproblemtosolveishowtorepresentthesequenceofmovesto
solvetheprobleminaformthatcanbeevolvedbythegeneticalgorithm.
Thefirstthingtoconsideristhatthereareonlyahandfulofmovesthatare
possible(thoughthesemaynotalwaysbelegal,dependingonthecurrent
configuration).Ifwenumberthepegsfromzerototwo,wecanthinkabout
EvolutionaryComputation 205
FIGURE7.13: IllustratingthemovesprovidedinthesolutionsequenceinFigure7.12.
thesolutionspaceasasequenceofmoveswherebythemoveencodesthe
sourcepegandthedestinationpeg.
FromFigure7.12,thefirstmove‘02’representsamoveofthediscfrom
peg0topeg2.Thenextmove‘01’movesthediscfrompeg0topeg1.The
sequenceofmovesshowninFigure7.12canbeshownvisuallyasillustrated
inFigure7.13.Thetopconfigurationrepresentstheinitialconfigurationof
theproblem,andsubsequentmovesshowhowtheconfigurationchanges
witheachmove(thegreydiscisthelastmoved).
Let’snowdigintothesourcethatprovidesthisrepresentation(seeListing
7.1).ThechromosomefortheGAissequenceofmoves,andeachgeneisa
singlemove.We’llrepresentthemovesverysimplyasintegernumbersfrom
0to5.We’lldecodethemovenumbertothepegto/fromcommandusing
themovesarray.Thisallowsustoavoidadirectrepresentation,andprovide
asimplerformattotheGAtoevolve.
The Towers of Hanoi simulation is provided by the pegs array. This
containsthreestructuresrepresentingthepegs,anduptothreediscs(with
thenumberofdiscscurrentlyonthepegdefinedbycount).Thesolutionis
representedbythetypesolution_t.Thiscontainsthesequenceofmoves(plan),
thenumberofactivemoves(op_count),andthecurrentfitnessevaluation.
206 ArtificialIntelligence
Listing7.1: RepresentingthesolutionspacefortheTowersofHanoi.
#defineA_TO_B 0x01
#defineA_TO_C 0x02
#defineB_TO_A 0x10
#defineB_TO_C 0x12
#defineC_TO_A 0x20
#defineC_TO_B 0x21
#defineMAX_OPERATIONS 6
charmoves[MAX_OPERATIONS]={
A_TO_B,A_TO_C,B_TO_A,B_TO_C,C_TO_A,C_TO_B};
typedefstruct{
charpeg[3];
intcount;
}peg_t;
peg_tpegs[3];
#defineNUM_OPERATIONS 12
typedefstruct{
intop_count;
unsignedcharplan[NUM_OPERATIONS+1];
doublefitness;
}solution_t;
ThepopulationoftheGAissplitintotwoparts.We’llkeepanarrayof
solutionsasthecurrentpopulation,andanotherarrayasthenextgeneration
ofsolutions.Thecurrentandnextgenerationcyclebetweeneachotheras
thealgorithmisperformed(nextbecomescurrent,etc.).
#definePOPULATION_SIZE 200
solution_tsolutions[2][POPULATION_SIZE];
Let’snowlookatthemainloopfortheGA,andthendiscussthemajorfunctions
thatprovidetheprocessofevolution.Webeginbyrandomlyinitializingthe
populationofpossiblesolutionswithacalltoinitialize_population,andthen
calculatethefitnessofeachmember(bytestingeachplanagainstasimulation
oftheTowersofHanoi)usingcompute_population_fitness.Wethenenter
theloopthatinvokesthecoreoftheGA.Thefunctionperform_gadoesa
singlecyclethroughthepopulation,selectingthememberstomoveon,and
recombining them in the next generation. The fitness of each member is
calculatedagain,andthefundamentalstatisticsareemitted(minimumfitness,
averagefitness,andmaximumfitnessoftheentirepopulation).
EvolutionaryComputation 207
Thisprocesscontinueswhilethere’sadequatediversityinthepopulation
(checkingtheaveragefitnessagainstthemaximum)andwehaven’tyetfound
asolution(aperfectsolutionbeingafitnessof75.0).
Listing7.2: Asnippetfromthemainloopforthegeneticalgorithm.
RANDINIT();
initialize_population(cur);
compute_population_fitness(cur);
while((avg<(0.999*max))&&(max<75.0)){
cur=perform_ga(cur);
compute_population_fitness(cur);
if(((generation++)%500)==0){
printf(“%6d:%g%g%g\n”,generation,min,avg,max);
}
}
Theperform_gafunctionisshowninListing7.3,andisthecoreofthe
geneticalgorithmimplementation.Itperformsselectionandrecombination
(usingsomesupportfunctions).Thefirststepisidentifyingwhichindexof
thesolutionsarraywe’lluseforthenextgeneration(essentiallytheopposite
ofthecurrentgeneration).Wethenwalkthroughthecurrentpopulation,
selectingtwoparentswiththeselect_parentfunction.Ifcrossoveristobe
performed(randomly,pertheprobabilityofcrossover),thenacrossoverpoint
isselected,andthetailsswappedonthetwoparents(usingtheminimumof
theop_countstoensurethesmallestpossiblesequenceofmoves).
Notealsothatasthegenes(individualmoves)arecopiedfromtheparentto
thechild,there’sapossibilityofmutationwiththeMUTATEmacro.Eachchild
receivesthenumberofoperationsusedbytheparent(sizeofmoveplan).
Listing7.3: Thecoreofthegeneticalgorithm.
intperform_ga(intcur_pop)
{
inti,j,new_pop;
intparent_1,parent_2;
intcrossover;
new_pop=(cur_pop==0)?1:0;
for(i=0;i<POPULATION_SIZE;i+=2){
/*iischild_1,i+1ischild_2*/
208 ArtificialIntelligence
parent_1=select_parent(cur_pop);
parent_2=select_parent(cur_pop);
if(RANDOM()<CROSSOVER_PROB){
crossover=RANDMAX(
MIN(solutions[cur_pop][parent_1].op_count,
solutions[cur_pop][parent_2].op_count));
}else{
crossover=NUM_OPERATIONS;
}
for(j=0;j<NUM_OPERATIONS;j++){
if(j<crossover){
solutions[new_pop][i].plan[j]=
MUTATE(solutions[cur_pop][parent_1].plan[j]);
solutions[new_pop][i+1].plan[j]=
MUTATE(solutions[cur_pop][parent_2].plan[j]);
}else{
solutions[new_pop][i].plan[j]=
MUTATE(solutions[cur_pop][parent_2].plan[j]);
solutions[new_pop][i+1].plan[j]=
MUTATE(solutions[cur_pop][parent_1].plan[j]);
}
}
solutions[new_pop][i].op_count=solutions[cur_pop][parent_1].op_
count;
solutions[new_pop][i+1].op_count=solutions[cur_pop][parent_2].op_
count;
}
returnnew_pop;
}
Listing7.4: Selectingaparentsolutionwithroulettewheelselection.
intselect_parent(intcur_pop)
{
inti=RANDMAX(POPULATION_SIZE);
intcount=POPULATION_SIZE;
doubleselect=0.0;
while(count--){
select=solutions[cur_pop][i].fitness;
if(RANDOM()<(select/sum))returni;
if(++i>=POPULATION_SIZE)i=0;
}
return(RANDMAX(POPULATION_SIZE));
}
Listing7.5: Calculatingthefitnessofapotentialsolution.
doublecompute_fitness(intcur_pop,intmember,inttrace)
{
inti,from,to,disc=3;
intillegal_moves=0;
intmove;
doublefitness;
/*Initializethepegs*/
for(i=0;i<3;i++){
210 ArtificialIntelligence
pegs[0].peg[i]=disc--;
pegs[1].peg[i]=0;
pegs[2].peg[i]=0;
}
pegs[0].count=3;
pegs[1].count=0;
pegs[2].count=0;
for(i=0;i<solutions[cur_pop][member].op_count;i++){
/*Gettheactualmovefromthemovesarray*/
move=moves[solutions[cur_pop][member].plan[i]];
/*Findthesourcepeg*/
from=(move>>4)&0xf;
if(pegs[from].count==0){
illegal_moves++;
}else{
/*Findthedestinationpeg*/
to=move&0xf;
/*Ensureit’salegalmove*/
if((pegs[to].count==0)||
(pegs[from].peg[pegs[from].count-1]<
pegs[to].peg[pegs[to].count-1])){
/*Performthemove,updatethepegsconfiguration*/
pegs[from].count--;
pegs[to].peg[pegs[to].count]=pegs[from].peg[pegs[from].count];
pegs[from].peg[pegs[from].count]=0;
pegs[to].count++;
}else{
illegal_moves++;
}
}
}
/*Calculatethefitness*/
fitness=(double)(pegs[2].count*25)-(double)illegal_moves;
if(fitness<0.0)fitness=0.0;
returnfitness;
}
Let’snowlookattheapplicationinaction.Listing7.6showstheoutput
of the application (using a mutation probability of 0.01 and a crossover
probabilityof0.07).
EvolutionaryComputation 211
HE C
NT
D
O
ThegeneticalgorithmimplementationcanbefoundontheCD-ROMat
./software/ch7/ga.c.
Listing7.6: S
ampleoutputofthegeneticalgorithmoftheTowersofHanoi
problem.
$./ga.exe
1:017.15548
501:2046.149
1001:044.19549
1501:044.34548
1578:045.6875
Trace:02012102101202
The complete optimal solution to the Towers of Hanoi problem is
showninFigure7.14.Theleastnumberofmovesthatsolvethisproblem
is seven. The visual execution of this plan is shown in Figure 7.14 (the
optimalsolution).Insomecases,thealgorithmwillfindasuboptimalplan.
Commonlythisoccurswhenalackofdiversityexistsinthepopulation.This
isanexpectedproblemwiththeGA,requiringeithermultipleruns,oran
automaticrestartwhenthissituationisdetected.
FIGURE7.14: TheoptimalsolutiontotheTowersofHanoiforthreediscs.
212 ArtificialIntelligence
GENETICPROGRAMMING(GP)
As we discussed in the early part of this chapter, genetic programming
is the biologically-inspired evolution of computer programs that solve a
predefinedtask.Forthisreason,GPisnothingmorethanageneticalgorithm
appliedtotheproblemprogramevolution.EarlyGPsystemsutilizedLISP
S-expressions (as shown in Figure 7.4), but more recently, linear genetic
programming systems have been used to evolve instruction sequences to
solveuser-definedprogrammingtasks.[Banzhaf1998]
EvolvingcompleteprogramswithGPiscomputationallyveryexpensive,
andtheresultshavebeenlimited,butGPdoeshaveaplaceintheevolution
ofprogramfragments.Forexample,theevolutionofindividualfunctions
thathaveveryspecificinputsandoutputsandwhosebehaviorcanbeeasily
definedforfitnessevaluationbyGP.Toevolveafunction,thedesiredoutput
mustbeeasilymeasurableinordertounderstandthefitnesslandscapeof
thefunctioninordertoincrementallyevolveit.
NOTE GPhasalsobeenappliedtotheevolutionofphysicalhardwareusing
programmablelogic.Thisisaninterestingareaofresearch,butdueto
theinabilitytounderstandthecomplexityoftheresultingdesign,met
withlimitedsuccess.
GeneticProgrammingAlgorithm
The genetic programming algorithm uses the same fundamental flow as
the traditional genetic algorithm. The population of potential solutions is
initializedrandomlyandthentheirfitnesscomputed(throughasimulation
of executed instructions with the stack). Selection of members that
can propagate into the next generation can then occur through fitness-
proportionateselection.Withthismethod,thehigherfittheindividual,the
FIGURE7.15: Sampleinstructionsetencodingforthelineargeneticprogrammingexample.
EvolutionaryComputation 213
FIGURE7.16: Demonstratingthecrossovergeneticoperatorontwosimplelinearprograms.
highertheprobabilitythattheywillbeselectedforrecombinationinthe
nextgeneration.
The chromosome, or program to be evolved, is made up of genes, or
individual instructions. The chromosome can also be of different lengths,
assignedatcreation,andtheninheritedduringtheevolution.We’lluseasimple
instructionsetdefinedfortheparticularproblem,asshowninFigure7.15.
With this minimal number of instructions, we’ll support computing a
numberofdifferenttypesoffunctions(suchasvolumeandareaequations
fortwo-andthree-dimensionalobjects).Eachoftheinstructionsoperateson
thestack,eitherpushinganumbertothestack,ormanipulatingthestack
insomeway.
Continuing with the GP algorithm, once two parents are selected,
solutionsarerecombinedwithsomesmallprobabilityusingthecrossover
andmutationoperations.ThisprocessisshownbelowinFigure7.16.
FromFigure7.16,weseehowtwounrelatedprogramscancombineto
produceaprogramtocomputetheareaofacircle(child1).
NOTE The architecture simulated here for GP is called a “zero-address”
architecture.Whatmakesthisarchitectureuniqueisthatitisastack-
focused architecture – there are no registers available. All arguments
andprocessingoccursonthestack.Thismakestheinstructionsetvery
simple, which is ideal for evolving instruction sequences for complex
operations.
Let’slookatonefinalexampletofullyunderstandtheoperationofthese
linearprogramsusingastackmachine(seeFigure7.17).Inthisexample,
wehaveasimplefour-instructionprogram(thatcomputesthecubeofthe
214 ArtificialIntelligence
valueatthetopofthestack).Thetopofthefigurerepresentstheinitialstate
before the program is executed. The instructions are shown at the right,
withapointertotheinstructionlastexecuted(withtheinstructionshown
inbold).Thestackisshownintheinitialconfigurationwiththevaluetobe
cubed(andapointertothenextelementthatcanbewritten).
TheDUPinstructiontakesthetopelementofthestackandduplicates
it(sothatthefirsttwoelementsofthestackwillbethesame).TheMUL
instructionmultiplesthefirsttwoelementsofthestack,andthenpushesthe
resultbackontothestack(butconsumingtheinitialtwovalues).Theresult
when all instructions have been executed is the cube of the initial value,
storedatthetopofthestack.
Remember that the representation is very important, and much care
should be taken when designing it for the problem at hand. Since we’ll
be executing many of these programs during the evaluation phase of the
algorithm(largepopulation,manyverificationiterations),itmustbesimple
andefficient.Let’snowdigintotheimplementationtoseehowitworks.
FIGURE7.17: Exampleinstructionsequenceandstackforthe‘cube’program.
EvolutionaryComputation 215
GeneticProgrammingImplementation
Let’sinvestigateanimplementationoflineargeneticprogrammingtoevolve
theinstructionsequencetosolveaspecificvolumeequation.Muchofthe
implementationissharedwithourexistinggeneticalgorithmsource,sowe’ll
focushereonthecoredifferencesintheimplementation(thechromosome
representation,andthefitnesscalculationfunction).
The chromosome (potential program solution) is represented by the
structure programs_t. This contains the number of instructions in the
program (op_count) and the program itself (program). The fitness is the
currentfitnessmeasureoftheprogram(ofop_countinstructions).Thetwo
populations of program solutions is represented as the two-dimensional
programs array. It’s two dimensional because we need to represent the
currentandnextgeneration(fortheentirepopulationsize).
HE C
NT
D
O
ThefullgeneticprogrammingimplementationcanbefoundontheCD-
ROMat./software/ch7/gp.c.
Finally,inListing7.7,isthelistoflegalinstructions.Figure7.15shows
themeaningsoftheseinstructions,andFigure7.17illustratestheirusein
asampleprogram.
Listing7.7: P
rogramandpopulationrepresentationforthelineargenetic
programmingexample.
#defineNUM_INSTRUCTIONS20
typedefstruct{
intop_count;
unsignedcharprogram[NUM_INSTRUCTIONS];
doublefitness;
}programs_t;
#definePOPULATION_SIZE 2000
programs_tprograms[2][POPULATION_SIZE];
#definePUSH_PI 0
#definePUSH_2 1
#definePUSH_3 2
#defineDUP 3
#defineSWAP 4
#defineMUL 5
#defineDIV 6
#defineADD 7
#defineMAX_OPERATIONS 8
216 ArtificialIntelligence
TIP
Whendefininganinstructionsetforgeneticprogramming,designthe
instructionsetaroundtheproblem,ifpossible.Specifytheinstructions
thatcancontributetothesolution,ratherthanincreasingthecomplexity
with a large number of instructions that simply cloud the solution
space.
Nowlet’slookatthedifferencesfromourpreviousgeneticalgorithm
example.Thefirstdifferenceisthemethodbywhichparentchromosomes
areselectedforrecombination.Inthepreviousexample,weusedroulette
wheelselection,butinthisexample,we’llmodifyselect_parenttoprovide
aformofelitistselection(seeListing7.8).Westartatarandommember
andthenworkthroughtheentirepopulationlookingforamemberwhose
fitnessismorethantheaverage.Whenoneisfound,wesimplyreturnit(no
probabilisticselection,otherthanbeginninginarandomlocation).
One advantage to this approach is that we can set the crossover and
mutationprobabilitieshigh(tocovermoreofthesearchspace)withoutmuch
concernaboutlosingexistinggoodsolutions.
Listing7.8: Choosingparentsusinganelitistselectionalgorithm.
intselect_parent(intcur_pop)
{
inti=RANDMAX(POPULATION_SIZE);
intcount=POPULATION_SIZE;
doubleselect=0.0;
/*Stepthrougheachofthepopulation’smembers*/
while(count--){
select=programs[cur_pop][i].fitness;
/*Selectthisparentifitsfitnessismorethantheaverage*/
if(select>=avg)returni;
/*Checkthenextmemberofthepopulation*/
if(++i>=POPULATION_SIZE)i=0;
}
/*Nothingfoundgreaterthantheaverage,returnarandommember*/
return(RANDMAX(POPULATION_SIZE));
}
Finally, let’s look at the fitness function. This function provides the
simulation for the simple instruction set. Also within this function is the
stackobjectfromwhichtheinstructionswilloperate(seeFigure7.9).This
EvolutionaryComputation 217
function very simply operates as a virtual machine. It loops through the
instructions, executing them given the data available on the stack. If the
instructionisdeemedillegal(overflowsorunderflowsthestack),thenthe
instructionissimplyignored,andwemoveontothenextinstruction.
NOTE Notethattheloopiteratesthroughthecurrentprogrammultipletimes,
to ensure it solves the equation for multiple sample values. We begin
by pushing a random value onto the stack, and then executing each
instructionserially.Whentheprogramiscomplete,wechecktheresult
andcalculateafitnessvalue.Thisisbasedontherebeingonevalueleft
onthestack,andcorrectlycalculatingoursampleequation(Eq7.1).
Listing7.9: Fitnessfunctionforthelineargeneticprogrammingexample.
doublecompute_fitness(intcur_pop,intmember)
{
inti,instruction;
intiteration=MAX_ITERATIONS;
doublefitness=0.0;
doubleexpected,d;
stack_tstack;
while(iteration--){
d=(double)RANDMAX(100)+RANDOM();
expected=(PI*(d*d*d))/4.0;
stack.index=0;
PUSH(stack,d);
for(i=0;i<programs[cur_pop][member].op_count;i++){
/*Gettheactualmovefromthemovesarray*/
instruction=programs[cur_pop][member].program[i];
switch(instruction){
casePUSH_PI:
if(!IS_FULL(stack)){
PUSH(stack,PI);
}
break;
casePUSH_2:
if(!IS_FULL(stack)){
PUSH(stack,2.0);
}
break;
218 ArtificialIntelligence
casePUSH_3:
if(!IS_FULL(stack)){
PUSH(stack,3.0);
}
break;
caseDUP:
if(!IS_EMPTY(stack)){
doubletemp=POP(stack);
PUSH(stack,temp);
PUSH(stack,temp);
}
break;
caseSWAP:
if(stack.index>=2){
doubletemp1=POP(stack);
doubletemp2=POP(stack);
PUSH(stack,temp1);PUSH(stack,temp2);
}
break;
caseMUL:
if(stack.index>=2){
doubletemp1=POP(stack);
doubletemp2=POP(stack);
PUSH(stack,(temp1*temp2));
}
break;
caseDIV:
if(stack.index>=2){
doubletemp1=POP(stack);
doubletemp2=POP(stack);
PUSH(stack,(temp1/temp2));
}
break;
caseADD:
if(stack.index>=2){
doubletemp1=POP(stack);
doubletemp2=POP(stack);
PUSH(stack,(temp1+temp2));
}
break;
EvolutionaryComputation 219
default:
assert(0);
}
}
/*Calculatethefitness*/
fitness+=(1.0/(double)stack.index);
if(stack.stk[0]==expected){
fitness+=30.0;
if(stack.index==1)fitness+=10.0;
}
}
fitness=fitness/(double)MAX_ITERATIONS;
returnfitness;
}
Theevaluationfunctionthatwe’retryingtosolveinListing7.9isthe
volumeofasphere(seeEq7.1).There’sasinglevariablefortheequation,
d,orthediameterofthesphere.Thisvalueispushedontothestackandthe
resultshouldbeasinglevalueV(asdefinedbyEq7.1).
(Eq7.1)
Consideringtheequation,andsomeknowledgeoftheoperationofthe
stackmachine,onesimplehand-craftedsolutiontotheproblemis:
PUSH2,PUSH3,MUL,SWAP,DUP,DUP,MUL,MUL,PUSHPI,MUL,DIV
which is a reasonable solution to the problem, but evolution doesn’t
knowanythingabouttheequation,anddoesabetterjoboffindingsolutions
(squeezingoneinstructionaway).Hereareanumberofothersolutionsthat
wereevolvedbythelineargeneticprogrammer:
DUP,DUP,PUSH2,PUSH3,MUL,PUSHPI,DIV,MUL,MUL,MUL
DUP,SWAP,DUP,PUSHPI,MUL,MUL,PUSH3,PUSH2,MUL,DIV,
SWAP,DIV
DUP,DUP,MUL,MUL,PUSH2,DIV,PUSH3,MUL,PUSHPI,DIV
DUP,DUP,PUSH3,DUP,SWAP,ADD,DIV,DIV,PUSHPI,DIV,MUL
220 ArtificialIntelligence
EVOLUTIONARYSTRATEGIES(ES)
Evolutionary Strategies (ES) are one of the oldest of the evolutionary
algorithms,andremainquiteuseful.It’sverysimilartothegeneticalgorithm,
butinsteadoffocusingonbinarystrings(asdidtheoriginalgeneticalgorithm),
evolutionarystrategiesfocusontheoptimizationofreal-valueparameters.
The evolutionary strategies algorithm was developed (in parallel to
geneticalgorithms)duringthe1960sattheTechnicalUniversityofBerlin
(TUB),GermanybyRechenbergandSchwefel.Evolutionarystrategieswere
initiallydesignedtosolvefluiddynamicsproblemsthroughsimulations.Their
initialexperimentsutilizedapopulationofone(parent+offspring,choosing
thebesttopropagatefurther),sincetheoptimizationwasdonemanually,
withoutaccesstoacomputertosimulatetheexperiments.Evenwiththis
simpleapproach(whichlaterevolvedintoapopulation-basedtechnique)
theirresultsweresuccessful.
FIGURE7.18: Schwefel’smethodfornozzleoptimizationusingevolutionarystrategies.
EvolutionaryComputation 221
Astheevolutionarystrategycontinuedtobedeveloped,itwassuccessfully
usedbySchwefeltooptimizetheshapeofasupersonictwo-phaseflashing
nozzle.[EvoNews1999]Tooptimizetheshapeofnozzles,Schwefelused
anencodingthatdefinedthediameterofthenozzleatvariouspointsacross
thetube(seeFigure7.18).
Theoriginalevolutionarystrategyusedasingleparent,andproduceda
singlechild.Thisiscalleda(1+1)strategy(oneparentproducesasingle
offspring).Ingeneralterms,thesestrategiesaredefinedas ,where
parentsareselectedand offspringresult.Inthisstrategy,thepopulation
of membersallcompeteforsurvival.Anotherapproachisreferred
toas ,where parentsareselectedand offspringresult.Onlythe
offspringcompeteinthenextgeneration,the parentsarecompletely
replacedinthenextgeneration.
EvolutionaryStrategiesAlgorithm
Oneoftheuniqueaspectsoftheevolutionarystrategiesalgorithm,andwhat
makesthealgorithmusefultoday,isthatit’srelativelysimpletoimplement.
Traditionally,thealgorithmisn’tassimpletounderstandasthegeneticalgorithm,
buttheapproachthatwillbeexploredherewillprovidethebasicconcepts,and
acoupleofmutationsimplificationsproposedbySchwefelhimself.
FIGURE7.19: Thefundamentalevolutionarystrategiesalgorithm.
222 ArtificialIntelligence
The flow of the evolutionary strategies algorithm is shown in Figure
7.19.We’llfirstwalkthroughthehigh-levelflowandthenexplorethedetails.
The process begins with the initialization of the population. Each of the
membersofthepopulationconsistsofavectorofreal-valuesthatrepresent
theproblemitself.
NOTE Theevolutionarystrategiesalgorithmiscalledaphenotypicalgorithm,
where the genetic algorithm is a genotypic algorithm. A phenotypic
algorithmrepresentsparametersofthesystemasthesolution,wherea
genotypicalgorithmrepresentsanabstractionofthesolution.Takefor
example,themodelingofthebehaviorofasystem.Inthephenotypic
approach,thesolutionswouldrepresenttheparameterofthebehavior
itself,butinthegenotypicapproach,thesolutionwouldrepresentan
intermediaterepresentationofthebehavior.
The next step is producing the next generation by selecting parent
solutions and generation offspring. We’ll explore the simplest case of
selectioninESwhereeachparenthasthepotentialtomoveintothenext
generation. Each parent is selected and an offspring generated given a
mutation.Theparentandchildarethencompared,andwhicheverhasthe
betterfitnessismovedtothenextgeneration.Thisprocesscontinuesfor
eachmemberofthecurrentpopulation.Thealgorithmcanthenterminate
(ifasolutionisdiscovered),orcontinueforsomenumberofgenerations.
Let’snowexplorewhatitmeanstomutateaparenttocreateanoffspring.
When we create random numbers (for example, with our RANDOM()
function),thenumberisjustaslikelytobesmallasitislarge(uniformly
distributed).Whatwereallywantisprimarilysmallrandomnumbers,with
anoccasionallargerrandomnumber.Thisallowsthesmallrandomnumbers
to “tweak” the solution, with the larger random numbers for extending
thesolutioninthefitnesslandscape.Thesetypesofrandomnumbersare
called normally distributed with an expectation rate of zero. To produce
theserandomnumbers,wefirstcalculatetwouniformlydistributedrandom
numbers(u1andu2),andthenuseEq7.2and7.3toproducethenormally
distributedrandomnumbers.
(Eq7.2)
(Eq7.3)
One advantageous modification to our mutation scheme is to further
limitthesizeofthechangesthatoccurovertime(similartowhatoccurs
EvolutionaryComputation 223
FIGURE7.20: Plotofthesamplefunctiontobemaximizedbytheevolutionarystrategy.
insimulatedannealing).Tosupportthis,wecanusethecurrentiteration
(itercur) and max iteration (itermax) to identify a multiplier, as shown in
Equation7.4).
(Eq7.4)
EvolutionaryStrategiesImplementation
Let’s now have a look at an implementation of evolutionary strategies for
a function optimization problem. The goal of this problem is to find the
parametersthatmaximizethefunction(resultinthegreatestvalue).We’lluse
thefunctionshowninEq7.5,whichisshowngraphicallyinFigure7.20.
(Eq7.5)
Let’s begin with a description of the fundamental structures that will
beusedbytheevolutionarystrategiesalgorithm.Thetwocoreelementsof
theevolutionarystrategiesisthesizeofthepopulation,andthenumberof
iterationsthatwilloccur(whichisequivalenttothenumberofgenerationsto
becreated).Thesolution_ttypedefspecifiesthearguments(xandy)andthe
result(fitness).We’llcreateatwo-dimensionalarrayofthesesolution_ttypes,
withthefirstdimensionusedtodefinethegeneration(whichoperatesina
ping-pongfashion),andtheseconddimensionusedtodefinethemembers
ofthepopulation.
Listing7.10: Fundamentalstructuresfortheevolutionarystrategiesalgorithm.
#defineMAX_POPULATION 50
#defineMAX_ITERATIONS 20
224 ArtificialIntelligence
typedefstruct{
doublex;
doubley;
doublefitness;
}solution_t;
solution_tsolutions[2][MAX_POPULATION];
ThemainloopfortheevolutionarystrategyisshowninListing7.11.
Weinitializethepopulationwitharandomsetofsolutionsusinginitialize_
population, and then calculate the population’s fitness with compute_
population_fitness. We then iterate the maximum number iterations
previouslydefinedbyMAX_ITERATIONS.Acalltoselect_and_recombine
createsthenextgenerationofsolutions,afterwhich,weswitchthecurrent
population(asdefinedbycur_pop)tothenext.Afterevaluatingthefitness
oftheentirepopulation(viacompute_population_fitness),wecontinuethe
processuntilthemaximumnumberofiterationsisreached.
Listing7.11: Themainloopfortheevolutionarystrategy
intmain(void)
{
intcur_pop=0;
inti=0;
RANDINIT();
initialize_population(cur_pop);
compute_population_fitness(cur_pop);
for(i=0;i<MAX_ITERATIONS;i++){
select_and_recombine(cur_pop,i);
cur_pop=(cur_pop==0)?1:0;
compute_population_fitness(cur_pop);
printf(“%g%g%g\n”,min,avg,max);
}
find_and_emit_best(cur_pop);
return0;
}
Thecoreoftheevolutionarystrategyisintheselectionandrecombination
ofcandidatesolutions(seethefunctionselect_and_recombine,Listing7.12).
Thisfunctionfirstdetermineswhichpopulationindexisthenextgeneration
(new_pop)andthecurrentmultiplier,whichisusedtoscaleanychanges
EvolutionaryComputation 225
in the mutation (see Eq 7.4). We then walk through the entire current
population(aspassedin,pop).
Foreachmemberofthecurrentpopulation,webeginbygenerating
our normally distributed random numbers (using Eqs 7.2 and 7.3). We
then mutate the parent to create the offspring into the next generation
(inanelitistfashion).Iftheoffspringisnotasfitastheparent,thenthe
parentsolutioniscopiedtothenextgeneration(overwritingthegenerated
offspring). This process continues for each member of the existing
generation.
Listing7.12: Creatingthenextgenerationofthepopulation.
voidselect_and_recombine(intpop,intiteration)
{
inti,new_pop;
doublemultiplier;
doubleu1,u2,z1,z2,fitness;
/*Figureoutwhichpopulationindexisthenextgeneration*/
new_pop=(pop==0)?1:0;
/*Equation7.4*/
multiplier=((double)MAX_ITERATIONS-(double)iteration)/
(double)MAX_ITERATIONS;
for(i=0;i<MAX_POPULATION;i++){
u1=RANDOM();u2=RANDOM();
/*Equation7.2*/
z1=(sqrt(-2.0*log(u1))*sin((2.0*PI*u2)))*multiplier;
/*Equation7.3*/
z2=(sqrt(-2.0*log(u1))*cos((2.0*PI*u2)))*multiplier;
/*Createthechildasthemutatedparent*/
solutions[new_pop][i].x=bound(solutions[pop][i].x+z1);
solutions[new_pop][i].y=bound(solutions[pop][i].y+z2);
fitness=compute_fitness(&solutions[new_pop][i]);
/*Ifthechildislessfitthanparent,movetheparenttochild*/
if(fitness<solutions[pop][i].fitness){
solutions[new_pop][i].x=solutions[pop][i].x;
solutions[new_pop][i].y=solutions[pop][i].y;
}
}
return;
}
226 ArtificialIntelligence
NoteinListing7.12,theboundfunctionisusedtoboundthevalues
from-20to20forbothxandy.
Finally,wepresentthefunctiontocalculatethefitnessofagivencandidate
solution.UsingEq7.5,thefunctionisevaluated,stored,andreturned.
Listing7.13: Computingthefitnessofacandidatesolution.
doublecompute_fitness(solution_t*sol_p)
{
doublex,y;
doublefitness;
/*Cachetheargumentstosimplifytheequation*/
x=sol_p->x;
y=sol_p->y;
/*Equation7.5*/
fitness=
sol_p->fitness=
(sin(x)/x)*(sin(y)/y)*(double)10.0;
returnfitness;
}
HE C
NT
D
O
Theevolutionarystrategiesalgorithmimplementationcanbefoundon
theCD-ROMat./software/ch7/es.c.
Let’snowlookatthealgorithminaction.Notethattheprogramwillemit
thefitnessvalues(minimum,average,andmaximum),foreachgeneration.
AscanbeseeninListing7.14,sixgenerationsareallthatarerequiredtoget
towithin90%ofthemaximum.Thealgorithmthentunesfor14generations
(togetwithin99.9%ofthemaximum).Notethatintheresults,stepsare
taken in maximum fitness, where the maximum fitness appears dormant,
andthenahigherfitmemberisfound.Thishasbeenobservedinanumber
ofevolutionaryalgorithms,andtiedbiologicallytowhat’scalledpunctuated
equilibrium.
Inthetheoryofpunctuatedequilibrium(fromevolutionarybiology),ithas
beenfoundthatreproducingspeciesoftenshownoevolutionarychangethrough
theirlifetime.Butwhenevolutionarychangedoesoccur,itseffectsareclearly
seenintheabilityofthenewspeciestosurviveandadapttoitsenvironment.
Listing7.14: Sampleexecutionoftheevolutionarystrategiesalgorithm.
$./es
-1.865340.2217983.15964
EvolutionaryComputation 227
-1.570370.3600163.15964
-0.1947020.8054248.64
-0.09590310.9616688.64
-0.09590311.156228.7512
-0.09590311.242449.06489
-0.008761971.352389.69174
-0.001790161.39659.96464
-0.001790161.508739.96464
0.0495771.540399.96464
0.0495771.591799.96464
0.05969691.620299.96464
0.05969691.653299.98932
0.05969691.703599.98932
0.05969691.713219.98932
0.05969691.73099.98932
0.05969691.745839.98932
0.05969691.751849.98932
0.05969691.754299.98932
0.05969691.757599.99414
Bestis[0.0561596,-0.0190792]=9.99414
$
DIFFERENTIALEVOLUTION(DE)
DifferentialEvolution(DE)isamorerecentstochasticpopulation-based
evolutionary method (introduced by Storn and Price in 1996). It follows
the standard evolutionary algorithm flow (mutation, recombine, select),
buthassomesignificantdifferencesinhowmutationandrecombinationis
performed.
The fundamental idea behind DE is the use of vector differences
(choosingtworandomlyselectedvectors,andthentakingtheirdifference
asameanstoperturbthevectorandprobethesearchspace).Thevector
difference is then added to a third randomly selected vector, making the
approachself-organizing.
DE also includes two tunable parameters, F (the weighting factor)
andCR(thecrossoverprobability).Theweightingfactorisappliedtothe
differencevector.Thecrossoverprobabilityspecifiestheprobabilitythat
multi-point crossover will occur for the initial vector and the resulting
targetvector.
228 ArtificialIntelligence
FIGURE7.21: High-levelflowofthedifferentialevolutionalgorithmwithnoteddifferencesforDE.
DifferentialEvolutionAlgorithm
The Differential Evolution algorithm is simple, but has some additional
complexitiesinthenumberofactivitiesthatoccurforrecombination.Let’s
startwithabigpictureoftheDEalgorithm,andthenexplorethedetailsof
mutationandrecombination.
The high-level flow for DE is shown in Figure 7.21. This is the
fundamentalevolutionaryalgorithmflow,butthedetailsdifferformutation,
recombination,andreplacement.
ThereareanumberofvariationsforDE,butwe’llfocushereonthe
nominalapproach.Afterinitialization,eachofthemembersofthepopulation
undergoes mutation and recombination. Once recombination occurs, the
newmemberiscomparedtotheoldmember,andwhicheverfitnessisbetter
ismovedtothenextgeneration(replacementpolicy).
With our member in the current generation (x i,G) we select three
uniformlyrandommembersfromthecurrentgenerationthatareunique
(xi,G!=xr1,G!=xr2,G!=xr3,G).Usingthesemembervectors,wecreatewhat’s
knownasamutantvector,ordonorvector,(vi,G+1)inthenextgenerationusing
theweighteddifferenceoftwoofthevectors(r2andr3)summedwiththe
thirdvector(r1).ThisisshowninEq7.6.
FIGURE7.22: Mutationprocessindifferentialevolution.
FIGURE7.23: TheDEcrossoverandreplacementprocess.
Thisnewmutantvectorhascompletedthemutationstageandisnowready
forrecombination.ThisprocessisshowngraphicallyinFigure7.22.
Inthisstage,we’llperformamulti-pointcrossoverofthemutantvector
(vi,G+1) with our original vector from the current generation (x i,G). This
constitutesusingelementsfromourmutantvectorwiththeoriginalmember
forfitnessevaluation.ThisprocessisshowninEq7.7.
Thecrossoverprocessisperformedoneachelementofthevector,with
theresultingvectorcandidatebeingprovidedtothereplacementprocess
(seeFigure7.23).Thecurrentmemberandmutantarerandomlypermitted
tocontributetothenewvector.Intheend,whichevervectorhasthegreater
230 ArtificialIntelligence
fitness(newvector,orvectorfromthecurrentgeneration)ispermittedinto
thenextgeneration.
Let’s now explore a sample implementation of DE for function
maximization.Forcomparativepurposes,we’llusethefunctionshownin
Figure7.20.
DifferentialEvolutionImplementation
Let’sbeginwithadiscussionofhowtheDEobjectswillberepresentedina
program(seeListing7.15).Thefundamentalvectortype(vec_t)willbeused
torepresentthevectorsofreal-valuestobeoptimized.Thisisintegratedinto
ourmemberobject(member_t),whichalsoincludesthefitnessmeasure.
NOTE Notethatthepopulationistwo-dimensionalinsize.Thefirstdimension
representsthegeneration(twoareusedbecausewe’llimplementaping-
ponggeneration,whereatanyonetime,theindexrepresentsthecurrent
generation,andtheoppositeindexrepresentsthenextgeneration).
Wealsomaintainabestmember_tthatkeepsthebestmemberfound
sofar(forhousekeepingpurposes).
We also define the tunable parameters here, F (the mutation factor)
andCR(thecrossoverprobability).Theseparameterscanbetunedforthe
particularproblemathand,butarereasonableattheircurrentlevelsforthis
optimizationproblem.
Listing7.15: DEtypesandsymbolics.
#defineMAX_ELEMENTS 2
typedefdoublevec_t[MAX_ELEMENTS];
#defineMAX_POPULATION 10
#defineMAX_ITERATIONS 100
typedefstruct{
vec_targs;
doublefitness;
}member_t;
member_tpopulation[2][MAX_POPULATION];
member_tbest={{0.0,0.0},0.0};
#defineF ((double)0.5)
#defineCR ((double)0.8)
ThemainloopforDEisshowninListing7.16.Thisfunctionimplements
the outer loop to the DE algorithm. It begins by initializing the current
EvolutionaryComputation 231
population(cur_pop),andtheninitializingtherandomnumbergenerator.
Thepopulationistheninitializedwithacalltoinit_population,whichnot
onlyinitializesthevectorsforeachmember,butalsocomputestheirinitial
fitnessvalues(withacalltocompute_fitness).
Withinitializationcomplete,weentertheDEalgorithm.We’llprovide
noexitcriteria,andinsteadsimplyexecutethealgorithmforsomemaximum
number of iterations (MAX_ITERATIONS). Each iteration consists of
performing the DE core algorithm (select_and_recombine) and then
emittingthebestmemberfoundsofar(asstoredinthebeststructure).
Listing7.16: TheDEmainloop.
intmain()
{
inti;
intcur_pop=0;
RANDINIT();
init_population(cur_pop);
for(i=0;i<MAX_ITERATIONS;i++){
cur_pop=select_and_recombine(cur_pop);
printf(“Bestfitness=%g\n”,best.fitness);
}
return0;
}
The core of the DE algorithm is implemented in the select_and_
recombinefunction(seeListing7.17).Webeginbydeterminingtheindexof
thenextgeneration(next_pop)andtheninitializingthebeststructurefitness
tozero(inordertofindthecurrentbestmemberinthepopulation).
Thenextstepistoiteratethrougheachofthemembersofthepopulation
tocreateanewcandidatesolution.Wecachethecurrentindextothenext
generationmember(mutant)toincreasethereadabilityofthecode.Next,
wecreatethreerandomnumbers(r1,r2,andr3)whichareallunique,and
differfromeachotherandthecurrentmemberindex(i).
The mutant vector is created next using Eq 7.6. Using the three
members from the current population (as defined by our three random
numbers),themutantvectoriscreated.WithEq7.7,thecrossoverprocess
isperformedusingthecurrentmemberfromthecurrentgeneration,and
themutantvector.Whencomplete,thefitnessiscomputedwithacallto
compute_fitness.
232 ArtificialIntelligence
Atthispoint,wehaveamutantvectorwithitsfitnesscalculated.We
comparethefitnessofthemutantwiththefitnessofthecurrentmember,
andwhicheveraregreatermovesintothenextgeneration(thereplacement
process).Wefollowthiswithsomehousekeepingtokeeptrackofthebest
vectorfoundsofar(emittedinthemainfunction).
Listing7.17: TheDEprocessofmutation,recombination,andreplacement.
intselect_and_recombine(intpop)
{
intnext_pop=(pop==0)?1:0;
inti,j;
member_t*mutant;
intr1,r2,r3;
best.fitness=0.0;
for(i=0;i<MAX_POPULATION;i++){
/*Cachethetargetvectorinthenextgeneration*/
mutant=&population[next_pop][i];
/*Calculatethreerandomnumbers(r1,r2,r3)whichareall
*unique.
*/
do{
r1=RANDMAX(MAX_POPULATION);
}while(r1==i);
do{
r2=RANDMAX(MAX_POPULATION);
r3=RANDMAX(MAX_POPULATION);
}while((r3==r2)||(r3==r1)||(r2==r1)||
(r3==i)||(r2==i));
/*Giventhecandidatemember,andourrandommembers,forma
*‘mutantmember’(Equation7.6).
*/
for(j=0;j<MAX_ELEMENTS;j++){
mutant->args[j]=population[pop][r1].args[j]+
(F*(population[pop][r2].args[j]-
population[pop][r3].args[j]));
}
/*Performcrossoverof‘mutant’vectorwiththecurrentgeneration
*member(Equation7.7)
*/
EvolutionaryComputation 233
for(j=0;j<MAX_ELEMENTS;j++){
if(RANDOM()<CR)mutant->args[j]=population[pop][i].args[j];
}
mutant->fitness=compute_fitness(mutant);
/*Iftheoriginalmemberhasagreaterfitnessthanthemutant,copy
*theoriginalmemberoverthemutantinthenextgeneration.
*/
if(population[pop][i].fitness>mutant->fitness){
for(j=0;j<MAX_ELEMENTS;j++){
mutant->args[j]=population[pop][i].args[j];
}
mutant->fitness=population[pop][i].fitness;
}
/*Housekeeping--savethebestmember*/
if(mutant->fitness>best.fitness){
for(j=0;j<MAX_ELEMENTS;j++){
best.args[j]=mutant->args[j];
}
best.fitness=mutant->fitness;
}
}
returnnext_pop;
}
Finally,thecompute_fitnessfunctionisusedtocalculatethefitness
ofamemberofthepopulation.Thepointertothecurrentmemberispassed
in (of type member_t), and the coordinates are extracted to enhance
readability. We bound the coordinates to the constraints of the function
(theareathatweintendtomaximize)andthenuseEq7.5tocalculateand
returnthefitness.
Listing7.18: CalculatingthefitnessofaDEmember.
doublecompute_fitness(member_t*member_p)
{
doublex,y;
doublefitness;
/*Cachethecoordinatestosimplythefunction.*/
x=member_p->args[0];
y=member_p->args[1];
234 ArtificialIntelligence
/*Boundthelocationoftheparticle*/
if((x<-10.0)||(x>10.0)||
(y<-10.0)||(y>10.0))fitness=0.0;
else{
/*Equation7.5*/
fitness=
(sin(x)/x)*(sin(y)/y)*(double)10.0;
}
returnfitness;
}
HE C
NT
D
O
Thedifferentialevolutionalgorithmimplementationcanbefoundonthe
CD-ROMat./software/ch7/de.c.
Thealgorithmdoesaverygoodjobofquicklyconvergingonasolution.
Listing7.19showsasamplerunoftheDEimplementation.Inaveryshort
time,thealgorithmmovesfrompoorsolutionstoanalmostoptimalsolution
(10.0).
Listing7.19: SamplerunoftheDEimplementation.
$./de.exe
Bestfitness=0.662495
Bestfitness=0.662495
Bestfitness=0.963951
Bestfitness=3.66963
Bestfitness=4.8184
Bestfitness=4.8184
Bestfitness=5.54331
Bestfitness=5.54331
Bestfitness=7.48501
Bestfitness=7.48501
Bestfitness=9.78371
Bestfitness=9.78371
Bestfitness=9.97505
Bestfitness=9.97505
Bestfitness=9.97505
Bestfitness=9.99429
Bestfitness=9.99429
Bestfitness=9.99429
Bestfitness=9.99429
EvolutionaryComputation 235
Bestfitness=9.99568
Bestfitness=9.99646
Bestfitness=9.99646
Bestfitness=9.9981
Bestfitness=9.9981
Bestfitness=9.9981
Bestfitness=9.99837
Bestfitness=9.99974
Bestfitness=9.99974
Bestfitness=9.99984
Bestfitness=9.99984
Bestfitness=9.99998
Bestfitness=9.99998
Bestfitness=9.99998
Bestfitness=9.99998
$
ThepriorrunisshownplottedinFigure7.24.Asshown,thealgorithm
isquicklyabletoconvergeandthenfine-tunetheresulttothenearoptimal
solution.
Whilethere’snotyetaproofofconvergencefortheDEalgorithm,it
hasbeenshowntobeeffectiveonawiderangeofoptimizationproblems.
TheoriginalauthorsalsofoundinastudythattheDEalgorithmwasmore
efficientthanboththegeneticalgorithmandsimulatedannealing.
FIGURE7.24: Bestfitnessplotforthedifferentialevolutionimplementation(populationsize100).
236 ArtificialIntelligence
FIGURE7.25: Anatomyofaparticleinalargerstorm.
PARTICLESWARMOPTIMIZATION(PSO)
The last population-based algorithm that we’ll explore in this chapter is
calledParticleSwarmOptimization(orPSO).PSOsimulatesacollectionof
particlesthatswarmwithoneanotherwithinanN-dimensionalspace(where
Nisthesizeofthesolutionvector).Averysimplesetofequationsisused
toimplementaflockingbehavior,whichgivestheparticlessomeamountof
freedomtosearchtheN-dimensionalsearchspace,butalsosomeconstraints
toexhibitflockingbehaviorbytrackingtheparticlethathasthecurrentbest
performance.
A particle within the swarm exists as an object that contains a vector
(with the same dimensionality as the solution space), a velocity (for each
elementofthedimensionalityvector,resultinginthevectorvelocity),the
fitness(forthecurrentvector),andavectorrepresentingthebestposition
foundsofar(seeFigure7.25).
Particlesintheswarmareinfluencedbytwouniquefactors.Thefirstis
thebestposition(vector)oftheparticleitself,andthesecondistheglobal
bestpositionfoundbyany particle in the swarm. Therefore, a particle is
influencedbyitsbestposition,andalsothebestpositionoftheswarm.The
amountofinfluenceofeachiscontrollable,aswe’llseeinthediscussionof
thealgorithmitself.
Let’s now dig into the PSO algorithm, to understand how particles
swarm,andtheequationsforswarminfluence.
ParticleSwarmAlgorithm
Theparticleswarmoptimizationalgorithmisverysimpletounderstand,but
isalsoquiteeffectiveforavarietyofoptimizationproblems.Thissection
willexplorethePSOalgorithminsufficientdetailtoimplementageneral
functionmaximizer.
EvolutionaryComputation 237
The use of particle swarms as an optimization technique is recent
comparedtotheotherevolutionaryalgorithmsdiscussedthusfar.Thesocial
behaviorofbirdsflockingandfishschoolinginspiredEberhartandKennedy
tocreatewhattheyrefertoasswarmintelligence.
ThebasicflowofthePSOisasfollows.First,apopulationofrandom
vectorsandvelocitiesarecreatedastheswarmofparticles.Initially,these
particlesarerandomlyplaced,andeachmoveinrandomdirections,butas
the algorithm is performed, swarming behavior emerges as the particles
probethemulti-dimensionalsurface.
Withourrandomsetofparticles,thefitnessofeachisevaluatedand
storedasthecurrentfitness.Wealsokeeptrackofaglobalbestparticlethat
hasthebestoverallfitness.Thisparticle,tosomeextent,isthecenterof
theswarm.Notethatwealsokeeptrackofthepersonalbestvectorforthe
particle,whichisstoredasthebestposition(seeFigure7.20).Atthispoint,
terminationcriteriacouldbeapplied.Ifasatisfactorysolutionisfound,or
some maximum number of iterations has been performed, the algorithm
couldexit,emittingthecurrentglobalbestsolutionfoundsofar.
Ifthealgorithmhasnotyetreacheditsterminationcriteria,thevelocity
oftheparticlesisupdated,andtheneachparticle’spositionisupdated(given
its current position and current velocity). The process then continues by
evaluatingeachparticle’sfitness,andcheckingourterminationcriteria.
CalculatingthenextpositionofanN-dimensionalparticleisshowninEq
7.8.Eachvectorelement(Xn)ofparticle(P)accumulatesthevelocityelement
(Vn)scaledbythetimeinterval(t)overwhichtheparticleistobemoved.
(Eq7.8)
Recallthatwealsoupdatethevelocityaftertheparticleismoved.As
showninEq7.9,therearetwoindependentinfluencesoverthechangein
velocity,thecurrentglobalbestparticle(definedasGXn),andthepersonal
best for this particle (PBXn). For each term, there exists what’s called an
accelerationconstant(c1,c2),whichisusedtodeterminehowmuchinfluence
theglobalorpersonalbestsolutionhasoverthevelocityequation.
Toaddsomevariabilitytotheequation,wealsoincludetwouniform
random numbers (R1, R2), which apply to the terms. How these uniform
randomnumbersaregeneratedprovidessomeemphasistoonetermover
another(globalvspersonalbest).Thegoalistoprobethesolutionspace
withgreatervariability.
Pvn = Pvn +(C1* R1* (Gxn - Pxn)) + (c2 * R2 * (PBvn - Pxn)) (Eq7.9)
238 ArtificialIntelligence
Usingtheseverysimpleequations(Eq7.8forparticlemovementand
Eq 7.9 for velocity adjustment), the PSO algorithm is able to minimize
or maximize functions with an efficiency similar to genetic algorithms or
evolutionarystrategies.
ParticleSwarmImplementation
Asshowninthealgorithmdiscussion,theimplementationofparticleswarm
optimizationissimple.Let’sbeginourdiscussionwithadescriptionofthe
representationofparticlesandtheswarminsoftware.
In this implementation, we’ll encode a solution as a two-dimensional
object, with the fitness defined as the function of the vector arguments
(twointhisexample,representingthexandyarguments).Forthefitness
function,we’llusethefunctionshowninFigure7.20(asdemonstratedby
theevolutionarystrategiesalgorithm).
Listing 7.20 provides the fundamental types and symbolics for the
particleswarmoptimizationimplementation.Themostfundamentaltype
isthevec_t,whichdefinesourvector(inthisexample,itspecifiesanxand
ycoordinate).Thisvectortypeisusedtorepresentthecoordinateposition
(coord), the current velocity, and the personal best vector coordinates
(best_coord).Structureparticle_tcollectsthesetogetherasasingleobject
to represent the entire particle. The particle swarm (of number MAX_
PARTICLES)isrepresentedbythearrayparticles.Theparticle_ttypeis
alsousedtorepresentthecurrentglobalbest(gbest).
Listing7.20: Particleswarmtypesandsymbolics.
typedefstruct{
doublex;
doubley;
}vec_t;
typedefstruct{
vec_tcoord;
vec_tvelocity;
doublefitness;
vec_tbest_coord;
doublefitness_best;
}particle_t;
#defineMAX_PARTICLES 10
#defineMAX_ITERATIONS 30
EvolutionaryComputation 239
particle_tparticles[MAX_PARTICLES];
particle_tgbest;
Theflowoftheparticleswarmalgorithmisimplementedinthemain
function(seeListing7.21).Thisinitializesandseedstherandomnumber
generator (RANDINIT) and the initializes the particle population with
randomlocationsandvelocities.Thelooptheniteratesforthemaximum
numberofiterations(MAX_ITERATIONS).Foreachiteration,eachparticle
intheswarmisupdatedthroughacalltoupdate_particle.Afterallparticles
intheswarmareupdated,thecurrentglobalbestparticle(gbest)isemitted
sothattheswarm’sprogresscanbetracked.
Listing7.21: Theparticleswarmoptimizationmainloop.
intmain()
{
inti,j;
RANDINIT();
init_population();
for(i=0;i<MAX_ITERATIONS;i++){
for(j=0;j<MAX_PARTICLES;j++){
update_particle(&particles[j]);
}
printf(“CurrentBest:%g/%g=%g\n”,
gbest.coord.x,gbest.coord.y,gbest.fitness);
}
return0;
}
Thecoreoftheparticleswarmoptimizationalgorithmisprovidedinthe
update_particlefunction(seeListing7.22).Thefunctionprovidesanumber
ofcapabilities,butbeginswiththeupdateoftheparticle’spositionusingEq
7.6.Withtheparticle’schangeinlocation,wecalculatethenewfitnessof
theparticlewithacalltocompute_fitness.Next,usingEq7.7,thevelocity
vectorfortheparticleisupdated(giventheparticle’spersonalbestposition
andthepositionoftheglobalbestposition).
Finally,thefunctionperformssomehousekeepingtomaintainthebest
positions.Wefirstchecktoseeifthefitnessfortheparticleisbetterthanthe
personalbestfitness.Ifitis,westorethiswithintheparticle.Iftheparticle’s
personalbesthasbeenupdated,wechecktoseeifit’sbetterthantheglobal
bestposition.Ifso,westorethisintothegbestparticle.
240 ArtificialIntelligence
Listing7.22: Updatingparticlepositionsandvelocities.
voidupdate_particle(particle_t*particle_p)
{
/*Updatetheparticle’sposition(Equation7.8)*/
particle_p->coord.x+=(particle_p->velocity.x*dt);
particle_p->coord.y+=(particle_p->velocity.y*dt);
/*Evaluatetheparticle’sfitness*/
particle_p->fitness=compute_fitness(&particle_p->coord);
/*Updatethevelocityvector(Equation7.9)*/
particle_p->velocity.x+=
((c1*RANDOM()*(gbest.coord.x-particle_p->coord.x))+
(c2*RANDOM()*(particle_p->best_coord.x-particle_p->coord.x))
);
particle_p->velocity.y+=
((c1*RANDOM()*(gbest.coord.y-particle_p->coord.y))+
(c2*RANDOM()*(particle_p->best_coord.y-particle_p->coord.y))
);
/*Ifthefitnessisbetterthanthepersonalbest,thensaveit.*/
if(particle_p->fitness>particle_p->fitness_best){
particle_p->fitness_best=particle_p->fitness;
particle_p->best_coord.x=particle_p->coord.x;
particle_p->best_coord.y=particle_p->coord.y;
/*Ifthefitnessisbetterthantheglobalbest,thensaveit.*/
if(particle_p->fitness_best>gbest.fitness){
gbest.fitness=particle_p->fitness_best;
gbest.coord.x=particle_p->coord.x;
gbest.coord.y=particle_p->coord.y;
}
}
return;
}
Listing7.23: Calculatingthefitnessofaparticle.
doublecompute_fitness(vec_t*vec_p)
{
doublex,y;
doublefitness;
/*Cachethecoordinatestosimplythefunction.*/
x=vec_p->x;
y=vec_p->y;
/*Boundthelocationoftheparticle*/
if((x<-10.0)||(x>10.0)||
(y<-10.0)||(y>10.0))fitness=0.0;
else{
/*Equation7.5*/
fitness=
(sin(x)/x)*(sin(y)/y)*(double)10.0;
}
returnfitness;
}
HE C
NT
D
O
Theparticleswarmoptimizationalgorithmimplementationcanbefound
ontheCD-ROMat./software/ch7/ps.c.
Let’s now look at the implementation in action. The implementation
emitsthecurrentglobalbestparticleasititerates(seeListing7.24).
Listing7.24: Sampleoutputoftheparticleswarmimplementation.
$./ps
CurrentBest:-9.13847-1.404570.216992
CurrentBest:0.92441.228426.62169
CurrentBest:0.09345271.179277.82673
CurrentBest:0.106661.174637.83906
CurrentBest:0.1198661.169997.85087
CurrentBest:0.1330731.165357.86217
CurrentBest:0.146281.160717.87295
CurrentBest:0.1594871.156077.8832
CurrentBest:0.1726931.151437.89293
...
CurrentBest:-0.08900250.04325639.98369
CurrentBest:-0.08900250.04325639.98369
242 ArtificialIntelligence
FIGURE7.26: Globalbestparticlefitnessshowingquickconvergenceandthensolutiontuning
withslowingvelocities.
FIGURE7.27: Objectivefunctionwiththeglobalbestparticlesoverlaid.
CurrentBest:0.016041-0.03385749.99766
CurrentBest:0.016041-0.03385749.99766
CurrentBest:0.0043322-0.004368279.99994
$
As shown in Figure 7.26, the global best fitness converges very
quickly to a solution (at around 70 iterations). The particle swarm is
abletofindagoodsolutionratherquickly,butthentendstoorbitthis
positionduetoitshighvelocity.Butasthevelocityequationsslowdown
theparticleswarm,theparticlesareabletofine-tuneinordertofinda
bettersolution.
EvolutionaryComputation 243
Wecanbettervisualizethisprocessbylookingattheobjectivefunction
surfacewiththeglobalbestparticlesoverlaid(seeFigure7.27).Inthisfigure,
weseethefitnessofthefunctionsurface(thelighterthecolor,thebetter
thefitness).Theparticleshowsaveryquickconvergenceontoareasonable
solution,andthenfine-tunesatthispeaktofindthebestsolution.Notethat
what’sshownhereistheglobalbestparticle(foundbytheentireswarmof
particles),andnottheswarmofparticlesthemselves.
Thealgorithmcanbetunedbythetwoaccelerationparameters(c1and
c2). These parameters determine how much influence the personal best
vectorandtheglobalbestvectorhaveovertheparticle’strajectory.Recall
thatthevelocityequationisusedtochangethevelocitytowardtheparticle’s
bestposition,ortheglobalbestposition.Thisaccelerationalsoincludesa
randomelement,tosupportprobingthesurfaceoftheobjectivefunctionin
thehopesofescapinglocalminima.
Particleswarmoptimizationisanotherusefuloptimizationmethodthat
isverybiologicallyplausible.Likemostotherevolutionarymethods,thePSO
FIGURE7.28: DemonstrationofevolutionprocessfromchromosometoPLD.
244 ArtificialIntelligence
reliesonapopulationofcandidatesolutionstofindthebestsolution.Ascan
beseenfromthesampleimplementation,PSOissimple,computationally
inexpensive,andeffectiveforfunctionoptimization.
EVOLVABLEHARDWARE
Theuseofevolutionaryalgorithmstogeneratehardwarehasbeenusedina
similarwayasGPisusedtoevolvesoftware.Hardwareevolutionrangesfrom
simplecircuitdesign(suchasforanalogfilters),ormorecomplexproducts
suchasevolvingthearchitectureforProgrammableLogicArrays(PLA,or
ProgrammableLogicDevice,PLD).
Evolvinghardwaresolutionshasbeenrestrictedtotoyproblems,but
researchinthisareaispromising.Nevertheless,hardwareevolutiondoes
haveitsproblems.Forexample,evolvingcircuitstoproblemstendstoresult
insolutionsthataredifficultorimpossibletounderstand.Evolutionhasno
understanding of aesthetics or readability, and evolution commonly finds
shortcutsthatmaketheirunderstandingdifficult.Thecircuitsfoundthrough
evolutionmaynotalwaysbetolerantofnoiseortemperature,makingtheir
deploymentdifficult.Intheend,usingdesignsthatareevolvedcommonly
requiresgreatertestingtoensurethatallvariabilityhasbeenexploredin
theresultingdesign.
CHAPTERSUMMARY
Whileevolutionaryalgorithmsarenotnew,you’llfindcontinuedresearch
andevennewalgorithmsbeingdevelopedtoday(forexample,differential
evolution and swarm intelligence). Evolutionary algorithms borrow
conceptsfromDarwiniannaturalselectionasameanstoevolvesolutions
to problems, choosing from more fit individuals to propagate to future
generations. In this chapter we explored a number of evolutionary and
biologically-inspired algorithms. After an introduction to evolutionary
algorithms, we presented the genetic algorithm which is at the core of
most evolutionary algorithms. Next, we explored genetic programming,
an evolutionary means to generate code sequences. We then reviewed
one of the original evolutionary methods, evolutionary strategies. Next,
wereviewedthenewmethodofdifferentialevolution,andfinishedwitha
reviewofparticleswarmoptimization(abiologicallyplausibleoptimization
method).
EvolutionaryComputation 245
REFERENCES
[Banzhaf 1998] Banzhaf, W., Nordin, P., Keller, R.E., Francone, F.D.,
GeneticProgramming:AnIntroduction:OntheAutomaticEvolutionof
ComputerProgramsandItsApplications,MorganKaufmann,1998.
[EvoNews1999]“ProfessorHans-PaulSchwefeltalkstoEvoNews.”1999.
Availableonlineat:http://evonet.lri.fr/evoweb/news_events/news_features/
article.php?id=5
[Fogel 1966] Fogel, L.J., Owens, A.J., Walsh, M.J. Artificial Intelligence
throughSimulatedEvolution.Wiley,NewYork,1966.
[Levenick1991]Levenick,JamesR.“InsertingIntronsImprovesGenetic
AlgorithmSuccessRate:TakingaCuefromBiology.”Proceedingsonthe
FourthInternationalConferenceonGeneticAlgorithms,1991.
[Rechenberg 1965] Rechenberg, I. “Cybernetic solution path of an
experimentalproblem.”TechnicalReportLibrarytranslationNo.1122,
RoyalAircraftEstablishment,Farnborough,Hants.,UK,1965.
RESOURCES
Higuchi,Testuya;Liu,Yong;Yao,Xin(Eds.)EvolvableHardware,Genetic
andEvolutionaryComputationSpringer,2006.
Iba,Hitoshi;Iwata,Masaya;Higuchi,Testuya“MachineLearningApproach
toGate-LevelEvolvableHardware,”1996.
Koza, J.R. (1990), Genetic Programming: A Paradigm for Genetically
Breeding Populations of Computer Programs to Solve Problems,
Stanford University Computer Science Department technical report
STAN-CS-90-1314.
ParticleSwarmOptimizationwebsite
Availableonlineat:http://www.swarmintelligence.org/
[Price, et al 1997] Price, K., and Storn, R. “Differential Evolution,” Dr.
Dobb’sJournal,pp18-24,1997.
EXERCISES
1. Describethefirstuseofanevolutionaryalgorithmandhowitworked.
2. Describe three of the evolutionary algorithms, and compare and
contrastthem.
3. Describe the fundamental flow of the genetic algorithm (each of the
phases).
246 ArtificialIntelligence
4. Describethebuilding-blockhypothesisasdefinedforgeneticalgorithms.
5. Describe the differences between roulette wheel selection and elitist
selection.
6. Describethemutationandcrossoveroperatorsforthegeneticalgorithm.
Whateffectdotheyprovideinthelocalsearchspace?
7. DescribetheinversionoperatorintroducedbyHolland.
8. Whatisaterminationcriteria?
9. Defineprematureconvergenceandthenwaystocombatit.
10. Whatothersequence-planningproblemscouldbeappliedtothegenetic
algorithm?Describeone,andthendiscusshowyouwouldimplement
thisforevolutionwiththeGA.
11. Howwasthegeneticprogrammingalgorithminitiallyused(whatkinds
ofprogramswereevolved)?
12. Whatarethefundamentalproblemswithgeneticprogramming?
13. Theimplementationofgeneticprogramminginthisbookfocusedona
simple(zeroaddress)instructionset.Whatvalueexistsforsimplifying
theinstructionset,andwhatproblemscouldresultwithalargermore
complexset?
14. WhydoesGPimplementationperformthecandidateprogrammultiple
timestorecordafitnessvalue?
15. The GP implementation found numerous solutions to the candidate
problem(evenwiththesimpleinstructionset).Whatdoesthissayabout
theviabilityofthisalgorithm?
16. What is the basic difference between the evolutionary strategies
algorithmandthegeneticalgorithm?
17. Whatproblemwastheearlyevolutionarystrategiesalgorithmappliedto
withsuccess?
18. Describethebasicdifferencesbetweenaphenotypicalgorithmanda
genotypicalgorithm.
19. Whatisauniformlydistributedrandomnumber?
20. Describetheprocessofpunctuatedequilibrium.
21. Describethebasicprocessofthedifferentialevolutionalgorithm.What
aretheprocessesofmutation,crossover,andreplacement?
22. WhateffectdotheFandCRtunableparametershaveonthedifferential
evolutionalgorithm?
23. In your own words, describe the basic process of the particle swarm
optimizationalgorithm.
24. Whatisthedefinitionofaparticleinaparticleswarmalgorithm?
25. Foragivenparticleinaswarm,definethetwoinfluencesthatspecify
howitshouldmoveinthesolutionspace.
EvolutionaryComputation 247
26. The velocity update equations for the particle swarm optimization
algorithmincludedtworandomnumbers.Whataffectdotheserandom
numbershave?
27. Whataffectdothetwoaccelerationconstants(c1andc2)haveonthe
particleswarmvelocityequation?
28. What is evolvable hardware, and what are the primary issues that it
faces?
Chapter 8 NEURAL
NETWORKSI
N
eural networks are biologically motivated computing structures
that are conceptually modeled after the brain. The neural
network is made up of a highly connected network of individual
computing elements (mimicking neurons) that collectively can be used
tosolveinterestinganddifficultproblems.Oncetrained,neuralnetworks
cangeneralizetosolvedifferentproblemsthathavesimilarcharacteristics.
Thischapterwillintroducethebasicsofneuralnetworksandintroducea
numberofsupervisedlearningalgorithms.InChapter11,we’llcontinueour
explorationofneuralnetworksandreviewsomevariantsthatcanbeused
tosolvedifferenttypesofproblemsandinparticular,thoseofunsupervised
learningalgorithms.
SHORTHISTORYOFNEURALNETWORKS
Thestoryofneuralnetworksisinterestingbecause,likeAIitself,it’sone
of grand visions, eventual disappointment, and finally, silent adoption. In
1943, McCulloch and Pitts developed a neural network model based on
theirunderstandingofneurology,butthemodelsweretypicallylimitedto
formallogicsimulations(simulatingbinaryoperations).Intheearly1950s,
250 ArtificialIntelligence
researchersinneuralnetworksworkedtowardneuralnetworkmodelswith
thesupportofneuroscientists.
Butitwasn’tuntilthelate1950sthatpromisingmodelsbegantoemerge.
ThePerceptronmodel,developedbyRosenblatt,wasbuiltforthepurposeof
understandinghumanmemoryandlearning.Thebasicperceptronconsisted
ofaninputlayer(forthestimulus)andanoutputlayer(result)thatwasfully
interconnected.Eachconnectionwasassignedaweightthatwasappliedto
theinputstimulus(towhichitwasconnectedontheinputlayer).Byadjusting
theweightsoftheconnections,adesiredoutputcouldbeformedforagiven
input.Thisallowedtheperceptrontolearntorecognizeinputpatterns.
Inthe1960s,anotherlearningmodelemergedfromWidrowandHoff
ofStanfordUniversitycalledADALINE,orAdaptiveLinearElement.This
particularalgorithmusedleast-mean-squaresforadjustingtheweightsof
thenetwork,butthisparticularmodelcouldbeimplementedinthephysical
worldusinganalogelectroniccomponents.
In1969,thegrowingpopularityofneuralnetworkswasbroughttoahalt.
MarvinMinskyandSeymourPapertwroteabookentitled“Perceptrons”
in which limitations of single-layer perceptrons were discussed, but then
generalized to more powerful multi-layer models. The result was severe
reductions in neural network research funding, and a corresponding
reduction in the effort applied to the field. Luckily, several researchers
continuedtoinvestigateneuralnetworkmodels,andsuccessfullydefined
newmodelsandmethodsforlearning.In1974,PaulWerbosdevelopedthe
backpropagationalgorithm,whichpermittedsuccessfullearninginmulti-
layerneuralnetworks.
Since the 1970s, research and successful results in neural network
designhaveattractedscientistsbacktothefield.Manytheoreticalpapers
and practical treatments of neural networks have emerged, and neural
networkscannowbefoundoutsideofthelabandinrealapplicationssuch
aspatternrecognitionandclassification.Tosupportlargeneuralnetworks,
integratedcircuitshavebeendevelopedtospeedtheoperationandtraining
inproductionapplications.
BIOLOGICALMOTIVATION
In1943,McCullochandPittsusedtheirunderstandingofneurologytobuild
anewinformationprocessingstructure.Theprocessingelementofaneural
network is modeled after a neuron, which is viewed as the fundamental
processingelementinourownbrains.
NeuralNetworksI 251
FIGURE8.1: Theneuroncellwithinputs(dendrites)andoutputs(axons).
The neuron is a simple processing device that has inputs (known as
dendrites) and outputs (known as axons). The axon splits at its end into
thousandsofbranches,eachpotentiallyinfluencingotherneuronsatasynapse
(asmallgapthatseparatesaxonsanddendrites).Whenaneuronreceives
excitatory inputs that exceed its inhibitory inputs, a signal is transmitted
downitsaxontootherneurons.Thisprocesscontinuesinotherneurons,
creatingamassivelyparallelnetworkofneuronsinanexcitedorinhibited
state.Learningcanthenbedefinedasthealteringofthesynapticjunctions
thatchangethemannerinwhichoneneuronisinfluencedbyothers.
NOTE Whileneuralnetworksaremodeledafterourunderstandingofthewayin
whichourbrainworks,surprisinglylittleisknownabouthowourbrains
actuallyfunction.Throughvarioustypesofinspection,wecanseeour
braininoperation,butbecauseofthemassivenumberofneuronsand
interconnectionsbetweentheseneurons,howitworksremainsamystery
(thoughmanytheoriesexist).
FUNDAMENTALSOFNEURALNETWORKS
Let’sbeginwithanexplorationofneuralnetworksapplications,fundamental
concepts behind neural networks, and then begin an investigation into a
numberofnetworkmodelsandlearningalgorithms.
You can find neural networks in a large variety of applications, from
classification tasks (such as credit-risk assessment), data-processing tasks
(adaptive signal processing), and approximation of arbitrary functions
(time-seriesmodelingandprediction).Inthischapter,we’llexploreneural
networksforclassification(characterrecognitionanddataclassification).
252 ArtificialIntelligence
FIGURE8.2: Simpleneuronwithbiologicalequivalents.
Aneuralnetworkismadeupofoneormoreneurons,whichisthebasic
processingelement.Aneuronhasoneormoreinputs(dendrites),eachof
whichareindividualyweighted.Aneuronhasoneormoreoutputs(axons)
that are weighted when connecting to other neurons. The neuron itself
includesafunctionthatincorporatesitsinputs(viasummation)andthen
normalizesitsoutputviaatransferfunction(seeFigure8.2).
ForeachinputofFigure8.2,aweightisapplied.Theseadjustedinputs
arethensummedandatransferfunctionisappliedtodeterminetheoutput.
Eq8.1providestheequationforthissimpleneuron.
(Eq8.1)
SingleLayerPerceptrons(SLPs)
SingleLayerPerceptrons(orSLPs)canbeusedtoemulatelogicfunctions
suchasNOT,NOR,OR,AND,andNAND,butcannotbeusedtoemulate
theXORfunction(twolayersofneuronsarerequiredforthisfunction).We’ll
explorethisproblemshortly.
TIP
Minsky and Papert documented the XOR limitation of single layer
perceptrons, which ultimately resulted in vast reduction in neural
networkfunctionduringthe1970s.
Abiasisalsocommonlyappliedtoeachneuron,whichisaddedtothe
weightedsumoftheinputspriortopassingthroughthetransferfunction.A
weightisalsocommonlyappliedtothebias.Thebiasdeterminesthelevel
NeuralNetworksI 253
FIGURE8.3: ExamplesofSingleLayerPerceptrons(SLPs).
ofincomingactivations(valueofweightedinputs)thatarerequiredinorder
fortheneurontofire.Thebiasiscommonlysettoone,butaweightisalso
presentforthebiaswhichcanbetunedbythelearningalgorithm.
An SLP should not be confused with a single neuron. Consider the
networkinFigure8.3.ThisisalsoanSLP,becauseitconsistsofasingle
layer. For problems of higher dimensionality, we must use the MLPs, or
Multi-LayerPerceptrons.
Representing SLPs is quite simple. As the inputs and weights have a
one-to-onecorrespondence,it’seasytocomputetheoutput.Considerthe
simplecodeinListing8.1.
Listing8.1: SamplecodeillustratingSLPrepresentation.
#defineNUM_INPUTS 3
/*Note:+1heretoaccountforthebiasinput*/
doubleweights[NUM_INPUTS+1];
doubleinputs[NUM_INPUTS+1];
intstep_function(doubleinput)
{
if(input>0.0)return1;
elsereturn-1;
}
intcalc_output(void)
{
inti;
doublesum=0.0;
/*Setthebias(couldbedoneonceatinit)*/
254 ArtificialIntelligence
FIGURE8.4: ExampleofaMultipleLayerPerceptron(MLP).
inputs[NUM_INPUTS]=1.0;
/*Computetheoutput(Equation8.1)*/
for(i=0;i<NUM_INPUTS+1;i++){
sum+=(weights[i]*inputs[i]);
}
/*Passtheoutputthroughthestep(activation)function*/
returnstep_function(sum);
}
Multi-LayerPerceptrons(MLPs)
As Minsky and Papert revealed in their book “Perceptrons,” single layer
perceptrons have the disadvantage that they can only be used to classify
linearly separable data. But what was found a short time later is that by
stackingthesinglelayerperceptronsintomultiplelayerperceptrons(see
Figure 8.4), the ability to solve any classification problem theoretically
couldberealized.TheMLPcanmodelpracticallyanyfunctionofarbitrary
complexity, where the number of inputs and number of hidden layers
determinethefunctioncomplexity.
TheneuronsinanMLPhavethesamebasicattributesoftheSLP(bias,
etc.).Butwithmultiplelayers,theoutputfromonelayerbecomestheinput
tothenext.TheimplementationfortheMLPisabitmorecomplicated,but
remainsstraightforward(seeListing8.2).Noteheretheuseofanactivation
functionforboththehiddenandoutputnodes.Thesigmoidfunctioncan
beusedtosquashtheoutputoftheneuronto0.0to1.0.
NeuralNetworksI 255
Listing8.2: SamplecodeillustratingMLPrepresentation.
#defineNUM_INPUTS 4
#defineNUM_HIDDEN_NEURONS 4
#defineNUM_OUTPUT_NEURONS 3
typedefmlp_s{
/*InputstotheMLP(+1forbias)*/
doubleinputs[NUM_INPUTS+1];
/*WeightsfromHiddentoInputLayer(+1forbias)*/
doublew_h_i[NUM_HIDDEN_NEURONS+1][NUM_INPUTS+1];
/*Hiddenlayer*/
doublehidden[NUM_HIDDEN+1];
/*WeightsfromOutputtoHiddenLayer(+1forbias)*/
doublew_o_h[NUM_OUTPUT_NEURONS][NUM_HIDDEN_
NEURONS+1];
/*OutputsoftheMLP*/
doubleoutputs[NUM_OUTPUT_NEURONS];
}mlp_t;
voidfeed_forward(mlp_t*mlp)
{
inti,h,out;
/*Feedtheinputstothehiddenlayerthroughthehidden
*toinputweights.
*/
for(h=0;h<NUM_HIDDEN_NEURONS;h++){
mlp->hidden[h]=0.0;
for(i=0;i<NUM_INPUT_NEURONS+1;i++){
mlp->hidden[h]+=(mlp->inputs[i]*mlp->w_h_i[h][i]);
}
mlp->hidden[h]=sigmoid(mlp->hidden[h]);
}
/*Feedthehiddenlayeractivationstotheoutputlayer
*throughtheoutputtohiddenweights.
*/
for(out=0;out<NUM_OUTPUT_NEURONS;out++){
mlp->output[out]=0.0;
for(h=0;h<NUM_HIDDEN_NEURONS;h++){
mlp->outputs[out]+=(mlp->hidden[h]*mlp->w_o_h[out][h]);
}
mlp->outputs[out]=sigmoid(mlp->outputs[out]);
256 ArtificialIntelligence
}
return;
}
Listing8.2implementstheMLPneuralnetworkshowninFigure8.5.
ThisMLPhasfourinputcells,fourhiddencells,andthreeoutputcells.Bias
cellsareimplementedasinputandhiddencells,butthesehaveaconstant
valueof1.0(thoughtheweightscanbeadjustedtomodifytheireffect).
NotetheflowinListing8.2.First,wecalculatetheoutputofthehidden
cells(usingtheinputcellsandtheweightsbetweenthehiddenandinput
cells),andthencalculatethenextlayerup,whichinthiscaseistheoutput
cells.Thisprocessiscommonlyreferredtoasfeedingthedataforward,or
moresimply,feedfoward.
NOTE Wecanthinkofneuralnetworksasparallelcomputingsystems.Each
neuronisaprocessingelementthattakesoneormoreinputsandgenerates
anoutput.Theinputsandoutputscanbethoughtofasmessages.InMLP
architectures,theoutputscanbesenttomultipleotherprocesselements
for further processing. The parallel nature of neural networks comes
intoplaywithmultipleneuronsinalayer.Eachoftheseneuronscan
processtheirinputsatthesametime,makingneuralnetworkswithlarge
numbersofneuronsinalayerfasterinmulti-processorarchitectures.
FIGURE8.5: GraphicNetworkImplementedinListing8.2
NeuralNetworksI 257
SupervisedvsUnsupervisedLearningAlgorithms
Therearetwobasiccategoriesoflearningalgorithmsforneuralnetworks:
supervisedlearningandunsupervisedlearning.
Inthesupervisedlearningparadigm,theneuralnetworkistrainedwith
data that has known right and wrong answers. By calculating the output
oftheneuralnetworkandcomparingthistotheexceptedoutputforthe
giventestdata,wecanidentifytheerrorandadjusttheweightsaccordingly.
ExamplesofsupervisedlearningalgorithmsincludethePerceptronlearning
algorithm, Least-Mean-Squares learning, and Backpropagation (each of
whichwillbeexploredinthischapter).
Unsupervisedlearningalgorithmsarethoseinwhichthere’snoanswer
given in the test data. What these algorithms do instead is analyze the
datainordertounderstandtheirsimilaritiesanddifferences.Inthisway,
relationshipsinthedatacanbefoundthatmaynothavebeenapparent
before.Examplesofunsupervisedlearningalgorithmsincludethek-Means
clustering algorithm, Adaptive Resonance Theory (ART), and Kohonen
Self-Organizing Maps (each of which, and more, will be discussed in
Chapter9).
BinaryvsContinuousInputsandOutputs
Neuralnetworkscanoperatewithacombinationofinputtypes.Forexample,
wecanusebinaryinputs(-1,1)andbinaryoutputs.We’llexplorethisin
our first two examples of SLPs. For other uses, such as audio or video
applications,we’llneedcontinuousinputs(suchasreal-valueddata).It’salso
possibletousecombinations,suchascontinuousinputsandbinaryoutputs
(forclassificationproblems).
Nowthatwehaveabasicunderstandingofneuralnetworktopologies
andlearningalgorithms,let’sstartwithaninvestigationoftheperceptron
andunderstandhowitcanbetrainedforsimplepatternclassification.
THEPERCEPTRON
Aperceptronisasingleneuronneuralnetworkthatwasfirstintroducedby
FrankRosenblattinthelate1950s.Theperceptronisasimplemodelfor
neuralnetworksthatcanbeusedforacertainclassofsimpleproblemscalled
linearseparableproblems(otherwisecalledlineardiscriminants).Theseare
oftenusedtoclassifywhetherapatternbelongstooneoftwoclasses(see
Figure8.6).
258 ArtificialIntelligence
FIGURE8.6: Alineardiscriminantcanbeusedtoclassifyinputpatternsintotwoclasses.
FIGURE8.7: Viewingthelineardiscriminantforsimplebooleanfunctions.
So given a set of inputs describing an object (sometimes called a feature
vector),aperceptronhastheabilitytoclassifythedataintotwoclassesif
thedataislinearlyseparable.Giventhesetofpossibleinputs,thetaskthen
istoidentifytheweightsthatcorrectlyclassifythedata(linearlyseparate)
intotwoclasses.
TIP
AnothernamefortheperceptronistheThresholdLogicUnit,orTLU.
TheTLUisalineardiscriminatorthatgivenathreshold(whetherthe
featuresumisgreaterthanthethreshold,orlessthanthethreshold).
Theperceptroncanaccuratelyclassifythestandardbooleanfunctions,
suchasAND,OR,NAND,andNOR.AsshowninFigure8.7,theANDand
ORfunctionscanbelinearlyseparatedbyaline(inthecaseoftwoinputs,or
ahyperplaneforthreeinputs),buttheXORfunctionislinearlyinseparable.
Abiascomponentprovidestheoffsetofthelinefromtheorigin.Ifnobias
existed, the line (or hyperplane) would be restricted to pass through the
originandtheweightswouldonlycontroltheangleofthediscriminant.
NeuralNetworksI 259
FIGURE8.8: Simpleperceptronusedforbinaryfunctionclassification.
One result of Rosenblatt’s work on perceptrons was the Mark I
perceptronatCornellAeronauticalLaboratory.TheMarkIwasananalog
computerbasedontheperceptronthathada20by20retinaandlearned
torecognizeletters.
PerceptronLearningAlgorithm
Perceptron learning is a supervised learning algorithm and is a simple
procedurewheretheweightsareadjustedtoclassifythetrainingset.Each
sample from the training set is applied to the perceptron, and the error
(expected result minus the actual result) is used to adjust the weights. A
learningrateisalsoapplied(smallnumberbetween0and1)tominimize
thechangesthatareappliedateachstep.
We’llusetheperceptronshowninFigure8.8toillustratetheperceptron
learning algorithm. The weights of the perceptron will initially be set to
zero.Therearetwoinputswithtwocorrespondingweightsandalsoabias
withaweight.Thebiaswillbesettoone,buttheweightforthebiaswill
beadjustedtoalteritsaffect.Calculatingtheoutputoftheperceptroncan
thenbedefined(Eq8.2).
(Eq8.2)
Thestepfunctionsimplypushestheresultto1.0ifitexceedsathreshold;
otherwise,theresultis-1.0.
Givenatrainingsetforourperceptron,weapplyeachoftheelements
ofthetrainingsettotheperceptronandforeachsample,adjusttheweights
basedontheerror.Theerrorisdefinedastheexpectedresultminusthe
actualresult.EachisadjustedusingEq8.3(calledthePerceptronRule).
wi = wi + aTii (Eq8.3)
260 ArtificialIntelligence
In this equation, (alpha) is the learning rate (small number less than
one),Tisthetarget(orexpected)result,andiiistheinputvalueforthe
currentweightwi.Eq8.3verysimplyadjuststheweightbyincrementing
or decrementing the weight based on the direction of the error and the
correspondinginput(whichisidentifiedbymultiplyingtheexpectedresult
bythetraininginputforthisconnection).
NOTE Theperceptronlearningalgorithmisanexampleofasupervisedlearning
algorithm.Wepresentatrainingsettoourperceptronandpenalizeit
whenitarrivesatthewronganswer(anincorrectclassification).
Theapplicationofthelearningalgorithmcontinuesuntilnochangesare
madetotheweightsbecausealltestsareproperlyclassified.
PerceptronImplementation
Theimplementationoftheperceptronlearningalgorithmisverysimple(see
Listing8.3).Inthisimplementation,theperceptronistrainedwithatraining
set(abooleanfunction),andaftercalculatingtheerror(ofdesiredvsactual
result),theweightsareadjustedperEq8.3.Calculatingtheoutputofthe
perceptronisshowninthecomputefunction.Thisfunctionimplementsthe
Eq8.2.Thetrainingprocesscontinuesforamaximumnumberofiterations,
andwhencomplete,thetruthtableisemittedforthebooleanfunction.
Listing8.3: Perceptronlearningimplementation.
#defineMAX_TESTS 4
training_data_ttraining_set[MAX_TESTS]={
{-1.0,-1.0,-1.0},
{-1.0,1.0,1.0},
{1.0,-1.0,1.0},
{1.0,1.0,1.0}};
doublecompute(inttest)
{
doubleresult;
/*Equation8.2*/
result=((training_set[test].a*weights[0])+
(training_set[test].b*weights[1])+
(1.0*weights[2]));
/*Cliptheresult*/
if(result>0.0)result=1.0;
elseresult=-1.0;
NeuralNetworksI 261
returnresult;
}
intmain()
{
inti,test;
doubleoutput;
intchange;
/*Initializetheweightsfortheperceptron*/
for(i=0;i<NUM_WEIGHTS;i++)weights[i]=0.0;
/*Traintheperceptronwiththetrainingset*/
change=1;
while(change){
change=0;
for(test=0;test<MAX_TESTS;test++){
/*Testontheperceptron*/
output=compute(test);
/*PerceptronSupervisedLearningAlgorithm*/
if((int)training_set[test].expected!=(int)output){
/*UseEquation8.3*/
weights[0]+=ALPHA*
training_set[test].expected*
training_set[test].a;
weights[1]+=ALPHA*
training_set[test].expected*
training_set[test].b;
weights[2]+=ALPHA*training_set[test].expected;
change=1;
}
}
}
/*CheckthestatusofthePerceptron*/
for(i=0;i<MAX_TESTS;i++){
printf(“%gOR%g=%g\n”,
training_set[i].a,training_set[i].b,compute(i));
}
return0;
}
HE C
NT
D
O
TheperceptronlearningimplementationcanbefoundontheCD-ROM
at./software/ch8/perceptron.c.
262 ArtificialIntelligence
LEAST-MEAN-SQUARE(LMS)LEARNING
TheLMSalgorithmgoesbyanumberofnames,includingtheWidrow-Hoff
ruleandalsotheDeltarule(LMSwasoriginallyintroducedbyWidrowand
Hoff in 1959). LMS is a fast algorithm that minimizes the Mean-Square
Error(MSE).Recallfromperceptronlearningthatthealgorithmoperates
untilitcorrectlyclassifiestheentiretrainingset.Atthispoint,asolutionhas
beenfound.Butjusthowgoodisthesolution?Isthedatalinearlyseparated
byanoptimaldiscriminator(asshowninFigure8.6)?
Anotherapproachistotraintheperceptronusinganothertermination
criterion. So instead of training the perceptron until a solution is found,
another criterion is to continue training while the MSE is greater than a
certainvalue.ThisisthebasisfortheLMSalgorithm.
NOTE LMSlearningisbasedongradientdescent,wherethelocalminimum
of the error is achieved by adjusting the weights proportional to the
negativeofthegradient.Additionally,theweightsareadjustedwitha
learningrate(ρ)toallowittosettleintoasolutionandavoidoscillating
aroundtheMSE.
First, let’s explore the MSE. The MSE is simply the average of the
weightedsumoftheerrorforNtrainingsamples(seeEq8.4).
N
∑(R – C ) j
2
j=1
MSE = (Eq8.4)
N
InEq8.4,Ristheoutputoftheperceptrongiventhecurrentsetof
weightsmultipliedbythecurrenttestinputs(Cj).
LMSLearningAlgorithm
TotraintheperceptronusingLMS,weiteratethroughthetestset,takinga
setofinputs,computingtheoutput,andthenusingtheerrortoadjustthe
weights.Thisprocessisdoneeitherrandomlyforthetestset,orforeach
testofthesetinsuccession.
Thelearningrule(seeEq8.5)adjuststheweightbasedontheerror
(R-C, or expected minus actual output). Once the error is calculated,
theweightsareadjustedbyasmallamount(p)inthedirectionofthe
input (E). This has the effect of adjusting the weights to reduce the
outputerror.
NeuralNetworksI 263
(Eq8.5)
One of the major differences between LMS and perceptron learning
isthatLMScanoperatewithreal-values.Recallthatperceptronlearning
operatessolelywithbinaryinputs.
NOTE LMS is a standard tool for adaptive signal processing and can
solve problems such as echo cancellation, noise cancellation, and
equalization.
LMSImplementation
Liketheperceptronalgorithm,LMSisalsoverysimple(seeListing8.4).
Initially, the weights vector is initialized with small random weights. The
mainloopthenrandomlyselectsatest,calculatestheoutputoftheneuron,
andthencalculatestheerror(expectedresultminustheactualresult).Using
theerror,Eq8.5isappliedtoeachweightinthevector(notethatweight[2]
isthebias,anditsinputisalways1.0).Theloopthencontinues,wherewe
checktheMSEtoseeifithasreachedanacceptablevalue,andifso,weexit
andemitthecomputedtruthtablefortheneuron.
Recall that single neuron models can only classify training data into
twosets.Inthiscase,theANDfunctionisseparable,sotheneuroncanbe
successfullytrained.Nonseparabletrainingsetswillresultinthealgorithm
neverconvergingonasolution.
Listing8.4: LMSlearningalgorithm.
doubleweights[NUM_WEIGHTS];
#defineMAX_TESTS 4
consttraining_data_ttraining_set[MAX_TESTS]={
/*abexpected*/
{-1.0,-1.0,-1.0},
{-1.0,1.0,-1.0},
{1.0,-1.0,-1.0},
{1.0,1.0,1.0}
};
doublecompute_output(test)
{
doubleresult;
result=((training_set[test].a*weights[0])+
(training_set[test].b*weights[1])+
264 ArtificialIntelligence
(1.0*weights[2]));
return(result);
}
intclassify(inttest)
{
doubleresult;
result=compute_output(test);
if(result>0.0)return1;
elsereturn-1;
}
doubleMSE(void)
{
inttest;
doublesum=0.0;
/*Equation8.4*/
for(test=0;test<MAX_TESTS;test++){
sum+=sqr(training_set[test].expected-compute_output(test));
}
return(sum/(double)MAX_TESTS);
}
intmain()
{
inti,test;
doubleresult,error;
RANDINIT();
/*Pickrandomweightsfortheperceptron*/
for(i=0;i<NUM_WEIGHTS;i++){
weights[i]=RAND_WEIGHT;
}
/*Traintheperceptronwiththetrainingset*/
while(MSE()>0.26){
test=RANDMAX(MAX_TESTS);
/*Computetheoutput(weightedsum)*/
result=compute_output(test);
/*Calculatetheerror*/
error=training_set[test].expected-result;
/*DeltaRuleLearningAlgorithm(Equation8.5)*/
weights[0]+=(RHO*error*training_set[test].a);
weights[1]+=(RHO*error*training_set[test].b);
weights[2]+=(RHO*error);
NeuralNetworksI 265
printf(“mse=%g\n”,MSE());
}
for(i=0;i<MAX_TESTS;i++){
printf(“%gAND%g=%d\n”,
training_set[i].a,training_set[i].b,classify(i));
}
return0;
}
TIP
The early ADALINE model (single layer perceptron) used the LMS
algorithmforlearning.ADALINEoriginallystoodfor“ADAptiveLInear
NEuron,”butwhenneuralnetworktechnologybecameunpopular,itwas
changedto“ADAptiveLINearElement.”[Gallant1994]
HE C
NT
D
O
TheLeastMeanSquarealgorithmimplementationcanbefoundonthe
CD-ROMat./software/ch8/lms.c.
LEARNINGWITHBACKPROPAGATION
Let’snowinvestigatewhatcanbeconsideredthemostpopularoftheMLP
learningalgorithms,backpropagation.Thebackpropagationalgorithmcan
besuccinctlydefinedasfollows.Foratestset,propagateonetestthrough
theMLPinordertocalculatetheoutput(oroutputs).Computetheerror,
FIGURE8.9: Simplenetworkillustratingforwardpropagationandbackwarderrorpropagation.
266 ArtificialIntelligence
which will be the difference of the expected value and the actual value.
Finally, backpropagate this error through the network by adjusting all of
theweights;startingfromtheweightstotheoutputlayerandendingatthe
weightstotheinputlayer(seeFigure8.9).
LikeLMSlearning,backpropagationadjuststheweightsinanamount
proportionaltotheerrorforthegivenunit(hiddenoroutput)multiplied
by the weight and its input. The training process continues until some
termination criterion, such as a predefined mean-squared error, or a
maximumnumberofiterations.
Backpropagationisoneofthemostpopularlearningalgorithms,andis
usedtotrainneuralnetworksforavarietyofapplications.We’llfirstlook
atthedetailsofthealgorithm,andthenexploreaneuralnetworkthatcan
recognizenumbersfromastandard5by7characterbitmapformat.
FIGURE8.10: Asimplifiedflowofbackpropagation.
NeuralNetworksI 267
BackpropagationAlgorithm
Thebackpropagationalgorithmisatypicalsupervisedlearningalgorithm,
whereinputsareprovidedandpropagatedforwardtogenerateoneormore
outputs.Giventheoutput,theerroriscalculatedusingtheexpectedoutput.
Theerroristhenusedtoadjusttheweights(seeFigure8.10).Propagating
theinputsforwardwaspreviouslyexploredinListing8.2.
It’s important to note that there are two types of error functions for
backpropagation.Thefirsterrorfunction(Eq8.6)isusedforoutputcells,
andthesecondisusedonlyforhiddencells(Eq8.7).
(Eq8.6)
(Eq8.7)
Notethatinbothequations,uistheoutputofthegivencell,otherwise
known as its activation. Y is the expected or correct result. Finally, w
representsalloftheweights(from1ton)connectingthehiddencelltoall
inputscells(inafullyconnectednetwork).
Theactivation,ortransfer,function(g)tobeusedwillbethestandard
sigmoidsquashingfunction(seeFigure8.11).Whilegrepresentsthesigmoid,
g’representsthefirstderivativeofthesigmoid,asshowninEq8.8.
(Eq8.8)
FIGURE8.11: Thesigmoidsquashingfunction.
268 ArtificialIntelligence
Atthispoint,givenourtestinputandexpectedresult,wehavetheerror
calculatedforeachoutputandhiddennode.Thenextstepistousethiserror
toadjustthecorrespondingweightsforthenode.We’lluseEq8.9forthis
purpose,whichutilizestheerrorpreviouslycalculatedforthenode(whether
hiddenoroutput).
Forthegivenerror(E)andactivation(orcelloutput,ui),wemultiply
by a learning rate (ρ) and add this to the current weight. The result is a
minimizationoftheerroratthiscell,whilemovingtheoutputcellactivation
closertotheexpectedoutput.
BackpropagationImplementation
Neuralnetworksareagreattoolforclassifyingasetofinputstoasetof
outputs. Let’s look at a very visual example of neural networks from the
domain of pattern recognition. Consider the bitmap character images in
Figure 8.12. We’ll train a neural network to take the cells of this image
as the input (35 independent cells) and activate one of ten output cells
representingtherecognizedpattern.Whileanyoftheoutputcellscouldbe
activated,we’lltakethelargestactivationasthecelltouseinastylecalled
winner-takes-all.
Since we could very simply implement a comparison classifier to
recognizethepattern(bylookingforthespecificpatternattheinput),we’ll
introducenoisetothepatternwhenwetesttheneuralnetwork.Thiswill
maketheclassificationproblemmoredifficult,andtestthegeneralization
featuresoftheneuralnetwork.
TIP
Generalizationisoneofthegreatestcharacteristicsofneuralnetworks.
Thismeansthataftertraininganeuralnetworkwithasetoftrainingdata,
itcangeneralizeitstrainingtocorrectlyclassifydatathatithasnotseen
before.Generalizationcanbetrainedoutofaneuralnetworkbytraining
FIGURE8.12: Samplebitmapsfortrainingthenumberrecognitionneuralnetwork.
NeuralNetworksI 269
thenetworkfortoolongwithadataset.Whenthishappens,thenetwork
overfitsthedataandisnotabletogeneralizefornewunseendata.
Theneuralnetworkthatwe’lluseiscalledawinner-takes-allnetworkin
whichwehaveanumberofoutputnodes,andwe’llselecttheonethathas
thelargestactivation.Thelargestactivationindicatesthenumberthatwas
recognized.Figure8.13showstheneuralnetworkthatwillbeusedforthe
patternrecognitionproblem.Theinputlayerconsistsof35inputcells(for
eachpixelintheimageinput),with10cellsinthehiddenlayer.Theoutput
layerconsistsof10cells,oneforeachpotentialclassification.Thenetwork
isfullyinterconnected,with350connectionsbetweentheinputandhidden
layer,andanother350 connections between the hidden layer and output
layer(foratotalof700weights).
For our implementation, let’s first discuss the neural network
representation(seeListing8.5).We’llmaintainthreevectorscontainingthe
inputvalues,currentactivationsofthehiddenlayerandcurrentactivations
oftheoutputlayer.Notethatwe’llalsomaintainanextracellattheinput
andhiddenlayerswhichwillrepresentthebias(settoaconstant1.0).The
weightswillberepresentedbytwo,two-dimensionalarraysrepresentingthe
hiddenlayerweightsandtheoutputlayerweights.
FIGURE8.13: Neuralnetworktopologyforthepatternrecognitionproblem.
270 ArtificialIntelligence
HE C
NT
D
O
Thefullsourceimplementationforbackpropagationlearningcanbefound
ontheCD-ROMat./software/ch8/backprop.c.Thefollowingdiscussion
provides only the relevant functions to illustrate backpropagation
learning.
Listing8.5: Neuralnetworkrepresentation(inputs,activations,andweights).
#defineINPUT_NEURONS 35
#defineHIDDEN_NEURONS 10
#defineOUTPUT_NEURONS 10
doubleinputs[INPUT_NEURONS+1];
doublehidden[HIDDEN_NEURONS+1];
doubleoutputs[OUTPUT_NEURONS];
doublew_h_i[HIDDEN_NEURONS][INPUT_NEURONS+1];
doublew_o_h[OUTPUT_NEURONS][HIDDEN_NEURONS+1];
Computingtheactivationsoftheoutputcellsisverystraightforward(see
Listing8.6).Notetheuseofthesigmoidfunctiontosquashtheactivations
intotherange0to1.
Listing8.6: Calculatingtheoutputactivationswiththefeed_forwardfunction.
voidfeed_forward(void)
{
inti,j;
/*Calculateoutputsofthehiddenlayer*/
for(i=0;i<HIDDEN_NEURONS;i++){
hidden[i]=0.0;
for(j=0;j<INPUT_NEURONS+1;j++){
hidden[i]+=(w_h_i[i][j]*inputs[j]);
}
hidden[i]=sigmoid(hidden[i]);
}
/*Calculateoutputsfortheoutputlayer*/
for(i=0;i<OUTPUT_NEURONS;i++){
outputs[i]=0.0;
for(j=0;j<HIDDEN_NEURONS+1;j++){
outputs[i]+=(w_o_h[i][j]*hidden[j]);
}
outputs[i]=sigmoid(outputs[i]);
NeuralNetworksI 271
}
}
Thebackpropagationalgorithm(showninListing8.7)isjustslightlymore
complicatedthanfeedingforward.UsingEq8.6andEq8.7,wecalculatethe
errorfortheoutputandhiddennodes.Finally,theweightsareupdatedgiven
thehiddenandoutputerrors,inputvalue,andasmalllearningrate.
Listing8.7: Updatingtheweightsgiventhebackpropagationalgorithm.
voidbackpropagate_error(inttest)
{
intout,hid,inp;
doubleerr_out[OUTPUT_NEURONS];
doubleerr_hid[HIDDEN_NEURONS];
/*Computetheerrorfortheoutputnodes(Equation8.6)*/
for(out=0;out<OUTPUT_NEURONS;out++){
err_out[out]=((double)tests[test].output[out]-outputs[out])*
sigmoid_d(outputs[out]);
}
/*Computetheerrorforthehiddennodes(Equation8.7)*/
for(hid=0;hid<HIDDEN_NEURONS;hid++){
err_hid[hid]=0.0;
/*Includeerrorcontributionforalloutputnodes*/
for(out=0;out<OUTPUT_NEURONS;out++){
err_hid[hid]+=err_out[out]*w_o_h[out][hid];
}
err_hid[hid]*=sigmoid_d(hidden[hid]);
}
/*Adjusttheweightsfromthehiddentooutputlayer(Equation8.9)*/
for(out=0;out<OUTPUT_NEURONS;out++){
for(hid=0;hid<HIDDEN_NEURONS;hid++){
w_o_h[out][hid]+=RHO*err_out[out]*hidden[hid];
}
}
/*Adjusttheweightsfromtheinputtohiddenlayer(Equation8.9)*/
for(hid=0;hid<HIDDEN_NEURONS;hid++){
for(inp=0;inp<INPUT_NEURONS+1;inp++){
w_h_i[hid][inp]+=RHO*err_hid[hid]*inputs[inp];
}
272 ArtificialIntelligence
}
return;
}
Themainfunction(showninListing8.8)performstheneuralnetwork
trainingaswellasthetestofthetrainednetwork.Thefirststepisinitializing
thenetworkbysettingeachweighttoasmallrandomvalue(viaacalltoinit_
network).Wethenenterthetrainingloopwhereatestisselectedatrandom,
theinputsloadedfromthetestintotheinputsvector(set_network_inputs),
andtheoutputactivationcalculated(backpropagate_error).Finally,theMSE
iscalculatedandtestedagainstourterminationcriteria.
Listing8.8: Thetrainingandtestloop(mainfunction).
intmain(void)
{
doublemse,noise_prob;
inttest,i,j;
RANDINIT();
init_network();
/*TrainingLoop*/
do{
/*Pickatestatrandom*/
test=RANDMAX(MAX_TESTS);
/*Grabinputimage(withnonoise)*/
set_network_inputs(test,0.0);
/*Feedthisdatasetforward*/
feed_forward();
/*Backpropagatetheerror*/
backpropagate_error(test);
/*CalculatethecurrentMSE*/
mse=calculate_mse(test);
}while(mse>0.001);
/*Now,let’stestthenetworkwithincreasingamountsofnoise*/
test=RANDMAX(MAX_TESTS);
/*Startwith5%noiseprobability,endwith25%(perpixel)*/
noise_prob=0.05;
for(i=0;i<5;i++){
set_network_inputs(test,noise_prob);
feed_forward();
NeuralNetworksI 273
for(j=0;j<INPUT_NEURONS;j++){
if((j%5)==0)printf(“\n”);
printf(“%d“,(int)inputs[j]);
}
printf(“\nclassifiedas%d\n\n”,classifier());
noise_prob+=0.05;
}
return0;
}
The final step in the main function (Listing 8.8) is the neural network
test.Thistestverifiesthegeneralizationcapabilitiesoftheneuralnetworkby
inducingnoiseintotheinputimage.Westartbyselectingoneofthetests(a
numbertorecognize)andthenaddincreasingamountsofnoisetotheimage.
Oncethenoiseisadded(aspartofthecalltoset_network_inputs),theoutput
activationsarecomputed(feed_forward)andthentheclassificationemitted
(throughacalltoclassifier).Thisclassifierfunctioninspectseachoftheoutput
activations,andchoosesthelargestoneinthewinner-takes-allfashion.
Figure8.14graphicallyillustratesthegeneralizationcapabilitiesofthe
networktrainedusingerrorbackpropagation.Inbothcases,oncetheerror
ratereaches20%,theimageisnolongerrecognizable.
What’sshowninmainisacommonpatternforneuralnetworktraining
anduse.Onceaneuralnetworkhasbeentrained,theweightscanbesaved
offandusedinthegivenapplication.
TIP
Thisapproachforpatternrecognitionisconsideredbrittleifyouconsider
the rotation of the image by a small degree. A production character
recognizer would instead use features of the image space, rather than
requiringthatpixelsberoughlyinthecorrectplace.
FIGURE8.14: Thepatternrecognitioncapabilitiesofthemulti-Layerneuralnetwork.
274 ArtificialIntelligence
TuningBackpropagation
Backpropagation is a great technique to train a neural network for a
classificationtask.Butthereareanumberofthingsthatcanbedonetotune
thealgorithmeitherforbettergeneralization,orforfastertraining.We’ll
exploresomeofthesetechniqueshere.
TrainingVariants
Oneoftheproblemsthatcanbecreatedintrainingiswhat’scalled“over-
learning,”or“over-fitting.”Thedesirefortheneuralnetworkistoprovidea
correctmappingofthetestdata,butmaintaintheabilitytogeneralizeforyet
tobeseendata.Thisisdifficulttoquantify,buttheresultsofover-learning
canbeveryapparentintheteststage.
Thereareanumberofthingsthatcanbedonetoavoidover-learning.Oneof
thesimplestiscalledearly-stopping.Asthenamesuggests,wetrainforalimited
amountoftime.Practically,wecantrainuntilthetrainingsetiscorrectlyclassified,
andignoretheMSE.Inthisway,we’venotoptimizedtheneuralnetworktothe
dataset,anditsgeneralizationcapabilitiesshouldremainintact.
Anothermethodtoavoidover-learningistoincorporatenoiseintothe
training. Rather than simply provide the test set verbatim to the neural
network,smallamountsofnoisecanbeinducedtomaintainsomeamount
offlexibilityinthenetwork’sgeneralizationcapability.Theadditionofnoise
keepsthenetworkfrombeingfocusedsolelyonthetestdata.
Thenetworkcouldalsobetrainedwithasubsetoftheavailabletraining
data.Inthisway,thenetworkisinitiallytrainedwiththesubset,andthen
tested with the remainder of the test data to ensure that it generalizes
properly.Theavailabilityoflotsoftrainingdatacanalsohelpingeneralization
andavoidoverfitting.
Finally, one could maintain generalization capabilities by minimizing the
changesmadetothenetworkastimeprogresses.Minimizingthechangescanbe
achievedbyreducingthelearningrate.Byreducingthechangesovertime,we
reducethepossibilitythatthenetworkwillbecomefocusedonthetrainingset.
Ultimately,there’snosilverbullet.Whatcanbedoneistoexperiment
withnetworktopologies,numberofhiddenlayers,numberofhiddennodes
perlayer,learningrate,etc.,tofindthebestcombinationthatworksforthe
problemathand.
WeightAdjustmentVariants
With backpropagation, there are a number of other weight adjustment
strategies that can be applied that can speed learning or avoid becoming
NeuralNetworksI 275
trapped in local minima. The first involves a momentum term where a
portionofthelastweightchangeisappliedtothecurrentweightadjustment
round.Eq8.1showsanupdatedweightadjustmentvariantbasedonEq8.9.
Thedifferenceisthataportionofthelastweightchange(identifiedasΔwi,j)
isaccumulatedusingasmallmomentummultiplier(m),asshowninEq8.10.
ThelastweightchangeisstoredusingEq8.11.
Inthisequation,γisalargenumber(suchas0.95)sothat5%ofthe
weightisdecayedforeachiteration.Inthisway,largeweightsarepenalized
bylargeramountsthansmallweights.Thisisasimilarmethodtoweight
elimination(orpruning),whosegoalistoremoveconnectionsbetweenlayers
thatdonotcontributetothenetwork’sclassificationcapabilities.
PROBABILISTICNEURALNETWORKS(PNN)
Ausefulneuralnetworkarchitecturewithfundamentaldifferencesfrom
backpropagationiscalledaProbabilisticNeuralNetwork(orPNN).This
architectureissimilartobackpropagationinthatitisfeedforwardinnature,
butdiffersverymuchinthewaythatlearningoccurs.Bothbackpropagation
andPNNaresupervisedlearningalgorithms,butPNNincludesnoweights
initshiddenlayer.Insteadeachhiddennoderepresentsanexamplevector,
withtheexampleactingastheweightstothathiddennode.Thesearenot
adjustedatall.Figure8.15illustratesasamplePNN.
As shown in Figure 8.15, the PNN consists of an input layer, which
represents the input pattern (or feature vector). The input layer is fully
interconnectedwiththehiddenlayer,whichconsistsoftheexamplevectors
276 ArtificialIntelligence
FIGURE8.15: SamplePNNillustratingthethreelayersofthenetworkmodel.
(the training set for the PNN). The actual example vector serves as the
weights as applied to the input layer. Finally, an output layer represents
eachofthepossibleclassesforwhichtheinputdatacanbeclassified.Note
herethatthehiddenlayerisnotfullyinterconnectedtotheoutputlayer.
Theexamplenodesforagivenclassconnectonlytothatclass’soutputnode
andnoneother.IntheexamplePNN,ournetworkhasfourclassesforwhich
therearetwotrainingexampleseach.
One other important element of the PNN is the output layer and the
determinationoftheclassforwhichtheinputlayerfits.Thisisdonethrough
a winner-takes-all approach. The output class node with the largest activation
representsthewinningclass.Whiletheclassnodesareconnectedonlytothe
example hidden nodes for their class, the input feature vector connects to all
examples,andthereforeinfluencestheiractivations.It’sthereforethesumofthe
examplevectoractivationsthatdeterminestheclassoftheinputfeaturevector.
PNNAlgorithm
Calculatingtheclass-nodeactivationsisthereforeasimpleprocess.Foreach
classnode,theexamplevectoractivationsaresummed,whicharethesumof
theproductsoftheexamplevectorandtheinputvector.Thehiddennode
activation,showninEq8.13,issimplytheproductofthetwovectors(Eis
theexamplevector,andFistheinputfeaturevector).
(Eq.8.13)
TheclassoutputactivationsarethendefinedinEq8.14.,
NeuralNetworksI 277
(Eq.8.14)
whereNisthetotalnumberofexamplevectorsforthisclass,hiisthe
hidden-nodeactivation,and�isasmoothingfactor.Thesmoothingfactoris
chosenthroughexperimentation.Ifthesmoothingfactoristoolarge,details
canbelost,butifthesmoothingfactoristoosmall,theclassifiermaynot
generalizewell.
What’s interesting about the PNN is that there’s no real training that
occurssincetheexamplevectorsserveastheweightstothehiddenlayerof
thenetwork.Givenanunknowninputvector,thehiddennodeactivations
arecomputedandthensummedattheoutput(classnodes)layer.Theclass
node with the largest activation determines the class to which the input
featurevectorbelongs.
Asnotrainingisrequired,classifyinganinputvectorisfast,depending
onthenumberofclassesandexamplevectorsthatarepresent.It’salsovery
easytoaddnewexamplestothenetwork.Wesimplyaddthenewhidden
node,anditsoutputisusedbytheparticularclassnode.Thiscanbedone
dynamicallyasnewclassifiedexamplesarefound.ThePNNalsogeneralizes
verywell,eveninthecontextofnoisydata.
Let’snowlookattheimplementationofaPNNwithagraphicalexample
thatwillillustrateitsclassificationcapabilities.
TIP
Probabilistic neural networks are a form of normalized Radial-Basis
Form (RBF) neural networks, where each hidden node is a “kernel”
implementingaprobabilitydensityfunction
PNNImplementation
BuildingaPNNisquitesimpleoncethestructuresareinplacetorepresent
thetrainingdataset(whichfundamentallydefinestheweightsbetweenthe
hiddenlayerandtheinputlayer).Let’sbeginwithashortdiscussionofthe
dataset.
Forthisexample,we’llimplementatwo-dimensionalclassifiersothat
we can visualize it graphically. The dataset represents points on a two-
dimensionalmapthathavebeenpre-classifiedintothreeclasses.Eachpoint
isafeaturevectorthatisrepresentedbytheexample_ttype.
#defineDIMENSIONALITY 2
typedefstructureexample_s{
278 ArtificialIntelligence
intfeature[DIMENSIONALITY];
}example_t;
Adatasetisthendefinedasacollectionofexamples.We’llhaveadataset
perclass,whichiseasilydefinedas:
#defineEXAMPLES 10
typedefstructdata_set_s{
example_texample[EXAMPLES];
}data_set_t;
#defineCLASSES 3
data_set_tdataset[CLASSES]={
/*Class0*/
{{{{13,1}},
{{11,2}},
...
{{13,10}}}},
/*Class1*/
{{{{36,4}},
{{34,5}},
...
{{37,11}}}},
/*Class2*/
{{{{24,27}}.
{{22,29}},
...
{{22,38}}}}
};
Wenowhaveacollectionofdatapointsthataresplitintoexamplesfor
ourthreeclasses.Let’snowlookathowtheseareusedbythePNNalgorithm
toclassifyanunseendatapoint.
Listing8.9providesthepnn_classifierfunction.Thesolepurpose
ofthisfunctionistotakeafeaturevector(example),andidentifytheclass
towhichitbelongs.Thefunctionbeginswithanouterloopthatiterates
through each of the classes in the dataset. For each class, each of the
examplesisiterated,calculatingthesumofthefeaturevectorandexample
vectorproducts.
Finally, the output array (class vector) is passed to a function called
winner_takes_all,whichreturnstheclasswiththelargestactivation.
ThisistheclasswhichthePNNclassifiedastheexamplefeaturevector.
NeuralNetworksI 279
Listing8.9: ImplementationofthesimplePNNclassifierfunction.
intpnn_classifier(void)
{
intc,e,d;
doubleproduct;
doubleoutput[CLASSES];
/*Calculatetheclasssumoftheexamplemultipliedbyeachof
*thefeaturevectorsoftheclass.
*/
for(c=0;c<CLASSES;c++){
output[c]=0.0;
for(e=0;e<EXAMPLES;e++){
product=0.0;
/*Equation8.13*/
for(d=0;d<DIMENSIONALITY;d++){
product+=
(SCALE(example[d])*SCALE(dataset[c].example[e].feature[d]));
}
/*Equation8.14--part1*/
output[c]+=exp((product-1.0)/sqr(SIGMA));
}
/*Equation8.14--part2*/
output[c]=output[c]/(double)EXAMPLES;
}
returnwinner_takes_all(output);
}
HE C
NT
D
O
Let’snowusethePNNclassifiertoclassifytheentiretwo-dimensional
space in which our example feature vectors exist. In this example, the
two-dimensionalspaceisiteratedtoidentifytheclasstowhicheachpoint
belongs.Thisclassificationisemitted,withspaceinplacefortheexample
featurevectors(seeListing8.10).
280 ArtificialIntelligence
Listing8.10: O
utputofthePNNclassifieroveratwo-dimensionalspaceof
threeclasses.
$./pnn
00000000000000000000000001111111111111111111111111
0000000000000000000000001111111111111111111111111
0000000000000000000000001111111111111111111111111
0000000000000000000000011111111111111111111111111
000000000000000000000001111111111111111111111111
000000000000000000000001111111111111111111111111
0000000000000000000001111111111111111111111111
00000000000000000000000111111111111111111111111111
00000000000000000000001111111111111111111111111
000000000000000000000011111111111111111111111111
0000000000000000000001111111111111111111111111111
00000000000000000000001111111111111111111111111
00000000000000000000022111111111111111111111111111
00000000000000000002222221111111111111111111111111
00000000000000000022222222211111111111111111111111
00000000000000002222222222221111111111111111111111
00000000000000022222222222222211111111111111111111
00000000000002222222222222222222111111111111111111
00000000000222222222222222222222221111111111111111
00000000022222222222222222222222222211111111111111
00000000222222222222222222222222222222111111111111
00000022222222222222222222222222222222221111111111
00002222222222222222222222222222222222222111111111
00222222222222222222222222222222222222222221111111
02222222222222222222222222222222222222222222211111
22222222222222222222222222222222222222222222222111
22222222222222222222222222222222222222222222222221
2222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
$
NeuralNetworksI 281
TheexampleshowninListing8.10illustratestheclusteringcapabilities
ofthePNNasappliedtothissimpleproblem.PNNshavebeenappliedto
complex problems such as speaker-independent speech recognition and
manyotherapplications.
OTHERNEURALNETWORKARCHITECTURES
In addition to single and multi-layer perceptrons, there are variations of
neuralnetworkarchitecturesthatcansupportdifferenttypesofproblems.
Let’slookattwoneuralnetworkarchitecturesthatsupporttime-series(or
signal)processingandfeedbacksystems(orthosewithmemory).
Time-SeriesProcessingArchitecture
ConsiderthetimeseriesshowninFigure8.16.Thissignalcouldbeaportion
ofspeech,afetalheartbeat,orthestockpriceofacompany.Thesignalcan
besampledatagivenfrequencyandusedasinputtoaneuralnetworkto
predictafuturevalue.Theneuralnetworkcouldalsobeusedtofilterthe
inputsignalfornoisecancellation.
Inadditiontosamplingpointsfromthetimeseries,theneuralnetwork
canoperateoverawindowofthetimeseriesthroughtheuseofasliding
window.Usingaslidingwindowallowstheneuralnetworktooperateon
multiple data points, making it a better predictor of future time-series
behavior.
FIGURE8.16: Exampleofatimeserieswithaslidingwindow.
282 ArtificialIntelligence
FIGURE8.17: Exampleofamulti-mayerneuralnetworkfortime-seriesprediction.
Figure8.17illustratesafour-layer,multi-layerperceptronfortime-series
processing.Asinputsamplesareacquired,thecontentsoftheinputnodesare
shiftedright(losingtheright-mostvalue)andtheninsertingthenewvaluein
theleft-mostnode.Thenetworkisthenfed-forward,resultinginanoutput
activation(inthismodel,predictingthenextvalueofthetimeseries).
This particular network could be trained with backpropagation on a
sampletimeseriesusingtheknownvalueandpredictedvaluetodetermine
theerrorthatistobebackpropagatedforweightadjustment.
TIP
Neuralnetworks,whichoperateoverawindowoftime-seriesdata,are
often called tapped-delay-line networks, as there is an implicit delay
betweeninputsamplesastheyshiftdownthelineofinputs.
NeuralNetworksI 283
RecurrentNeuralNetwork
Arecurrentneuralnetworkisonethatincludesfeedbackconnections,where
higher layer nodes in the network feed lower layer nodes. Consider the
recurrentneuralnetworkasshowninFigure8.18.Inthisnetworkmodel,
theoutputfeedsbackasinputtothenextiteration.
Recurrentneuralnetworksareverybiologicallyplausible,asourown
brainsarerecurrent.Neuralnetworkswithfeedbackareabletoimplement
almostanyarbitrarysequentialbehaviorandarethereforeusefulincontrol
systems,robotics,andmanyotherapplications.
Recurrentneuralnetworksarealsoaninterestingtakeonlong-and-short
termmemory.Thefeedbackloopprovidesthemeansforshort-termmemory
(recallingthelastoutput,intheexample,andusingitasaninput).Atapped-
delay-linecouldbeusedtokeepthepreviousstatelonger(asinthetapped-
delay-linemodel).Thetapped-delay-linecouldalsobemodifiedtokeepthelast,
10thprevious,and100thpreviousoutputtogivethenetworkalongerhistory.
TIPSFORBUILDINGNEURALNETWORKS
Neural networks can benefit from careful design of inputs, outputs, and
internalstructure.Let’sexploreafewtechniquesthatcanhelpinbuilding
efficientneuralnetworkarchitectures.
DefiningtheInputs
Howtheinputsaredefinedtoaneuralnetworkcanhelpspeedlearning,or
helptominimizetheoverallsizeoftheneuralnetwork.Considerasetof
inputsthatdescribetheweatherasoneoffourstates{sunny,cloudy,raining,
snowing}.Asimplerepresentationistodefineaninputnodeforeachofthe
inputs,asshowninFigure8.20.
FIGURE8.20: Usingasingleinputpercategory.
284 ArtificialIntelligence
FIGURE8.21: Usingabinaryencodingtoreducethenumberofinputs.
An alternative for a distinct set of inputs is to encode as a binary set
of inputs (increasing the input information density, while reducing the
numberofinputnodesthatarenecessary).Figure8.21illustratesthisbinary
encoding.
Note that this works only for mutually exclusive inputs. Using real-
valuedinputs,wecouldsetsunnyandcloudyeachto0.5(andrainingand
snowingto0.0)torepresentpartlycloudy.Usingreal-valuedinputsinthis
wayprovidesameansoffuzzinesstotheinput,andintroducestheconcept
ofprobabilityorinputconfidence.
DefiningtheOutputs
Thewinner-takes-allmodelisgreatfordeterminingaclassification(mapping
the inputs to a particular output node representing a class). Real-valued
outputsareidealforreal-valuedoutputsrepresentingsignalvaluesortime-
seriespredictions.
Notethatoutputvaluesarenotrestrictedtoactivationsintherange0to
1.0(ascanbeforcedthroughthesigmoidactivationfunction).Activations
cantakeanyreal-value,includingnegativevalues,iftheparticularoutput
activationfunctionsupportsthis.
It’salsousefultousetargetoutputvaluesoftherange0.1to0.9(fora
validclassification).Thisavoidssaturatingtheweightsinthenetworktoforce
theactivationtowardtheextremes.
ChoiceofActivationFunctions
There are a number of activation functions that can be used, but it’s
importanttonotethatthepowerofmulti-layerneuralnetworksisthatthey
arenon-linear.Therefore,usingalinearactivationfunction(forexample)
resultsinalinearactivation.
Forsingle-layernetworks,linearactivationsandalsostepfunctionscan
beused.Formulti-layernetworks,thesigmoid,Gaussian,andhyperbolic
NeuralNetworksI 285
tangent(tanh)canbeused.Theonlyrequirementforbackpropagationis
thattheactivationfunctionmustbedifferentiable.
NumberofHiddenLayers
Asinglehiddenlayercanmodelanycontinuousfunction,andiseasilytrained
withbackpropagation.Withtwohiddenlayers,anyarbitraryfunctioncan
becomputed,withacomplexitydefinedbythenumberofnodes.Forthis
reason, neural networks with two hidden layers are universal computing
devices. But neural networks with multiple hidden layers can be more
difficulttotrain,andtherefore,modelswithasinglehiddenlayershouldbe
usedifthetargetproblemsupportsthis.
CHAPTERSUMMARY
Neuralnetworksareanidealsolutiontoproblemsthatcan’tbeformulated
easilytoanalgorithm,andforwhichlotsofexamplesexistfromwhichthe
networkcanbetrained.Additionally,neuralnetworksarehighlyparalleland
distributedstructureswiththedesirablepropertythattheygeneralizewell
tounseendata.Thischapterpresentedanintroductiontoneuralnetworks
using supervised learning algorithms, including perceptron learning, the
delta-rule,backpropagation,andprobabilisticneuralnetworks.
REFERENCES
[Gallant1994]Gallant,StephenI.“NeuralNetworkLearningandExpert
Systems.”MITPress,Cambridge,Massachusetts.,1994.
[Krogh1995]Krogh,AndersandHertz,JohnA.“ASimpleWeightDecay
Can Improve Generalization.” In Advances in Neural Information
ProcessingSystems4,MorganKauffmannPublishers,SanMateoCA,
1995,p.950-957.
EXERCISES
1. Describe the difference between supervised and unsupervised
learning.
2. Whatarethemajorcomponentsofaneuralnetwork?
3. Describehowlearningoccursinneuralnetworks.
4. Whatistheprimarylimitationofsingle-layerperceptrons?
286 ArtificialIntelligence
FIGURE8.22: SampleSingle-LayerNetwork(SLP).
FIGURE8.23: SampleMulti-LayerNetwork(MLP).
5. Multi-layerperceptronsincludenon-linearactivationfunctionsatthe
hiddenandoutputlayers-whyisthisimportant?
6. Whatisthepurposeofthebiascomponent?
7. Describethefundamentalprocessbehindperceptronlearning.
8. What is a principle advantage of the Delta rule over perceptron
learning?
9. DescribethebasicprocessbehindlearningwiththeDeltarule.
10. ConsidertheperceptronshowninFigure8.22.Findtheerrorandthen
calculate the new weights given the training example. After the new
weightsareavailable,recalculatetheerrorforthegivensampleandverify
thattheerrorhasbeenreducedgiventhepriorweightadjustment.
NeuralNetworksI 287
FIGURE8.24: SampleclassdataforPNNcalculation.
11. WiththeexampleneuralnetworkshowninFigure8.23,calculatethe
weight adjustments using backpropagation learning. Recalculate the
outputtoverifythattheerrorhasbeenreducedgiventhepriorweight
adjustment.
12. Describe the major architectural differences between MLP neural
networkswithbackpropagationandprobabilisticneuralnetworks.
13. Using the test data shown in Figure 8.24, identify to which class the
unseenfeaturebelongs.
14. Describeaneuralnetworkarchitecturefortime-seriesdataprediction.
15. How can long-term memory be implemented in a neural network
architecture?
Chapter 9 NEURAL
NETWORKSII
I
nChapter8,weexploredanumberofneuralnetworkarchitecturesand
learningalgorithmsthatwereabletotrainwithasetofexampledata,
andthengeneralizeforunseendata.Thisiscalledsupervisedlearning,
asthenetworklearnswiththeaidofateacher(definitionofoutputerror).
Thischapterwillpresentadifferentmodelforlearning,calledunsupervised
learning.Inthismodel,thenetworkisgivenasetofrawdata,forwhichit
mustanalyzewithnopriorknowledgeofitsstructure,andthenclassifyit
basedontheinherentsimilarities.
UNSUPERVISEDLEARNING
Unsupervised learning is a valuable method for data classification as it
requiresnothingmorethanrawdata,anddoesnotrequireanunderstanding
oftheunderlyingstructureofthedata.Unsupervisedlearningalgorithmscan
infactidentifytheunderlyingstructurebysegregatingthedataintodistinct
groupsforlateranalysis.Itdoesthisbyfindingsimilaritiesintherawdata,
andthengroupingthosepiecesofdatatogether.Whatunsupervisedlearning
algorithmscannotdoisnametheclustersofdata;ithasnoknowledgeofwhy
trainingdataaregroupedtogether,andwhatthegroupingrepresents(see
Figure9.1).Itsimplyidentifiesthesimilaritiesandclustersappropriately.
290 ArtificialIntelligence
FIGURE9.1: Unsupervisedlearningcanclusterbasedonsimilarities,buthasnoknowledgeof
themeaningoftheclusters.
NOTE Reinforcementlearningalsousesaformofunsupervisedlearning.
Whileconceptuallysimple,unsupervisedlearningisextremelyvaluable
andcanbeusedinavarietyofproblemdomains.Inadditiontobeingused
toidentifytheunderlyingstructureofasetofrawdata,itcanalsobeused
tocompressortransformthedata.We’llexploreanumberofunsupervised
learningalgorithmsinthischapter,alongwiththeirrelativestrengthsand
weaknesses.
HEBBIANLEARNING
TheworkofDonaldHebbrepresentstheearliestdevelopmentofalearningrule
thatisbothbiologicallyplausibleandcanbeimplementedasanalgorithm.
Thishasbeensummarizedconciselyinwhat’scalledHebb’spostulate
oflearning:
“WhenanaxonofcellAisnearenoughtoexciteacellBandrepeatedly
or persistently takes part in firing it, some growth process or metabolic
changetakesplaceinoneorbothcellssuchthatA’sefficiency,asoneofthe
cellsfiringB,isincreased.”[Hebb1949]
In simpler terms, if two neuron cells are firing simultaneously, then
any weight connecting between them should be increased. In this way,
connected neurons reinforce each other to support learning. Note here
thatHebbintroducedtheconceptofincreasingweights,butnotdecreasing
NeuralNetworksII 291
weights.Nevertheless,Hebb’sworkservedasthebasisforfurtherresearch
inconnectionistmodels,suchasFrankRosenblatt’sworkontheperceptron
learningalgorithm(seeChapter8).
NOTE In the period that Hebb introduced his work in neuron modeling,
NorbertWeinerintroducedtheconceptofCybernetics,orthe“control
andcommunicationintheanimalandthemachine.”Cyberneticswasa
multi-disciplinaryfieldthatincludedsuchestablishedfieldsaselectrical
engineering,mathematics,biology,neurophysiology,anthropology,and
psychology.
Hebb’sRule
ThebasicideabehindHebb’sruleisthatwhenevertherearetwocellswith
similaractivations,theweightsthatconnectthemshouldbestrengthened.
From a pattern recognition perspective, this provides the means to
strengthen weights when inputs are similar, and to weaken them when
they’redissimilar.ThisresultsintheverysimpleEq9.1,whichimplements
thelearningalgorithmforHebb’srule.
(Eq9.1)
whereEisalearningrate(0<E<=1),xistheinput,yistheoutput,and
wi,jistheweightthatconnectsthem.
TheneuralnetworkthatusestheHebbRuleisaverysimplenetwork
that utilizes some number of input cells, and an identical set of output
cells (see Figure 9.2). The network is fully interconnected, with weights
connectingbetweeneachoutputcellandeachinputcell(sothateveryinput
hassomeinfluenceovertheoutput).
FIGURE9.2: SimplepatternrecallnetworkusingHebb’srule.
292 ArtificialIntelligence
FIGURE9.3: Anabstractpatternrecognizerprovidesamappingbetweentheinputtothe
output.
Totrainthenetwork,weprovidethenetworkwiththeinputpattern(andalso
duplicatethevalueoftheinputcellsattheoutput,sothattheHebbrulebuilds
theassociation).OncetheweightshavebeentrainedusingHebb’slearning
rule,thenetworkcanrecalloutputpatternsfromthosepresentedtotheinput
(theweightsencodethememoryofthepatterns)asshowninFigure9.3.
ThemajordisadvantagewithHebb’sRuleisthatitcanonlycreateamap
overorthogonalpatternsofinputs.Thisisduetothelackofahiddenlayer
withinthenetwork.
HebbRuleImplementation
ImplementingtheHebbruleisquitesimple,andcanbedonewithaminimal
amountofcode.We’llrepresentournetworkaswehavewithothersingle-
layerneuralnetworks(seeChapter10).
We’ll show three functions from this implementation: locking in the
input and expected output pattern, computing the activations, and the
functionimplementingtheHebbrule.
HE C
NT
D
O
TheimplementationofpatternrecallusingtheHebbrulecanbefound
ontheCD-ROMat./software/ch9/hebb.c.
Inthefirstfunction,wesettheinputpatternandalsolockthissame
patternintotheoutput(define_pattern).Thiswillbesubsequentlyusedby
theHebbruletotraintheweightsforthepattern(seeListing9.1).Theinput
andoutputvectorsarealsoshown.Notethatwhiletheinputsaredefinedas
integers,theoutputsaredouble,sinceweusedareal-valuedlearningrate.
We’llusealinearthresholdtoclipthevalues,anythinggreaterthan0isa
‘1,’andanythingequalto,orlessthan0isa‘-1.’
NeuralNetworksII 293
Listing9.1: SettingtheinputandoutputpatternsforHebblearning.
intinputs[MAX_CELLS];
doubleweights[MAX_CELLS][WEIGHTS_PER_ACTIVATION];
doubleoutputs[MAX_CELLS];
voiddefine_pattern(int*inp)
{
inti;
for(i=0;i<MAX_CELLS;i++){
inputs[i]=inp[i];
outputs[i]=(double)inp[i];
}
return;
}
Computing the activations using Hebb’s rule is shown in Listing 9.2
(compute_activations).Foreachoutputcell,weiteratethrougheachinput
andaccumulateitbymultiplingtheassociatedweightbytheinput.Once
theoutputactivationshavebeencomputed,astepfunctionisusedtoforce
theoutputstoeitheravalueof1or-1.
Listing9.2: ComputingtheoutputactivationsusingHebb’sRule.
voidcompute_activations(intadjust_weights)
{
intout,weight;
/*Computetheactivations*/
for(out=0;out<MAX_CELLS;out++){
outputs[out]=0.0;
for(weight=0;weight<WEIGHTS_PER_ACTIVATION;weight++){
outputs[out]+=(weights[out][weight]*(double)inputs[weight]);
}
/*Cliptheoutputs*/
if(outputs[out]>0.0)outputs[out]=1.0;
elseoutputs[out]=-1.0;
}
return;
}
Finally, adjusting the weights with Hebb’s rule is the simple process
ofaccumulatingweightchangesusingEq9.1.Foreachoutputcell,each
294 ArtificialIntelligence
weightconnectingtotheinputisadjustedbymultiplyingtheinputbythe
outputandasmalllearningrate(seeListing9.3).Thisadjuststheweights
inanefforttomaptheinputvectortotheoutputvector.
Listing9.3: WeightadjustmentwithHebb’srule.
voidadjust_weights(void)
{
intout,weight;
/*HebbianLearningRule*/
for(out=0;out<MAX_CELLS;out++){
for(weight=0;weight<WEIGHTS_PER_ACTIVATION;weight++){
/*Equation9.1*/
weights[out][weight]+=ETA*(outputs[out]*
(double)inputs[weight]);
}
}
return;
}
Tolearnanewpattern,wesimplyspecifyanewimageandloaditwith
the define_pattern function. A call to adjust_weights can then be
performedtocreateamappingforthenewpattern,forexample:
{
intpattern1[MAX_CELLS]={-1,1,1,-1,
1,-1,-1,1,
1,-1,-1,1,
-1,1,1,-1};
define_pattern(pattern1);
}
/*TrainforPattern1*/
adjust_weights();
Wecanthentestforthispatternbycomputingtheoutputactivations
withacalltocompute_activiations.Notethatinthisexample,we’ve
presentedapartialinputpatterninanattempttotestitsrecallcapabilities:
/*TestforPattern1*/
{
NeuralNetworksII 295
intpatternA[MAX_CELLS]={-1,-1,-1,-1,
-1,1,1,-1,
1,1,1,1,
1,1,1,1};
define_pattern(patternA);
}
show_activations();
Notethatshow_activationssimplyemitstheinputandoutputvectorto
illustratethemapping.
Nowlet’slookatthealgorithminaction.We’lltrainournetworkfortwo
distincttrainingpatternsina4by4matrix.Patternoneisasimplebox,and
patterntwoisarepresentationofan‘X’pattern(seeFigure9.4).
FIGURE9.4: TrainingpatternsfortheHebbruleneuralnetwork.
FIGURE9.5: ResultofpatternrecallusingtheHebbiannetwork.
296 ArtificialIntelligence
Totesttherecallcapabilitiesofthenetwork,we’llpresentthenetworkwith
elementsoftheinputpattern,butnottheentirepattern(seeFigure9.5).
AsisshowninFigure9.5,Hebbianlearningdoesareasonablejobof
recallingpatternsgivenpiecesoftheoriginalpatterns.TheHebbruleisvery
simple,andcanbeveryeffectivewhenthere’sastrongcorrelationbetween
thepresentedinputpattern,anddesiredrecallpattern.
SIMPLECOMPETITIVELEARNING
Incompetitivelearning,theoutputnodesofthenetwork(whichrepresentthe
distinctclassesavailableforclassification),competewithoneanothertofire
andthereforerepresenttheclassofthefeaturevectorattheinputnodes.
NOTE Clusteringisaveryinterestingtechniquewithverypracticalapplications.
Clusteringarawdatasetallowsabetterunderstandingofthestructure
of the data. Using clustering, relationships in data can be found that
couldotherwisenotbeseen.Oneveryusefulexampleofclusteringisin
recommendationsystems,wherecustomerdataisclustered.Customersin
agivenclusterarethenuniqueclassesofcustomersandtheirdifferences
canbeusedtorecommendpurchasestothoseintheclusterwholackthat
purchase.
Thecompetitivelearningnetworkismadeupoftwolayers.Thefirst
layeristheinputlayerandrepresentsthefeaturevector,andthesecond
layeristheoutputlayerrepresentingtheclass(seeFigure9.6).
FIGURE9.6: Samplecompetitivelearningnetwork.
NeuralNetworksII 297
Theweightsbetweentheinputandoutputlayerencodetheabilityto
classifyaparticularinputfeaturevectortoanoutputclassnode.Insome
competitive learning architectures, inhibitory connections between the
output layer are included. These connections allow the output nodes to
influenceeachotherinatruecompetitivesense.
VectorQuantization
Oneexampleofcompetitivelearningiscalledvectorquantization.Vector
quantization is a surprisingly simple network architecture and learning
algorithm.Theinputlayerhasanumberofnodesdefinedbythelengthof
thefeaturevector,andtheoutputlayerhasanumberofnodesdefinedby
thenumberofanticipatedclasses.Eachoutputnodeconnectstoeveryinput
nodebyatrainableweight.
Aseachprototypevector(sampletoclassify)isappliedtothenetwork,a
simplefeedforwardalgorithmcomputestheactivationofeachoutputnode,
asdefinedbyEq9.2.ForeachoutputnodeM,thesumoftheproductsof
theprototypevector(x)andweightsdeterminetheactivation.
(Eq9.2)
NOTE Vectorquantizationeffectivelydividestheprototypevectorinputspace
intoaVoronoitessellation.AVoronoitessellation(namedafterGeorgy
Voronoi)isadecompositionofaspacetoaspecificsetofdiscreteobjects
inthespace(suchascentroids).
Afteraprototypevectorisfoundtobeclosesttoagivenclass(output
node),onlytheweightsassociatedwiththisoutputnodeareupdated.The
weightsassociatedwiththewinningoutputnode(M)areupdatedforinput
x,andlearningrateρ,asshowninEq9.3.
(Eq9.3)
298 ArtificialIntelligence
The process of training continues until we reach some termination
criteria. This could be after some number of runs, or when the network
hasreachedequilibrium(nochangesaremade,suchasprototypevectors
changingclassification).
VectorQuantizationImplementation
Vector quantization, as illustrated by the introduction, is very easily
implemented.We’llexploreafewfunctionsthatmakeuptheimplementation,
theremainingsourcecanbefoundontheCD-ROM.
HE C
NT
D
O
Theimplementationofobjectclassificationusingvectorquantizationcan
befoundontheCD-ROMat./software/ch9/vq.c.
Let’sbeginwithadiscussionoftheneuralnetworkrepresentationfor
vectorquantization(seeListing9.4).Inthisapproach,theprototypefeature
vectorsarerepresentedwiththefeature_vector_ttype.Thiscontainsthe
currentclasstowhichtheprototypebelongs,andthefeaturevectoritself
(features).
The neural network is implemented very simply. Since there are no
hidden layers, we need only maintain the value of the output nodes (the
outputsarray).Theweightsbetweentheoutputlayerandinputlayerare
representedbyatwo-dimensionalarray(firstdimensionistheoutputnode,
secondaretheindividualweightsfortheoutputnode).
Listing9.4: Majortypesandvariables.
#defineMAX_FEATURE_VECTORS 40
#defineMAX_FEATURES 3
typedefstruct{
intclass;
intfeatures[MAX_FEATURES];
}feature_vector_t;
feature_vector_tfvec[MAX_FEATURE_VECTORS];
/*
*NeuralNetworkRepresentationforVectorQuantization
*/
#defineOUTPUT_NODES 3 /*Uniqueclasses
*/
#defineCLASSES OUTPUT_NODES
doubleoutputs[OUTPUT_NODES];
NeuralNetworksII 299
doubleweights[OUTPUT_NODES][MAX_FEATURES];
#defineLEARNING_RATE ((double)0.01)
Notethatwe’llnormalizethefeaturevectorsothatthevaluesscaleto
therange0to1,butwe’llnotusethevaluezeroandone(theextremes)to
avoidsaturatingtheweightsofthenetwork(seeFigure9.7).
Next,let’scontinuetheimplementationdiscussionwiththemainloop
(seeListing9.5).Themainfunctionprovidesafundamentalalgorithmfor
vector quantization, including the termination criteria. We begin by first
initializingtheprototypefeaturevectorswithacalltoinitialize_vectors.This
functioncreatesarandom set of feature vectors given the feature length
andavailableindividualfeatures(seethesourceontheCD-ROMforthe
detailsofthisfunction).Next,weinitializetheneuralnetworkwithacallto
initialize_network.ThisfunctionsimplytakesthefirstNfeatureprototype
vectors (N being the number of classes available for classification), and
associatestheoutputnodewiththefeaturevector.Thisisdonethroughacall
toupdateNetwork,whichinitializestheweightsfortheoutputnodegiven
theprototypefeaturevector(we’llreviewthisfunctionshortly).Notethat
thiscanbeproblematic,especiallyiftherearestrongsimilaritiesbetween
thefirstNfeatureprototypevectors,butitissimple,andworkswellgiven
agoodrandomdistribution.
Next, we enter the main loop. This loop continues to operate while
changesoccur(andbychange,wemeanafeatureprototypevectorchanging
classes, based on the class feature centroids changing themselves). Each
of the sample feature prototype vectors are applied in succession to the
network,usingthefeed_forwardfunction.Thisfunctionreturnstheclass
to which the prototype was assigned. This information is then applied to
the updateNetwork function, which updates the particular output node’s
(class’s)weightsgiventhecurrentprototypefeaturevector.Thisprocessthen
continuesforthenextsample,untilforeachiterationthroughthesample
set,nochangesoccur.
FIGURE9.7: Representingtheprototypefeaturevector.
300 ArtificialIntelligence
Listing9.5: Vectorquantizationmainloop(portion).
intmain()
{
inti,j;
intsample=CLASSES;
intclass;
changes=0;
/*Initializethefeaturevectors*/
initialize_vectors();
/*Initializetheneuralnetwork*/
initialize_network();
/*Continuetorunwhilefeaturevectorschangeclasses*/
while(changes>0){
if(sample>=MAX_FEATURE_VECTORS){
/*Resetthesampleindex*/
sample=0;
/*Ifnochangesoccurredinthelastiteration,theexit,
*otherwise,resetthechangescountertozeroandcontinue.
*/
if(changes==0)break;
elsechanges=0;
}
/*Feedthesampleprototypevectorthroughthenetwork.*/
class=feed_forward(sample);
/*Updatetheweightsforthewinningoutputnode(class).*/
updateNetwork(sample,class);
/*Nextsample*/
sample++;
}
...
return0;
}
The feed_forward function implements the output node activations
basedonEq9.2(seeListing9.6).Giventheinputprototypefeaturevector
index,wecalculatetheactivationsforeachoutputnode.This,usingEq9.2,
isthesumoftheproductsoftheweightsfortheparticularclass,andthe
valuesoftheinputfeaturevector.Inawinner-takes-allfashion,weidentify
theoutputnodewiththelowestvalue,whichrepresentstheclasstowhich
theinputfeaturevectorbelongs.Finally,wechecktoseeifthefeaturevector
NeuralNetworksII 301
haschangedclasses,andifso,weincrementthechangesvariabletoletthe
mainloopthataclasschangeoccurred.
Listing9.6: Computingtheoutputnodeactivationswithfeed_forward.
intfeed_forward(intfeature_vector)
{
intoutput,weight,best;
doublebest_value;
/*Computeeachoutputnodeactivationforthecurrent
*prototypevector.
*/
for(output=0;output<CLASSES;output++){
outputs[output]=0.0;
/*Equation9.2*/
for(weight=0;weight<MAX_FEATURES;weight++){
outputs[output]+=
weights[output][weight]*
SCALE(fvec[feature_vector].features[weight]);
}
}
/*Setthecurrentbesttoclass0*/
best=0;
best_value=outputs[0];
/*Iteratethroughtheremainingclassestoidentifywhichwasbest.*/
for(output=1;output<CLASSES;output++){
if(outputs[output]<best_value){
best=output;
best_value=outputs[output];
}
}
/*Keeptrackofwhenaprototypevectorchangesclasses,foruseas
*aterminationcriteria.
*/
if(best!=fvec[feature_vector].class){
changes++;
fvec[feature_vector].class=best;
}
returnbest;
}
302 ArtificialIntelligence
Finally,duringtraining,onceweidentifythataprototypefeaturevector
belongstoaparticularclass,weupdatetheweightsfortheclass.Thisisdone
throughacalltoupdateNetwork (seeListing9.7).Tothefunction,we
providetheprototypevectorindexandtheclasstowhichitbelongs.Each
of the weights associated with the particular output node (class) is then
updatedperEq9.3.Notethatweapplyalearningratetotheweightupdate
toprovideagradualchangetotheweights.
Listing9.7: UpdatingtheweightsforaparticularclasswithupdateNetwork.
voidupdateNetwork(intfeature_vector,intclass)
{
intweight;
for(weight=0;weight<MAX_FEATURES;weight++){
/*Equation9.3*/
weights[class][weight]+=LEARNING_RATE*
(SCALE(fvec[feature_vector].features[weight])-
weights[class][weight]);
}
return;
}
Let’snowseevectorquantizationinpractice(Listing9.8).Perthesample
implementation,asetofrandomdataiscreatedthatrepresentsavarietyof
objectsthatdifferinshape,size,andcolor.Theseareencodedintoafeature
vector(shape,texture,color)andappliedtothenetworkinordertotrainthe
weights.Whentrainingiscomplete(equilibriumisreachedinthenetwork),
theclassificationisemitted.
Forthissetofrandomizeddata,weseeaveryclearclassificationemerge
fromthedata.Fromthefeaturevectors,wecanseethatclass0contains
all objects that have the box shape. Class 2 includes all objects that are
smooth(exceptforboxes).Finally,Class3includesallobjectsnotpreviously
classified.Thisincludesanyobjectthatisn’taboxandisn’tsmooth.That’s
onepossibleclassification,butgiventhenumberofboxshapesthatexist,it’s
notsurprisingthatthisparticularcategorizationwasfound.
Listing9.8: Sampleclassificationofrandomobjectdata.
$./vq.exe
Class0contains:
NeuralNetworksII 303
1[BLACKROUGHBOX ]
2[BLACKSMOOTHBOX ]
3[REDSMOOTHBOX ]
7[BLACKSMOOTHBOX ]
8[BLACKSMOOTHBOX ]
13[BLACKROUGHBOX ]
16[BLUESMOOTHBOX ]
18[BLACKDIMPLEDBOX ]
21[REDROUGHBOX ]
23[BLACKSMOOTHBOX ]
25[BLACKSMOOTHBOX ]
27[REDROUGHBOX ]
29[REDSMOOTHBOX ]
32[REDROUGHBOX ]
39[REDDIMPLEDBOX ]
Class1contains:
0[BLACKSMOOTHSPHERE ]
12[BLACKSMOOTHSPHERE ]
14[REDSMOOTHSPHERE ]
15[BLUESMOOTHSPHERE ]
17[REDSMOOTHSPHERE ]
24[REDSMOOTHCYLINDER ]
26[REDSMOOTHCYLINDER ]
33[BLUESMOOTHCYLINDER ]
34[REDSMOOTHCYLINDER ]
37[REDSMOOTHCYLINDER ]
Class2contains:
4[BLUEDIMPLEDCYLINDER ]
5[BLACKDIMPLEDSPHERE ]
6[BLACKROUGHSPHERE ]
9[BLACKROUGHSPHERE ]
10[BLACKDIMPLEDCYLINDER ]
11[BLACKROUGHSPHERE ]
19[BLACKROUGHSPHERE ]
20[BLUEROUGHSPHERE ]
22[BLACKROUGHSPHERE ]
28[BLUEDIMPLEDCYLINDER ]
30[REDROUGHCYLINDER ]
304 ArtificialIntelligence
31[BLACKROUGHCYLINDER ]
35[REDDIMPLEDCYLINDER ]
36[BLACKROUGHSPHERE ]
38[BLACKDIMPLEDSPHERE ]
$
Note that this method was unsupervised. The algorithm had no
knowledgeofthedata,butdidareasonablejobofsegregatingthedatabased
oncharacteristicsofthedata.Inonecluster,thedataissegmentedbasedon
theshape,butinanother,it’ssegmentedbasedonatexture.
Thesimplicityofthisalgorithmmakesitagreatchoiceforclustering.
One primary disadvantage of the algorithm is that the number of output
classesmustbedefinedupfront.Thisisasignificantdisadvantagebecause
it assumes that we have some general knowledge of the data and how it
shouldbeclassified.
Inadditiontoitsclusteringcapabilities,you’llfindvectorquantization
in other applications such as image and audio compression, and even
speaker recognition. Identifying a speaker is an interesting problem and
fundamentallycomesdowntoaclassificationproblem.Theincomingvoice
audioisreducedtoafeaturevector,whichisappliedtoavectorquantizer
toidentifytheclass(orspeaker)thatfitsbest.
K-MEANSCLUSTERING
Averypopularalgorithmforunsupervisedclusteringoffeaturevectordata
iscalledk-Means(wheretherearekclusters,andtheaverageofthecluster
contentsdeterminetheclustercentroid).Thisalgorithmispopularprimarily
becauseitworksrelativelywellandisextremelysimplebothtounderstand
andtoimplement(seeFigure9.8).
FIGURE9.8: Basicflowofthek-Meansclusteringalgorithm.
NeuralNetworksII 305
FIGURE9.9: Demonstratingacentroidchangewiththelossofadataitem.
Thefundamentalapproachtok-Meansclusteringisbasedoncentroids
representingtheaverageofthecurrentsetoffeaturevectorscontainedwithin
thecluster.Thecentroidistheaverageofallfeaturevectors,andisrecalculated
wheneveranobjectmovesintooroutofacluster.Choosingaclusterforagiven
featurevectorisbasedontheEuclideandistanceofthefeaturevectorandthe
availableclustercentroids(seeFigure9.9).TheclosertheEuclideandistance,
thehighertheprobabilitythefeaturevectorbelongstothecluster.
Notethateachtimeaclusterchanges,itscentroidalsochanges(asit’s
recalculatedaftereverychange).Thismeansthatoverthedurationofthe
algorithm,asthecluster’smembershipchanges,thecentroidschange,which
canmeanthatadditionalfeaturevectorscanchangeclustermembership.For
thisreason,atypicalterminationcriteriaisaniterationthroughallfeature
vectorswithnonechangingclustermembership.
k-MeansAlgorithm
Thek-Meansalgorithmisoneofmanyclusteringalgorithms,butremains
popular due to its simplicity. It’s also relatively efficient, but does have
somedisadvantages(suchasinefficientmethodsfordefiningtheclustersat
initializationtime).Let’sfirstexplorethedetailsofthek-Meansalgorithm,
andthenwe’lldiscusstheadvantagesanddisadvantagesofthismethod.
NOTE Aswithvectorquantization,we’lluseaverysimplefeaturevectortodiscuss
andexplorek-Meansinanimplementation.Featurevectorswilldescribe
objectsintermsofanumberofdimensions(color,texture,andshape).
Webeginwithasetofprototypefeaturevectorsthataretobeclustered
based on their similarities and differences (see the flow shown in Figure
9.10).Thenumberofclusters(k)mustalsobedefinedapriori.Eachcluster
hasanassociatedcentroidfeaturevectorwhichrepresentstheaverageof
theprototypefeaturevectorscontainedwithinthatcluster.
306 ArtificialIntelligence
FIGURE9.10: Basicflowofthek-Meansclusteringalgorithm.
NOTE Acentroidisdefinedasthecenterofthemass.Inthiscontext,thecentroid
isthecenterofasetofmulti-dimensionalprototypefeaturevectors.
We then take k of the prototype feature vectors and assign one each
totheavailableclusters.Thesekprototypefeaturevectorscanbepicked
at random, but ideally, we would choose vectors that are dissimilar. The
simplestapproachistochoosethefirstkfeaturevectorsandassignthemto
theclusters.Withtheclustersnowcontainingoneprototypefeaturevector,
we initialize the centroid. Since the cluster contains only one prototype
featurevector,thecentroidisequaltotheprototypefeaturevector.
Next,weiteratethrougheachoftheavailableprototypefeaturevectors,
andcomputetheEuclideandistanceofthecentroidtotheprototypefeature
vector(seeEq9.4,iisthefeatureindex,xistheinputfeaturevector,and
c is the centroid). We pick the nearest centroid (defined as the smallest
Euclideandistance)andassigntheprototypevectortothatcentroid.Wethen
recalculatethecentroidstotakeintoconsiderationtheirnewmembers.Note
thatthiscanbedoneaftereachevaluationorinabatchmodewherethe
featurevectorsareassigned,andthenthecentroidsupdatedattheend.
(Eq9.4)
NeuralNetworksII 307
If at the end of evaluating all prototype feature vectors, none have
changedclusters,wecanassumethatwe’vereachedequilibriumandthe
clusteringiscomplete.Ifacentroiddidchange(becauseofgainingand/or
losing a prototype feature vector), we re-evaluate each prototype vector
again. Recalculating the centroid simply involves summing the prototype
feature vectors and dividing the resulting vector by the total number of
prototypevectorsinthecluster.
Theprimarydisadvantageofk-Meansisthatthenumberofclustersmust
bedefinedapriori.Thiscanrequiresomeexperimentationfortheproblem
athand,toidentifyhowmanyclustersshouldbepresenttoproperlyclassify
thedata.Asdefinedintheintroduction,initializationoftheclusterscanalso
beproblematic.Therefore,multiplerunsofthealgorithmmayberequired
tofindaproperclassification.
That’sthebasicalgorithmfork-Meansclustering.Let’snowexplorea
sampleimplementationthatclustersobjects(basedonFigure9.7).
k-MeansImplementation
Todemonstratethek-Meansalgorithm,we’llreusethedatainfrastructure
fromthevectorquantizationexample(objectclassificationbasedonshape,
color,andtexture).Theprototypevectorscontainbothafeaturevectorand
thecurrentclasstowhichtheprototypevectorbelongs.Thecentroidsare
representedasafloating-pointvector(seeListing9.9).Thecentroidsdiffer
fromtheprototypefeaturevectorsbecausethecentroidswillrepresentthe
averageofthememberprototypevectors,andthereforerequirefloating-
pointprecision.
Listing9.9: k-Meanstypesandsymbolicconstants.
#defineMAX_FEATURE_VECTORS 40
#defineMAX_FEATURES 3
typedefstruct{
intclass;
intfeatures[MAX_FEATURES];
}feature_vector_t;
/*PrototypeFeatureVectors*/
feature_vector_tfvec[MAX_FEATURE_VECTORS];
/*Numberofclusters*/
#defineK 3
/*ClusterCentroids*/
doublecentroids[K][MAX_FEATURES];
308 ArtificialIntelligence
We’ll present a few of the functions that implement the k-Means
algorithm:theentireimplementationcanbefoundontheCD-ROM.Forthe
purposesofexplanation,we’llexplorethek-Meansmainloop,identifyingthe
nearestcentroid,computingthegeometricdistance,andfinallyrecalculating
acentroidgiventhecurrentprototypefeaturevectors.
HE C
NT
D
O
Theimplementationofobjectclassificationusingk-Meansclusteringcan
befoundontheCD-ROMat./software/ch9/kmeans.c.
Listing9.10: Thek-Meansalgorithmmainloop.
voidk_means_clustering(void)
{
intdone=0,i;
intold,new;
intproto_vector;
/*Createtherandomsetofprototypefeaturevectors*/
initialize_prototype_vectors();
/*SetKvectorstoclusters(toinitializethecentroids)*/
initialize_membership();
/*Computethecentroidsforeachcluster*/
for(i=0;i<K;i++){
compute_centroids(i);
}
while(!done){
done=1;
NeuralNetworksII 309
/*Iteratethroughtheavailableprototypefeaturevectors*/
for(proto_vector=MAX_FEATURE_VECTORS-1;proto_vector>=0;
proto_vector--){
/*Findtheclustertowhichthisprototypevectorbelongs*/
new=partition_feature_vector(proto_vector);
/*Didtheprototypefeaturevectorchangeclasses*/
if(new!=fvec[proto_vector].class){
old=fvec[proto_vector].class;
fvec[proto_vector].class=new;
/*Recomputetheaffectedcentroids(-1=notyetclustered)*/
if(old!=-1){
compute_centroids(old);
}
compute_centroids(new);
done=0;
}
}
}
}
Listing9.11: Findingthenearestcluster.
intpartition_feature_vector(intproto_vector)
{
intcluster,best=0;
doublegdCur,gdBest=999.99;
/*Findthecentroidthatbestmatchestheprototypefeaturevector*/
for(cluster=0;cluster<K;cluster++){
gdCur=geometricDistance(proto_vector,cluster);
/*Keeptrackoftheclosestclustercentroid*/
if(gdCur<gdBest){
best=cluster;
310 ArtificialIntelligence
gdBest=gdCur;
}
}
returnbest;
}
CalculatingtheEuclideandistancebetweentheprototypefeaturevector
andtheclustercentroidisdonewithacalltogeometricDistance.UsingEq
9.4,westepthrougheachelementofthevector,summingthesquaresofthe
difference.Aftereachelementofthevectorhasbeeniterated,thesquare
rootoftheresultisreturned.
Listing9.12: C
omputingthegeometricdistancebetweenthefeaturevector
andthecentroid.
doublegeometricDistance(intproto_vector,intcentroid)
{
intfeature;
doublegd=0.0;
/*Equation9.4*/
for(feature=0;feature<MAX_FEATURES;feature++){
gd+=(((double)fvec[proto_vector].features[feature]-
centroids[centroid][feature])*
((double)fvec[proto_vector].features[feature]-
centroids[centroid][feature]));
}
return(sqrt(gd));
}
Finally,thecompute_centroids functionisusedtorecalculatethe
centroidforthedefinedcluster.Thisfunctionverysimplysumsthefieldsof
theprototypefeaturevectorsthatarecontainedwithinthecurrentcluster,and
thendivideseachfieldbythetotalnumber.Theresultingcentroidrepresents
themulti-dimensionalcenterofthecluster’sprototypefeaturevectors.
Listing9.13: Recalculatingtheclustercentroids.
voidcompute_centroids(intcluster)
{
intproto_vector,feature;
NeuralNetworksII 311
inttotal=0;
/*Clearthecentroidvector*/
for(feature=0;feature<MAX_FEATURES;feature++){
centroids[cluster][feature]=0.0;
}
/*Calculatethecentroidvectorforthecurrentcluster*/
for(proto_vector=0;proto_vector<MAX_FEATURE_VECTORS;
proto_vector++){
if(fvec[proto_vector].class==cluster){
for(feature=0;feature<MAX_FEATURES;feature++){
centroids[cluster][feature]+=
(double)fvec[proto_vector].features[feature];
}
total++;
}
}
/*Computetheaverageforthecentroid*/
for(feature=0;feature<MAX_FEATURES;feature++){
centroids[cluster][feature]/=(double)total;
}
return;
}
Let’snowlookatasampleresultofthek-Meansalgorithm(seeListing
9.14).Class0isclearlyaclusterofsmoothobjects,andClass2containsred
objectsthatarenotsmooth.Class1thenbecomesallobjectsthatareneither
red,norsmooth.
Listing9.14: Samplek-Meansclassificationofrandomobjectdata.
$./kmeans.exe
Class0contains:
5[REDSMOOTHBOX]
6[REDSMOOTHBOX]
7[REDSMOOTHBOX]
8[REDSMOOTHSPHERE]
11[BLACKSMOOTHSPHERE]
12[BLACKSMOOTHBOX]
14[BLUESMOOTHCYLINDER]
16[BLACKSMOOTHBOX]
312 ArtificialIntelligence
17[BLACKSMOOTHCYLINDER]
19[BLUESMOOTHBOX]
24[REDSMOOTHCYLINDER]
25[REDSMOOTHSPHERE]
30[REDSMOOTHBOX]
31[BLACKSMOOTHCYLINDER]
32[BLUESMOOTHSPHERE]
33[BLUESMOOTHCYLINDER]
35[BLACKSMOOTHCYLINDER]
36[BLACKSMOOTHBOX]
39[REDSMOOTHSPHERE]
Class1contains:
0[BLUEDIMPLEDCYLINDER]
1[BLACKDIMPLEDCYLINDER]
4[BLUEDIMPLEDBOX]
9[BLACKROUGHSPHERE]
10[BLUEDIMPLEDSPHERE]
15[BLACKROUGHSPHERE]
18[BLUEDIMPLEDCYLINDER]
20[BLUEDIMPLEDBOX]
22[BLACKDIMPLEDSPHERE]
23[BLUEDIMPLEDSPHERE]
26[BLUEDIMPLEDCYLINDER]
27[BLUEROUGHSPHERE]
29[BLACKDIMPLEDBOX]
34[BLUEDIMPLEDCYLINDER]
Class2contains:
2[REDDIMPLEDCYLINDER]
3[REDROUGHSPHERE]
13[REDDIMPLEDSPHERE]
21[REDDIMPLEDCYLINDER]
28[REDDIMPLEDCYLINDER]
37[REDROUGHSPHERE]
38[REDROUGHCYLINDER]
$
Recallthatfrominitialization,thefirstkprototypefeaturevectorsare
assignedtotheirsamenumberedcluster(vector0tocluster0,vector1to
NeuralNetworksII 313
cluster1,etc.).Noteinthisexamplethatwhileprototypevector2remained
incluster2,prototypevector0movedtocluster1(sincecluster0wasused
toclassifysmoothobjects).
The k-Means algorithm is a useful algorithm because it’s simple and
workswell.It’snotwithoutitsissues(suchasaprioridefinitionofk,and
sensitivitytoinitialization),butthesecanbecombatedthroughmultipleruns
ofthealgorithmonthedataset.
TIP
It’sgoodtonotethatthere’snotheoreticalsolutionforunderstandingthe
optimalnumberofclassesforanydataset.Anexperimentalsolutionisto
executethek-Meansalgorithmonthedatasetmultipletimesandreview
the results. Fewer clusters mean better generalization in the results,
wheremoreclusterstendstoendinclusterswithveryspecificattributes
andariskofover-fitting.
Fork-Meansandvectorquantization,theprimarydisadvantageisthat
thenumberofclustersmustbepredefined.Let’snowexploreanalgorithm
thatcandynamicallycreatenewclusterswhenthedatasetwarrantsthem.
ADAPTIVERESONANCETHEORY(ART)
Adaptive Resonance Theory is a collection of models for unsupervised
learning.Inthissection,we’llfocussolelyonART-1,whichisapplicableto
binaryinputvectors.Forcontinuousvariables,theART-2algorithmcanbe
used.
ART-1 in particular was designed to resolve the stability-plasticity
dillema. This refers to a conflict in the ability to maintain old learned
informationwhilestillbeingadaptivetolearnnewinformation.Analgorithm
is defined as plastic if it can adapt to new information. Additionally, an
algorithmisstableifitcanretainpreviouslylearnedknowledge.Thegoal
thenistocreateanalgorithmthatcanretainpreviouslylearnedknowledge
whileatthesametimeintegratingnewlydiscoveredknowledge.Inthisway,
thealgorithmisbothstableandplastic.Manyclusteringalgorithmsareone
ortheother,butnotnecessarilyboth.
Aswediscussedink-Meansclustering,aninterestingadvantagetoART-
1isinitsabilitytocreateanewclusteriftheunderlyingdatawarrants.It
accomplishesthiswithavigilanceparameterthathelpstodeterminewhen
toclusterafeaturevectortoa“close”cluster,orwhentosimplycreateanew
clusterintowhichthisfeaturevectorisinserted.
314 ArtificialIntelligence
ART-1Algorithm
TheART-1algorithmissimple,notquiteassimpleasthek-Meansalgorithm,
butstraightforward,nonetheless.Let’sbeginwithaquickoverviewofART-1
andthenwe’lldigdeeperintothealgorithmtounderstandhowitworks.
Webeginthealgorithmwithasetofunclusteredfeaturevectorsand
somenumberofclusters.Wetakeeachfeaturevector,inturn,andattempt
tofindtheclustertowhichit’sclosest.Ifnoclustersarefoundthatarenear
thefeaturevector,wecreateanewclusterandassignthefeaturevectorto
it.Otherwise,withaclusterthat’snearthefeaturevector,wetesthowclose
thefeaturevectoristothecluster.Ifthefeaturevectorisnearthecluster,
then we assign the feature vector to the cluster, and update the cluster.
Otherwise,wecontinuetotestthefeaturevectortoallavailableclusters.If
allavailableclustershavebeenexhausted,thenwesimplyassignthefeature
vectortoanewcluster.
Wethenstarttheprocessagainwithanewfeaturevector.Ifwework
through all available feature vectors, and none change their cluster, all
sampleshavebeensatisfactorilyclusteredandtheprocessiscomplete.That’s
ART-1inanutshell.Nowlet’sexplorealittlefurthertounderstandhowthe
algorithmmaintainsbothstabilityandplasticity.
First,eachfeaturevectorisabinarystringofagivenwidth(n).Each
clusterisrepresentedbyavectorthatisthebooleanANDoperatorofall
featurevectorscontainedwithinthecluster(seeFigure9.11).
Note from Figure 9.11 that the width of the feature vector (and the
cluster,whichmustbethesame)isdefinedasn(or8).We’llusethesymbol
ptorepresentthecurrentcluster(typicallyindexed,asinpi).ThesymbolI
representsthecurrentfeaturevector.Finally,symbolsβandρareconstant
values,andwe’lldiscusstheseshortly.
Givenafeaturevector,andalistofavailableclusters(initially,allwillbe
empty),thefirststepistotestthesimilarityofafeaturevectortothecluster’s
vector.ThisisdoneusingEq9.5.
FIGURE9.11: BooleanANDrelationshipoffeaturevectorstotheclustervectorforART-1.
NeuralNetworksII 315
(Eq9.5)
TIP
Note that in Eq 9.5, the double vertical bars simply represent the
number of 1s that are set in the particular vector. For example, the
vectorrepresentingclusterAinFigure9.11wouldbe3.Theinverted‘U’
representsthebooleanANDoperationbetweentheclustervectorand
thecurrentfeaturevector.
ThesimilaritytestinEq9.5calculateshownearthefeaturevectoristo
theclustervector.Thehigherthevalue,thecloserthevectoristothecluster.
Therefore,ifEq9.5issatisfied,thenthefeaturevectorcanbedefinedas
sufficientlyclosetothecluster.Ifthisequationfailsforallclustervectors,
then we simply create a new cluster for this feature vector and continue
tothenextfeaturevector.Otherwise,ifitissufficientlyclose,wetestfor
vigilanceacceptability.Theβparameterisusedasa“tie-breaker”togive
deferencetoclusterswithmore1sinthecasethatthefeaturevectorand
clusteraresimilar.Thisparameteristypicallyasmallnumber(greaterthan
zeroandlessthann).
Thevigilancetestisthefinaldeterminerforwhetherthefeaturevector
shouldbeaddedtotheparticularcluster(seeEq9.6).
(Eq9.6)
Thisequationsimplyidentifiesifthefeaturevectorissufficientlyclose
to the cluster (as a ratio of the matching 1s between the feature vector
andthecluster).Thismeansthatifvigilance(ρ)ishigh(suchas0.9)more
clusterswilltendtobecreated,andifvigilanceislow,thenfewerclusters
arecreated.Ifρissetto1.0,thenthefeaturevectormustmatchthecluster
exactlyinordertojoinit.
If the feature vector fails the vigilance test, and there are no further
clusterstotest,thenanewclusteriscreatedforthisfeaturevector(asthere
arenosimilarclustersavailable).
Notethatwhileclustersarecreated,featurevectorsmaydropoutof
aclusterandintoanotherbasedonnewfeaturevectorsbeingadded,and
adjustingtheclustervector.Whennoclusterchangesaremadeforanentire
iterationthroughtheavailablefeaturevectors,thealgorithmiscomplete.
When a feature vector joins a cluster, the cluster’s vector must be
updatedtoincorporatethefeaturesofthenewaddition.Ifthefeaturevector
316 ArtificialIntelligence
FIGURE9.12: Representingananimal’scharacteristicsfortheART-1algorithm.
addedistheonlymemberofthecluster,thentheclustervectorisidentical
tothefeaturevector.Otherwise,eachofthecluster’sfeaturevectors(Ii)is
assimilatedusingEq9.7.
(Eq9.7)
ART-1isstable(retainsknowledge),butisalsoplasticinthatitcan
indefinitely incorporate new knowledge given a sufficient number of
clusters. Let’s now explore the implementation of ART-1 for an object
classificationtask.
ART-Implementation
Let’sstartwithadiscussionoftheclusteringproblemathandandthenreview
therepresentationforthefeaturevectorandclustervector.Todemonstrate
theART-1algorithm,we’lluseafeaturevectorrepresentingattributesofa
numberofanimals(seeFigure9.12).Thesecharacteristicsincludewhether
theanimalgiveslivebirth,orlayseggs,orwhethertheanimalhasfur,hair,
ornaked-skin,etc.
HE C
NT
D
O
TheimplementationofanimalclusteringusingART-1canbefoundon
theCD-ROMat./software/ch9/art1.c.Themajorfunctionsthatmakeup
ART-1areexploredhere,buttheentireimplementationisprovidedon
theCD-ROM.
Both feature vectors and clusters are represented with the vector_t
type. This contains not only the binary feature vector, but a union which
represents the current class (in the case of a feature vector, for fvec) or
themembercount(inthecaseofacluster,pvec).Thefeaturevectorsare
staticallyinitialized,while the clusters (pvec) are initialized with a call to
initialize_prototype_vectors.
NeuralNetworksII 317
Listing9.15: FeaturevectorandclusterrepresentationforART-1.
/*Numberofclusters*/
#defineMAX_CLUSTERS 5
/*Numberofprototypefeaturevectors*/
#defineMAX_FEATURE_VECTORS 19
/*Size(width)offeaturevector*/
#defineMAX_FEATURES 13
typedefstruct{
union{
intclass; /*ForFeatureVectors*/
intcount; /*ForClusters*/
}u;
intfeatures[MAX_FEATURES];
}vector_t;
/*PrototypeFeatureVectors*/
vector_tfvec[MAX_FEATURE_VECTORS]={
/*Robin */{{-1},{1,1,0,0,0,0,1,0,0,1,1,0,0}},
/*Spider */{{-1},{0,0,0,1,1,0,0,1,0,0,1,0,0}},
/*Cat */{{-1},{0,0,1,0,0,1,0,0,0,1,0,1,0}},
/*Salmon */{{-1},{0,0,0,0,0,0,0,0,0,1,1,0,1}},
/*Mouse */{{-1},{0,0,1,0,0,1,0,0,0,1,0,1,0}},
/*Moose */{{-1},{0,0,0,1,0,1,0,0,1,0,0,1,0}},
/*Bat */{{-1},{0,1,1,0,0,0,1,0,0,1,0,1,0}},
/*Dog */{{-1},{0,0,1,0,0,1,0,0,0,1,0,1,0}},
/*Snake */{{-1},{0,0,0,0,0,0,0,1,0,0,1,0,1}},
/*Lion */{{-1},{0,0,1,0,0,1,0,1,0,0,0,1,0}},
/*Iguana */{{-1},{0,0,0,0,0,1,0,0,0,1,1,0,1}},
/*Dolphin */{{-1},{0,0,0,0,0,0,0,1,0,0,0,1,1}},
/*Zebra */{{-1},{0,0,1,0,0,1,0,0,1,0,0,1,0}},
/*Horse */{{-1},{0,0,1,0,0,1,0,0,1,0,0,1,0}},
/*Ostrich */{{-1},{1,0,0,0,0,0,1,0,1,0,1,0,0}},
/*Penguin */{{-1},{1,0,0,0,0,0,1,0,0,0,1,0,0}},
/*Tiger */{{-1},{0,0,1,0,0,1,0,1,0,0,0,1,0}},
/*Platypus */{{-1},{0,0,1,0,0,1,0,1,0,0,1,0,0}},
/*Octopus */{{-1},{0,0,0,0,1,0,0,1,0,0,1,0,1}}
};
318 ArtificialIntelligence
/*Clusters*/
vector_tpvec[MAX_CLUSTERS];
/*Algorithmconstants*/
#defineBETA ((double)8.0)
#defineVIGILANCE ((double)0.2)
Let’snowhavealookatthesupportfunctionsforART-1,whichinclude
creatinganewcluster,addingafeaturevectortoacluster,andrecomputing
thevectorforacluster.
Creatinganewclusterinvolvesfindingaclusterthat’sempty,andthen
adding the defined feature vector to it. The feature vector’s class is then
adjustedfortheclusterandthecluster’scountset(seeListing9.16).The
routine begins by searching for a cluster that has no members. If one is
notfound,thefeaturevectorremainsunclustered(classsetto-1)andthe
functionreturns.Otherwise,theclustervectoriscopiedfromthefeature
vector,andthefeaturevectorclassandclustercountareinitialized.
Listing9.16: Creatinganewclusterwithcreate_new_cluster.
voidcreate_new_cluster(intvector)
{
intcluster,i;
/*Findanemptycluster*/
for(cluster=0;cluster<MAX_CLUSTERS;cluster++){
if(pvec[cluster].u.count==0)break;
}
/*Noclusteravailable--unclassified*/
if(cluster==MAX_CLUSTERS){
fvec[vector].u.class=-1;
return;
}
/*Setthefeaturevector’sclasstothisnewcluster*/
fvec[vector].u.class=cluster;
/*Copythefeaturevectortothecluster*/
for(i=0;i<MAX_FEATURES;i++){
pvec[cluster].features[i]=fvec[vector].features[i];
}
pvec[cluster].u.count=1;
return;
}
NeuralNetworksII 319
Addingafeaturevectortoanexistingcluster(onewithexistingmembers)
isachievedwiththefunctionadd_to_cluster.Thefirstthingthisfunction
mustdoisdetermineifthefeaturevectorisbeingremovedfromanother
cluster(iftheclassisnot-1).Ifanotherclusterislosingthisfeaturevector,
thenwemustresettheclassforthefeaturevectorandthenmakeacallto
recompute_clustertoredefinethecluster’svector.Then,weaddthefeature
vectortotheintendedclusterandrecomputeitsvector(withanothercall
torecompute_cluster).
Listing9.17: Addingafeaturevectortoaclusterwithadd_to_cluster.
voidadd_to_cluster(intcluster,intvector)
{
intold_cluster;
/*Iffeaturevectorhadpreviouslybeenclustered*/
if(fvec[vector].u.class!=-1){
old_cluster=fvec[vector].u.class;
fvec[vector].u.class=-1;
pvec[old_cluster].u.count--;
recompute_cluster(old_cluster);
}
/*Addthefeaturevectortothenewcluster*/
fvec[vector].u.class=cluster;
recompute_cluster(cluster);
pvec[cluster].u.count++;
return;
}
Nowlet’sbringitalltogetherwiththeART-1mainloop.Thisfunction,
art1, implements the fundamental ART-1 algorithm, using the previously
discussedvectorsupportfunctions.Weiteratethrougheachofthefeature
vectors,lookingforthenearestcluster.Thisbeginswiththesimilaritytest
(perEq9.5),whichisfollowedbythevigilancetest(Eq9.6).Ifthefeature
vectorisneartheclusterandpassesthevigilancetest,thenthefeaturevector
isaddedtothecluster(throughacalltoadd_to_cluster).Ifthefeaturevector
isnotclusteredaftercheckingalloftheavailableclusters,thenanewcluster
iscreatedforitusingcreate_new_cluster.Thisprocesscontinuesuntilno
changestoclustersaremade.
320 ArtificialIntelligence
Listing9.18: TheART-1mainloop(art1).
voidart1()
{
intdone=0,cluster,i,clustered;
vector_tresult;
doublemax,sim,res_magnitude,fvec_magnitude,pvec_magnitude;
while(!done){
done=1;
/*Iteratethrougheachoftheprototypefeaturevectors*/
for(i=0;i<MAX_FEATURE_VECTORS;i++){
clustered=0;
/*Iteratethrougheachoftheactiveclusters*/
for(cluster=0;cluster<MAX_CLUSTERS;cluster++){
/*Skipemptyclusters*/
if(pvec[cluster].u.count==0)continue;
/*Maskthefeaturevectorwiththeclustervector*/
vectorAnd(&result,&fvec[i],&pvec[cluster]);
res_magnitude=(double)vectorMagnitude(&result);
fvec_magnitude=(double)vectorMagnitude(&fvec[i]);
pvec_magnitude=(double)vectorMagnitude(&pvec[cluster]);
max=res_magnitude/(BETA+fvec_magnitude);
sim=pvec_magnitude/(BETA+(double)MAX_FEATURE_VECTORS
);
/*Testsimilarityoffeaturevectortocluster*/
/*Equation9.5*/
if(max>sim){
/*Featurevectorissufficientlysimilartocluster.Next,test
*forvigilanceacceptability(Equation9.6).
*/
if((res_magnitude/pvec_magnitude)>=VIGILANCE){
if(fvec[i].u.class!=cluster){
add_to_cluster(cluster,i);
done=0;
}
clustered=1;
break;
}
}
}/*clustersloop*/
NeuralNetworksII 321
if(!clustered){
/*Addtoanemptycluster*/
create_new_cluster(i);
done=0;
}
}/*vectorsloop*/
}
return;
}
Let’snowhavealookatART-1inaction.Listing9.19showstheresult
of clustering the animal feature vector data shown in Listing 9.15 (with
thefeaturecolumnsshowninFigure9.12).What’sshownisareasonable
clusteringofthedataintofiveclasses.Class0representsanimalsthatfly
whileClass1containsallanimalsthatarecarnivores.Class2includesall
four-leggedanimalsandClass3hasasinglemember(salmon),representing
fish.Finally,Class4representsnon-flyingbirds,buthaveclusteredbasedon
theirhavingfeathersandlayingeggs.
Listing9.19: SampleoutputofART-1clustering.
$./art1.exe
Class0contains:
0[1100001001100]Robin
6[0110001001010]Bat
Class1contains:
1[0001100100100]Spider
8[0000000100101]Snake
9[0010010100010]Lion
11[0000000100011]Dolphin
16[0010010100010]Tiger
17[0010010100100]Platypus
18[0000100100101]Octopus
Class2contains:
2[0010010001010]Cat
4[0010010001010]Mouse
5[0001010010010]Moose
7[0010010001010]Dog
10[0000010001101]Iguana
12[0010010010010]Zebra
322 ArtificialIntelligence
13[0010010010010]Horse
Class3contains:
3[0000000001101]Salmon
Class4contains:
14[1000001010100]Ostrich
15[1000001000100]Penguin
Note that these clusters were based on a β of 8.0 and a ρ of 0.2. By
decreasingthevigilanceparameter,wecouldclassifythefeaturedatainto
fewerclusters.
TheART-1algorithmisausefulclusteringalgorithmwiththeobvious
advantageoverk-Meansinthatnewclusterscanbecreatedifthefeature
datarequiresit.ART-1canalsobetunedusingtheB(“tie-breaker”)and
p (vigilance) parameters. Regardless of these settings, ART-1 is stable in
thatoncetheclustershaveformed,performingadditionaliterationsofthe
algorithmonthesamedatawillnotchangetheclusters.
HOPFIELDAUTO-ASSOCIATIVEMODEL
As our final example of unsupervised learning, let’s explore the Hopfield
auto-associativemodelforpatternrecall.Recallthediscussionofasimple
auto-associatorearlyinthischapterforHebbianlearning.
Anauto-associativenetworkhastheabilitytostorethesetoftraining
examplessothattheycanberecalledlater.Additionally,ifincompleteor
noisyinputpattersareprovided,theauto-associativemodelcanrecallthe
originalpattern(ormemory),makingthemoperateasaContent-Addressable
Memory(orCAM).
FIGURE9.13: TheHopfieldauto-associativenetworkusesarecurrentweightstructure.
NeuralNetworksII 323
We’ll focus on the Hopfield model here, which is a recurrent neural
networkwithdiscreteinputsandactivations(inthedomain{-1,1}).Every
inputconnectstoeveryotherinput,butself-connectionsarenotpermitted.
Additionally,theinputsandoutputsarethesamecells(seeFigure9.13).
Early learning algorithms for auto-associators were commonly called
one-shotlearningalgorithms.Usingthisalgorithm,thetrainingexamplesare
usedoncetogeneratetheweightsforthenetwork,ratherthantuningthe
weightsbyiteratingthroughtheexamplesmultipletimes.One-shotlearning
isbothsimple,andalsoveryfast.
HopfieldAuto-AssociatorAlgorithm
Building a simple Hopfield auto-associator is quite simple, and the
algorithm is very straightforward. The first step is the creation of the
weightsfortherecurrentneuralnetwork.Thisisdonebysummingthe
outerproductsofeachtrainingexamplethatistobe“memorized”bythe
network(seeEq9.8).
(Eq9.8)
Duringrecall,eachcelloftherecallvectorisasumoftheproductsofthe
currentinputandassociatedweights(seeEq9.9).Further,theoutputisbound
tothediscreterangeof{-1,1}usingthesignfunction(1ifSi>=0,-1ifSi<0).
(Eq9.9)
ThepatternrecallYcanthereforebedefinedasasimplematrixproduct
ofthetest(example)vectorE,andtheweightmatrixW(seeEq9.10).
(Eq9.10)
HopfieldImplementation
Let’sbeginwithadiscussionofthenetworkrepresentationandtheessential
typesthatareusedtoimplementtheHopfieldalgorithm(seeListing9.20).
We’llusea9by9matrixforpatternsintheHopfieldimplementation,but
representthemasaone-dimensionalvectorofsizeN.Aswe’reimplementing
the discrete model of Hopfield, a two-dimensional int array is used to
representtheweights.Theweightsarraycanbeviewedasthefirstdimension
representingthesourcevectorindex,andtheseconddimensionrepresenting
theweightindextothealternatecells.
Thetypeexample_tisusedtorepresenttheexamples,inputsvector,and
outputsvector.Recallthatwe’reimplementingsymmetricupdates,sowe’ll
maintainseparateinputandoutputvectors.Asingleexampleisshownin
Listing9.20,whichdemonstratestherepresentationofthetrainingvector.
Listing9.20: FundamentaltypesandsymbolicsforHopfieldimplementation.
#defineM 9
#defineN (M*M)
#defineMAX_EXAMPLES 4
typedefintexample_t[N];
intweights[N][N];
example_tinputs;
example_toutputs;
#defineSGN(x) ((x)>=0?1:-1)
example_texamples[MAX_EXAMPLES]={
/*Plus*/{1,1,1,1,-1,1,1,1,1,
NeuralNetworksII 325
1,1,1,1,-1,1,1,1,1,
1,1,1,1,-1,1,1,1,1,
1,1,1,1,-1,1,1,1,1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,
1,1,1,1,-1,1,1,1,1,
1,1,1,1,-1,1,1,1,1,
1,1,1,1,-1,1,1,1,1,
1,1,1,1,-1,1,1,1,1},
...
};
ThefirststepintheHopfieldalgorithmisthetrainingoftheweights
usingtheexampletrainingdata(Listing9.21).Weiteratethrougheachofthe
trainingexamples,summingtheirouterproductstoproduceanewmatrixof
connectionweights.Notethatselfconnectionsarenotpermitted,soazero
diagonalwillbepresentintheweightmatrix.
Listing9.21: Generatingtheweightsarrayusingtheexamplevectors.
voidgenerate_weights_from_examples(void)
{
inte,r,c;
/*First,cleartheweights*/
for(r=0;r<N;r++){
for(c=0;c<N;c++){
weights[r][c]=0;
}
}
/*Equation9.8*/
for(e=0;e<MAX_EXAMPLES;e++){
for(r=0;r<N;r++){
for(c=0;c<N;c++){
/*Don’tpermitself-connections*/
if(r==c)continue;
weights[r][c]+=examples[e][r]*examples[e][c];
}
}
}
return;
}
326 ArtificialIntelligence
Listing9.22showsthefunctionforcomputingthenetworkactivations.
Thisverysimplyisamatrixmultiplicationoftheinputmatrix(1by81)by
theconnectionweightmatrix(81by81)resultingintheoutputmatrix(81
by1).Wethenscaletheactivations(outputmatrix)usingtheSGNfunction
toboundittothediscreteoutputvalues(-1and1).
Listing9.22 Computingtheoutputactivationsforagiveninputvector.
voidcompute_activations(void)
{
intr,c;
inttemp[N][N];
bzero((void*)temp,sizeof(temp));
for(r=0;r<N;r++){
for(c=0;c<N;c++){
if(r==c)continue;
temp[r][c]+=inputs[r]*weights[r][c];
}
}
for(c=0;c<N;c++){
outputs[c]=0;
for(r=0;r<N;r++){
outputs[c]+=temp[r][c];
}
outputs[c]=SGN(outputs[c]);
}
return;
}
Using the Hopfield network model for training and recall (using the
functionsdiscussedhere)isthenasimplelinearprocess(seeListing9.23).
Itbeginswiththegenerationoftheconnectionweightmatrix(generate_
weights_from_examples).Next,wetakeatrainingexampleandcopyitto
theinputsmatrix,withsomeamountofnoise(usingset_inputs_to_example).
Finally,wecomputetheoutputactivationsusingcompute_activationsand
thenemittheresultingoutputmatrixwithemit_result.
Listing9.23 UsingtheHopfieldnetworkmodel.
generate_weights_from_examples();
set_inputs_to_example(e,noise);
NeuralNetworksII 327
FIGURE9.14: SampleactivationsfortheHopfieldmodelimplementation.
compute_activations();
emit_result(outputs);
Let’snowlookattheHopfieldnetworkmodelinaction.We’llusea9x9
matrix to represent the input and output matrices. The implementation
supportsfourdifferentinputpatternswithincreasingamountsofnoiseto
test the recall capabilities of the network. Figure 9.14 demonstrates one
instanceofpatternrecallforeachofthefourpatterns.Ontheleftsideare
theinputspatterns(samplepatternwithupto20%noise)andontheright
istheactivationoftheHopfieldnetwork(theoriginalinputpattern).
NotethesimilaritiesbetweenHopfield’smodelandtheHebbianlearning
model. Weight connections are strengthened when the example cells are
similarinsign,andareweakenedwhenthesignsofthecellsaredifferent.
CHAPTERSUMMARY
Thischapterexploredavarietyofunsupervisedneuralnetworkarchitectures
andlearningalgorithms.Unsupervisedlearningalgorithmsareusefultodiscover
theunderlyingstructureofadataset.Webeganwithabiologicallyplausible
learningmethodforpatternscalledHebbianLearninganddemonstratedits
patternstorageandrecallcapabilities.Next,weexploredanumberofclustering
algorithmsthatwerecapableofsegregatingdatabasedontheirsimilarities
anddifferences.Algorithmsexploredincludedvectorquantization,k-Means
clustering,andAdaptiveResonanceTheory(ART-1).Finally,weendedthe
328 ArtificialIntelligence
chapterwithareturntopatternstorageandrecallwithadiscussionofthe
Hopfieldauto-associator(anotherbiologicallyplausiblemodel).
REFERENCES
[Gallant 1994] Gallant, Stephen I. Neural Network Learning and Expert
Systems.TheMITPress,Cambridge,Massachusetts,1994.
[Hebb1949]Hebb,D.O.,TheOrganizationofBehavior:ANeuropsychological
Theory.Wiley,NewYork,1949.
EXERCISES
1. Explainsomeoftheusesofunsupervisedlearningalgorithms.
2. DefinethefundamentalideabehindHebbianlearning.
3. UsingtheHebbruleimplementationontheCD-ROM,experimentwith
twodifferentdatasets.Inthefirst,usepatternsthataresimilar,andin
thesecondusepatternsthatareverydifferent.Howdoesrecalldiffer
inthepresenceofnoiseorincompleteinputpatterns?
4. In a typical winner-takes-all network, the largest activation is used as
the proper classification. What is the relevance of using the smallest
activationinvectorquantization?
5. Define the type of neural network used for vector quantization, and
explainthelearningalgorithm.
6. Whatisaprimarydisadvantageofvectorquantization?
7. Describethefundamentalideasbehindk-Meansclusteringandtheuse
ofcentroidsforclusterrepresentation.
8. Definetheprimaryadvantageanddisadvantageofthek-Meansclustering
algorithm.
9. Whataretheissuesfork-Meansclusterinitialization,andinwhatways
coulditbeimproved?
10. What is meant by the terms plastic and stable for a clustering
algorithm?
11. DescribethebasicprocessoftheART-1algorithm.
12. WhatisthepurposeofthevigilancetestintheART-1algorithm?
13. DescribethepurposeoftheBetaandRhoparametersforART-1.
14. TheHopfieldauto-associativemodelisusefulforbiologicallyplausiblememory
storageandrecall.Describeitsarchitectureandlearningalgorithm.
15. Whatisone-shotlearningandwhatareitsadvantages?
16. BothHebbianlearningandHopfieldcanbeusedforpatternstorage
and recall. Describe the fundamental differences between these two
approaches.
Chapter 10 ROBOTICSANDAI
F
rom the early days of AI, robots have existed as the physical
embodiment of AI algorithms and methods. Outside of the
realm of simulations, robots allowed AI algorithms to interact
and explore the real world. Robotics also fit the mold of early Strong
AI,wherethedesirewastocreateanintelligentmachine(ideallyinour
image).Inthischapter,we’llexplorethefundamentalsofroboticsand
itsapplicationofAI.
INTRODUCTIONTOROBOTICS
While AI and robotics can be considered two independent disciplines,
they are inexorably tied given the benefits they provide one another. AI
provides the means to embed intelligence into a physical robot, where a
robotprovidesthemeanstovisualizethebehaviorthatisprovidedbyanAI
algorithm.Therefore,thetwodisciplinescomplementoneanother.
This chapter will begin with an introduction to robotics, its history,
and then a short discussion of a taxonomy of robotics. The chapter will
thenexploreroboticsingreaterdetail,reviewingthevariousinterfacesand
architectures.
330 ArtificialIntelligence
WhatisaRobot?
ISO(InternationalStandard)8373definesarobotthisway:
Anautomaticallycontrolled,reprogrammable,multipurpose,manipulator
programmableinthreeormoreaxes,whichmaybeeitherfixedinplaceor
mobileforuseinindustrialautomationapplications.
Butthisolderdefinitioncanunfairlyconstrainthedefinitionofarobot.
JosepthEngelberger,apioneerinearlyindustrialrobotics,definesarobot
asfollows:
Ican’tdefinearobot,butIknowonewhenIseeone.
Likeart,thisdefinitionfitsroboticsthebestasmodern-dayrobotsexist
asawidespectrumofdesignstofitagivenneed.Earlyrobotswereviewed
asfantasticalentities,builtoftinandrepresentedbyevilintentions.The
realisticviewislesshysterical.Thefirstrobotwasatele-operatedboatthat
wasdemonstratedatan1898exhibitionbyNikolaTesla(withtheendgoal
beingawirelesstorpedo).[WikiRobot]ThetermRobotwouldnotbecoined
foranother20years,whenitwasusedinKarelCapek’ssciencefictionplay,
“Rossum’sUniversalRobots.”
Anotherwaytodefinearobotisthroughitsdecomposition.Arobotis
anentitythatsatisfiessomegoalandismadeupofaplatform(consistingof
someformofmechanicals),asourceofpower(stored,orgatheredinreal-
time),asetofsensorstoperceivetheenvironment,asetofactuatorstomove
and/ormanipulatetheenvironment,andfinally,asourceofintelligencefor
rationaldecisionmaking(seeFigure10.1).
Usingamodelofdecompositiontodefinearobotpermitsthebreakdown
ofthecomponentstoidentifyhowandwheretheyareapplied.Aswe’llsee,
everyrobotisdifferentbasedonitsapplication.Themechanicals,sensors,
andactuatorscanbeverydifferentfromasatellite(aformofanautonomous
robot)whencomparedtoanautonomousunderwaterrobot.Whatcanbe
verysimilaristheAIalgorithmsappliedtotheproblem.
In the following sections, we’ll explore these components in greater
detailtounderstandhowtheintelligenceblockinteractswiththevarious
othercomponents.
FIGURE10.1: Arobotfromtheperspectiveofdecomposition.
RoboticsandAI 331
ASamplingfromtheSpectrumofRobotics
Aconcisehistoryofroboticsisbeyondthescopeofthisbook,butasummary
ofsomeofthemoreimportantepochsinrobotichistorywillbepresented.
Thefirstmobilerobotthatincorporatedartificialintelligencealgorithms
wascalledShakeyandwasbuiltfrom1966through1972atSRI(theStanford
ResearchInstitute).ShakeywasamobilerobotthatincludedaTVcamera,
range finder, and bump sensors to sense its environment. The Shakey
platform was connected to a PDP-10 and PDP-15 computer via a radio
link.Shakey’sAIwasbuiltonthreelevelsofaction.Atthebottomwerethe
basicactionroutines(formoving,turning,andbasicrouteplanning).Atthe
intermediatelayerwascontrolthatlinkedlower-levelactivitiestogetherto
createmorecomplextasks.Atthehighestlevelwastheplannerthatcould
executeplanstoachievegoals.Later,inthe1970satStanford,HansMoravec
built a mobile robot cart that included a very robust vision system. This
providedtherobotwithgreaterautonomyandresultedinsomeofthefirst
3Denvironmentmappingexperiments.
Robotsintheearlydaysweren’trestrictedtotheground.Someobjects
representingrobotstookflighttotheMoon,Venus,Mars,andbeyond.While
SputnikwasthefirstartificialEarthsatellite,ithadacoupleofsensorsbutno
actuators(exceptforatransmitterthatgenerated“beeps”forthoselistening
viaradioontheground).UsingourmodelshowninFigure10.1,Sputnik
lackedonlyinintelligence.
Other important robotic spacecraft included Luna 2, a Soviet Lunar
impacter (1959), and Venera 7. Venera 7 was a Soviet spacecraft that
succeededinthefirstsoftlandingonanotherplanet(Venus)in1970.The
U.S.followedsixyearslaterwiththeViking-1Marslander(andwhilenot
mobile,itdidincludearoboticarmforsoilsampling).
NOTE Someexamplesofrobotsthatareoperatedremotelyarecalledtele-robots.
Thesetypesofrobotsareusefulwheretherobotcansurvive(atleastfor
atime)whereahumanwouldnot.Aprimeexampleisonthesurfaceof
Mars,suchasViking1.
AnothertypeofrobotconstructedatMITwascalledKismet(developed
by Cynthia Breazeal). Kismet is another non-traditional robot in that it’s
simply a head, but includes a variety of input devices (audio, video and
proprioception) as well as effectors to enable facial expressions (such as
movingitsears,eyebrows,eyelids,lips,andhead).ThegoalofKismetwas
tostudyhumansocialinteraction.Kismetcouldsimulatehumanbehaviors,
demonstrating(simulated)humanemotionandappearancethroughfacial
332 ArtificialIntelligence
expressionsandvocalizations.Aninterestingsideeffectofthisresearchwas
thatthesecharacteristicsgavetherobotapersonality.
NOTE Newresearchfocusesonemotionsandtheirinfluenceoverintelligence
and learning. It’s understandable that emotions would be helpful
in building machines that can more easily interact with humans.
Emotions can be problematic (imagine a paranoid AI auto-pilot),
butnotallemotionsareirrationalandemotionscanresultinamore
rationalrobot.
Inmorerecenthistory,Hondademonstratedaveryadvancedhumanoid
robotinYokohama,JapancalledAsimo.Asimoisahumanoidrobot,meaning
thatitresemblesahumanwithlegsandarms.Asimowasdesignedforreal-
lifeenvironments,andcangraspobjectsandwalkaroundanenvironment
(including the use of stairs). This robot incorporates a variety of human-
likesensorsincludingvisionandhearing,andisabletorecognizeobjects,
gesturesfromhumans,andevenrecognizefacesandsounds.Forexample,
Asimocanunderstandpointinggesturesforattentionormovement.
Finally,theDARPAgrandchallenge,acompetitiontobuildautonomous
groundvehiclesthatracebetweentwolocationsindifficultterrain,continues
topushtheenvelopeforautonomousrobotics.Thefirstchallenge,in2004,
sawmanyorganizedteams,butnotasinglerobotfinishedthecourse.By
2005,fivevehicleshadcompletedthedifficultcourse(ledbyStanfordand
CarnegieMellon).
NOTE Combining AI and vehicles is not new; in fact, Carnegie Mellon
introducedALVINN(orAutonomousLandVehicleInaNeuralNetwork)
inthe1990s.A30by32two-dimensionalretina(usinginputfromavideo
camera)fedafive-layerneuralnetwork(trainedviabackpropagation).
The output of the neural network determined the direction that the
vehicle should travel. Using this method, the vehicle controlled by
ALVINNcouldautonomouslydriveandstayonaroad.
TaxonomyofRobotics
While modern robotics still fails to live up to the expectations set by
sciencefiction,roboticshasgrownfromtheearlymobilecartsandrobotic
appendages.Today,robotsnotonlynavigateonland,butalsointheairand
inthewater.Inthissection,ashorttaxonomyofroboticswillbepresented,
listingsomeofthevarioustypesofrobotsthathavebeendevelopedorare
underactiveresearch.
RoboticsandAI 333
Fixed
Manyoftheindustrialrobotsinusetodayareofthefixedvariety.Robotic
armsperformassemblyandweldingtoveryprecisespecifications,repeatedly
withoutmistakes.Theseroboticarmsincludeamanipulatorortoolattheir
extremityandoneormorejointstogivethearmtheabilitytomovearound
initsspace.
Legged
Theleggedvarietyofrobotcanalsobecalledthewalkingorhoppingstyle
ofrobot.Robotswithoneleg(calledmonopods,suchastheonebuiltatthe
MITLegLaboratory)hopinastylesimilartoapogostick.Bipedalrobots
(thosewithtwolegs)mimichumanlocomotion(suchasAsimo).Butmore
legsarealsointeresting,suchasquadrupeds(fourlegs),hexapods(sixlegs),
andoctapods(eightlegs).
Wheeled
A popular variety of robot given its simplicity is the mobile, or wheeled
robot.Hobbyistroboticplatformsarecommonlyofthisdesignduenotjust
toitssimplicity,buttheminimalcost.Thecontrolofthemobileplatformis
relativelysimplewhencomparedtoaleggedvarietyofrobot.Forexample,a
mobilerobotisstablewithoutcontrol,wherealeggedrobotmustdynamically
balancetobestable.
Underwater
Underwaterrobotsareveryuseful,butrequiredifferentformsoflocomotion.
Many tele-robots of this variety are called ROVs, or Remotely Operated
Vehicles. These can take a number of forms, mimicking what nature has
taughtusaboutnavigatinginwater.Notonlyhaveunderwaterrobotsbeen
demonstratedthatmimicfish,butalsocrabandlobsterstructures.Robotic
wormshavealsobeenusedtoprovidelocomotion.
Aerial
Robots of the aerial variety, like underwater robots, have a number of
different mechanisms for movement. For example, traditional aerial
robotscanusecommonaircraftmodels(suchasaplaneorheliocopter).
Evenapassengerairlinercanbeconsideredaflyingrobot,ashumans
typicallyattendtotheoperationoftheplanebuttheauto-pilot(aform
of AI). [WikiRobot] Satellites are another example of an aerial robot,
thoughtheyhaveanentirelydifferentsetofsensorsandactuators(sun
sensors, gyroscopes for sensing, momentum wheels, and thrusters for
effectors).
334 ArtificialIntelligence
OtherTypesofRobots
Finally,thereareanumberofothertypesofrobotswhichdon’tfitcleanly
withinthepreviouslydefinedcategories.Thisisbecausetheycouldapply
equallytotheotherrobottypes.
Polymorphic, or shape-changing, robots have the ability to self-
reconfigurebasedontheparticulartaskathand.Theserobotsaremulti-
functionalandcantackleavarietyoftasksbyreconfiguringthemselves.The
robot’sdesignindicateshowpolymorphicitis.TheSuperBot,designedat
theUSCPolymorphicRoboticsLab,isanearlyexampleofareconfigurable
robot.Thisrobotcanreconfigureitselftowalk,crawl,roll,orclimb.
Distributedrobots,orrobotswarms,areanotherinterestingaspectof
robotics.Inthismodel,multiplerobotsswarmtogethertoperformatask.
Thiscanbeviewedserially(suchasantscooperatingtomovefoodfroma
locationbacktothenest)oratthesametime(suchasworkingtogetherto
move an object that an individual robot could not move). Robot swarms
are an interesting area of research today, including topics such as task
distribution and communication. How do individual robots communicate
with one another, and how are assignments made in the absence of a
centralizedplanner?
HardvsSoftRobotics
Whilemostrobotsarehardinnature(madephysicallyintherealworld),
there is another category of robots that don’t exist physically. These are
called soft robots and exist within the confines of computer systems. But
insteadofrobotsimulations,thesesoftrobotsincludeasoftsetofsensors
andeffectorsthatallowthemtointeractandnavigatetheirenvironments.
Forexample,asoftrobot(calledawebspider)cannavigatetheInternet
usingtheHTTPprotocoltocommunicatewithservicesonagivenmachine.
Using advanced mobility protocols, agents can move around a network,
transportingthemselvesfromonemachinetoanotherbringingtheirstate
with them. An example of this is the Aglets framework from IBM that
provides a Java framework for agent mobility using the Agent Transport
Protocol(orATP).
BRAITENBURGVEHICLES
Adiscussionofroboticswouldnotbecompletewithoutatleastmentioning
what are now known as Braitenberg vehicles. Valentino Braitenberg was
a cybernetician, someone who studied the processes of biological and
RoboticsandAI 335
mechanical(andelectronic)systems,particularlyinthecontextofcomparing
themtonaturalorbiologicalsystems.Braitenberg’sbook“Vehicles”remains
aworthwhileread.
Braitenbergvehiclescanbethoughtofasasetofthoughtexperiments
forroboticsystemswithsimplebuteffectivecontrolsystems.Asanexample,
take the simple robot shown in Figure 10.2. This two-wheeled robotic
platform (with a front castor wheel) includes two light sensors (the left
sensorandrightsensor)atthefrontthatdirectlyattachtothemotorson
thewheels(theleftmotorandrightmotor).Whenlightshinesinasensor,it
proportionallydrivesthemotortowhichitattaches.Whenthelightshines
intheleftsensor,itdrivestherightmotormovingittowardthelightsensor.
Ifthelightshinestotherightsensor,theleftmotorisdriven,causingthe
leftmotortoturn(thuscausingtherobottotravelrighttowardthelight).
Thissimplecontrolsystemdemonstratesanattractivebehavior(therobot
isattractedtowardthelight).
An alternative robotic architecture is shown in Figure 10.3. In this
example the sensors attach to the same side wheel motors. This has the
oppositeeffect,inthatlightshiningin(forexample)theleftsensordrivesthe
leftmotorcausingtherobottoturnawayfromthelightsource.Thissimilar
behavioroccursfortherightsensorsandrightmotor.Thisisanexampleof
repulsivebehavior,therobotmovesawayfromthelightsource.
Lightshininginbothmotorshasaproportionaleffect,butstillleaning
toward a strong effect to the sensor to which a stronger light signal is
received. From these simple examples, you can see how a very simple
architecturecanyieldveryinterestingbehaviors.
NATURALSENSINGANDCONTROL
Before we talk about the various robotic sensors, actuators, and control
systems, it’s important to explore the senses, effectors, and control
mechanismsthatweashumansuse.
Fromthehumanperspective,therearefiveclassicalsenses(seeTable
10.1).Thesearethesenseofsight(vision),hearing(audition),taste(gustation),
smell(olfaction),andtouch(tactition).Thesebasicsensesgiveustheability
toperceivetheworldaroundus.Therearealsoothersensesatwork.For
example,humanshavetheabilityofproprioception,orkinetheticsense.
Thissenseallowsustobeawareofourselvesandourbodies.Humans
alsohavetheabilityofbalance,orequilibrioception,andtheabilitytosense
heatorcoldthroughthermoception.
Table10.1: Fiveclassicalhumansenses.
Sense Sensor Description
Vision Eyes TheSenseofSight(ElectromagneticWaves)
Auditon Ears TheSenseofHearing(PressureWaves)
Gustation Tounge TheSenseofTaste(Chemicals)
Olfaction Nose TheSenseofSmell(Chemicals)
Tactition Skin TheSenseofTouch(Pressure)
Buttherearealsosensesthatarenotavailabletohumans,butexistin
otherorganisms(seeTable10.2).Forexample,someanimalshavetheability
to sense infrared light. Bats, for example, use echolocation to determine
thepositionofobjects.Thedetectionofelectricfields(electroception)and
fluctuationsinmagneticfields(magnetoception)isalsoavailabletocertain
animals(suchasfish,bees,andbirds).
Table10.2: Examplesofnon-humansenses.
Sense Description
Echolocation DetectionofObjectsthroughecho
Electroception DetectionofElectricFields
Magnetoception DetectionofFluctuationofMagneticFields
While many robots include senses modeled after humans, it’s not
necessarilyadvantageous.Thecomplexityofsomehumansenses(suchas
vision)canbesupportedbyothersimplermechanismssuchastouchsensors
andultrasonicdetectors(forobjectlocation).
RoboticsandAI 337
Humansalsoincludeavarietyofeffectorsthatareusedtomanipulatethe
environment.Inadditiontotheobviousmechanismssuchasourarmsand
handstomanipulateobjects,orlegswhichprovideuswithmobility,humans
havetheabilitytospeaktocommunicatewithothersinourenvironments.
There are also effectors which are hidden, such those embodied in non-
verbalcommunication.Thesemechanismspermitcommunicationwhichis
indirect(andmanytimes,unintentional).
PERCEPTIONWITHSENSORS
Thesensorsavailabletoroboticsystemsareopentotheimaginationandthe
needsoftheroboticapplication.Artificialsensorscanmimicthoseofhuman
sensors,butextendtheminwaysthatareuseful.Forexample,roboticeyes
canseeoutsideofthehumanvisualrangeandseeheat.Roboticearscan
amplifyorhearbeyondthehumanfrequencyrange.SeeTable10.3foralist
ofsensorsandtheirhumancounterparts.
Table10.3: Roboiticsensorsandthehumancounterparts.
HumanSense RoboticSensors
Vision Camera,InfraredCamera,RadiationSensors,Ranging
Sensors
Audition Microphone
Gustation ChemicalSensors
Olfaction ChemicalSensors
Tactition Contact(bump)Sensors,ForceSensors
Proprioception WheelEncoders
Equilibrioception TiltSensor,Accelerometer,Gyroscope
Thermoception Thermocouple
RobotscanincludevisioncapabilitiesusingsimpleCCD(Charge-
Couple-Device) cameras, or video cameras. Sonar is also possible,
which provides a lesser form of vision (identifying when objects are
near).Camerascanalsoincludeinfraredforheatdetection,orsensors
thatdetectradiation(“seeing”inotherspectrum).Ametaldetectorcan
alsobeviewedasaformofvision(thoughobviouslydifferentthanour
own). Audition can be performed using microphones, but not only in
thehumanaudiblespectrum.Higherorlowerfrequenciescanalsobe
measured.
338 ArtificialIntelligence
Thesenseofgustationandolfactioncanbeprovidedthroughanarrayof
chemicalsensors.Thesesensorscanbeusedtodetectbiological,chemicals
orradiation.Tactitionisoneofthemorecommonsensorsavailableonrobots,
suchascontactsensors.
Other robotic sensors can be used for the other senses, such as
wheelencoders(tomeasuretherateatwhichthewheelsareturning)for
proprioception. The sense of equilibrioception can be provided by tilt-
sensors,acceleratometers,oragyroscope.Finally,athermocouplecanbe
usedtodetectheattoprovidethermoception.
ACTUATIONWITHEFFECTORS
Theeffectors(oractuators)availabletoarobotarevaried,anddependon
thegoaloftherobotitself.Someofthemorecommonincludemotorsthat
canbeusedtodrivewheelsorend-effectors,whichconsistofadeviceat
theendofaroboticarm(suchasagripper,apaintsprayer,orothertool).
Likesensors,actuatorsaredrivenbasedonthetaskathandandareopen
totheimagination.
ROBOTICCONTROLSYSTEMS
Asdefinedearlierinthischapter,arobotismadeupofanumberofthings,
butthreekeyelementsarethesensors,effectors,andthecontrolsystem.
Thesensorsallowtherobottoperceiveitsenvironment,theeffectorsthe
ability to manipulate its environment, and the control system provides a
waytomapsensors(theperceptionoftheenvironment)totheeffectors(to
rationallyinteractwiththeenvironment).ThisisshowninFigure10.4,and
isanimportantaspectofroboticcontrolsystems.
Another way to think about this is shown in Figure 10.5. The loop
aspectisimportantbecausetheactofcontrolshouldbeviewedasacycle,
startingwithperception,leadingtodecision-making,andendingincontrol.
Thisimpliesfeedback,whichisthebasisforcontrol.Iftherobotwereto
trytocontroltheenvironmentwithsensors,thiswouldbecalledanopen
loop(asthere’snofeedback,there’snowaytoguaranteeproperaction).By
closingtheloop(sense-control-act)feedbackisincorporated,resultinginan
effectivecontrolsystem.
Acommonfeedbackmechanisminroboticsistheshaftencoder.Ashaft
encodermeasurestherotationrateofashaft.Thisiscommonlydonewith
aphotoreflectorwhichreflectslightbacktoaphototransistor(thesensor).
Thetransitionsbetweenreflectivespotsandnon-relectivesurfacesresultin
apulsetrainthatcanbecounted(seeFigure10.6).Thecountercoupled
withareal-timeclockcanthenbeusedtodeterminetherateatwhichthe
shaft(andwheel)isturning.
Using the shaft encoder, the speed of the wheel (and corresponding
objectpropelledbythewheel)canbemeasured.Thisallowstherobotto
roughlydetermineitsrelativeposition.Byenablingthemotor,thecontrol
systemcouldcountoffeightpulsetransitionstomeasureacompleteturn
ofthewheel.Whilefundamental,thisshaftencoderexampleillustratesthe
concept of feedback in a control system. Without a shaft encoder, there
couldbenopracticalwaytounderstandhowfartherobottraveled.Thisis
anexampleofanopen-loopsystem(nofeedback).
SIMPLECONTROLARCHITECTURES
Roboticplatformscanusealargenumberofcontrolarchitecturestoachieve
their desired goals. In the following sections, a few of the more popular
architecturesareexplored.Whilethesearchitecturesareuseful,they’realso
conceptuallysimpleandeasytoimplement.
FIGURE10.6: Usingashaftencodertodeterminewheelspeed.
340 ArtificialIntelligence
FIGURE10.7: Thereactivecontrolsystemarchitecture.
Thekeytocontrolarchitecturesisthatnosinglearchitectureisbest.
Eachareapplicabletodifferentscenarios,andthearchitectureshouldmatch
theproblemathand.
ReactiveControl
Thereactivecontrolarchitectureisonewherethere’snorealintelligence.
Instead, the control system reacts to its sensors and drives its actuators
(simplestimulus-response).ThisarchitectureisshowninFigure10.7.Note
herethatthesensorsmaptotheeffectorsinadirectmapping.
In this architecture, the sensors are read and directly mapped to
the effectors. This differs from a deliberative architecture where some
considerationismadeaboutthesensorsandinternalstate.Thedeliberative
architectureisthemoreclassicalintelligentapproach,asconsiderationis
madeaboutthestateoftheenvironment(sensors)andthestateoftherobot
(internalstate).
Anadvantagetothisarchitectureisthatit’sverysimpleandveryfast.
Sincenodeliberationismadeaboutwhichactionshouldbeperformedbased
onsensorsinputs,thedirectmappingprovidesforafastresponsetime.This
architectureisalsoveryusefulindynamicenvironments.Sincetherobot
simplyrespondstotheimmediateenvironment,itcanreactequallywellto
staticordynamicenvironments.Thedisadvantagetothisarchitectureisthat
it’sfundamentallyunintelligentandhasnocapacitytolearn.
Subsumption
RodneyBrook’ssubsumptionarchitecturegrewfromthebeliefthathuman-
levelintelligencecouldnotbebuiltfromthegroundup,butinsteadmust
RoboticsandAI 341
be built from simple behaviors that collectively could exhibit emergent
higher-levelbehavior.
Buildingarobotthatusedsimplebehaviorswasnotnew,buttheproblem
thatexistedwashowtocoordinatethosesimplebehaviorssuchthattheright
behavior could be in control at the right time. Using insects as a model,
Brooksmovedforwardwithanarchitecturethathadnorealcentralcontrol
mechanism,butinsteadasharedcontrolarchitecturewherebehaviorswere
activeinparallel,butonlyoneincontrolatatime.
Thesubsumptionarchitectureexhibitsanumberofpropertiesthatmodel
aspectsofinsectsverywell.Thereexistprewiredbehaviors(asexploredin
thereactivearchitecture),butlayersofbehaviorswiththeabilitytosubsume
controloflowerbehaviorswhenappropriate.
Conside the control system shown in Figure 10.8. The sensors are
provided to the behaviors, and the effectors are driven by the behaviors.
Note the parallel nature of this processing. Each behavior module has
accesstothesensorinputsandeffectoroutputs.Butonlyonebehavioris
permittedcontrolatatime(byinhibitingexecutionofotherbehaviors).For
example,ifnoobjectsareintheareaofmovement,thentheupper-level
modulemaysubsumecontrol.Inthiscase,theexplorebehaviorwillhave
control.Butiftherobotislowonpower,thentheseekpowerbehaviorcan
subsumetheexplorebehavior.Whiletherobotisseekingpower,theobject
avoidancebehaviorcantakecontrolifneeded(ifanobjectisintheareaof
movement).
The subsumption architecture is advantageous over the reactive
architecture because it can incorporate more complex goal-oriented
behaviors.Eachlevelinthesubsumptionarchitecturecanbethoughtofas
FIGURE10.8: Thesubsumptioncontrolsystemarchitecture.
342 ArtificialIntelligence
FIGURE10.9: ApplyingotherAIalgorithmstorobotcontrolsystems.
OtherControlSystems
Throughoutthisbook,thereareanumberofalgorithmsandmethodsthat
would apply as a control architecture for a robot. These include neural
networks,classicalsearchalgorithms,orevolutionaryalgorithms.
MOVEMENTPLANNING
Planningisanintegralpartofintelligentrobotbehavior.Planningisoneof
thekeydifferentiatorsbetweenintelligenthumanbehavior,andseemingly
random insect behavior. In this section, two interesting approaches to
movementplanningwillbeexplored.
ComplexitiesofMotionPlanning
Oneofthemajorcomplexitiesofmotionplanningisinenvironmentsthat
are dynamic. Consider movement planning in an environment that does
notchange.Thenconsiderhowplanningdiffersinanenvironmentthatis
constantlychanging.Dynamicenvironmentsofferconsiderablechallenges
tomotionplanning.
RoboticsandAI 343
One approach to planning in dynamic environments is what is called
anytime planning. Traditional planning algorithms will create a complete
plan from start to finish. The problem that this creates is that the plan
becomesbrittleiftheunderlyingassumptionschange(suchasindynamic
environments).Anytimeplanningisanalgorithmthatcanbeinterrupted
duringtheplanningprocess,butstillresultinaworkableplan.Themore
timethatisgiventotheplanningalgorithm,thebettertheresultingplan.
Thisisusefulindynamicenvironmentsbecauseaplancanbecreated,and
whilethatplanisexecuted(andtheenvironmentchanges),theplannercan
continuetorefinetheplandynamicallygivennewinformation.
CellDecomposition
Planningarobot’smovementthroughanenvironmentcanbeaccomplished
throughanumberofmethods,andaconceptuallysimplymethodiscalled
CellDecomposition.Theideabehindcelldecompositionistodecompose
thefreespaceoftherobot’senvironmentintosmallerregionscalledcells.
Once the cells are defined, a graph can be created using the adjacency
informationfromthecells.Withthegraph,atraditionalsearchalgorithm
canbeusedtodetermineapathfromthestartcelltotheendcell.
An example of cell decomposition begins with Figure 10.10. In this
figure,asampleenvironmentisshownwithfreespace(clear)andobstacles
FIGURE10.10: Samplerobotenvironmentwithfreespaceandobstacles.
FIGURE10.11: Environmentdecomposedintocells.
344 ArtificialIntelligence
FIGURE10.12: AgraphrepresenationofFigure10.11.
FIGURE10.13: Potentialfieldexampleofagoalandobstacle.
(showningray).Attheupperleft-handcorneristhestartposition,andat
thebottomrightistheendgoal.
Next,thefreespaceintheenvironmentisdecomposedintocells.This
isdoneinthisexamplebydrawingparallellinesegmentsattheboundaryof
eachobstacle.Theboundedfreespaceisthenlabeledtouniquelyidentify
it(seeFigure10.11).
Then,usingtheadjacencyinformationfromthecelldecomposition,a
graphisconstructed(seeFigure10.12).Fromthisrepresentation,simple
graphalgorithmscanbeusedtoplanapathfromthestartposition(incell
A)tothegoalposition(incellL).
PotentialFields
Potentialfieldsisaveryefficientmethodtoproviderobotmovementinboth
staticanddynamicenvironments.Withthepotentialfieldmethod,therobot
isconsideredaparticlemovingthroughafieldcontainingasetofinfluences.
Theinfluencesarepotentialfieldsthatdefinehowtheparticleshouldbehave
wheninproximitytothem.
Consider the potential fields shown in Figure 10.13. The goal has a
potentialfieldthatattractstherobot,whiletheobstaclehasarepulsivefield
thatopposestherobot.
A robot then navigating an enviroment with other objects appears as
showninFigure10.14.Inthisdiagram,therobotavoidstheobstacle(asit
isrepulsedbyit)butthenmigratestowardthegoal(asitisattractedtoit).
RoboticsandAI 345
FIGURE10.14: Potentialfieldexamplewithanobstacleandgoal.
What makes potential fields so efficient is that at any instance in the
environment,themotionoftherobotisdeterminedbythepotentialfieldat
therobot’scurrentlocation.Ifanewobstacleisaddedtotheenvironment,
the potential field is adjusted given the new influence (additive), so the
approachcanalsoworkindynamicenvironments.
An issue with potential fields is the possibility of local minimums,
whichcanresultintherobotbeingstuckinaposition.Thisproblemcan
beovercomebyincludingmechanismstoescapethelocalminimumwhen
itisdetected.
TIP
Manyothertechniquesexistfornavigatingenvironments.Landmark-
based navigation uses easily recognizable landmarks to determine
position.Thistechniquehasbeenappliedtospacecraftusingstarfieldsas
therecognizablelandmarks.Usingfourwell-knownstars(basedontheir
brightness),aspacecraftcandetermineitsorientation.Othermethods
suchasvisibilitygraphsandVoronoidiagramsarealsouseful.
GROUPORDISTRIBUTEDROBOTICS
Group or distributed robotics is an interesting area of research today. In
this area, a group of robots accomplish a task that alone they could not
accomplish.Swarmsofidenticalrobotscanalsobeusedtocompletetasks
much faster than a single robot acting on its own. Applications include
agricultureandconstruction.
Animportantelementingrouproboticsistheirabilitytocommunicate
with one another to relay information about the task at hand. Examples
fromnatureincludestigmergy,inwhichinsectscommunicatethroughtheir
environment. Ants, for example, communicate with one another through
pheromonetrailsthataredepositedintheenvironment.Similarbehaviors
areseenintermiteswhoalsousepheromones,butintheircasetheyare
usedtoconstructnests.
346 ArtificialIntelligence
ROBOTPROGRAMMINGLANGUAGES
Tosimplifythetaskofprogrammingrobotsystems,andalsoforeducational
purposes,anumberofrobotprogramminglanguageshavebeendeveloped.
Examples include LAMA from MIT and the RAPT language from the
UniversityofEdinburgh.
From an educational perspective, a useful language developed by
RichardPattisofStanfordUniversityisKarel(namedaftertheCzechwriter
whocoinedthetermrobot).Karelisalanguagewithasimplesyntaxthatis
also reminiscent of LOGO (another educational programming language).
Karelhasbeenupdatedtoincludeobject-orientedsemantics,whichgoes
bythenameKarel++.
ROBOTSIMULATORS
Robotsimulationsofferanotherwaytotestandvalidateroboticalgorithms
withoutphysicaldevelopment.Thesealgorithmsalsoprovideawaytoverify
algorithmspriortoembeddingthemintophysicalembodiments.
SomeofthemoreinterestingincludetheOpenDynamicsEngine,which
isaphysicslibraryforsimulatingrigidbodydynamics.Simbadisa3Drobot
simulator that allows immediate visualization of programmed behaviors.
Finally,TeamBotsisaportablemulti-agentroboticsimulatorthatincludes
visualizationforteamsofsoccer-playingrobots.
CHAPTERSUMMARY
ThefieldofroboticsandAIareinexorablytiedbecauseroboticspermitthe
visualizationofAIalgorithmsinthephysicalworld.Roboticsexhibitthetrue
systemslevelofAI,astherearedistinctinputs(sensors),outputs(effectors),
and the AI algorithms that provide rational decision-making. Robotics is
aninterestingareaofstudybecausetheyencompassmanyelementsofAI.
The algorithms demonstrated throughout this book are applicable to AI
systems,fromsearchforuseinplanning,toneuralnetworksandevolutionary
systemsforlearning.RoboticsaretheextensionofAIalgorithmsintothe
realworld.
REFERENCES
[WikiRobot]“Robot,”Wikipediafreeencyclopedia.Availableonlineat:
http://en.wikipedia.org/wiki/Robot
RoboticsandAI 347
RESOURCES
[Braitenberg] Braitenberg, Valentine Vehicles, Experiments in Synthetic
Psychology.MITPress,1986.
[Shakey]“ShakeytheRobot,”SRIInternational.Availableonlineat:
Availableonlineathttp://www.sri.com/about/timeline/shakey.html
Breazeal,Cynthia“SociableMachines:ExpressiveSocialExchangeBetween
Humans and Robots.” Sc.D. dissertation, Department of Electrical
EngineeringandComputerScience,MIT,2000.
Hawes,Nick“AnytimePlanningforAgentBehavior,”SchoolofComputer
Science,TheUniversityofBirmingham,1997.
Jones, M. Tim, “Open Source Robotics Toolkits,” IBM Developerworks,
2006.
Availableonlineat:http://www-128.ibm.com/developerworks/linux/library/l-
robotools/
Krough, B. H. “A Generalized Potential Field Approach to Obstacle
Avoidance Control,” Proc. of International Robotics Research
Conference,Bethlehem,Pennsylvania,August,1984.
MIT Leg Laboratory. Available online at: http://www.ai.mit.edu/projects/
leglab
Pattis, Richard E. Karel The Robot: A Gentle Introduction to the Art of
Programming.JohnWiley&Sons,1981.
Whal, F. M., Thomas, U. “Robot Programming - From Simple Moves to
Complex Robot Tasks,” Institute for Robotics and Process Control,
TechnicalUniversityofBraunschweig,2002.
EXERCISES
1. What are the major elements of a robot from the perspective of
decomposition?
2. Comparetheearlydevelopmentofroboticstothosebeingdeveloped
today. How have advancements in the field of electronics affected
roboticsresearchtoday?
3. Whatwasoneoftheearliestimplementationsofaself-drivingvehicle,
andwhichAImethodwasused?
4. What applications can you envision for shape-changing robots in the
future?
5. Whatadvantagesexistforroboticsimulationsoverthedevelopmentof
realphysicalrobotembodiments?Whatissuestosimulationspresent?
348 ArtificialIntelligence
6. Using the example Braitenberg vehicles shown in this chapter, what
other Braitenberg-like architectures can you see? How do alternate
connectionsbetweensensorsandmotors(suchasinhibition)affectthe
robot’sbehavior?
7. Whatismeantbyopenandclosedloopsincontrolsystems?
8. Comparethetwomotionplanningmethodsdiscussedinthischapter
(celldecompositionandpotentialfields).Whataretheadvantagesand
disadvantagesofeach?
9. Distributedroboticspertainstothedistributionofaproblemtoagroup
ofrobots.Whatproblemsexistinthisdomainandwhatadvantages?
Chapter 11 INTELLIGENT
AGENTS
I
n this chapter, we’ll explore the topic of Intelligent Agents. The
definitionofanintelligentagentcanbedifficulttosuccinctlyprovide,
asthetermhasbeenusedinavarietyofsettings.Acommondefinition
of an intelligent agent from the perspective of artificial intelligence is an
autonomousentitythatexistsinanenvironmentandactsinarationalway.
What is rational is dependent on the environment. For example, is the
agentattemptingtosolveaproblem,orprotectitselfagainstotherentities?
Inthischapter,we’lldiscussthevarioustypesofagents,theirapplications,
architectures,andlanguages.
FIGURE11.1: Thefundamentalanatomyofanagent.
350 ArtificialIntelligence
ANATOMYOFANAGENT
Anagentiscommonlymadeupofanumberofelements.Theseinclude
oneormoresensorsthatareusedtoperceivetheenvironment,oneormore
effectorsthatmanipulatetheenvironment,andacontrolsystem.Thecontrol
systemprovidesamappingfromsensorstoeffectors,andprovidesintelligent
(orrational)behavior(seeFigure11.1).
Thisanatomycanbeappliedtohumans,asafirstexample.Thehuman
body has a rich set of sensors that cover a wide variety of domains. This
includes vision (eyes), smell (nose), hearing (ears), taste (tongue), touch
(variousincludingskin),balance(vestibularsense),nociception(pain),and
others.Thehumanbodyalsohasanumberofeffectorsystems,including
ourfingers,limbs(armsandlegs),andothervariousmotorcontrolsystems.
Thecontrolsystemincludesourbrainandcentralnervoussystem.
This can also be applied to other types of agents, both virtual and
physical.Awebspider,forexample,isavirtualagentthatgathersandfilters
informationforanotherparty.Awebspiderusesaprimarysensorofthe
HyperTextTransportProtocol,orHTTP,asameanstogatherdatafromweb
pages.Itscontrolsystemisanapplication,whichcanbewritteninalmost
any language, that drives the behavior of the web spider. This behavior
includesweb-dataparsingandfiltering.Thewebspidercanidentifynew
linkstofollowtocollectadditionalinformation,andusetheHTTPprotocol
tonavigatethewebenvironment.Finally,thewebspidercancommunicate
with a managing user through email using the Simple Mail Transport
Protocol,orSMTP.Theusercanconfigurethewebspiderforcollection,
navigation,orfiltering,andalsoreceiveemailsindicatingitscurrentstatus
(seeFigure11.2).
Awebspiderisoneexampleofanagent,butwe’venotyetdescribed
thepropertiesthatseparateanagentfromaprogram.
FIGURE11.2: Webspiderasanagent.
IntelligentAgents 351
FIGURE11.3: Simpleagenttaxonomy.
NOTE Anotherusefulperspectiveofagentsisthatofanagency.Anagentisan
intermediarybetweentwoormoreparties.Forexample,theprevious
web spider agent example illustrates this property of an agency. The
webspiderisanintermediaryagentforweb-datagatheringandfiltering
forauser.Thewebspideractsontheuser’sbehalffordatagathering,
givenasetofconstraintsfromtheuser.We’lllookatanexampleofthis
applicationlater.
A robot can also be considered an agent. A robot includes a variety
of sensors including vision (cameras, ultrasonic transducers, infrared
detectors),hearing(microphones),touch(bumpsensors),aswellasother
types of sensors for pressure, temperature, and movement detection
(accelerometers).Effectorsincludemotors(todrivewheels,tracks,orlimbs),
andaspeakerforsoundvocalization.Arobotcanalsoincludeanumberof
othereffectorsthatcanmanipulatetheenvironment,suchasavacuum,a
waterpump,orevenaweapon.
While a complete taxonomy of agents is not feasible, we can reduce
agentstoasmalltree,asshowninFigure11.3.Thisdivisionsplitsagents
intohardandsoftagents.Animals(includinghumans)androbotsfallinto
thehardagentscategory,whilesoftware,orintelligent,agents(andviruses)
fallintothesoftcategory.FranklinandGraesserfirstidentifiedvirusesas
agentsintheir1996taxonomy.[Franklin/Graesser1996]
AGENTPROPERTIESANDAI
Wecanthinkofagentsasasuper-setofartificialintelligence.Whatmakesa
programanagentisthatitutilizesoneormorepropertiesthatexhibitsome
typeofintelligence(oratleastexhibitpropertiesthatappearintelligent).
We’llnowexploresomeofthepropertiesthatformthebasisofintelligent
softwareagents(seeTable11.1).
352 ArtificialIntelligence
Table11.1: Agentproperties.
Property Description
Rationale Abletoactinarational(orintelligent)way
Autonomous A ble to act independently, not subject to external
control
Persistent Abletoruncontinuously
Communicative Abletoprovideinformation,orcommandotheragents
Cooperative Abletoworkwithotheragentstoachievegoals
Mobile Abletomove(typicallyrelatedtonetworkmobility)
Adaptive Abletolearnandadapt
Rationale
Thepropertyofrationalitysimplymeansthattheagentdoestherightthing
attherighttime,givenaknownoutcome.Thisdependsontheactionsthat
areavailabletotheagent(canitachievethebestoutcome),andalsohow
theagent’sperformanceismeasured.
Autonomous
Autonomysimplymeansthattheagentisabletonavigateitsenvironment
without guidance from an external entity (such as a human operator).
Theautonomousagentcanthereforeseekoutgoalsinitsenvironment,
whethertosustainitselforsolveproblems.Anexampleofanautonomous
agent is the remote agent that rode along in NASA’s Deep Space 1
spacecraft.
Persistent
Persistenceimpliesthattheagentexistsovertimeandcontinuouslyexists
initsenvironment.Thispropertycanalsoimplythattheagentisstatefulin
conditionswheretheagentmustbeserializedandmovedtoanewlocation
(aswouldbethecaseformobileagents).
Communicative
An agent having the ability to communicate provides obvious
advantagestoagentsystems.Agentscancommunicatewithotheragents
toprovidethemwithinformation,orcommunicatewithusers(forwhom
theagentrepresents).Anexampleofagentcommunicationwasshownin
Figure11.2.
IntelligentAgents 353
Cooperative
Relatedtothepropertyofcommunicationisthepropertyofcooperation.
Thispropertyimpliesthatanagentcanworkwithotheragentstocollectively
solveproblemsinanenvironment.Inordertocooperate,agentsmusthave
theabilitytocommunicate(insomeformoranother).Arelatedproperty
is that of deception. Instead of communicating to cooperatively solve a
problem,anagentcancommunicatedisinformationtodeceiveanotheragent
tomaxmizeitsownreward.
Mobile
Mobility in agent systems is commonly defined as the agent’s ability to
migratebetweensystemsoveranetwork.Thiscanbedoneautonomously,
usingaframeworkthatsupportsthisfunctionality(suchastheAgletsmobile
agentframework).Mobilityalsoappliestoviruses,whichuseeitheremail
(SMTP)ortheweb(HTTP)tomoveamongsystemsandusers.
NOTE It’s difficult to classify agents in one dimension when they’re actually
multi-dimensional. For example, an agent that is both mobile and
cooperative (implying communication) can also be called Distributed
ArtificialIntelligence(DAI)orDistributedProblemSolving(DPS).
Adaptive
Thelast,butlikelymostimportant,istheabilityforanagenttolearnand
adapt to the environment. From the perspective of the agent, learning
meanscreatingamappingofsensorstoeffectorsthatdemonstrateintelligent
behavior, or behavior that satisfies a set of defined constraints. To adapt
meansthatthesemappingsareflexible,andcanbemodifiedduetochanges
intheenvironment.
AGENTENVIRONMENTS
Whetheranagent’senvironmentistheInternet,virtuallandscapeofagame,or
theuniquespaceofaproblemenvironment,allenvironmentsshareacommon
setofcharacteristics.ThesecharacteristicsareshowninTable11.2.
Table11.2: Agentenvironmentproperties.[Russell/Norvig2003]
Property Description
Observability Areallelementsvisibletotheagent?
354 ArtificialIntelligence
environmentforanFPScanbeconsideredareal-
timestrategy.
Multi-Agent AnFPSenvironmentisinterestingbythefactthat
it’s multi-agent. Typically, these are competitive
environments,thoughsomealsoincludecooperative
elementsthroughsupportNPCagents.
Even though the Chess-game environment appears to be quite a bit
simplerthanthatofanNPCagentinanFPS,thetaskathandforaChess
agent is considerably more difficult. While the environment in an FPS
game is much more dynamic and random, the agents are typically built
withstate-machinebehaviorsandminimalamountsofAI.TheChess-board
environmentisdeterministic,butactionselectionisthekey.Therefore,the
Chess-playing agent includes much greater complexity (as illustrated in
Chapter4,AIandGames).
AGENTTAXONOMIES
Now that we’ve provided an introduction to agent properties and the
characteristicsofagentenvironments,let’sexploresomeofthemajoragent
typesthatexistinpreparationfordiscussingagentapplications.
InterfaceAgents
One of the earliest applications of intelligent agents was in the design of
intelligent user interfaces. To minimize information overload on a user,
intelligent user agents were built to reduce the amount of information
presentedtoauserasawaytohelptheuserfocusonwhatismostimportant
atanygiventime.
FIGURE11.4: AuserscenarioforemailandUseNet.
IntelligentAgents 357
For example, consider a user that uses email and the UseNet. When
newemailarrives,typicallyaniconappearsorasoundisplayedtoindicate
thatnewemailisavailable.Theusercanthenbringuptheirmailclientto
identifythesenderoftheemailtodecidewhethertoreaditnow,orwait
untillater.ThisusermayalsooccasionalyreviewUseNetnewsgroupsfor
newinformationaboutagiventopic(seeFigure11.4).
Tocombattheinformationoverloadandhelptheuserfocusonwhat’s
most important, an intelligent agent interface was proposed to assist the
userinfilteringwhatinformationshouldbepresentedwhen.Toassist,the
agentwouldneedtolearnabouttheuser’sneeds.Thiswastypicallydonein
alearningphase,wheretheagentwouldmonitorwhattheuserdidunder
whichcircumstances. For example, if the user always read emails from a
givensender,theagentcouldemittheemailnotificationonlywhenemail
wasreceivedbythatperson.Also,iftheuserreadUseNetpostsaboutagiven
topic,theagentcouldautonomouslymonitortheUseNetfortheseposts,
andthennotifytheuseratsomelatertime.
Thegoaloftheintelligentinterfaceagentistominimizetheinformation
overheadontheuserbylearningunderwhatcircumstancescertainactions
aretaken.Thisminimizesinterruptionsontheuser,allowingthemtofocus
moreonwhat’simportant(seeFigure11.5).
Early development of intelligent interface agents focused on web
interfaces, but others exist as well (which we’ll explore in the section on
agentapplications).
VirtualCharacterAgents
Ausefulagentapplicationthattakesonanumberofformsiscalledvirtual
characteragents,orsyntheticagents.Theseagentscantakeonanumberof
FIGURE11.5: Anintelligentinterfaceagenttominimizedistractions.
358 ArtificialIntelligence
forms,focusedontheirparticularapplication.Theserangefromsynthetic
agents,andgameagents(non-playercharacters),andconversationalagents
(otherwiseknownaschatbots).
EntertainmentAgents
Syntheticagentsareaspecialtypeofagentusedinvirtualrealityenvironments.
Thesetypesofagentscanbeusedascharactersincomputer-generated(CG)
moviesorfortrainingpurposesinmilitarysimulations.Variousmoviestudios
haveadoptedCGandproducedavarietyoffeature-lengthfilmsusingCG
synthetic characters. Rather than each character being animated by an
animator, a character is created complete with articulated joints and then
trainedtomove.Theanimatorcanthensimplyrequestthecharactertomove
orperformanaction,ratherthanspecifyitonaframe-by-framebasis.
An interesting variation on the entertainment agent is the “talking
head”createdatAnanova.Ananovaisthefirstvirtualnewscasterthatreads
newsfromreal-timenewsinformationusingtext-to-speechtechnologyand
computeranimationtocreatealifelikenewscasterthatlipsyncswiththe
textbeingspoken.
GameAgents
Oneofthemostcommonusesofagents,andatestbedforthedevelopmentof
AIisinthegenreofvideogames(seealsoChapter4,AIandGames).Called
Non-PlayerCharactersingames(NPCs),theseagentsbringlifetoavarietyof
gamesbyintroducingcharactersthatareautonomousandaddtotherealism
ofvideogames.NPCscanbecooperativeingames;inotherwords,characters
thatworkwiththeplayinthegame.NPCscanalsobeneutralcharactersinthe
gamethatprovideguidanceorhelptotheplayerduringthegame(orsimply
addtotheambientbackgroundofopen-worldgames).ButNPCsaremore
oftencompetitive,workingagainsttheplayerfromsatisfyingthedesiredgoals
ofthegame(forexample,enemyNPCsinwarorfantasygames).
To enable the development of NPCs, many games include scripting
capabilities with APIs focused on the integration of realistic NPC
behaviors.Oneofthemoreprominentexamplesofthiscapabilityiscalled
UnrealScript, which was developed for the UnrealEngine of the popular
UnrealTournamentfirst-person-shooter.UnrealScriptisanobject-oriented
scriptinglanguageoftheJavaflavor.
TIP
The simplest implementation of an NPC is the reactive (or reflexive)
agent.Thisagentincludesnostate,andinsteaddirectlymapsanaction
fromthecurrentsetofinputs.
IntelligentAgents 359
A simple (though incomplete) example of an NPC in UnrealScript is
shown in Listing 11.1. The function SpawnSimpleActor is used to create
a new NPC. This function picks a random location and spawns the new
characteratthatlocation.Thestatecodeimplementsthebehaviorsofthe
agent.
Listing11.1: ExtendingtheactorclassforasimpleNPC.
classSimpleActorextendsActor;
varintHealth;
//SpawnanewSimpleActor
functionSpawnSimpleActor()
{
localSimpleActorSA;
localvectorNewLocation;
//Pickanewlocation
NewLocation=Location+VRand();
NewLocation.Z=Location.Z;
//SpawnSimpleActorthere
SA=Spawn(class’SimpleActor’,,,NewLocation);
}
//StateCode
autostateWaiting
{
functionTouch(actorOther)
{
GotoState(‘Moving’);
}
Begin:
sleep(5);
goto‘Begin’;
}
autostateMoving
{
Begin:
//Movearound
GotoState(‘Waiting’);
}
DefaultProperties
{
360 ArtificialIntelligence
//SpecifyDefaults
Health=100
}
NOTE ManygameenvironmentsimplementNPCcharactersusingthescripted
Finite-StateMachine(FSM)model.Thismodelisconceptuallysimple
andmakesiteasytobuildcharacterswithcomplexbehaviors.
ChatterBots
Chatterbots,orconversationalagents,havealonghistoryinAI.Asdiscussed
inChapter1,AlanTuringproposedwhatisnowknownasthe“TuringTest”
asameanstojudgetheintelligenceofacomputingsystem.Thetestwas
basedonthemachine’sability(tomakeajudgeoperator)believethatitwas
speakingtoanotherhuman,insteadofamachine.Ifajudgecouldn’ttellthe
differencebetweenahumanandamachine,thenitcouldbeinferredthat
the machine possessed the characteristics of intelligence. This was really
nothingmorethanatestofamachine’slinguisticaspects,andnotentirely
intelligence.
TherecurrentlyexistsnoacademicfocusoncompetingintheTuring
test, but a less stringent form has resulted in a number of competitive
solutions.TheLoebnerPrizeawardsprizestotheconversationalagentthat
demonstratesthemosthuman-likeconversation.
ElizaandParry
OneoftheearliestexamplesofaconversationalagentisJosephWeizenbaum’s
Eliza (written in the LISP language). Eliza was written in 1966 and in
general,simplyrephrasedtheinputbacktothehuman,typicallyintheform
ofaquestion.Listing11.2providesasampleconversationwithEliza.
Listing11.2: AconversationwithEliza.
Patient: I’dliketotalkaboutmymother.
Eliza: Whatelsecomestomindwhenyouthinkofyourmother?
Patient: Shewantstocontrolmeandrunmylife.
Eliza: Yourlife?
Patient: Yes,therearetimesthatIhaveone.
Eliza: Youaresure.
Patient: Whydon’tyouthinkIhavealife?
Eliza: DoyoubelieveIdon’tthinkyouhavelife?
IntelligentAgents 361
Elizasimplylookedforpatterns,andthenrephrased,orpickedarandom
comment. You can see how this is done in Chapter 15, which offers an
introductiontothePOP-11language.
Another interesting and early example of a conversational agent was
Parry.ThisagentdifferedfromEliza,inthatinsteadofpretendingtobea
therapist,Parrysimulatedapatientwithparanoidschizophrenia.Listing
11.3providesasampleconversationbetweenParryandElizafromRFC439
(January1973).[RFC439]
Listing11.3: AconversationbetweenParryandEliza.
Eliza: Pleasegoon.
Parry: Doyouknowanythingaboutbookies?
Eliza: WhatmakesyouthinkIknowanythingaboutbookies?
Parry: Abookiedidn’tpaymeoffonce.
Eliza: Whatdoesthatsuggesttoyou?
Parry: Ibeatupthebookiewhodidnotpayoff,andIthoughthemight
usehisfriendsintheunderworldtogetevenwithme.
Eliza: Whatelsewouldyouliketodiscuss?
Parry: Whatdoyouknowaboutracketeers?
Eliza: Whydoyouask?
Parry: Whatexperiencehaveyouhadgambling?
Eliza: Whydoesthatquestioninterestyou?
Parry: Ilikehorseraces.
Eliza: Whatelsedoyoulike?
Parry: Iknowthemobcontrolsthebigrackets.
Eliza: Whoareyoureallytalkingabout?
Parry, like Eliza, used patterns to formulate a response to a prior
questionorcomment.Itwaswritteninmeta-LISPontheDECPDP-10
intheearly1970s.
ArtificialIntelligenceMarkupLanguage(AIML)
AIML,orArtificialIntelligenceMarkupLanguage,isaninterpeterthatcan
beusedtobuildconversationalagents.Asitsnameimplies,AIMLisadialect
of the Extensible Markup Language, or XML, which is a meta-language
thatallowsdefinitionofdata,aswellasitsmeaningandstructure.Takethe
followingexampleofasimpleAIMLtemplate.InAIML,thefundamental
unitofknowledgeiscalledacategory.Acategorycontainsapattern(the
inputpattern)andthetemplate(theresponse).
362 ArtificialIntelligence
<category>
<pattern>WHOAREYOU</pattern>
<template>WhoIamisnotimportant.</template>
</category>
AIMLalsosupportsvariables,sothatyoucanstoreinformationlearned
fromtheuser.Thiscanthenbeintroducedlaterintheconversation.
AIMLisausefultoolforbuildingconversationalagents,andhaswon
the Loebner Prize for most human computer three times, in addition to
otherawards.
MobileAgents
Mobileagentsarethoseagentsthatpossessthecharacteristicsofmobility.
This means that the agents have the ability to migrate from one host
computer to another. This may seem like a trivial characteristic, but the
advantagestomobilityarebothsubtleandimportant.
ConsidertheagentexampleinFigure11.6.Inthisexample,theagent
is stationary at a host node and collecting data from another node. The
physical constraint in this example is that the network be available for
communication. If the network becomes unavailable, the ability for the
agentstocommunicateisseveredresultinginabrittlesystem.
FIGURE11.6: Thebrittlenessofnon-mobilesoftware.
FIGURE11.7: Increasingsystemreliabilitywithmobilesoftware.
IntelligentAgents 363
Analternativeistobringthedatacollectionsoftwaretothesource.As
showninFigure11.7,amobileagentmigratesfromHostAtoHostBwhere
thedataissourced.Thisagentcouldbeacontrolsystem(inthesimplest
case,athermostat),thatreadsthesourcedataandtheninteractswiththe
systemtomakeadjustments.Inthismodel,themobileagentinteractswith
thedatacollectionagentatthesource.Further,theagentcouldcollectand
filterdata,andthenreturntotheoriginalhost.Thistypeofagentcouldbe
usefulinsituationswherefull-timeconnectionsarenotalwayspractically
possible(suchassatellitesinlow-earthorbit).
Mobileagents,whichcanalsobeviewedasaformofavirus,havepotential
in many areas of computing. Outside of data collection, mobile agents are
an interesting method for software deployment. For example, customer
purchasingsoftwarecouldacceptamobileagentthatpackagesthesoftware,or
isthesoftwareitself.Theagentcouldmaintainlicensingwithaserver,andalso
keepintouchwitharemoteservertoensurethatpatchesareup-to-date.
Withmobileagents,acommunicationthemeemerges.Agents,particularly
thosethataremobile,requirethemeanstocommunicateeitherlocallyor
remotely.Laterinthischapter,we’llexploresomeoftheprotocolsthatare
usedforagentcommunication(somedesignedspecificallyforthispurpose).
Mobileagentshavebeenusedinavarietyofapplicationsincludingprocess
controlandnetworkmonitoring.Networkmonitoringisanidealapplication
formobileagents.Anagentisprovideddetailsofdatacollection,andthen
disbursedintoanetwork.Theagentcollectsdata,andeithercommunicates
thedatabacktoacentralserver,ormigratesbacktoitselfwithitsdata.
Process control is another interesting application. Instead of purely
collectingdatafromremoteservers,theagentsmustalsomonitorandcontrol
thedevicestowhichthey’reattached.Priortomigrating,theagentscanbe
configuredfortheirparticulardestination.Fromthisperspective,mobile
agentsareaninterestingdeploymentmethodfordistributedsystems.
IBM’sagletsframeworkforJavaisaninterestingexampleofamobility
API.Agletsusewhat’scalledatickettoidentifywheretheagentistobe
migrated.Theagentuseitselfcreatesaticket,andthencallsthedispatch
method with this ticket to serialize the Java program and migrate to the
destination(seeListing11.4).Oncedispatchediscalled,theagentandits
dataarepackagedandrestartedatthedefineddestination.
Listing11.4: JavaagletsasamobilityAPI.
publicclassMyAgentextendsAglet{
publicvoidrun()
364 ArtificialIntelligence
{
....
QoCqoc=newQoC(QoC.NORMALINTEGRITY,QoC.
NOCONFIDENTIALITY);
Ticketticket=newTicket(“atp://192.168.1.100/”,qoc);
try{
//Serializeandmigratetothehostdefinedbytheticket
dispatch(ticket);
}catch(Exceptionexcp){
excp.printStackTrace();
}
...
}
}
In this example, a URL is defined for ticket. The URL specifies
“atp” which represents the Agent Transfer Protocol. This protocol
implementstheabilitytomigrateagletsbetweenhosts(whereeachsupport
theATP).
To enable the mobility with agents, frameworks are commonly built
toprovidethiscapability.ExamplesincludeIBM’sAgletsframework,and
alsoANTS,orActiveNodeTransferSystem.Anotherusefuladaptationof
mobileagentsiswhat’scalledActiveNetworking,wherepacketstransferred
throughanetworkcontaindatainterpretedascode.Theseactivepackets
(sometimescalledcapsules)canbeusedforvariousapplications,including
routerconfiguration.
UserAssistanceAgent
Oneoftheearliestapplicationsforintelligentagentswasforthepurposeof
simplifyingourexperienceswhendealingwithcomputers.Let’sexploresome
oftheseapplicationsfromtheperspectiveofthemodern-dayInternet.
EmailFiltering
Asimpleexampleisthatofemail.Whenanewemailarrives,ourcomputers
typicallynotifyusofthisfactusingeitheravisualque(amailboxflag)and
possiblyatoneindicatingthatourfocusshouldchangefromwhateverwe
happentobedoingtothatofreadingandrespondingtoemail.Insomecases,
thisiswhatwe’llnaturallydo(inthecaseofimportantemail).Butinother
cases,suchasspamorlowerpriorityemails,we’dratherignorethistoavoid
theinterruptionandcontinuewithourwork.
IntelligentAgents 365
Theemailexampleisaperfectoneforagentification.Theagentfirst
entersastateinwhichitmonitorstheactionsoftheuserfromtheperspective
ofinputstimulus(suchasnewemail).Thisisthelearningphase.Whenan
emailarrives,theuseropensthemailclientandscansthenewlyreceived
email. Some of the emails are read, some are simply deleted, and others
remainunreadforsometime.Overtime,theagentcanbuildprobabilities
thatmodeltheuser’sbehavior.Whentheagentisabletomodeltheuserto
asufficientthreshold(forexample,95%stimulusAresultsinresponseB),
thenitcanmediatefortheuserinthedomainofemail.Ifanemailarrives
forwhichtheusernormallyreadsimmediately(abovethe95%threshold),
theagentcouldpresentawindowtotheuserthatidentifiesthattheemail
wasreceived,andaskstheuseriftheywouldliketoreadit.Otherwise,if
the email falls below the threshold, the agent could withhold the email
notification,sothattheuserisnotdisturbedbythenewinformation.
AnexampleagentthatimplementsintelligentemailhandlingisMaxims.
ThisagentwasdevelopedinCommonLISPfortheMacintoshplatformand
learnedtointelligentlyprioritizeemailforauser,aswellassortingandarchiving.
TheMaximsagentalsousedcaricaturestoconveythestateoftheagenttothe
user.Inthisway,theusercouldknowiftheagentwasworking,suggesting,or
unsureofwhattodo(aswellasotheremotivestates).[Lashkari1994]
Many new mail clients provide the ability to classify the route email
accordingtouser-definedrules.Thesefeaturesprovideamechanismforthis
capability,thoughitcanbedifficulttoclassifythemasagents.
InformationGatheringandFiltering
Informationgatheringandfilteringisanotherusefulexampleofusingagents
for user assistance. Keeping up on the latest information in our fields is
important,butcanbeverytime-consuming.Butratherthandothiswork
onourown,Internetsearchagentscandothisworkforus,providingthe
resultswhensomethingnewisavailable.
AninterestingexampleofinformationgatheringandfilteringistheGoogle
Alertsservice.GoogleAlertsallowausertocreatesearch‘alerts’whichare
searchkeywords.WhenGooglerunsacrossanitemthatmatchesyoursearch
criteria,itcollectstheselinkstogetherandthenemailsyouonaperiodicbasis.
TheemailsareintheformoftextplusaURLlink,makingitsimpletoreview
thenewinformationandthenpresentitinaneasy-to-useformat.
OtherUser-AssistanceApplications
Many other applications have been developed in the category of user
assistance.Theseincludeapplicationsthatrequirecommunicationbetween
366 ArtificialIntelligence
agents.Oneexampleisacalendaragentwhichisusedtoscheduleaperson’s
time.Thecalendaragentrepresentingausernegotiatesmeetingswithother
calendaragentstooptimizeallparticipants’time.Otherexamplesinclude
auction agents, which communicate with other auction agents to bid on
goodsperauser’srequest.
HybridAgents
Inmostcases,agentscan’tbeclassifiedsuccinctlybyasinglelabelasmost
arehybridinnature.Insteadofasinglecharacteristic,suchasmobile,agents
implementmultiplecharacteristics,suchasmobileandcommunicative.
Consider an interface agent that securely relays information between
consolesinanoperationscenter.Theagentconsolidatestheinformationtobe
relayedandthenmigratestothedestinationconsole.Oncethere,itopensa
windowtotheconsole’suser(firstauthenticatingtheusertoensureit’stheright
person)andthenprovidesthedatainitsneededform.Thisagentdemonstrates
a number of characteristics such as mobility, autonomy, and the ability to
communicate(withauserinthiscontext)throughadefinedinterface.
NOTE Theabilityforagentstocommunicateforpurposesofrelayinginformation
ordirectionsismostoftennotedasthecharacteristicofcommunication.
Thischaracteristichasalsobeendefinedasasocialabilitywhereagents
interactwithoneanotherforbothcollaborationandcoordination.
AGENTARCHITECTURES
Let’snowexploresomeoftheagentarchitecturesthathavebeencreatedto
supportthedevelopmentofagentsystems.We’llfirstdiscussthemeaning
ofarchitecture,andthenreviewsomeofthemoreimportanttypesofagent
architecturesthathavebeencreated.We’llthenfinishthisdiscussionwith
areviewofsomeoftheagentarchitecturesthathavebeendevelopedand
reviewsomeoftheapplicationsforwhichtheycanbeused.
WhatisArchitecture?
When we refer to architecture, we’re referring to a framework from which
applicationscanbebuilt.Architecturesarecommonlydefinedtosupportaspecific
typeofproblem,suchasdependable,orreal-time.Architecturesarecommonly
defined from a perspective or a viewpoint. This perspective could be from a
functionalview,codeview,oruserview(tonameafewfromtheRecommended
PracticeforArchitectureDescriptionofSoftware-IntensiveSystems).
IntelligentAgents 367
TIP
Agentarchitectures,likesoftwarearchitectures,areformallyadescription
oftheelementsfromwhichasystemisbuiltandthemannerinwhich
theycommunicate.Further,theseelementscanbedefinedfrompatterns
withspecificconstraints.[Shaw/Garlin1996]
Anumberofcommonarchitecturesexistthatgobythenamespipe-and-
filterorlayeredarchitecture.Notethatthesedefinetheinterconnections
betweencomponents.Pipe-and-Filterdefinesamodelwheredataismoved
throughasetofoneormoreobjectsthatperformatransformation.Layered
simplymeansthatthesystemiscomprisedofasetoflayersthatprovide
a specific set of logical functionality and that connectivity is commonly
restrictedtothelayerscontiguoustooneanother.
From the perspective of agent architectures, patterns can exist that
supportthedevelopmentandoperationofagents.Forexample,components
can exist to provide communication between agents. Other components
cansupportperception(viewingtheagent’senvironment)andalsoactions
(manipulatingtheenvironment).Thesetypesofcomponentssimplifythe
developmenttaskforagentdesigners,allowingthemtoconcentrateontheir
particulartaskathandinsteadofcommonenvironmentalconcerns.
NOTE Itshouldbenotedthatarchitecturecanbeappliedatmultiplelevelsto
agentsystems.Anagentitselfcanhavearchitecture.Considerpatterns
thatdefinehowparticularagentsaredeveloped.Therearealsolower-
levelarchitecturesthatprovidetheagentenvironment(aswouldbethe
caseformobileagentarchitectures).
In this section, we’ll introduce a variety of architecture types as a
precursortoexplorespecificframeworksthathavebeencreatedforagent
development.
TypesofArchitectures
Basedonthegoalsoftheagentapplication,avarietyofagentarchitectures
existtohelp.Thissectionwillintroducesomeofthemajorarchitecturetypes
andapplicationsforwhichtheycanbeused.
ReactiveArchitectures
A reactive architecture is the simplest architecture for agents. In this
architecture,agentbehaviorsaresimplyamappingbetweenstimulusand
response. The agent has no decision-making skills, only reactions to the
environmentinwhichitexists.Figure11.8illustratesthisarchitecture.
368 ArtificialIntelligence
FIGURE11.8: Reactivearchitecturedefinesasimpleagent.
FIGURE11.9: Adeliberativeagentarchitectureconsidersitsactions.
AsshowninFigure11.8,theagentsimplyreadstheenvironmentand
thenmapsthestateoftheenvironmenttooneormoreactions.Giventhe
environment,morethanoneactionmaybeappropriate,andthereforethe
agentmustchoose.
Theadvantageofreactivearchitecturesisthattheyareextremelyfast.
Thiskindofarchitecturecanbeimplementedeasilyinhardware,orfastin
softwarelookup.Thedisadvantageofreactivearchitecturesisthattheyapply
onlytosimpleenvironments.Sequencesofactionsrequirethepresenceof
state,whichisnotencodedintothemappingfunction.
DeliberativeArchitectures
Adeliberativearchitecture,asthenameimplies,isonethatincludessome
deliberation over the action to perform given the current set of inputs.
Insteadofmappingthe sensors directly to the actuators, the deliberative
architecture considers the sensors, state, prior results of given actions,
IntelligentAgents 369
and other information in order to select the best action to perform. The
deliberativearchitectureisshowninFigure11.9.
ThemechanismforactionselectionasshowninFigure11.9isundefined.
Thisisbecauseitcouldbeavarietyofmechanismsincludingaproduction
system,neuralnetwork,oranyotherintelligentalgorithm.
Theadvantageofthedeliberativearchitectureisthatitcanbeusedto
solvemuchmorecomplexproblemsthanthereactivearchitecture.Itcan
performplanning,andperformsequencesofactionstoachieveagoal.The
disadvantage is that it is slower than the reactive architecture due to the
deliberationfortheactiontoselect.
BlackboardArchitectures
Theblackboardarchitectureisaverycommonarchitecturethatisalsovery
interesting.ThefirstblackboardarchitecturewasHEARSAY-II,whichwas
aspeechunderstandingsystem.Thisarchitectureoperatesaroundaglobal
workareacalltheblackboard.Theblackboardisacommonworkareafor
anumberofagentsthatworkcooperativelytosolveagivenproblem.The
blackboardthereforecontainsinformationabouttheenvironment,butalso
intermediateworkresultsbythecooperativeagents(seeFigure11.10).
The example shown in Figure 11.10 illustrates how a blackboard
architecture could be applied to an agent system. In this example, two
separateagentsareusedtosampletheenvironmentthroughtheavailable
sensors(thesensoragent)andalsothroughtheavailableactuators(action
agent).Theblackboardcontainsthecurrentstateoftheenvironmentthatis
constantlyupdatedbythesensoragent,andwhenanactioncanbeperformed
(asspecifiedintheblackboard),theactionagenttranslatesthisactioninto
FIGURE11.10: Theblackboardarchitecturesupportsmulti-agentproblemsolving.
370 ArtificialIntelligence
controloftheactuators.Thecontroloftheagentsystemisprovidedbyone
ormorereasoningagents.Theseagentsworktogethertoachievethegoals,
whichwouldalsobecontainedintheblackboard.Inthisexample,thefirst
reasoningagentcouldimplementthegoaldefinitionbehaviors,wherethe
secondreasoningagentcouldimplementtheplanningportion(totranslate
goalsintosequencesofactions).
Since the blackboard is a common work area, coordination must be
provided such that agents don’t step over one another. For this reason,
agentsarescheduledbasedontheirneed.Forexample,agentscanmonitor
theblackboard,andasinformationisadded,theycanrequesttheabilityto
operate.Theschedulercanthenidentifywhichagentsdesiretooperateon
theblackboard,andtheninvokethemaccordingly.
The blackboard architecture, with its globally available work area, is
easilyimplementedwithamulti-threadingsystem.Eachagentbecomesone
ormoresystemthreads.Fromthisperspective,theblackboardarchitecture
isverycommonforagentandnon-agentsystems.
Belief-Desire-Intention(BDI)Architecture
BDI, which stands for Belief-Desire-Intention, is an architecture that
follows the theory of human reasoning as defined by Michael Bratman.
Beliefrepresentstheviewoftheworldbytheagent(whatitbelievestobe
thestateoftheenvironmentinwhichitexists).Desiresarethegoalsthat
definethemotivationoftheagent(whatitwantstoachieve).Theagentmay
havenumerousdesires,whichmustbeconsistent.Finally,Intentionsspecify
thattheagentusestheBeliefsandDesiresinordertochooseoneormore
actionsinordertomeetthedesires(seeFigure11.11).
FIGURE11.11: TheBDIarchitecturedesirestomodelmentalattitudes.
IntelligentAgents 371
As we described above, the BDI architecture defines the basic
architecture of any deliberative agent. It stores a representation of the
state of the environment (beliefs), maintains a set of goals (desires), and
finally,anintentionalelementthatmapsdesirestobeliefs(toprovideoneor
moreactionsthatmodifythestateoftheenvironmentbasedontheagent’s
needs).
HybridArchitectures
As is the case in traditional software architecture, most architectures are
hybrids. For example, the architecture of a network stack is made up of
a pipe-and-filter architecture and a layered architecture. This same stack
alsosharessomeelementsofablackboardarchitecture,asthereareglobal
elementsthatarevisibleandusedbyeachcomponentofthearchitecture.
Thesameistrueforagentarchitectures.Basedontheneedsoftheagent
system,differentarchitecturalelementscanbechosentomeetthoseneeds.
MobileArchitectures
The final architectural pattern that we’ll discuss is the mobile agent
architecture.Thisarchitecturalpatternintroducestheabilityforagentsto
migrate themselves between hosts. As shown in Figure 11.12, the agent
architectureincludesthemobilityelement,whichallowsanagenttomigrate
fromonehosttoanother.Anagentcanmigratetoanyhostthatimplements
themobileframework.
The mobile agent framework provides a protocol that permits
communication between hosts for agent migration. This framework also
requiressomekindofauthenticationandsecurity,toavoidamobileagent
frameworkfrombecomingaconduitforviruses.
Alsoimplicitinthemobileagentframeworkisameansfordiscovery.For
example,whichhostsareavailableformigration,andwhatservicesdothey
provide?Communicationisalsoimplicit,asagentscancommunicatewith
oneanotheronahost,oracrosshostsinpreparationformigration.
FIGURE11.12: Themobileagentframeworksupportsagentmobility.
372 ArtificialIntelligence
The mobile agent architecture is advantageous as it supports the
developmentofintelligentdistributedsystems.Butadistributedsystemthat
isdynamic,andwhoseconfigurationandloadingisdefinedbytheagents
themselves.
ArchitectureDescriptions
In the previous section, we explored some of the architectures that have
beencreatedfortheconstructionofagents.Nowlet’sreviewsomeofthe
implementations of these architectures. We’ll review the architectures as
showninTable11.5.
Table11.5: Pleaseattributethearchitecturesasfollows:
Subsumption Brooks
Atlantis Gat
Homer Bickmore
BB1 Hayes-Roth
OpenAgentArch Stanford
PRS Ingrand,Georgeff,andRao
Aglets IBM
Messengers Fukada
SOAR UniversityofMichigan
TIP
You’ll note that the goal of each of these architectures is to select an
actiontoperformgiventhecurrentstateoftheenvironment.Fromthis
perspective,wecanrefertotheseasactionselectionarchitectures.
SubsumptionArchitecture(ReactiveArchitecture)
The Subsumption architecture, originated by Rodney Brooks in the
late 1980s, was created out of research in behavior-based robotics. The
fundamental idea behind subsumption is that intelligent behavior can be
createdthroughacollectionofsimplebehaviormodules.Thesebehavior
modules are collected into layers. At the bottom are behaviors that are
reflexiveinnature,andatthetop,behaviorsthataremorecomplex.
ConsidertheabstractmodelshowninFigure11.13.Atthebottom(level
0)existthereflexivebehaviors(suchasobstacleavoidance).Ifthesebehaviors
arerequired,thenlevel0consumestheinputsandprovidesanactionatthe
output.Butnoobstaclesexist,sothenextlayerupispermittedtosubsume
control. At each level, a set of behaviors with different goals compete for
control based on the state of the environment. To support this capability,
IntelligentAgents 373
FIGURE11.13: Architecturalviewofthesubsumptionarchitecture.
levelscanbeinhibited(inotherwords,theiroutputsaredisabled).Levelscan
alsobesuppressedsuchthatsensorinputsareroutedtohigherlayers.
AsshowninFigure11.13,subsumptionisaparallelanddistributed
architectureformanagingsensorsandactuators.Thebasicpremiseisthat
webeginwithasimplesetofbehaviors,andoncewe’vesucceededthere,
weextendwithadditionallevelsandhigher-levelbehaviors.Forexample,
we begin with obstacle avoidance and then extend for object seeking.
Fromthisperspective,thearchitecturetakesamoreevolutionarydesign
approach.
Subsumptiondoeshaveitsproblems.Itissimple,butitturnsoutnot
to be extremely extensible. As new layers are added, the layers tend to
interfere with one another, and then the problem becomes how to layer
thebehaviorssuchthateachhastheopportunitytocontrolwhenthetime
isright.Subsumption is also reactive in nature, meaning that in the end,
thearchitecturestillsimplymapsinputstobehaviors(noplanningoccurs,
forexample).Whatsubsumptiondoesprovideisameanstochoosewhich
behaviorforagivenenvironment.
BehaviorNetworks(ReactiveArchitecture)
Behavior networks, created by Pattie Maes in the late 1980s, is another
reactivearchitecturethatisdistributedinnature.Behaviornetworksattempt
toanswerthequestion,whichactionisbestsuitedforagivensituation.As
thenameimplies,behaviornetworksarenetworksofbehaviorsthatinclude
activationlinksandinhibitionlinks.
AnexamplebehaviornetworkforagameagentisshowninFigure11.14.
As shown in the legend, behaviors are rectangles and define the actions
that the agent may take (attack, explore, reload, etc.). The ovals specify
374 ArtificialIntelligence
FIGURE11.14: Behaviornetworkforasimplegameagent.
the preconditions for actions to be selected, which are inputs from the
environment.Preconditionsconnecttobehaviorsthroughactivationlinks
(theypromotethebehaviortobeperformed)orinhibitionlinks(thatinhibit
thebehaviorfrombeingperformed).
ThenetworkinFigure11.14illustratesatypicalNPCinanFPSgame
environment.Theenvironmentissampled,andthenthebehaviorforthe
agentisselectedbasedonthecurrentstateoftheenvironment.Thefirst
thingtonoteistheactivationandinhibitionlinks.Forexample,when
the agent’s health is low, attack and exploration are inhibited, leaving
the agent to find the nearest shelter. Also, while exploring, the agent
maycomeacrossmedkitsorammunition.Ifamedkitorammunitionis
found,it’sused.
Maes’ algorithm referred to competence modules, which included
preconditions (that must be fulfilled before the module can activate),
actionstobeperformed,aswellasalevelofactivation.Theactivationlevel
isathresholdthatisusedtodeterminewhenacompetencemodulemay
activate.Thealgorithmalsoincludesdecay,suchthatactiviationsdissipate
overtime.
Likethesubsumptionarchitecture,behaviornetworksareinstancesof
Behavior-Based Systems (BBS). The primitive actions produced by these
systemsareallbehaviors,basedonthestateoftheenvironment.
Behavior networks are not without problems. Being reactive, the
architecture does not support planning or higher-level behaviors. The
architecturecanalsosufferwhenbehaviorsarehighlyinter-dependent.With
manycompetinggoals,thebehaviormodulescangrowdramaticallyinorder
torealizetheintendedbehaviors.Butforsimplerarchitecture,suchasthe
FPSgameagentinFigure11.14,thisalgorithmisideal.
IntelligentAgents 375
FIGURE11.15: Thethree-layerarchitectureofATLANTIS.
ATLANTIS(DeliberativeArchitecture)
ThegoalofATLANTIS(AThree-LayerArchitectureforNavigatingThrough
Intricate Situations), was to create a robot that could navigate through
dynamicandimperfectenvironmentsinpursuitofexplicitlystatedhigh-level
goals. ATLANTIS was to prove that a goal-oriented robot could be built
fromahybridarchitectureoflower-levelreactivebehaviorsandhigher-level
deliberativebehaviors.
Wherethesubsumptionarchitectureallowslayerstosubsumecontrol,
ATLANTIS operates on the assumption that these behaviors are not
exclusiveofoneanother.Thelowestlayercanoperateinareactivefashion
to the immediate needs of the environment, while the uppermost layer
cansupportplanningandmoregoal-orientedbehaviors.Thefundamental
architectureofATLANTISisprovidedinFigure11.15.
In ATLANTIS, control is performed from the bottom-up. At the
lowestlevel(thecontrollayer)arethereactivebehaviors.Theseprimitive-
level actions are capable of being executed first, based on the state of
the environment. At the next layer is the sequencing layer. This layer is
responsible for executing plans created by the deliberative layer. The
deliberative layer maintains an internal model of the environment and
createsplanstosatisfygoals.Thesequencinglayermayormaynotcomplete
theplan,basedonthestateoftheenvironment.
Thisleavesthedeliberationlayertoperformthecomputationallyexpensive
tasks.Thisisanotherplacethatthearchitectureisahybrid.Thelower-level
behavior-basedmethods(inthecontrollerlayer)areintegratedwithhigher-
376 ArtificialIntelligence
levelclassicalAImechanisms(inthedeliberativelayer).Interestingly,the
deliberativelayerdoesnotcontrolthesequencinglayer,butinsteadsimply
advisesonsequencesofactionsthatitcanperform.Theadvantageofthis
architectureisthatthelow-levelreactivelayerandhigher-levelintentional
layersareasynchronous.Thismeansthatwhiledeliberativeplansareunder
construction,theagentisnotsusceptibletothedynamicenvironment.This
isbecauseeventhoughplanningcantaketimeatthedeliberativelayer,the
controllercandealwithrandomeventsintheenvironment.
Homer(DeliberativeArch)
Homerisanotherinterestingdeliberativearchitecturethatisbothmodular
and integrated. Homer was created by Vere and Bickmore in 1990 as
a deliberative architecture with some very distinct differences to other
architectures.Someofthenotabledifferencesincludeatemporalplanner
andanaturallanguageprocessor.
AtthecoreoftheHomerarchitectureisamemorythatisdividedinto
two parts. The first part contains general knowledge (such as knowledge
abouttheenvironment).Thesecondpartiscalledepisodicknowledge,which
isusedtorecordexperiencesintheenvironment(perceptionsandactions
taken).Thenaturallanguageprocessoracceptshumaninputviaakeyboard,
andparsesandrespondsusingasentencegenerator.Thetemporalplanner
createsdynamicplanstosatisfypredefinedgoals,andiscapableofreplanning
iftheenvironmentrequires.Bytemporal,wemeanthattheplannercanplan
actionstotakeplacewithinagiventime,whichcanbereplannedifthisdoes
notoccur.Thearchitecturealsoincludesaplanexecutor(orinterpreter),
which is used to execute the plan at the actuators. The architecture also
includedavarietyofmonitorprocesses.
FIGURE11.16: ThearchitectureofHomerintegratesseparatefieldsofAI.
IntelligentAgents 377
The basic idea behind Homer was an architecture for general intelligence.
The keyboard would allow regular English language input, and a terminal
woulddisplaygeneratedEnglishlanguagesentences.Theusercouldtherefore
communicatewithHomertospecifygoalsandreceivefeedbackviatheterminal.
Homercouldlogperceptionsoftheworld,withtimestamps,toallowdialogue
withtheuserandrationalanswerstoquestions.Reflective(monitor)processes
allowHomertoaddorremoveknowledgefromtheepisodicmemory.
Homer is an interesting architecture implementing a number of
interesting ideas, from natural language processing to planning and
reasoning. One issue found in Homer is that when the episodic memory
growslarge,ittendstoslowdowntheoveralloperationoftheagent.
BB1(Blackboard)
BB1isadomain-independentblackboardarchitectureforAIsystemscreated
by Barbara Hayes-Roth. The architecture supports control over problem
solvingaswellasexplainingitsactions.Thearchitectureisalsoabletolearn
newdomainknowledge.
BB1includestwoblackboards;adomainblackboardwhichactsasthe
global database and a control blackboard, which is used for generating a
solutiontothegivencontrolproblem.ThekeybehindBB1isitsabilityto
incrementallyplan.Insteadofdefiningacompleteplanforagivengoal,and
then executing that plan, BB1 dynamically develops the plan and adapts
tothechangesintheenvironment.Thisiskeyfordynamicenvironments,
whereunanticipatedchangescanleadtobrittleplansthateventuallyfail.
As a blackboard architecture, knowledge sources introduce new
knowledgetotheblackboardforoneormoreusers.Thechangeofknowledge
inablackboardservesasatriggerforoperationbyusers.InBB1,control
solutions are dynamically generated using knowledge from the domain
blackboardfromcontrolknowledgeinthecontrolblackboard.Ascheduler
manageswhichblackboardusersshouldgettheabilitytoexecute.
OpenAgentArchitecture(Blackboard)
TheOpenAgentArchitecture(orOAA)isablackboardarchitectureinwhich
allagentscommunicatethroughtheblackboard(viaaserverprocess).The
serverprocessactsinanumberofroles.Itcoordinatesactivitiesbetween
theclientagents(decidingwhichcanactonknowledgeontheblackboard)
aswellasprovidingcommunicationbetweenclientagents.Whenknowledge
is applied to the blackboard (through the server), the server decides
which agent should be notified and then schedules them accordingly. An
architecturalviewofthe
OAAisprovidedinFigure11.16A.
378 ArtificialIntelligence
FIGURE11.16A: TheOAAusesablackboardandaserverforcoordination.
ThelanguageusedtocommunicatebetweenagentsinOAAisanextension
ofProlog.Anewagentregistersitselfwiththeserver,andcaninstalltriggers
that monitor incoming data into the blackboard. The trigger serves as a
requestsuchthatwhenknowledgearrives,theservercanroutethedatato
theagentforfurtherprocessing.Agentsmayalsocommunicatewithother
agentsthroughtheserver.Thiscanbedoneforthepurposeofrequesting
activitiesofotheragents.Theserver(andblackboard)alsoprovidethemeans
forbroadcastcommunicationstogloballycoordinatebetweentheagents.
TheOAAisausefulgenericarchitectureformulti-agentprogramming
with a common communication structure. The agent communication
language, using Prolog, makes it useful to communicate not only generic
requestsandresponses,butalsoknowledgeandsemanticqueries.
ProceduralReasoningSystem(BDI)
TheProceduralReasoningSystem(PRS)isageneral-purposearchitecture
that’s ideal for reasoning environments where actions can be defined
by predetermined procedures (action sequences). PRS is also a BDI
architecture,mimickingthetheoryonhumanreasoning.
PRSintegratesbothreactiveandgoal-directeddeliberativeprocessing
inadistributedarchitecture.AsshowninFigure11.17,thearchitectureis
abletobuildaworld-modeloftheenvironment(beliefs)throughinteracting
withenvironmentsensors.Actionscanalsobetakenthroughanintentions
module.Atthecoreisaninterpreter(orreasoner)whichselectsagoalto
meet(giventhecurrentsetofbeliefs)andthenretrievesaplantoexecute
IntelligentAgents 379
FIGURE11.17: PRSisaBDIarchitectureforplanexecution.
toachievethatgoal.PRSiterativelyteststheassumptionsoftheplanduring
itsexecution.Thismeansthatitcanoperateindynamicenvironmentswhere
classicalplannersaredoomedtofail.
Plans in PRS (also called knowledge areas) are predefined for the
actionsthatarepossibleintheenvironment.Thissimplifiesthearchitecture
becauseitisn’trequiredtogenerateplans,onlyselectthembasedonthe
environmentandthegoalsthatmustbemet.Whileplanningismoreabout
selectionthansearchorgeneration,theinterpreterensuresthatchangesto
theenvironmentdonotresultininconsistenciesintheplan.Instead,anew
planisselectedtoachievethespecificgoals.
PRS is a useful architecture when all necessary operations can be
predefined.It’salsoveryefficientduetolackofplangeneration.Thismakes
PRSanidealagentarchitectureforbuildingagentssuchasthosetocontrol
mobilerobots.
Aglets(Mobile)
AgletsisamobileagentframeworkdesignedbyIBMTokyointhe1990s.
AgletsisbasedontheJavaprogramminglanguage,asitiswellsuitedfora
mobileagentsframework.First,theapplicationsareportabletoanysystem
(bothhomogeneousandheterogeneous)thatiscapableofrunningaJava
VirtualMachine(JVM).Second,aJVMisanidealplatformformigration
services. Java supports serialization, which is the aggregation of a Java
application’s program and data into a single object that is restartable. In
thiscase,theJavaapplicationisrestartedonanewJVM.Javaalsoprovides
380 ArtificialIntelligence
FIGURE11.18: Theagletsmobileagentframework.
asecureenvironment(sandbox)toensurethatamobileagentframework
doesn’tbecomeavirusdistributionsystem.
The Aglets framework is shown in Figure 11.18. At the bottom of
the framework is the JVM (the virtual machine that interprets the Java
bytecodes).Theagentruntimeenvironmentandmobilityprotocolarenext.
Themobilityprotocol,calledAgletTransportProtocol(orATP),provides
themeanstoserializeagentsandthentransportthemtoahostpreviously
definedbytheagent.TheagentAPIisatthetopofthestack,whichinusual
Javafashion,providesanumberofAPIclassesthatfocusonagentoperation.
Finally,therearethevariousagentsthatoperateontheframework.
TheagentAPIandruntimeenvironmentprovideanumberofservices
thatarecentraltoamobileagentframework.Someofthemoreimportant
functionsareagentmanagement,communication,andsecurity.Agentsmust
be able to register themselves on a given host to enable communication
fromoutsideagents.Inordertosupportcommunication,securityfeatures
mustbeimplementedtoensurethattheagenthastheauthoritytoexecute
ontheframework.
Agletsprovidesanumberofnecessarycharacteristicsforamobileagent
framework,includingmobility,communication,security,andconfidentiality.
Agletsprovideweakmigration,inthattheagentscanonlymigrateatarbitrary
pointswithinthecode(suchaswiththedispatchmethod).
Messengers(Mobile)
Messengers is a runtime environment that provides a form of process
migration (mobile agency). One distinct strength of the messengers
IntelligentAgents 381
environmentisthatitsupportsstrongmigration,ortheabilitytomigrateat
arbitrarypointswithinthemobileapplication.
The messengers environment provides the hop statement which
defineswhenandwheretomigratetoanewdestination.Aftermigrationis
complete,themessengersagentrestartsintheapplicationatthepointafter
theprevioushopstatement.Theendresultisthattheapplicationmoves
tothedata,ratherthanusingamessagingprotocoltomovethedatatothe
agent.Thereareobviousadvantagestothiswhenthedatasetislargeand
themigrationlinksareslow.
The messengers model provides what the authors call Navigational
Programming, and also Distributed Sequential Computing (DSC). What
makestheseconceptsinterestingisthattheysupportthecommonmodelof
programmingthatisidenticaltothetraditionalflowofsequentialprograms.
Thismakesthemeasiertodevelopandunderstand.
Let’snowlookatanexampleofDSCusingthemessengersenvironment.
Listing11.5providesasimpleprogram.Consideranapplicationwhereona
seriesofhosts,wemanipulatelargematriceswhichareheldintheirmemory.
ThiscanbesimplydemonstratedonListing11.4.
Listing11.5: Exampleofdistributedsequentialcomputing.
//BeginonhostA
r1=value(A)
//HoptohostB
hop(B)
r2=f1(r1,B)
//HopandendathostC
hop(C)
r3=f2(r2,C)
What’s interesting about this simple program, and a strength of the
messengersapproach,isthattheresultingprogramhasthesamestructure
and flow as the original sequential variant. The only difference between
this program, and a single processor non-distributed variant is that hop
statementsareinsertedtoinitiatetransferoftheprogramtothenewhost.
AnotherusefulapplicationofDSCisinthedomainofsensornetworks.
Rather than moving sensor data among hosts for processing, messenger
applicationsmigratetothesensornodes(suchasmulti-megapixelimagers),
process them (calculate their centroids), and migrate their results on to
subsequentprocessingnodes.
382 ArtificialIntelligence
Soar(Hybrid)
Soar, which originally was an acronym for State-Operator-And-Result,
isasymboliccognitivearchitecture.Soarprovidesamodelofcognition
alongwithanimplementationofthatmodelforbuildinggeneral-purpose
AI systems. The idea behind Soar is from Newell’s unified theories of
cognition.Soarisoneofthemostwidelyusedarchitectures,fromresearch
into aspects of human behavior to the design of game agents for first-
person-shootergames.
ThegoaloftheSoararchitectureistobuildsystemsthatembodygeneral
intelligence.WhileSoarincludesmanyelementsthatsupportthisgoal(for
example,representingknowledgeusingprocedural,episodic,anddeclarative
forms), but Soar lacks some important aspects. These include episodic
memoriesandalsoamodelforemotion.
Soar’sunderlyingproblem-solvingmechanismisbasedonaproduction
system(expertsystem).Behaviorisencodedinrulessimilartotheif-then
form.SolvingproblemsinSoarcanbemostsimplydescribedasproblem-
spacesearch(toagoalnode).Ifthismodelofproblemsolvingfails,other
methodsareused,suchashillclimbing.Whenasolutionisfound,Soaruses
amethodcalledchunkingtolearnanewrulebasedonthisdiscovery.Ifthe
agentencounterstheproblemagain,itcanusetheruletoselecttheaction
totakeinsteadofperformingproblemsolvingagain.
AGENTLANGUAGES
Agents can be built in any language, though a number of agent-focused
languages (and language extensions) are available to simplify their
development. In this section, we’ll explore some of the languages and
languageextensionsthatcanbeusedforagentdevelopment.
Telescript
Telescriptisbothalanguageandenvironmentforthedevelopmentofagents
andagentsocieties.It’salsooneoftheoldestlanguagesthatfocussolelyon
agentdevelopment(includingthosewithmobileattributes).Telescriptwasan
object-orientedlanguagethatwasinterpretedbytheenvironment.Telescript
couldalsointegratewithCapplicationsforbuildinglargesystems.
ThetwoprimaryelementsofTelescriptaretheagentandplace(each
representedasbaseclasses).Usingtheseclasses,mobileandcommunicating
agents can be developed along with locations for them to migrate and
interact. Telescript includes the necessary security controls for places to
IntelligentAgents 383
authenticateoneanother(throughtheplatforminterconnectprotocol)to
supportthesecuretransportofmobileagents.
A very simple example of Telescript migration is provided in Listing
11.6. In this example, a Telescript function is provided encapsulate the
movementmethod(go).Aswiththeagletsframework,aticketisusedto
representtherequesttotraveltoaremotehost.Thegomethodisusedto
initiatethetransfer.Notebelowthatthe‘*’symbolsrepresenttheobject
beingmanipulated,inthiscase,thecurrentobject.
Listing11.6: Asimplemethodintelescript.
changeLocation:op(locName:Telename;locAddress:Teleaddress)=
{
//Clearevents
*.disableEvents();
*.clearEvents();
//Movetonewlocation
*.go(Ticket(locName,locAddress));
};
Aglets
As described in the agent architectures section, Aglets is a Java-based
frameworkforagentconstruction.Inparticular,theJavaextensionsprovide
themeansformigrationofagents(includingtheirdata)betweenhoststhat
supporttheAgletsframework.TheAgletframeworkwasdevelopedatIBM’s
Tokyoresearchlaboratory.
AswithTelescript,Agletssupportsthedevelopmentofmobileagents
(using the Aglet class), and also places (implemented as Aglet contexts).
Given their mobility, Aglets communicate using messages which are
supportedbytheframework.TheAgletsframeworkalsosupportaglobal
namespace,suchthateachAgletisgivenauniqueidentifier.
The Aglets API supports the creation of Aglets, cloning of Aglets
(duplication),disposal(removaloftheAglet),andotherbehaviors.ThisAPI
is interesting because it extends a currently popular language with agent
characteristics.Thismeansthatdevelopersneednotlearnanewlanguage
384 ArtificialIntelligence
inordertodeployagents(andpotentiallycomplexsemantics),butinstead
simplythenewclassesforanexistinglanguage.
AnexampleofAglet’smobilitymechanismisprovidedinListing11.4.
Obliq
Obliq was an interpreted language developed by the Digital Equipment
Corporation(DEC).Thegoalwastocreatealanguageinwhichnetworking
and migration was a part of the core kernel. The language supports the
migration of procedures across a network as a closure. This provides a
secureformofmigrationwhichminimizestheagent’saccesstoremotehost
resources(onlythoseprovidedbytheremoteside).
Obliqusestheconceptofthehopinstructiontomigratetoanewhost.
Theagentismigrated,alongwithitssuitcase,whichrepresentsthedatathat
itmaycarrywithittothenewhost.Whentheagentarrivesatthenewhost,
itreceivesabriefing,whichrepresentsthedatamadeavailabletoitbythe
host(acontainmentmechanism).Thebriefingcancontainmorethanjust
data,andmayalsodescribefunctionsavailabletotheagentandthenewhost
aswellasotheragentsthatareavailable.
AgentTCL
AgentTCL,asthenameimplies,isasetofextensionsforagentprogramming
using TCL (Tool Command Language developed at the University of
CaliforniaatBerkeley).AgentTCLtransformsthestandardTCLlanguage
andinterpreterintoatransportableagentsystem.
OneofthefeaturesprovidedbyagentTCLismigration.Thisisprovided
by a new command called agent_jump. When the agent_jump
command completes, the agent restarts at the new host at the command
followingthepreviouslyexecutedagent_jumpcommand.
In addition to migration using the agent_jump command, Agent
TCLsupportsthosefeaturescommoninagentframeworks.Forexample,
communication using message passing, agent creation, cloning, and
destructioncommands,andtherequiredfeaturesofsecurity.
AportionofasimpleexampleisshowninListing11.7.
Listing11.7: SimpleexampleofagentTCLmigration.
#Catchanyexceptionsthatoccurfortheagent_jumpcommand
if{[catch{agent_jump$machine}result]}{
#Unabletomigrateto$machine
IntelligentAgents 385
puts“Couldn’tmigrate...”
}else{
#Migratedto$machine
#Notifyparentagentofmigration
agent_send$agent(root)0“I’mhere.\n”
}
AgentTCLisanotherinterestingexampleofanexistingpopularlanguage
that’sbeenextendedforagentdevelopment.
TraditionalLanguages
Agentsystemshavebeendevelopedinawiderangeoflanguages,fromthe
specializedlanguagesdesignedforAI(domain-specificlanguages)andalso
themoretraditionallanguagesinwideusetoday(C,C++,andothers).While
thedevelopmentofcertaintypesofagentsystemsmayrequirespecialized
capabilities(suchasthosedemonstratedbymobileagentsystems,ornatural
language systems), an intelligent application can be developed in any
language,fromLISPtoobject-orientedscriptinglanguagessuchasRuby.
AGENTCOMMUNICATION
In the domain of multi-agent systems, communication is an important
characteristictosupportbothcoordinationandthetransferofinformation.
Agentsalsorequiretheabilitytocommunicateactionsorplans.Buthow
thecommunicationtakesplaceisafunctionofitspurpose.Inthissection,
we’llexploresomeofthepopularmechanismsforcommunicationandtheir
semantics.
KQML(KnowledgeQueryandManipulationLanguage)
TheKQMLisaninterestingexampleofcommunicationfromanumberof
facets.Forexample,communicationrequirestheabilitytolocateandengage
apeerinaconversation(communicationlayer).Amethodforpackagingthe
messagesisthennecessary(messaginglayer),andfinallyaninternalformat
thatrepresentsthemessagesandissufficientlyexpressivetoconveynotonly
informationbutrequests,responses,andplans(contentlayer).
InanetworkofKQML-speakingagents,thereexistsprogramstosupport
communication.Theseconsistoffacilitatorsthatcanserveasnameservers
toKQMLcomponents,andhelpfindotheragentsthatcansatisfyagiven
386 ArtificialIntelligence
agent’srequest.AKQMLroutersupportstheroutingofmessagesandisa
front-endtoaspecificKQMLagent.
As KQML was originally written in Common LISP, it’s message
representationfollowstheLISPexample(balancedparentheses).AKQML
messagecanbetransferredtoanyparticulartransport(suchassockets)and
hasaformatthatconsistsofaperformativeandasetofargumentsforthat
performative. The performative defines the speech act which defines the
purposeofthemessage(assertion,command,request,etc.).Forexample,
thefollowingdescribestheKQMLmessagestructure(seeListing11.8).The
performative-namedefinestheparticularmessagetypetobecommunicated
(evaluate,ask-if,stream-about,reply,tell,deny,standby,advertise,etc.).The
senderandreceiverdefinetheuniquenamesoftheagentsinthedialogue.
Thecontentisinformationspecifictotheperformativebeingperformed.
Thiscontentisdefinedinalanguage(howtorepresentthecontent),andan
ontologythatdescribesthevocabulary(andmeaning)ofthecontent.Finally,
theagentcanattachacontextwhichtheresponsewillcontain(in-reply-to)
inordertocorrelatetherequestwiththeresponse.
Listing11.8: ThestructureofaKQMLmessage.
(performative-name
:senderX
:receiverY
:contentZ
:languageL
:ontologyY
:reply-withR
:in-reply-toQ
)
Let’snowlookatanexampleconversationbetweentwoKQMLagents.
Inthisexample,anagentrequeststhecurrentvalueofatemperaturesensor
in a system. The request is for the temperature of TEMP_SENSOR_1A
that’ssampledatthetemperature-serveragent.Thecontentisthe
request,definedintheprologlanguage.Ouragentmakingtherequestis
calledthermal-control-appl.
(ask-one
:senderthermal-control-appl
:receivertemperature-server
IntelligentAgents 387
:languageprolog
:ontologyCELSIUS-DEGREES
:content“temperature(TEMP_SENSOR_1A?temperature)”
:reply-withrequest-102
)
Ouragentwouldthenreceivearesponsefromthetemperature-server,
definingthetemperatureofthesensorofinterest.
(reply
:sendertemperature-server
:receiverthermal-control-appl
:languageprolog
:ontologyCELSIUS-DEGREES
:content“temperature(TEMP_SENSOR_1A45.2)”
:in-reply-torequest-102
)
KQML is very rich in its ability to communicate information as well
higher-level requests that address the communication layer. Table 11.6
providesashortlistofsomeoftheotherKQMLperformatives.
Table11.6: KQMLperformatives.
Performative Description
evaluate Evaluatethecontentofthemessage
ask-one Requestfortheanswertoaquestion
reply Communicateareplytoaquestion
stream-about Providemultipleresponsestoaquestion
sorry Returnanerror(can’trespond)
tell Informanagentofasentence
achieve Arequestofsomethingtoachievebythereceiver
advertise Advertisetheabilitytoprocessaperformative
subscribe Subscribetochangesofinformation
forward Routeamessage
KQML is a useful language to communicate not only data, but the
meaningofthedata(intermsofalanguageandontology).KQMLprovides
arichsetofcapabilitiesthatcoverbasicspeechacts,andmorecomplexacts
includingdatastreamingandcontrolofinformationtransfer.
388 ArtificialIntelligence
ACL(FIPAAgentCommunicationLanguage)
WhereKQMLisalanguagedefinedinthecontextofauniversity,theFIPA
ACLisaconsortium-basedlanguageforagentcommunication.ACLsimply
meansAgentCommunicationLanguageanditwasstandardizedthroughthe
FoundationforIntelligentPhysicalAgentsconsortium.AswithKQML,ACL
isaspeech-actlanguagedefinedbyasetofperformatives.
NOTE TheFIPA,orFoundationforIntelligentPhysicalAgents,isanon-profit
organization that promotes the development of agent-based systems.
Itdevelopsspecificationstomaximizetheportabilityofagentsystems
(includingtheirabilitytocommunicateusingtheACL).
TheFIPAACLisverysimilartotheKQML,evenadoptingtheinner
andoutercontentlayeringformessageconstruction(meaningandcontent).
TheACLalsoclarifiescertainspeech-acts,orperformatives.Forexample,
communicationprimitivesarecalledcommunicativeacts,whichareseparate
fromtheperformativeacts.TheFIPAACLalsousestheSemanticLanguage,
orSL,astheformallanguagetodefineACLsemantics.Thisprovidesthe
meanstosupportBDIthemes(beliefs,desires,intentions).Inotherwords,
SL allows the representation of persistent goals (intentions), as well as
propositionsandobjects.
Eachagentlanguagehasitsuse,andwhilebothhavetheirdifferences,
theycanalsobeviewedascomplementary.
XML
XMListheeXtensibleMarkupLanguageandisanencodingthatrepresents
dataandmeta-data(meaningofthedata).Itdoesthiswitharepresentation
thatincludestagsthatencapsulatethedata.Thetagsexplicitlydefinewhat
thedatarepresents.Forexample,considertheask-onerequestfromKQML.
ThiscanberepresentedasXMLasshownbelow:
<msg>
<performative>ask-one</performative>
<sender>thermal-control-appl</sender>
<receiver>temperature-server</receiver>
<sensor-request>TEMP_SENSOR_1A</sensor-request>
<reply-with>request-102</reply-with>
</msg>
TherearesomeobvioussimilaritiestoXMLandKQML.InKQML,the
tagsexist,butusedifferentsyntaxthanisdefinedforXML.Onesignificant
IntelligentAgents 389
differenceisthatKQMLpermitsthelayeringoftags.Noteherethatthe
<msg>tagistheouterlayeroftheperformativeanditsarguments.XMLis
veryflexibleinitsformatandpermitsverycomplexarrangementsofboth
dataandmeta-data.
XMLisusedinanumberofprotocols,includingXML-RPC(Remote
ProcedureCall)andalsoSOAP(SimpleObjectAccessProtocol).Eachof
theseusetheHyperTextTransportProtocol(HTTP)asitstransport.
CHAPTERSUMMARY
Intelligent agents are an interesting exploration in artificial intelligence
as they are key users of AI methods and techniques. While agents can
beviewedasanendapplicationofAI,theirstudyintroducesconceptsof
intelligentsystemsandfundamentalarchitecturesfortheirimplementation.
This chapter provided an introduction to agent systems, characteristics,
and architectures. You’ll find agent systems in games, web applications
(such as user assistance) as well as internally in complex and distributed
applications.
RESOURCES
[ANTS2003]“ActiveNetworksattheUniversityofWashington,”2003.
Availableonlineat:http://www.cs.washington.edu/research/networking/ants/.
[Aglets2002]“AgletsSoftwareDevelopmentKit,”2002,IBM.
Availableonlineat:http://www.trl.ibm.bom/aglets/.
[Bratman1987]“Intention,Plans,andPracticalReason,”CSLIPublications,
1987.
[Brooks 1986] “A Robust Layered Control System For a Mobile Robot,”
IEEEJournalofRoboticsandAutomation,April1986.
[GoogleAlerts]GoogleAlerts,2006.Availableonlineat:
http://www.google.com/alerts
[Gat 1991] Gat, E. “Integrating planning and reacting in heterogeneous
asynchronousarchitectureformobilerobots,”SIGARTBulletin2,1991.
[Hayes-Roth 1984] Hayes-Roth, Barbara, “BB1: An architecture for
blackboard systems that control, explain, and learn about their own
behavior,”StanfordUniversity,1984.
[IEEE-1471] “Recommended Practice for Architecture Description of
Software-IntensiveSystems.”AlsoknownasISO/IECDIS25961.
390 ArtificialIntelligence
Kotz,etal,“AgentTcl:TargetingtheNeedsofMobileComputers,”IEEE
InternetComputing,July/August1997.
Labrou,etal.“AgentCommunicationLanguages:TheCurrentLandscape,”
UniversityofMaryland,BaltimoreCounty,IEEEIntelligentSystems,
March/April1999.
Labrou,etal.“AProposalforanewKQMLSpecification,”TRCS-97-03,
1997.
Availableonlineat:http://www.cs.umbc.edu/kqml/papers/kqml97.pdf
Laird,John,Newll,Allen,andRosenbloom,Paul.“Soar:AnArchitecturefor
GeneralIntelligence,”ArtificialIntelligence,33:1989.
Mabry,SusanL.,Bic,Lubomir,F.“BridgingSemanticGapswithMigrating
Agents,” International Conference on Parallel and Distributed
ComputingSystems(PDCS),1999.
Muscettola,etal.“RemoteAgent:ToBoldlyGoWhereNoAISystemHas
GoneBefore,”ArtificialIntelligence,1998.
Newell, Allen Unified Theories of Cognition Harvard University Press,
1990.
Nicolescu, Monica, and Mataric, Maja. “A hierarchical architecture
for behavior-based robots,” Proceedings, First International Joint
Conference on Autonomous Agents and Multi-Agent Systems, pages
227-233,BolgnaItaly,July2002.
“Soar(cognitivearchitecture),”Wikipedia.Availableonlineat:
http://en.wikipedia.org/wiki/Soar_(cognitive_architecture)
REFERENCES
Franklin,S.,andGraesser,A.“IsItanAgentorJustaProgram?ATaxonomy
forAutonomousAgents”fromtheProceedingsoftheThirdInternational
WorkshoponAgentTheories,Architectures,andLanguages,1996.
Lashkari,Y.;Metral,M.;andMaes,P.1994.“CollaborativeInterfaceAgents.”
In the proceedings of the Twelfth National Conference on Artificial
Intelligence,444-450.MenloPark,CA.
Shaw,M.,andGarlin,D.SoftwareArchitecture:PerspectivesonanEmerging
DisciplinePrentice-Hall,1996.
Stuart Russell, Peter Norvig. Artificial Intelligence: A Modern Approach
SecondEdition,Addison-Wesley,2003.
Ananova,VirtualNewscaster.Availableonlineat:
http://www.ananova.com/video
[RFC439],Cerf,V.“ParryEncounterstheDoctor,”January21,1973.
IntelligentAgents 391
EXERCISES
1. Inyourownwords,definehowanagentandaprogramdiffer.
2. Definethemajoragentpropertiesandhowtheyaffectanagent.
3. Given the agent environment properties in Table 11.2, provide two
environmentexamplesandthepropertiestheysupport.
4. What other types of interface agents could be used in an Internet
environment?
5. ExploreagameenvironmentthatsupportsNPCscripting.Howrichis
thescriptandwhatkindsofbehaviorscouldbeimplemented?
6. DescribehowElizacarriedoutaseeminglyintelligentconversationwith
auser.
7. Defineanadvantageofmobileagentsovertypicalsystemsthatrelyon
remotecommunication.
8. If you were to create a new mobile agent framework, describe the
mannerinwhichserializationandmigrationwouldoccur.Howwould
yourapproachdifferfromthemobileagentframeworksdiscussedinthis
text?
9. Compare and contrast the reactive and deliberative agent
architectures.
10. Explain how communication and coordination work in a blackboard
architecture.
11. While subsumption and behavior networks are both reactive
architectures, they are architecturally very different. Define the two
architecturesandhowtheycompareanddiffer.
12. Homerisadeliberativeagentarchitecture.WhatisuniqueaboutHomer,
andforwhichapplicationswoulditbeused?
13. Explain the planning mechanism used by the Procedural Reasoning
System.
14. Telescript and aglets are both mobile agent runtime environments.
Definehoweachspecifiesmigration.
BIOLOGICALLY
Chapter 12 INSPIREDAND
HYBRIDMODELS
F
romtheperspectiveofbiologicallyintelligentsystems,wefindahybrid
of methods at play. Humans are adept at numerical computation
and symbolic computation. We support associative memories, and
canrecallimagesandconceptsbasedonincompletefacts.Wearehighly
parallelandconnectionistinnature.Inshort,weareahybridofindependent
methodsthatintegratetogetherandresultinthehighestformofintelligence
ontheplanet.Inthischapter,we’llexploresomeofthehybridmethodsthat
resultfromintegratingdifferentAItechniquestogether.
CELLULARAUTOMATA(CA)
CellularAutomata,orCA,isaninterestingexampleofsystemswithvery
simplerulesthatexhibitverycomplexbehavior.CAsarediscretesystems
that have been used in a variety of contexts, from theoretical biology to
computabilitytheory.ACAiswhat’sknownasTuringcompleteasitcanbe
usedtocomputeanycomputablefunction,albietinanunusualfashion.
ThefirstcellularautomatonwasdevelopedbyJohnVonNeumannand
wasbasedonaconversationwithStanislawUlam.Thepurposeofthisfirst
CA was as a vehicle to understand the conceptual requirements for self-
replicatingmachines.
394 ArtificialIntelligence
CAscanbesimplydescribedasafinite-statemachine.Thestatesofa
systemarethenafunctionofsomecollectionofotherstatesinthesystem.
Atadiscretetimestep,eachstateofthesystemisupdatedgiventheother
currentstatesofthesystem,producingagloballychangingsystem.These
changescanexhibitcomputation,classification,andevenself-replication.
Let’snowlookatsomecellularautomataclassestobetterunderstand
theircapabilities.
One-DimensionalCAs
ThesimplestformofCAistheone-dimensionalCA.Thisconsistsofarowof
cells,whereeachcellfollowsagloballydefinedsetofrules.Eachcellhasa
givenstate,whichisalteredateachdiscretetimestepusingtheglobalrules.
Thecellstatescanbebinary,ortakeonmultiplevalues.
Additionally,therulescanbesimple(suchasusingthecellstatestothe
currentcellanditstwoneighbors)uptomanyneighborsindicatingalarge
numberofrules.
Note that in the case of a rule set that incorporates the state of the
currentcellandthatofitstwoneighbors,thereareeightrulespossible.A
FIGURE12.1: Simplerulesetforourone-dimensionalCA.
FIGURE12.2: GenerationalbehaviorofthesimpleonedimensionalCA.
BiologicallyInspiredandHybridModels 395
samplerulesetisshowninFigure12.1.Theserulessimplydefinethatacell
isbornwhenacellinthepreviousgenerationappearstoitsleftorright(but
onlyone)ofthecurrentneighborhood.
Theresultofthisruleset(fromFigure12.1)isshowninFigure12.2,
giventheinitialgeneration0.NotetheneighborhoodshowninGeneration
0astheboldlineinthecenter(whichwilluserule2).Wecanthinkofthe
cellsasliving(darkcell)andempty(whitecell).Therulesthendefinethe
birthcriteriaforanewcellgiventhestateofitsneighbors.
Givensuchsimplerules,theCAiscapableofgeneratingverycomplex
and interesting patterns. But while interesting, one-dimensional CAs are
quitelimited.Let’snowtakeamoredetailedlookattheCAthatextends
anotherdimension.
Two-DimensionalCAs
Themostfamoustwo-dimensionalCAwasintroducedinthe1970sbyJohn
Conway (and popularized through Scientific American). This was called
theGameofLife.First,theuniverseforthisCAisatwo-dimensionalgrid.
Each cell has eight neighbors (for a given cell in the grid, each cell that
surroundsthecellisaneighbor).Liketheone-dimensionalCA,lifeoccurs
ingenerations.Ateachgenerationalstep,anewuniverseiscreatedfromthe
old.Thismeansthatifacelldies,survives,orgivesbirth,itoccursinthenext
generation(whereeachcellismanipulatedinparallelandbirthsinthenew
generationdonotcontributetoneighborsintheoldgeneration).
TherulesofConway’sGameofLifeareverysimple(seetheConway
neighborhoodshowninFigure12.3).Ifacelliscurrentlyaliveandhasless
thantwoneighbors,thenitdiesinthenextgeneration.Ifacelliscurrently
aliveandhastwoorthreeneighbors,thenitsurvivesintothenextgeneration.
Butifacellisalive,andhasmorethanthreeneighbors,thenitdiesfrom
overcrowding. Finally, if a cell is vacant but has three neighbors, then it
becomes alive in the next generation. From this very simple set of rules,
very complex behaviors are possible, even to the extent of being Turing
complete.
NotethatforConway’srule,onlythenumberofneighborsisrelevant.
Thismeansthattherecanbeamaximumof18rules(ninerulesforeachof
thecellonoroffstate).Otherrulesystemscouldinsteadusespecificcell
configurations(foramaximumof512rules).
A simple example that shows the changes to a universe over time is
showninFigure12.4.Thisexampleillustrateswhatiscalledaglider.This
objectmoves(translates)diagonallyonecelleveryfourgenerations.
ConwayApplication
Implementing Conway’s game of life is very simple. A portion of this is
providedbelowtodemonstrateConway’srule.Twocompleteuniversesmust
bemaintainedwhichalternatelyrepresentthepresentandpast.Thecontents
ofthepresentuniversearegeneratedfromthepastusingConway’srules.
Oncewedeterminewhichuniverseisthepastandwhichisthepresent,we
simplyapplytherulestothepasttoidentifywhichcellsareset(orreset)in
thefuture.
HE C
NT
D
O
ThecompletesourcefortheConway’slifeapplicationcanbefoundon
the CD-ROM at ./software/ch12/life.c. This application uses ncurses,
whichisavailableinGNU/Linux,orinCygwinforWindows.Alsoonthe
CD-ROMaresampleCApatternfilesthatcanbeusedtodemonstrate
interestingCAobjects.
Listing12.1: Conway’sLifeRuleImplementationinC.
voidgenerateUniverse(intgen)
{
inty,x,neighbors,past,newcell;
/*Whichisthepreviousuniversetotestfrom?*/
past=(gen==0)?1:0;
/*Evaluateeachcellfromthepasttodeterminewhatthefuturecell
*shouldbe.
*/
for(y=0;y<MAX_Y_GRID;y++){
BiologicallyInspiredandHybridModels 397
for(x=0;x<MAX_X_GRID;x++){
/*Computeneighborscellsaroundthecurrent*/
neighbors=cell(past,y-1,x-1)+cell(past,y-1,x)+
cell(past,y-1,x+1)+cell(past,y,x-1)+
cell(past,y,x+1)+cell(past,y+1,x-1)+
cell(past,y+1,x)+cell(past,y+1,x+1);
/*Conwayrule
*
*Death(cellon,lessthan2neighbors)
*Survival(cellon,2or3neighbors)
*Birth(celloff,3neighbors)
*Otherwise,newcellisoff
*
*/
newcell=0;
if(cell(past,y,x)&&(neighbors<2))newcell=0;
if(cell(past,y,x)&&((neighbors==2)||(neighbors==3)))
newcell=1;
if((!cell(past,y,x))&&(neighbors==3))newcell=1;
/*Birth,survival,ordeath*/
cell(gen,y,x)=newcell;
}
}
return;
}
WhileConway’sruleuseseightneighborstodeterminethecellstate,
wecouldincreasethesizeoftheneighborhoodfordifferentinteractions.
Also,what’sdescribedhereisahomogeneouswhereeachcellimplements
thesamerule,butcellscouldoperatewithdifferentrules,basedontheir
locationorbasedonaglobalsystemstate(heterogeneousCA).
FIGURE12.5: Cellularautomataglidergun(P30).
398 ArtificialIntelligence
TheoutputofthesampleapplicationisshowninFigure12.5.Thisshows
aP30glidergunemittinggliders(tothelowerright).
TuringCompleteness
TheTuringcompletenessofCA(abilitytoemulateanothercomputational
modelwithCA)wasproveninStephenWolfram’s“ANewKindofScience.”
Thisbookshowedthatrule110ofaone-dimensionaltwo-stateCAexhibited
theabilitytocreatestructuresthatwouldsupportuniversalcomputation.
Thiswasexcitingbecauseitopenedupthepossibilityofnaturalphysical
systemssupportinguniversalcomputation.
EmergenceandOrganization
CellularAutomata,andConway’sLife,isinterestingbecauseevenwithsimple
rulesetssuchasConway’s,characteristicssuchasemergentcomplexityappear.
Emergenceisapropertyofcomplexsystemsandcanbeverysimplydescribed
astheappearanceofcomplexsystembehaviorsfromtheinteractionsofthe
lower-levelelementsofthesystem.Thetermself-organizationhasalsobeen
usedtodescribeCA,wheretheinternalorganizationofasystemincreases
incomplexitywithoutanyexternalguidanceorpressure.
ARTIFICIALIMMUNESYSTEMS
Autonomiccomputingsystemsarethosesystemsthathavetheabilitytomanage
themselvesforself-configuration,self-optimization,self-protection,andself-
healing.Suchsystemsperformthesetasksinawaythatmimicshumanbiology.
Our autonomic nervous system, for example, maintains our heart rate and
FIGURE12.6: Thelayeredarchitectureofanautonomiccomputingsystem.
BiologicallyInspiredandHybridModels 399
stabilizesourbodytemperaturewithoutourconsciousinvolvement.Inthesame
way,anautonomiccomputingsystemallowssystemadministratorstofocuson
higher-levelbusinessgoalsinsteadofthedetailsofsystemmanagement.
Anautonomiccomputingsystemismadeupofseveralcomponentsthat
workinconcerttoimplementself-managementcapabilities.Thesecomponents
areimplementedinalayeredarchitecture,asshowninFigure12.6.
Atthebottomofthearchitecturearemanagedresources,whichmake
uptheelementsthatthesystemwillmonitorandcontrol.Theseelements
canbehigh-levelinformationtechnology(IT)systems(suchasservers)or
applicationswithinasinglesystem.Theinterfacetothemanagedresourceis
calledatouchpoint.ThetouchpointprovidesasetofGetandSetoperations
formonitoringandmanagingtheresource.
Atthenextlevelaretouchpointautonomicmanagers.Theseautonomic
managers work with the touchpoint to gather data about one or more
resources. An autonomic manager commonly implements an intelligent
algorithmtoprovideself-management,andwhenachangeisrequired,you
usethetouchpointagaintoaltertheenvironment.Eachautonomicmanager
implementsapolicyofself-managementforself-configuration,self-healing,
self-optimization,orself-protection.
Anothersetofautonomicmanagersiscalledtheorchestratingautonomic
managers. These managers provide coordination capabilities over several
lower-levelautonomicmanagers.Finally,atthetopofthearchitectureis
the management console, which you use to monitor the activities of the
autonomiccomputingsystemandalsodefinethepoliciestobeusedinself-
management.
Self-Managementcapabilities
Control can exist at multiple layers within the autonomic computing
system architecture but is most often implemented in the autonomic
FIGURE12.7: Controlloopforanautonomiccomputingelement.
400 ArtificialIntelligence
managers.Theembeddedcontrolloopfollowsaspecificflow,thoughnot
allelementsmaybeimplemented.Figure12.7illustratestheelements
ofthecontrolloop.
Themonitorelementcollectsandfiltersincomingdata,whichresults
inasymptom.Thesymptomisfedtotheanalyzeelement,whichanalyzes
thesymptom,potentiallyperformsmodelingandanalysisofthesymptom,
identifies the course of action to take (if any), and generates a change
request.Fromthechangerequest,theplanelementcreatesachangeplan
toachievethegoal.Finally,theexecuteelementexecutestheplantoachieve
theintendedchange.Theknowledgeelementencapsulatestheshareddata
thatthemonitor,analyze,plan,andexecuteelementsuse.
Thiscollectionofcontrolloopsatthevariouslayersintheautonomic
computingstructureimplementstheattributesofself-management.These
attributesinclude:
• Self-configuring
• Self-healing
• Self-optimizing
• Self-protecting
Autonomicmanagerscanimplementasingleself-managementattribute
and operate within that domain. Orchestrating autonomic managers can
work with the lower-level autonomic managers within attributes (such as
self-configuring)oracrossattributestoachievehigher-levelgoals.
Touchpoints
A touchpoint is an interface to an underlying managed resource. The
touchpoint implements sensor and effector behaviors that higher-level
autonomicmanagerscanusetomanagetheresource.Thesensorsgather
informationaboutaresource,andtheeffectorschangethestateorbehavior
of the underlying resource. A touchpoint may simply present a sensor/
effectorinterfaceintheformofanAPI,oritmayimplementsomelevel
ofinternalintelligencetooffloadfromhigher-levelmanagers(suchasdata
filteringordataaggregation).
TouchpointAutonomicManagers
The touchpoint autonomic managers provide intelligence for managing
resourcesthroughtheirtouchpoints.Thesemanagersmayimplementthe
controlloop(orpiecesofit)dependingonthegoalofthemanager.
BiologicallyInspiredandHybridModels 401
OrchestratingAutonomicManagers
A touchpoint autonomic manager manages a single resource through the
touchpointforthatmanagedresource.Orchestratingautonomicmanagers
coordinateacrosstouchpointautonomicmanagerstoprovidesystem-wide
autonomicbehavior.
Orchestrating autonomic managers may operate in one of two
configurations.Inthefirstconfiguration,themanagerscoordinatetouchpoint
autonomicmanagersofthesametype(suchasself-configurationautonomic
managers). In the second configuration, the managers coordinate across
disciplines (such as working with a self-protecting touchpoint autonomic
managerandaself-healingtouchpointautonomicmanager).Ineithercase,
theorchestratingautonomicmanagershavegreatervisibilityintothesystem
tobemanagedandcanbettercontributetooverallsystemmanagement.
Further, autonomic managers at all layers can work against each other
to achieve their goals. An orchestrating autonomic manager that views the
underlyingsystemfromagreaterperspective(havinggreatervisibilityintothe
overallsystemthanafocusedautonomicmanager)canmoreoptimallymanage
the system from one or more self-management disciplines. For example,
considertwoself-optimizingautonomicmanagersthatattempttomanipulatea
commonparameterforindependentgoals.It’spossiblethatintheircompeting
goals,theyde-tuneeachanother,resultinginnooptimization.Anorchestrating
autonomicmanagercouldhelparbitratesituationssuchasthese.
IntegratedManagementConsole
AtthetopofthelayeredautonomiccomputingarchitectureistheIntegrated
ManagementConsole(seeFigure12.8).Thisconsoleistheuserinterface
(UI)fortheautonomicsystem.Itallowsyoutomanagethesystemanddefine
thepoliciesforself-management.Italsoallowsyoutomonitortheactivities
oftheautonomicsystem.Dependingonthebehaviorofthesystem,youcan
changepoliciestobettermanagethesystemandachieveyourgoals.
402 ArtificialIntelligence
FIGURE12.8: IntegratedManagementConsole.
AutonomicSummary
Autonomic systems is an interesting architectural pattern for the
developmentofintelligentsystems.It’sbuiltaroundalayeredarchitecture
with very distinct responsibilities at each of the layers. The autonomic
systemspatterncanbeusedtodevelopawiderangeofsystems,fromthose
thatprotectandmanagerservers(suchasIBM’sgoalforautonomicsystems)
tothedevelopmentofintelligentagentsystems.
ARTIFICIALLIFE
ArtificialLife(oralife)isthestudyoflifethroughsimulation.It’salsomore
commonlyassociatedwiththeevolutionofsimplelifeformsthatexhibitsome
desired behavior. For example, early alife simulations focused on the ability
to breed creatures with varying control systems that could survive in their
environments.Othersimulationsfocusedoneconomicbehaviorsandtrading,
andothersonthesynthesizingoflife,albietinaformdifferentthanourown.
NOTE Note that the earlier discussion of Cellular Automata was a form
of artifical life (prior to the term being coined). CAs are low-level
implementationsofartificiallife(atthecelllevel)wheretherulesand
stateoftheenvironmentdeterminebehaviors.
BiologicallyInspiredandHybridModels 403
Inthissection,we’llexploresomeoftheexamplesofartificiallifeand
thenlookatasimplesimulationthatdemonstratesthebasicpropertiesof
artificiallifeandsimulatedevolution.
Echo
JohnHolland,apioneerinthefieldofcomplexadaptivesystems,createdone
oftheearliestartificiallifesimulationscalledEcho.Holland’sEchoprovides
anenvironmentwhereagentsinteractinactivitiessuchascombat,mating,
andtrade.Alongtheway,agentsdevelopstrategiestosurviveandcompete
in environments that are resource constrained. The environments and
agentsincludethenecessary“knobs”totweakthesimulation’sparameters
toprovidetheabilitytoplay“what-if”experiments.
Holland’s Echo remains an important artificial-life simulation and is
uniqueinitseconomicmodeling.
Tierra
Tierra is another interesting simulator for the evolution of artificial life
(fromTomRay).Inparticular,thelifeinthissimulatorconsistsofevolved
programs. The environment consists of a simulated computer where the
evolvedlifeexecute.TheprogramsvieforCPUtime(energy)toexecute
andthenreproduceintheenvironment(theRAM-space).
With Tierra, very complex behaviors have been evolved, including
complexinteractionsbetweenagents.Forexample,evolutionaryarmsraces
havebeenobservedincludinghost/parasiterelationshipsofagents.TomRay’s
Tierraisanotherusefulsimulatorforthestudyofevolutionandecologyof
novelagentsinauniqueenvironment.
SimulatedEvolution
Let’snowlookatasimulationthatprovidesthebasicelementsnecessary
tosupportevolutionthroughnaturalselection.We’llstartwithareviewof
theenvironment,andthenreviewthebug(agentintheenvironment),its
sensors,andavailableactions.Finally,we’lllookataportionofthesimulation
tobetterunderstandhowitworks.
Environment
TheenvironmentforoursimulationisasimpleNxNgrid.Theenvironment
contains cells that can be empty, or occupied by a bug or food. The
environment typically contains a number of cells with food, and a lesser
numberofbugs.
404 ArtificialIntelligence
Thegoalistoevolveabugsuchthatitcansurviveintheenvironment.
Witheachstepthatthebugtakes,itlosesenergy.Ifthebug’senergyfallsto
zero,thenitdiesandisreplacedbyamutatedversion.Butifthebugisthe
oldestinthesimulation,thenitisallowedtoberebornwithoutmutation.
This allows the simulation to breed in an elitist fashion. The best bug is
rebornwithoutmodification,butlesserbugsaremutatedinthehopesthat
the mutation produces a better bug. If it does, one could postulate that
evolutionisoccurring.
Ifabugmovesintoacellthatcontainsfood,thenthefoodisconsumed
bythebugandanewfooditemplacedsomewhereelseintheenvironment
(intoanemptycell).Eachtimefoodisconsumed,energyisincreasedforthe
bug.Buteachtimeamoveismade,energyisconsumed.Thegoal,therefore,
istocreateabugthatisconstantlyinsearchoffoodtominimizeenergyuse
andmaximizeenergyconsumption.
TheBug(orAgent)
Thebugsthatpopulatetheenvironmentareprovidedwithareactivecontrol
systemthatreadsthecurrentstateofthesurroundingenvironment,andthen
choosesanoptionbasedonthesensorinputs.Let’sbeginwithadiscussion
oftheinputsensor.
Figure12.9providesadiagramofthebug’sfieldofperception.Thebug
isatthecenter(andiffoodhadbeenpresent,itwouldhavebeenconsumed).
Thereareeightseparatefieldsinthebug’sview.Atthetopleft(field0)are
fourcells.Ifafooditemispresentinanyoneofthese,thenthesensorinput
0isset.Similarly,sensor1containstwocells,andifeitherofthemcontain
afooditem,thesensorinputisset.
Let’sthelookattheapplicationofthesensorforabuginanenvironment
(seeFigure12.10).Thisexampleshowsa9by9fragmentoftheenvironment.
The cells marked with ‘F’ are food, while the center marked ‘B’ is the
locationofthebuginquestion.Theboldlinesdifferentiatethesensorinputs
asdefinedbeforeinFigure12.9.AtthebottomofFigure12.10isthesensor
inputthatresultsfromtheenvironment.Notethatthenumberofelements
isn’tasimportantastheexistenceofafooditeminthatsensorregion.
Thebugisprovidedwithaminimalsetofactions,essentiallytheability
tomoveNorth,South,East,orWestasasinglemove.Thereactivecontrol
systemfortheagentisthensimplyasensormappingfordirectiongivena
classificationoftheinputs.Theclassificationthatmatchesclosestistheone
permittedtoexecute.Notethatinthecaseofatie,we’lltakethefirstaction
that was encountered. A sample classifier is shown in Figure 12.11. The
classifiercanbedifficulttoread,asit’safunctionoftheenvironment,but
onethingthatdoesstandoutisthattheagenthasamotivationtomoveto
theEast(asitmatchesallsensorinputsexcepotforNorth).Thisparticular
classifierwasevolvedbythesimulatorandcouldsurvivefortensofthousands
ofmoves.
Let’s now look at the moves that will be made by our bug given the
evolvedcontrolsystemshowninFigure12.11.InFigure12.12isouroriginal
environment from Figure 12.10. We’ll apply the classifer and count the
numberofmatches(numberoftimesasensorisnon-zero,andtheclassifier
elementisnon-zero).Thesumofthesedeterminestheapplicabilityofthe
classifiertothecurrentenvironment.Theonewiththehighest(orthefirst
ifthereismorethanone)ischosenastheactiontotake.
For the initial position of the bug (as shown in Figure 12.12), the
classifiercountsareNorth(2),South(3),West(2),andEast(4).Usingthe
largestmatch(4),thebugmakesamovetotheEast.Theremainingmoves
canbedeterminedbythereader,butconsideringtheagent’sinitialenergy
of10,afoodvalueof5,andthecostofamove1,theagent’sendingenergy
is20.Thecontrolsystem evolved for this bug is obviously beneficial and
allowsittofindandconsumefoodinitsenvironment.
InListing12.2isthecoreofthesimulationwhichisusedtodetermine
the next move to make for the current bug. Any number of bugs can be
competingintheenvironmentatthesametime,sothisfocusesonone.After
adjustingthebug’sage,thecurrentsensorsareevaluatedgiventhebug’s
positionandtheenvironment.Thesensorsarethenmatchedtotheclassifiers
andtheonethatmatchesbestischosen.Thebugismoved,andiffoodisat
thenewlocation,itisconsumedandthebug’senergyisincreased.
HE C
NT
D
O
ThefullsourcefortheartificiallifesimulationcanbefoundontheCD-
ROMat./software/ch12/simevol.c.
Listing12.2: Coreofthebugsimulation-theactionselectionroutine.
voidmakeMove(bug_t*bug)
{
unsignedcharsensors[MAX_SENSORS];
unsignedcharc_sum[MAX_CLASSIFIERS];
intx,y,c,s;
/*Agethebugsoweknowhowlongitlived*/
bug->age++;
/*Keeptrackoftheoldestbug*/
if(bug->age>maxAge)maxAge=bug->age;
/*
*Sensors:
*00122
*00122
*33.44
*55677
*55677
*
*/
y=bug->loc.y;
x=bug->loc.x;
/*Functionenvreturnsthecontentsofthecell*/
sensors[0]=env(y-2,x-2)+env(y-2,x-1)+env(y-1,x-2)+env(y-1,x-1);
sensors[1]=env(y-2,x)+env(y-1,x);
sensors[2]=env(y-2,x+2)+env(y-2,x+1)+env(y-1,x+2)+env(y-1,x+1);
BiologicallyInspiredandHybridModels 407
sensors[3]=env(y,x-2)+env(y,x-1);
sensors[4]=env(y,x+2)+env(y,x+1);
sensors[5]=env(y+2,x-2)+env(y+2,x-1)+env(y+1,x-2)+env(y+1,x-1);
sensors[6]=env(y+2,x)+env(y+1,x);
sensors[7]=env(y+2,x+2)+env(y+2,x+1)+env(y+1,x+2)+env(y+1,x+1);
/*Matchtheclassifierstothesensors*/
for(c=0;c<MAX_CLASSIFIERS;c++){
c_sum[c]=0;
for(s=0;s<MAX_SENSORS;s++){
if(bug->classifier[c][s]&&sensors[s])c_sum[c]++;
}
}
/*Nowpicktheclassifier(action)withtheclosestmatch*/
s=0;
for(c=1;c<MAX_CLASSIFIERS;c++){
if(c_sum[c]>c_sum[s])s=c;
}
/*Removethebugfromthecurrentlocation*/
setLocation(bug->loc.y,bug->loc.x,““);
/*Changelocation*/
bug->loc.y=upd(bug->loc.y+actions[s].y);
bug->loc.x=upd(bug->loc.x+actions[s].x);
/*Istherefoodatthisnewlocation?*/
if(environment[bug->loc.y][bug->loc.x]){
/*Consumefoodandincreaseenergy*/
bug->life+=FOOD_VALUE;
environment[bug->loc.y][bug->loc.x]=0;
/*Addanotherfoodtotheenvironment*/
findEmptySpot(&y,&x);
environment[y][x]=1;
setLocation(y,x,“*”);
}
/*Reduceenergyformove*/
bug->life--;
/*Bughasdied,mutate*/
if(!bug->life)mutateBug(bug);
/*Placethebugbackintotheenvironment*/
setLocation(bug->loc.y,bug->loc.x,“B”);
return;
}
408 ArtificialIntelligence
Ifthebugdiesbecauseitrunsoutofenergy,afunctioncalledmutateBug
isinvoked.Thisfunctioncheckstoseeifthecurrentbugisthelongestlived
of the current set, and if so, it’s allowed to be reborn without mutation.
Otherwise,theclassifiersofthebugaremutatedatagivenrate.Thebug’s
ageisthensettozero(anewborn)andthesimulationcontinues.Thisisthe
naturalselectionportionofthesimulation.Ifthebugisthebest,itisthe
fittestandispermittedtocontinuewithoutmodification.Otherwise,thebug
ismutatedatsomerateandthenewbugisevaluatedintheenvironment.
Thisallowsthesimulationtomaintainthebestbug,butthenmutatethe
others that don’t measure up with the intent that these will evolve into
betterbugs.
Whenthathappens,theolderelitistbugwillbemutated,causingthe
processofcompetitiontocontinue.
VariationsofArtificialLife
Inadditiontoartificiallifeusedintheformofnaturalselectionandsynthetic
evolution, the concepts have also been applied in such fields as artificial
chemistry. This was started in the artificial-life community as a way to
simulatetheprocessesofchemicalreactions.
LindenmayerSystems
Lindenmayer Systems (otherwise known as L-systems) are a model of
biological development that’s based on mathematical formalisms. An
interestingcharacteristicofbiologicalformsisthattheyarebranchedintheir
growth.ThetwomostprevalentusesofL-systemsareinthegenerationof
fractalsandintherealisticmodelingofplants.ButL-systemshavefound
there way into other applications such as generation of neural network
structures (nodes and their connections). This section will provide an
introductiontoL-systemsandthebasicsofgrammarrewritingsystems.
FIGURE12.13: SimpleL-systemillustratingabasictransformation.
BiologicallyInspiredandHybridModels 409
AtthecoreofanL-systemisarewritingsystem.Givenanalphabet,and
asetofrulesforalphabettransformation,theabilitytotransformanelement
fromthealphabet,orasequenceofelementsfromthealphabetisprovided.
Thisissometimesreferredtoasasubstitutionsystem.
Let’s now look at a couple of simple examples. The first is shown in
Figure12.13anddemonstratesanL-systemofatwo-letteralphabetandtwo
rules.Ateachstep,thecurrentstringisrewrittentothestringbelow.Each
letterundergoesatransformation,asindicatedbytheconnectinglines.
Another example is provided in Figure 12.14. In this example, our
alphabet consists of the numbers 0 and 1. Note the symmetry produced
bythissubstitutionsystem.Ateveryothergeneration,theinitialstringis
reproducedbutattwicethesize.
Each of these examples are a class of L-systems called deterministic
and context-free. From the branching behaviors seen in the previous
two examples, we can see how they could be applied to neural network
architectures.Nowlet’slookathowL-systemsareusedtogenerategraphical
structures,whichcouldalsobeusedforneuralnetworkstructures.
ConsidertheexampleshowninFigure12.15.Withasinglerule,we’re
abletogenerateabranchingtreestructure.Butnoteattherightmostportion
ofthefigureistherepresentationasatwo-layerneuralnetwork.
FIGURE12.14: SimpleL-systemillustratingsymmetryofstringgeneration.
FIGURE12.15: SimpleL-systemresultinginaneuralnetworktopology.
410 ArtificialIntelligence
FIGURE12.16: AplantstructurecreatedbysimpleL-systemrules.
L-systems (of the bracketed form) can develop very life-like plant
structures (as shown in Figure 12.16). The bracketed L-system includes
operators for pushing and popping branches. This allows a trunk to be
branched,tooperateonthatbranch,andthenrevertbacktothetrunk.
FUZZYSYSTEMS
Fuzzylogicisanidealwaytotakeanalogconceptsfromtherealworldand
makethemmanageableinthediscreteworldofcomputersystems.Instead
of dealing with crisp values and their semantics, we deal instead with
membershipinsetsthatrepresentthesemanticsofthevaluesystem.Inthis
way,wecanoperateinthedomainofconditionalexpressionsusingdegrees
ofmembershipinfuzzymembershipfunctions.
FuzzylogicwascreatedbyLotfiZadehattheUniversityofCaliforniaat
Berkeleyin1965.Themethodwascontroversial,butadoptedwithsuccessin
Japaninavarietyofsuccessfulapplications.AdoptionintheU.S.wasmuch
slower,butapplicationshavegrownwiththismethod.Thisisbecausefuzzy
logiccanbeeasilyimplementedinlowcostandlow-endmicroprocessors.
IntroductiontoFuzzyLogic
From a control perspective, fuzzy logic solves a real problem when
considering the semantics of values. In human language, we talk about
discretestates,butthiscreatesproblemsforcontinuousrangesofvalues.
Forexample,wecanrefertotemperatureashotorcold,butwhatdothey
reallymean?Further,ifwetrytobuildlogicbasedonadiscretefactorsuch
ascold,wequicklyrunintoproblems.Thecontinuousnatureofthingslike
temperaturearenotwellsuitedforthebinarynatureoflogic.Forexample,
BiologicallyInspiredandHybridModels 411
FIGURE12.17: Fuzzylogicmembershipfortemperature.
ifwehavejusttwotemperatures,thenthere’satemperaturebywhichwe
transitionfromcoldtohot,andtherealworlddoesn’tworkthatway.
The answer is to use a mechanism that takes real-world ranges and
translatesthemintodiscretestates,butinafuzzyway.Forexample,instead
ofourtemperaturebeinghotorcold,ourtemperatureisinsteadamember
ofthestates.Let’slookatanexampletoillustratethisfurther.
Figure12.17providesgraphindicatingthemembershipfunctionsforour
temperaturestates.Amembershipfunctionisonethatdefinesthedegree
ofmembershipforavaluetoagivenstate(insteadofsimpletrueorfalse,
aswouldbethecasefortraditionallogic).Forexample,ifourtemperature
isT1orbelow,thenwecansaythatthetemperatureis1.0coldand0.0hot.
Butsincethemembershipforhotis0,wecanomitit.Ontheotherside,
temperatureT3is1.0hotand0.0cold(orsimply,Hot).
Between T1 and T2 is where fuzzy logic begins to make sense. For
example,attemperatureT2,thefuzzytemperatureis0.5coldand0.5hot.
Soit’sneitherhot,norcold.Thisallowsustorefertotemperaturesinhuman
terms,whileallowingacomputertodealwiththeminanumericsense(in
termsofmembershipinatemperaturecategory).
NOTE NoteinFigure12.17thatthere’snotadistinctjumpfromthecoldstateto
thehotstate.Insteadthere’sagradualtransitionfromcold(1.0/0.0)to
warm(0.5/0.5)tohot(0.0/1.0)withmanyintermediatestatesbetween.
Thisisthepowerbehindfuzzylogic.Ratherthandistinctsteps,there’s
agradualtransitionbetweenthetwo.
FuzzyLogicMapping
Let’snowexplorehowwewouldrepresentthefuzzymembershipinasimple
application.Let’sreturntoourtemperatureexample,butinthiscase,we’ll
notevaluesforeachinordertodevelopthemembershipfunctions.
412 ArtificialIntelligence
FIGURE12.18: Temperaturemembershipfunctions.
Figure 12.18 provides a graph of the membership functions for the
coldandhottemperatures.Asshown,ifthetemperatureis45degreesFor
below,thenit’scold.Ifit’s75degreesForabove,thetemperatureishot.At
atemperatureof60degreesF,it’sbothcold(0.5)andhot(0.5).
WecanrepresentthisveryeasilyasisshowninListing12.3.Thetwo
membershipfunctionsm_coldandm_hot testfortheextreme,and
thentheslopetodeterminethemembershipdegree.Theslopecalculation
returnstheyvaluebetweentwoxcoordinates.
Listing12.3: Encodingfuzzylogicmembershipfunctionsfortemperature.
/*Calculateyvalueofslopebetweentwoxcoordinates*/
#definedownslope(x,left,right)((right-x)/(right-left))
#defineupslope(x,left,right)((x-left)/(right-left))
floatm_t_cold(floatx)
{
floatleft=45.0;
floatright=75.0;
if(x<=left)return1.0;
elseif(x>=right)return0.0;
elsereturndownslope(x,left,right);
}
floatm_t_hot(floatx)
{
floatleft=45.0;
floatright=75.0;
if(x<=left)return0.0;
elseif(x>=right)return1.0;
elsereturnupslope(x,left,right);
BiologicallyInspiredandHybridModels 413
FIGURE12.19: Pressuremembershipfunctions.
}
intmain()
{
floatx;
x=45.0;
printf(“%fFisCold(%f)andHot(%f)\n”,x,m_t_cold(x),m_t_hot(x));
x=60.0;
printf(“%fFisCold(%f)andHot(%f)\n”,x,m_t_cold(x),m_t_hot(x));
x=75.0;
printf(“%fFisCold(%f)andHot(%f)\n”,x,m_t_cold(x),m_t_hot(x));
return0;
}
Tobuildafuzzylogicapplication,let’sintroduceonemoresetoffuzzy
membership functions. These will represent pressures, but these will be a
littledifferentthanthosefortemperature.Forourpressuremeasurement,
we’llprovidethreefuzzymembershipfunctions;low,nominal,andhigh.But
inthiscase,we’llintroduceatrapezoidmembershipfunctionfornominalthat
extendstoboththelow-andhigh-pressurefuzzyvalues(seeFigure12.19).
Thesenewpressuremembershipfunctionsareimplementedasshown
inListing12.4.Thelowandhighpressureisimplementedsimilarlytothe
temperaturefunctions.Thenominalpressurefunctionincludessupportfor
twoslopefunctionsandalsoaplateau.
Listing12.4: Encodingfuzzylogicmembershipfunctionsforpressure.
floatm_p_low(floatx)
{
floatleft=40.0;
414 ArtificialIntelligence
floatright=70.0;
if(x<=left)return1.0;
elseif(x>=right)return0.0;
elsereturndownslope(x,left,right);
}
floatm_p_nominal(floatx)
{
floatleft_b=40.0;
floatleft_t=70.0;
floatright_t=90.0;
floatright_b=120.0;
if(x<=left_b)return0.0;
elseif((x>left_b)&&(x<left_t))
returnupslope(x,left_b,left_t);
elseif(x>=right_b)return0.0;
elseif((x>right_t)&&(x<right_b))
returndownslope(x,right_t,right_b);
elseif(x>=right_b)return0.0;
elsereturn1.0;/*ForPlateau*/
}
floatm_p_high(floatx)
{
floatleft=90.0;
floatright=120.0;
if(x<=left)return0.0;
elseif(x>=right)return1.0;
elsereturnupslope(x,left,right);
}
FuzzyLogicOperators
Thetermfuzzylogicimpliesthattherearelogicaloperationsthatcanbe
performed.Fuzzylogicisappliedasasetofif/thenrulesusingthefuzzy
membershipfunctions.Thefuzzylogicoperatorsmimicthoseprovidedin
traditionallogic,butdifferintheirdetails.Thethreefuzzylogicaloperators
areNOT,AND,andOR.Thesearedefinedas:
NOTx=(1.0-x)
xANDy=minimum(x,y)
xORy=maximum(x,y)
BiologicallyInspiredandHybridModels 415
Theresultingvaluesarenormalizedtoarriveatabinarylogicvalue.If
theresultis0.5orgreater,thenthenormalizedvalueis1.0.Otherwise,the
normalizedvalueis0.0.Listing12.5explorestheimplementationofthese
operators.
Listing12.5: Fuzzyoperatorsimplmentation.
#defineEXTEND(x) (((x)<0.5)?0.0:1.0)
floatf_and(floatx,floaty)
{
if(x<y)returnx;
elsereturny;
}
floatf_or(floatx,floaty)
{
if(x<y)returny;
elsereturnx;
}
FuzzyControl
Let’s use our previously defined temperature and pressure membership
functionstodemonstratethefuzzyoperatorsinafuzzycontrolsystem.In
thepreviouslistings,weprovidedmembershipfunctionsforadevicefrom
whichwecanreadtemperaturesandpressures.Ourdevicehasacontrol
mechanismthatcanbethrottledhigh,medium,orlow,dependingonthe
stateofthesensors.Let’snowdefineacoupleofrulestocontrolthethrottle
(seeListing12.6).
Listing12.6: Naturallanguagerulesformanagingthethrottle.
if(temperatureishot)AND(pressureishigh)then
throttle=low
endif
if((temperatureiscold)AND
(pressureislow)OR(pressureisnominal))then
throttle=high
endif
The rules shown in Listing 12.5 are very easily translated into an
application using fuzzy logic as shown in Listing 12.6 (note that the
416 ArtificialIntelligence
EXTENDfunctionsimplyextendsthefuzzyvaluetoabinaryvalue).These
functionsprovidethefuzzyrulesforthrottlemanagementgiventemperature
andpressurevalues.
Listing12.6: Fuzzyrulesforthrottlemanagement.
if(EXTEND(f_and(m_t_hot(t),m_p_high(p)))){
/*Setthrottletohigh*/
}
if(EXTEND(f_and(m_t_cold(t),f_or(m_p_low(p),m_p_nominal(p)))))
{
/*Setthrottletolow*/
}
Thissimpleexampleillustratesthepowerbehindfuzzylogic.Givenaset
ofnaturallanguagerules,real-worldanalogvaluescanbetransformedinto
fuzzyvalueswhichcanthenbemanipulatedthroughasetoffuzzyoperators.
Thismakesiteasiertounderstandanddebug.
Fuzzylogicbringsthefuzzynatureoftherealworldtothebinarynature
ofcomputersystems.You’llfindfuzzylogicinimageprocessing,machine
vision,robotnavigation,medicine,andintelecommunications.
EVOLUTIONARYNEURALNETWORKS
AninterestingcombinedbiologicalmetaphorofAIistheevolutionofneural
networks.AsweexploredinChapter8,neuralnetworkscanbetrainedand
constructedinanumberofways,butevolutionofneuralnetworksoffers
some signficant advantages to other methods. For example, by evolving
neuralnetworks,wecannotonlyevolvetheweightsofanetwork,butalso
itstopology(theconnectivityoftheneurons).Thisallowsustoconstruct
networkswithveryspecificconstraints,suchassize,orminimalconnectivity
butstillsolvetheproblemathand.
Inthissection,we’llexploretwomethodsforneuralnetworkevolution
andthenreturntoourartificial-lifeexampletoevolvenetworkstocontrol
bugsinsyntheticenvironments.
GeneticallyEvolvedNeuralNetworks
Oneofthemostcommonandsimplestmethodsforevolvingneuralnetworks
issimpleweightevolution.Thisimpliesthatthearchitectureofthenetwork
BiologicallyInspiredandHybridModels 417
FIGURE12.20: Combiningneuralnetworkswithgeneticalgorithms.
isalreadyformed,andwhat’smissingistheweightsthatconnectthecells
ofthenetwork.
The evolution of the weights is provided by the genetic algorithm.
Forthisreason,thistypeofsolutionistypicallycalledGANN(orGenetic
Algorithms/Neural Networks). The weights of the neural network are
definedinachromosomewhichhasadirectmappingtotheweightsofthe
network.Figure12.20showsanexampleofthisrepresentation.Ontheleft
isthechromosomethatrepresentstheweightsofthenetwork;eachweight
isrepresentedasitsowngeneinthechromosome.
Oncearandompopulationofchromosomesiscreated,theirfitnessis
basedonhowwelltheysolvethegivenproblemwhentheweightsinthe
chromosomeareappliedtotheneuralnetwork.Thebettertheysolvethe
problem,themorelikelytheyaretobeselectedforrecombinationandto
survivetofuturegenerations.RecallthediscussionofGeneticAlgorithms
inChapter7.
Thebasicdisadvantagetothismethodisthatanassumptionismadeon
thearchitecturethatitwillsolvetheproblemathand.Thisassumptionmay
notbecorrect,butthenetworkarchitecturecanbealteredandnewweights
evolved.Thispropertycansometimesbeadvantageousifwe’reattemptingto
understandthelimitsofagivennetworkarchitecture.Givenasmallnetwork,
how well does it solve a given problem? Or given a particular network
topology,canitsolveagivenproblem?Inthisway,wecanchooseanetwork
thatclassifiesperhaps95%oftheinputs,but5%areleftunclassified,which
maybesuitablefortheproblemathand.
Another method for the evolution of neural networks involves
creatingboththenetworkarchitectureandtheweights.Thismeansthat
the weights and the architecture of the network must be encoded into
the chromosome. A very common way to achieve this is by creating a
chromosome that unfolds into a connectivity matrix. The connectivity
418 ArtificialIntelligence
FIGURE12.21: Theconnectivitymatrixrepresentationofaneuralnetwork.
matrixdefinestheweightsbetweenthecellsofthenetwork.Forexample,
inFigure12.21,weseetheentireflowfromchromosometoconnectivity
matrixtoneuralnetwork.Thechromosomeisasimpleone-dimensional
stringthatisfoldedtobecomeatwo-dimensionalmatrix.Eachcellinthe
matrixrepresentsasingleweight.Whereverazeroappearsinthematrix,
noconnectionexists.
Usingthismethod,wecanevolveneuralnetworksofvarioussizesand
connectivity. The example shown in Figure 12.21 is a simple multilayer
feedforwardnetwork,butthismethodalsosupportsrecurrentconnections
(where neurons feed back to neurons in lower layers). Connections that
bridgeunconnectedlayersarealsopossible(forexample,inputlayerneurons
feedingdirectlytotheoutputlayer).
Evolving the weights and topology has some interesting advantages
oversimpleweightevolution.Manytimesthecorrectnetworkarchitecture
isnotintuitive.Therearealsotimesthattheproblemissimplerthanwe
originallythought,andthereforeasimplersolutioncanbefound.Thefitness
parameters for the genetic algorithm can be tuned to not only find the
solutiontotheproblem,butalsotheonethatrequiresthefewestnumberof
neurons,orfewestconnectionsbetweenneurons.Forthisreason,evolving
thenetworktopologyispreferred.
Anotherapproachtoneuralnetworkevolution(relatedtothegenetic
algorithm) is through what can be called synthetic evolution (or situated
evolution).Inthismethod,theneuralnetworkscompetewithoneanother
inaformofsurvivalofthefittest.Ingeneticalgorithms,thiscanbecalled
elitistselection,asonlythefittestispermittedtoremaininthepopulation.
Allothermembersmustbemutatedinsomewaytoextendthesearchfor
anoptimalnetwork.
BiologicallyInspiredandHybridModels 419
FIGURE12.22: Agentfield-of-viewtoneuralnetworkinputs.
SimulatedEvolutionExample
Let’sreturntoourbugexampleforartificiallife,andapplytheconceptsof
neuralnetworkevolution.Recallthattheenvironmentismadeupofagrid
thatcontainscellsoffood,otherbugs,ornothing.Bugssurvivebyoccupying
thesamecellfood(whichisconsumed),therebyincreasingtheirhealth.For
eachmovethatabugmakes,it’shealthisreduced.Therefore,thebugmust
findfood,otherwiseitwilldie.Therefore,theneuralnetworkcontrolling
thebugmustbeinconstantsearchoffood.
RecallfromFigure12.9thatthefieldofperceptionforthebugisthe
24surroundingcells.Thesecellsaregroupedintoeightregionsthatserve
astheinputtotheneuralnetwork(seeFigure12.22).
Theagentimplementsasimpletwo-layerneuralnetwork.Eachcellin
theinputlayerconnectstoeachcelloftheoutputlayer.Theoutputlayer
containsfourcells,whichrepresenttheactionsthatarepossiblebythebug
(North, South, East, West). The neural network implements the winner-
takes-allstrategy,whichsimplymeansthattheoutputnodewiththelargest
valueisthecellthatfires(theactiontobetaken).
Thepowerthatguidestheevolutionofthebugsissimplysurvivalofthe
fittest.Ifabugisthemostfit(longestliving),thenitispermittedtobereborn
withoutmutation.Anybugthatdies(andisnotthemostfit)ismutatedand
thenreborn.Inthisway,lesserfitbugsaremodifiedandhavethepotentialto
berebornwithabetterstrategy,therebytakingthemostfitroleandforcing
thepreviouslyfitbugtobemutated.Thisprocesscontinuesforsomenumber
ofiterations,andintheend,thebestbugisemittedwithitsneuralnetwork.
Theactionselectionroutinefortheneuralnetworkversionofthesimulated
HE C
bugsisshowninListing12.7.
NT
D
O
The complete source for evolved neural network application can be
foundontheCD-ROMat./software/ch12/nnevol.c.Thisapplicationuses
ncurses,whichisavailableinGNU/Linux,orinCygwinforWindows.
420 ArtificialIntelligence
Listing12.7: Neuralnetworkactionselectionfunction.
voidmakeMove(bug_t*bug)
{
shortsensors[MAX_SENSORS];
shortoutputs[MAX_ACTIONS];
intx,y,a,s,best;
/*Agethebugsoweknowhowlongitlived*/
bug->age++;
/*Keeptrackoftheoldestbug*/
if(bug->age>maxAge)maxAge=bug->age;
/*
*Sensors:
*00122
*00122
*33.44
*55677
*55677
*
*/
y=bug->loc.y;
x=bug->loc.x;
/*Functionenvreturnsthecontentsofthecell*/
sensors[0]=(env(y-2,x-2)+env(y-2,x-1)+env(y-1,x-2)+env(y-1,x-1))?1:0;
sensors[1]=(env(y-2,x)+env(y-1,x))?1:0;
sensors[2]=(env(y-2,x+2)+env(y-2,x+1)+env(y-1,x+2)+env(y-1,x+1))?1:0;
sensors[3]=(env(y,x-2)+env(y,x-1))?1:0;
sensors[4]=(env(y,x+2)+env(y,x+1))?1:0;
sensors[5]=(env(y+2,x-2)+env(y+2,x-1)+env(y+1,x-2)+env(y+1,x-1))?1:0;
sensors[6]=(env(y+2,x)+env(y+1,x))?1:0;
sensors[7]=(env(y+2,x+2)+env(y+2,x+1)+env(y+1,x+2)+env(y+1,x+1))?
1:0;
/*Feedforwardtheinputsensorsthroughtheneuralnetwork*/
for(a=0;a<MAX_ACTIONS;a++){
outputs[a]=0.0;
for(s=0;s<MAX_SENSORS;s++){
outputs[a]+=bug->network[a][s]*sensors[s];
}
}
best=0;
BiologicallyInspiredandHybridModels 421
/*Winner-takes-alloutputnodeselection*/
for(a=1;a<MAX_ACTIONS;a++){
if(outputs[a]>outputs[best])best=a;
}
/*Removethebugfromthecurrentlocation*/
setLocation(bug->loc.y,bug->loc.x,““);
/*Changelocation*/
bug->loc.y=upd(bug->loc.y+actions[best].y);
bug->loc.x=upd(bug->loc.x+actions[best].x);
/*Istherefoodatthisnewlocation?*/
if(environment[bug->loc.y][bug->loc.x]){
/*Consumefoodandincreaseenergy*/
bug->life+=FOOD_VALUE;
environment[bug->loc.y][bug->loc.x]=0;
/*Addanotherfoodtotheenvironment*/
findEmptySpot(&y,&x);
environment[y][x]=1;
setLocation(y,x,“*”);
}
/*Accountformove*/
bug->life--;
/*Bughasdied*/
if(!bug->life)mutateBug(bug);
setLocation(bug->loc.y,bug->loc.x,“B”);
return;
}
FIGURE12.23: Sampleevolvedagentneuralnetwork.
422 ArtificialIntelligence
Asamplebugthatwasabletosurviveformanythousandsofiterations
isshowninFigure12.23.Thisneuralnetworkcontainsonlysixconnections,
butremainsastablestrategyforfindingfoodintheenvironment.Eachof
thefouractionsarecoveredbyinputs,butbasedonactionselection,some
actionsarefavoredoverothers.Forexample,thewinner-takes-allwillselect
thelargestoutputcell,butifthere’satie,itwillchoosethelargestthatwas
foundfirst.Inthisway,ifthere’satiebetweenallactions,theNorthaction
willbechosen.
NOTE TheneuralnetworkshowninFigure12.23showsonlyactivitorylinks
(inputscontributepositivelytowardactions).Thenetworkcouldhave
alsoincludedinhibitorylinksleadingtomorecomplexstrategies.
Thestrategyisfairlystraightforwardfromlookingatthenetwork.Iffood
liestotheNorth,thentheNorthactionistaken.IffoodistotheSoutheast,
thentheSouthactionistaken.ButiffoodliestoboththeSoutheastandto
theEast,thentheEastactionistaken(Eastisalsoselectediffoodliestothe
East).Finally,iffoodliestotheWestorNorthWest,thentheWestaction
istaken.ThisstrategyisshownvisuallyinFigure12.24.Inthisexample,we
canseetheagent’spreferencetomoveNorthandEastwhenfoodliesin
thosedirections.
The use of evolved neural networks is interesting in this application
becauseineffecttheevolutionprovidesaformoftrainingwithcompetetion.
The best bug is permitted to remain in the environment without being
mutated,butonceanewbugappearsthatismorefit,thenittakesoverthe
mostfitroleandthepreviousbug’sneuralnetworkismutated.Inthisway,the
bugscompeteagainstoneanothertonotbemutated.Mutationisn’tnecessarily
abadthing,asultimatelyamutatedbugwilltakethemostfitspot.
FIGURE12.24: Visualdisplayofthebug’sstrategy.
BiologicallyInspiredandHybridModels 423
ANTCOLONYOPTIMIZATION(ACO)
Ant Colony Optimization, or ACO, is a biologically inspired optimization
method that is very useful for solving problems in physical space. As the
nameimplies,ACOisanoptimizationalgorithmusingsimulatedants.Recall
thatantshavetheabilitytobuildinvisibletrailsmadeupofpheromones.
Thesetrailscanbeusedtoleadantstoandfromfood,thusoptimizingtheir
roundtriptimestothefoodandbacktothenest.
Inthesamewaythatantsplacedownpheromonetrailstocollectively
optimizethefoodforragingstrategy(inaprocessknownasstigmergy),this
sameconceptcanbeusedinavarietyofproblemdomains,fromnetwork
routingtopathfinding,andmore.
ThebasicideabehindACOcanbevisualizedwithagraph.Consider
the nodes of a graph as locations, and edges as the path between those
locations. A collection of ants are distributed around the graph and then
probabilisticallychoosetheirpathbasedonthestrengthofthepheromone
ontheedgesopenforthemtotake.Thehigherthepheromonelevelonan
edge,themorelikelytheyaretotakethatedge.Aftertheantscomplete
a tour of the graph, their tour is measured (using whatever scheme is
meaningfulfortheproblem),andanewpheromoneisplacedonthetour
representativewithitsfitness.Overtime,thecorrectpathbecomesclear,
and each ant begins to take the path found by others. From this simple
description,onecanseehowthismodelsthestrategyusedbyantsusingthe
processofstigmergy.
TravelingSalesmanProblem(TSP)
Let’s now have a look at the ACO algorithm as applied to the traveling
salesmanproblem(findingtheshortestorcheapestpaththroughagraphof
FIGURE12.25: SimplegraphtodemonstrateTSPtours.
424 ArtificialIntelligence
locations,visitingeachlocationonlyonce).TheTravelingSalesmanProblem
(orTSP)wasfirstexploredinthe1800s.
ConsiderthesimplegraphshowninFigure12.25.Thissimplegraph
consistsoffourcities(labeledA-D).Eachcityisconnectedtoeachother,
sothatthesalesmancantakeanypath.Thismeansthatatourthroughall
citiescouldbeA->C->D->B->A(foracompletetour,thesalesmanmust
endbackattheoriginatingcity),orA->B->D->C->A.Eachofthesetoursis
suboptimal,withoneofthetwooptimaltoursbeingA->B->C->D->A.
TheTSPisanNP-hardproblem,butvariousalgorithmsandheuristics
have been devised to solve it (such as branch-and-bound algorithms or
progressiveimprovementalgorithms).ACOmaynotfindtheoptimalpath,
butitwillfindagoodone.
An interesting advantage to the ACO algorithm to TSP is dynamic
changestothecities.Forexample,ifcitiesareaddedorremoved,theACO
canruncontinuouslyandadapttothechangesdynamically,yieldingagood
solution.
ACOAlgorithm
TheelementsthatmakeuptheACOarethegraph(setofcities,assuming
fullyinterconnectedandbidirectional),andapopulationofants.Eachant
maintainsasetofinformationaboutitsmovement,whatcitiesithasvisited,
andwhichremainforthetour.Thelengthofthetourisalsocalculatedalong
theway,whichisusedbytheantalgorithm.
The ant algorithm then relies on a number of simple steps that are
repeatedforsomenumberofiterations.Theseare:
• DistributingAntstotheGraph
• PerformingaTourwithEachAnt
• CalculatingtheTourCost
• DistributingPheromone
• EvaporatingPheromone
AntDistributiontotheGraph
Initialization of the algorithm begins with the distribution of the ants to
thegraph.Theantsareasevenlydistributedtothecitiesinthegraphasis
possible.Theentrypointofeachantbecomesthefirstcityinitstour.
TIP
Forbestresults,thenumberofantsshouldbeequaltothenumberof
cities(numberofnodesinthegraph).
BiologicallyInspiredandHybridModels 425
Onceantsaredistributed,thenextstepisforeachtovisiteachofthe
citiestomakeupatour.Thisnextstepinvolvespathselection.
PathSelection
Themechanismbywhichanantselectsthenextcitytovisitisbasedona
relativelysimpleprobabilityequation.Thisequation(Eq12.1)determines
theprobabilitythatacityshouldbevisited.Notethatwhentheantissitting
onitsfirstlocationfortheinitialtour,eachpathtothenextcityhasequal
probabilityofbeingselected.
(Eq12.1)
Thelevelofpheromoneonaedgeofthegraph(betweentwolocations)
isrepresentedby andthedistancebetweenthedistancebetweenthe
currentlocationandcandidatelocationisrepresentedby .Eachof
theseparametersisadjustedfortheeffectbywhichtheywillhaveoverthe
pathselection.Theαparameter,withavaluebetween0and1,represents
how much influence the pheromone plays in the path selection. The β
parameter,withavaluebetween0and1,representshowmuchinfluence
thedistancemeasureplays.AsshownbyEq12.1,thecitieslefttobevisited
arecalculatedandsummed(withkrepresentingthecitieslefttovisit).The
ratioofthecurrentcandidatecitycalculationtothesumofthiscalculation
isthentheprobabilitythatthepathshouldbeselected.We’llseehowthis
iscalculatedandusedshortly.
Thisprocesscontinuesuntileachantmakesacompletetourofthegraph
(visitseachcity).Whenthetouriscomplete,pheromonesaremanagedin
thegraphasdefinedbythefollowingtwosteps.
PheromoneIntensification
Nowthateachoftheantshascompletedatourofthegraph(visitedeach
ofthenodes),thepheromoneisdistributedtotheedgesinanamountthat
commensurateswiththelengthofthetour.Theshorterthetour,thehigher
thepheromonethatisdistributedtotheparticularedgesthatmakeupthe
tour.TheamountofpheromonedistributedisbasedonEq12.2.
(Eq12.2)
Inthisequation, representstheamountofpheromonetointensify
onthepath,Qisaconstantvalue,and isthelengthofthetour(overk
426 ArtificialIntelligence
cities).Notethatthisequationisappliedoneachedgeindividually,wherei
andjrepresenttheedgebetweenthecities.Eq12.3isthenusedtoapplythe
pheromonetotheparticularedge.Notealsoherethatρisusedtocontrolthe
amountofpheromonetoapply.Thiswillbeavaluebetween0and1.
(Eq12.3)
PheromoneEvaporation
Initialtoursoftheantsmayusemanyoftheedgesthatareavailable,butover
time,bettertoursthroughthelocationsarefound(asdefinedbythelengthofthe
tour),andmorepheromoneisplacedonthesepaths.Pheromoneevaporation
reducestheintensityofpheromonesonallpathsandhelpsmakeclearwhere
thebestpathsare(tobeusedbythepathselectionequationinEq12.1).
Eq12.4istheevaporationalgorithm,whichisappliedtoalledgesonce
theanttouriscomplete.Noteherethatρisrepeatedagain,butintheinverse
tothepheromoneintensification(Eq12.3).
(Eq12.4)
NewTour
Once all ants make a complete tour of all locations, the pheromone is
intensified on the paths taken by the ants, and pheromone evaporated,
the next step is to allow the ants to take another tour. Note that since
pheromoneisonthepathsinanintensitybasedonthetourlength,antswill
probabilisticallyfollowthebestpathswhilerandomlyselectingotherones.
Inthisway,giventheαandβparameters,pheromoneisintensifiedonthe
bestpathwhileallowingtheantstoexplorerandomly.Theantswilltake
somenumberoftours,eitherbasedonauser-definedmaximum,orafter
somedurationofanewbesttournotbeingfound.
SampleUse
Using the ACO for the traveling salesman problem is quite simple to
implementandyieldsverygoodsolutions.Inthissection,we’lllookatsome
oftheimplementationdetailsoftheACO.Thefocuswillbethefunctions
devotedtopathselection,andpheromoneintensificationandevaporation.
HE C
NT
D
O
ThecompletesourcefortheACOTSPprogramcanbefoundontheCD-
ROMat./software/ch12/ants.c.
BiologicallyInspiredandHybridModels 427
Choosingthepathananttakesthroughallpossibleedges(tounvisited
cities)isaccomplishedwiththefunctionchoose_next_city(seeListing12.8).
ThefirsttaskistocomputethedenominatorforEq12.1.Thisisperformed
bysearchingthroughthetabulist(forthecitiesthathavenotbeenvisited),
and then computing the denominator. This denominator is then used to
completeEq12.1andresultsinaprobabilityvalue.Thisvalueisusedwith
arandomnumbertodeterminewhichcityshouldbevisitednext(searching
throughthetabulisttoidentifythecitiesthathavenotyetbeenvisited).
TIP
Forefficiencyreasons,thedistancesbetweencitiesisprecalculatedandthen
usedhere(precomputed_distance).Thisallowsaveryfastlookup,rather
thanthemathinstructionsinvolvedincalculatingitonthefly.Thisrequires
abitmorememorytostore,butyieldsbenefitsinfasterexecution.
Onceanewcityisselected,theant’sdatastructuresareupdated(tabu,
tour_length,etc.).Ifthetouriscomplete,wecompletetheant’stour
totheinitialcitytocompletethepath.
Listing12.8: Selectingthenextcityintheant’stour.
voidchoose_next_city(ANT_T*ant)
{
intfrom,to;
doubled=0.0,p;
from=ant->current_city;
/*Ifcitynotyetvisited*/
for(to=0;to<NUM_CITIES;to++){
if(ant->tabu[to]==0){
/*Equation12.1*/
d+=pow(pheromone[from][to],alpha)*
pow((1.0/precomputed_distance[from][to]),beta);
}
}
/*Probabilisticallyselectthenextcity*/
to=0;
while(1){
/*Ifcitynotyetvisited*/
if(ant->tabu[to]==0){
/*Equation12.1*/
p=(pow(pheromone[from][to],alpha)*
pow((1.0/precomputed_distance[from][to]),beta))/d;
428 ArtificialIntelligence
if(RANDOM()<p)break;
}
to=((to+1)%NUM_CITIES);
}
/*Wehaveournewdestination,updateforthenewcity*/
ant->next_city=to;
ant->tabu[ant->next_city]=1;
ant->tour[ant->tour_index++]=ant->next_city;
ant->tour_length+=
precomputed_distance[ant->current_city][ant->next_city];
if(ant->tour_index==NUM_CITIES){
ant->tour_length+=
precomputed_distance[ant->tour[NUM_CITIES-1]][ant-
>tour[0]];
}
ant->current_city=ant->next_city;
return;
}
Once all tours are complete, the pheromone is intensified on those
edgesthatwereusedbytheants.Eachantisiterated,andtheneachedge
ofthetourforeachantisusedtocalculatethepheromoneleveltoadd(see
Listing12.9).
Listing12.9: Intensifyingpheromonelevelsonthepathsthattheantsused.
voidintensify_pheromone_trails(void)
{
intfrom,to,i,city;
for(i=0;i<NUM_ANTS;i++){
for(city=0;city<NUM_CITIES;city++){
from=ants[i].tour[city];
to=ants[i].tour[((city+1)%NUM_CITIES)];
/*Equation12.2/12.3*/
pheromone[from][to]+=((qval/ants[i].tour_length)*rho);
pheromone[to][from]=pheromone[from][to];
}
}
return;
}
BiologicallyInspiredandHybridModels 429
Thenextstepistoevaporatepheromonefromtheanttrails(seeListing
12.10).Justlikeintensifyingpheromoneonthetrails,evaporatingapplies
toalledgesandsimplyremovesasmallamountofpheromone(depending
upthevalueofρ).Pheromoneevaporationistheinverseofintensification.
Forexample,ifρwas0.9,thenduringpheromoneintensification,only90%
ofthepheromonelevelwouldbeadded.Duringevaporation,only10%of
thepheromonewouldberemoved.
Listing12.10: Evaporatingpheromonefromtheanttrails.
voidevaporate_pheromone_trails(void)
{
intfrom,to;
for(from=0;from<NUM_CITIES;from++){
for(to=0;to<NUM_CITIES;to++){
/*Equation12.4*/
pheromone[from][to]*=(1.0-rho);
if(pheromone[from][to]<0.0)pheromone[from][to]=BASE_
PHEROMONE;
}
}
return;
}
AsampleresultofthealgorithmisshowninFigure12.25.Thisillustrates
a20-cityTSPandareasonable(thoughsub-optimal)solution.
The ACO has more practical applications than the TSP. It has been
appliedtootherproblems(includingNP-Hardproblems).Examplesinclude
FIGURE12.25: Asamplesolutionofa20-cityTSPusingtheantalgorithm.
430 ArtificialIntelligence
dynamicandadaptivenetworkrouting,vehicleroutingforlogistics,andeven
fordiscoveryofclassificationrules.
ACOParameters
Anumberofparametersexistthatallowadevelopertocontrolthewayin
whichthealgorithmworks.ThefourparametersareshowninTable12.1.
The number of ants to be used is also a useful variable; typically it’s the
number of cities in the problem space. Finally, the number of iterations
canalsobeuseful,especiallyiftheantsareallowedtoexplore(andfavor
pheromoneanddistanceequally).
Table12.1:ACOparametersfortuning
α FavorPheromoneLeveloverDistance
β FavorDistanceoverPheromoneLevel
ρ Intensification/Evaporationvalue
Q ConstantValueforPheromoneDistribution
AFFECTIVECOMPUTING
Affectivecomputingisallaboutemotion,butfromtwodifferentperspectives.
First,it’saboutthedetectionandclassificationofemotionsofauser.Second,
it’saboutthesynthesisofemotionsfromacomputertoauser.Let’sexplore
whatthismeansandwhyit’simportant.
Emotionsareacriticalaspectofhumancommunication(aside-band,if
youwill,ofinformation).Considertheemotionofconfusion.Ifacomputer
coulddetectthisemotionfromauser,itcouldalterthewayinwhichit’s
presentinginformation.Inordertopresentacomputerasabelievableentity,
emotionmustbepartofitscommunication(eitherthroughspeech,oracts
ofspeech).
Emotionsmayalsobemorefundamentalinnaturethanintelligence.
For example, animals are capable of understanding emotion in humans
(consideradog’sresponsetoahuman’sgesturesorspeech),andwhilethey’re
intelligent,it’scertainlyatadifferentlevel.
CharacterizingHumanEmotion
Characterizinghumanemotioncanoccurinnumerousways,butprovide
differenttypesofinformation.Forthisreason,ahybridapproachisnecessary
BiologicallyInspiredandHybridModels 431
foracompleteandunambiguousviewoftheemotionofahuman.Speechis
amulti-channelinterface,andinadditiontotheinformationbeingconveyed,
informationisalsoavailableinthepatternsandpitchofthespeaker.For
example,thepitchofthespeech,thespeedofthespeech,andtheinflections
inthespeechthatcanbetunedbyemotion,arealldetectablethrougha
microphoneandsoftwareforanalysis.
Gesturesarealsoanimportantchannelofcommunicationforemotion.
Someofthisinformationiscontext-dependent.Forexample,inattentionis
animportantgesturetounderstandforlearning,butisevenmoreimportant
when used to understand a driver’s awareness. Hand gestures are also
important,asarepostureandfacialexpressions.
Otherimportantindicatorsofemotionincludephysiologicalparameters,
most of which can require more than just visually monitoring a subject.
Gauging a person’s temperature can be done through infrared, but heart
rateorbloodpressurerequiresmoreinvasivemonitoring.
Givingacomputertheabilitytocharacterizeandunderstandahuman’s
emotivestateisveryimportantinfuturehuman-computerinterfaces.With
thiswecanbuildsystemsthatnotonlycommunicatewithauser(fromthe
perspectiveofStrongAI),butcanusesubtlequeuestobuildmoreefficient
interfaces.
SynthesizingEmotion
SynthesizinghumanemotioninmachinesisanimportantaspectofStrong
AI, particularly for the development of believable characters (computer
agents)orinroboticsystems.Buthumanemotionsareverycomplexand
multi-dimensional.Forexample,happinessandsadnesscanbeconsidered
polaropposites,butahumancanbebothhappyandsadatthesametime,so
they’renotoppositeendsofaspectrum.Consideringthevarietyanddepth
ofhumanemotion,synthesizinganagentwithbelievableemotionswillbe
difficultindeed.
Researchexiststopursuethesimulationofemotionsgivenitsimportance
toanumberofapplications,fromStrongAItocomputergameagents.The
emotionstobesynthesizedarebasedonwhat’savailabletothesimulated
agent. Computer agents (those in computer games or other applications)
typically have a full range of channels, as characters are typically fully
modeled with synthesized speech. But visualization of emotions is the
simpleraspectofthisproblem.
The more difficult problem of synthesizing emotion is generating
emotionfromavarietyofinternalagentstates.Howdotheseinternalstates
maptothedimensionalityofemotion?Firststepswouldinvolveminimizing
432 ArtificialIntelligence
theemotivestatestoamoremanageableset,andthenevolvingthissetwith
experience.
Emotions may also play an important role in the internal function of
simulatedagents.Considerthesurvivalresponseofanimals.Animalscan
intellectuallyunderstandasituationanddeterminearesponse,butemotions
and other physiological factors certainly apply in determining a rationale
responsetoasituation.
RESOURCES
Dittrich, Peter, et al. “Artificial Chemistries - A Review,” University of
Dortmund,2001.
Dorigo, M. and Blum, C. “Ant Colony Optimization Theory: A Survey,”
TheoreticalComputerScience,344(2-3):243-278,2005.
Dorigo, M., Stutzle, Thomas. “Ant Colony Optimization,” MIT Press,
2004.
Gambardella, et al. “Ant Colony Optimization for Vehicle Routing in
AdvancedLogisticsSystems,”Multi-Agent-Systems,2003.
Availableonlineat:http://www.idsia.ch/~luca/MAS2003_18.pdf
Holland,John.“EchoArtificialLifeSimulator”
Availableonlineat:http://www.santafe.edu/~pth/echo/
J.vonNeumann.TheTheoryofSelfReproducingAutomataUniversityof
IllinoisPress,UrbanaIllinois,1966.
Lindenmayer, A. “Mathematical models for cellular interaction in
developmentI.Filamentswithone-sidedinputs,”JournalofTheoretical
Biology,1968.
Liu,Bo,Abbas,H.A,McKayB.“Classificationrulediscoverywithantcolony
optimization,”IntelligentAgentTechnology,IAT2003,2003.
Prusinkiewicz P., Lindenmayer A. “The Algorithmic Beauty of Plants,”
Springer-Verlag,NewYork,1990.
Wolfram,Stephen.ANewKindofScienceWolframMedia,Inc.,2002.
Zadeh,Lotfi.“Thebirthandevolutionoffuzzylogic,”InternationalJournal
ofGeneralSystems17,1990.
Chapter 13 THELANGUAGES
OFAI
W
hileAIalgorithmsandtechniquescanbeimplementedinawide
spectrumoflanguages,thereareanumberoflanguagesthatare
bettersuitedtoAIproblems.FornumericalAIproblemssuch
asneuralnetworksorgeneticalgorithms,languagessuchasCareeffective.
For relational problems that depend on predicate logic, Prolog is ideal.
Historically, LISP has been the foundational AI language as it emerged
during the early days of AI (referred to as a functional language). In the
end,anylanguagecanbeusedtobuildAIapplications,butsomearebetter
suitedtocertainproblemsthantoothers.Thischapterwillintroducethe
majorlanguagetaxonomies,andthenexplorethemajorlanguagesusedin
AItoidentifytheapplicationsforwhichthey’rebestsuited.
LANGUAGETAXONOMY
Programminglanguagesareaveryinterestingareaofstudy,astheyeach
fundamentallydothesamething;theyallowaprogrammertosolveproblems
usingacomputer.Buthowtheysolvethoseproblemscanvaryinawide
spectrum.Forexample,whereloopsareacommonprogrammingconstruct
in C or Ada, recursion is commonly used to emulate loops in a language
434 ArtificialIntelligence
like Haskell, or LISP. Each language provides fundamentally the same
functionality,buteachdoessoinverydifferentways.
Anotherexampleisthecomparisonofimperativelanguagestoobject-
orientedlanguages.Inanimperativelanguage,asystemisbuiltupfroma
setoffunctionsinahierarchy.Filestypicallyactascontainersforfunctions
thatlogicallybelongtogether(suchasfunctionsthatimplementaqueue).
In an object-oriented system, containers for like functions and data are
encapsulated into objects. The object embodies a data structure and the
functionsthatoperateonthedata.Asystemisthenbuiltfromacollection
ofobjects.
Table13.1listssixofthemajorlanguagetypes,fromfunctionallanguages
toconcurrentlanguages.
Table13.1: Majorlanguagetypes.
Type Focus
Functional Buildsprogramsasfunctionsovertypes.
Imperative Buildsprogramsascommandsoverdata.
Object-Oriented Builds programs from cooperative collections of
objects.
Logic Drawsconclusionsfromfactsandrelationships.
Concurrent Usesmultipleprocesseswithcommunication.
NOTE Mostprogramminglanguages,particularlytheonesthatwe’llexplore
here,arewhatisknownasTuringComplete.Thismeansthatthelanguage
hasthecomputationalpowerequivalenttotheabstractcomputingmodel
calledtheTuringMachine.Simply,thismeansthatusingthelanguage
weareabletoperformanycomputationaltask(regardlessofefficiency
ofsimplicityofthesolution).
In the early days of AI, the focus of AI systems development was
symboliclanguageslikeLISP(1950s)andlogiclanguagessuchasPROLOG
(1970s).Butaswe’veseeninthisbook,avarietyoflanguagescanbeusedfor
AIdevelopment.Let’slookatthepopularprogramminglanguageparadigms,
andhowtheyapplytoAIsystemsdevelopment.
FunctionalProgramming
Functional programming is a paradigm deeply rooted in the theoretical
frameworkcalledtheLambdacalculus.Functionalprogrammingisbased
onanumberofcharacteristicsincludinghigher-orderfunctions,first-class
TheLanguagesofAI 435
functions, recursion, a lack of side effects, continuations, and closures to
namejustafew.
Table13.1: Samplefunctionallanguages.
Language YearIntroduced Focus
LISP 1958 T he original functional progamming
language.
Scheme 1975 ElegantandsimplifieddialectofLISP.
ML 1980 Apureandstrictfunctionallanguage.
Haskell 1987 Generalpurposepurebutlazyfunctional
language.
Functional programming is best understood my comparing it to the
imperativeprogrammingparadigm.Ratherthanrelyingonthesequential
executionofcommandsthataltermemoryandstate,functionalprogramming
reliesontheapplicationoffunctionsoverdata.Let’snowlookatthemajor
characteristicsoffunctionalprogramming.
Higher-orderfunctionsareaprimaryattributetofunctionalprogramming.
Ahigher-orderfunctionisonesimplythateitherisabletotakeafunctionas
input,orreturnafunctionasoutput.Thisconceptiscommoninmathematics;
forexample,thederivativeinthecalculusmapsonefunctiontoanother.
TIP
Theclassificationoflanguageshasbecomemoredifficult,aslanguages
strivetosupportmultipleprogrammingparadigms.Forexample,Ruby
andPythonareobject-orientedscriptinglanguages,buteachsupportthe
abilityforprogrammingusingfunctionalcharacteristics.
Listing13.1: UsingthePythonmapfunction.
>>>defsquare(x):returnx*x
...
>>>map(square,[1,2,3])
[1,4,9]
>>>
First-classfunctionsareotherwiseknownasfirst-classobjects,andlack
therestrictionstypicallyassociatedwithotherelementsofalanguage.For
436 ArtificialIntelligence
example,inalanguagethatsupportsfirst-classfunctions,wecouldcreate
functionsdynamically(seeListing13.2),treatthemasdata(orvice-versa),
passfunctionstootherfunctions,orreturnthemfromfunctions.First-class
functionsarevalues,justlikeanyotherdata,andcanbestoredwithindata
structures. While languages like C can store functions (or references to
functions)indatastructures,passthemtootherfunctions,orreturnthem
fromfunctions,Cdoesnotprovidesupportfordynamicfunctioncreation.
Listing13.2: First-classfunctionsintheRubylanguage.
irb>defmult_by(factor)
irb>returnProc.new(|n|n*factor)
irb>end
=>nil
irb>mult_by_5=mult_by(5)
irb>mult_by_2=mult_by(2)
irb>mult_by_5.call(10)
=>50
irb>mult_by_2.call(8)
=>16
irb>
Recursivefunctionsarefunctionsthatcallthemselves.Thisistypically
howiterationisperformedwithinfunctionlanguages.Sincerecursioncan
implymaintainingastackofreturnaddresses,manylanguagesthatrelyon
recursionimplementwhatisknownastailrecursion.Thisobviatestheneed
forastack,whichisefficientforbothmemoryandprocessing.
Listing13.3: UsingrecursioninLISPtocomputethefactorial.
(defunfact(n)
(if(=n1)
1
(*n(fact(-n1)))))
ImperativeProgramming
Thedefiningcharacteristicofimperativeprogramming(otherwiseknownas
proceduralprogramming)isthatcomputationisthesequentialexecutionof
statementsthatchangeprogramstate.
TheearliestimperativelanguagewasFORTRAN,developedin1954.
FORTRANwasdevelopedbyoneofthepioneersofearlycomputing,John
Backus(whileemployedat,IBM).FORTRANisattherootofaverylarge
language tree that includes BASIC, Pascal, Modula, C, Ada, and a huge
numberofdomain-specificlanguagessuchasPythonandRuby.
Table13.2: Sampleimperativelanguages
Language YearIntroduced Focus
FORTRAN 1957 F irst general-purpose programming
language.
ALGOL 1958 S e c o n d i m p e r a t i v e p r o g r a m m i n g
language.
C 1972 P ervasive procedural programming
language.
POP-11 1975 Multi-paradigmprogramminglanguage.
Ada 1983 Multi-paradigmconcurrentprogramming
language.
TIP
We can also think of machine languages (or assembly languages) as
imperativelanguages.Theinstructionsofagivencomputerarchitecture
are also an example of an imperative language. The instructions of a
machine language not only alter memory, but also the machine state
(statusflags,forexample).
Listing13.4illustratestheorganizationofasimpleimperativeprogram
inBASIC.Notehowthisexamplecanbedescribedasarecipe.Forexample,
requesttheuser’snamethroughtheterminal,gettheuser’sname,andfinally
emittheuser’snamethroughtheterminal.
Listing13.4: Illustratingthesequentialcommandsofanimperativelanguage
(BASIC).
print“Pleasetypeinyourname:”
readname
print“Hello,“+name
end
438 ArtificialIntelligence
Whilerecursionispermittedinimperativelanguages,theselanguages
lack tail-recursion and therefore they are less efficient than functional
implementations. Commonly, loops provide the iteration capabilities (as
shown in the factorial implementation in Listing 13.5). As shown here,
thewhilereservedwordinCimplementstheloopingbehaviorwhilethe
expression‘(n>0)’remainstrue.
Listing13.5: Computingthefactorialinanimperativelanguage(C).
unsignedlongfactorial(intn)
{
unsignedlongfact=1;
while(n>0){
fact=fact*n;
n--;
}
returnfact;
}
Object-OrientedProgramming(OOP)
Object-OrientedProgramming(orOOP)isaparadigminwhichprograms
areconstructedfromasetofobjectsthatconnecttooneanother.Objectsare
abletomessageotherobjectsthroughtheirpublicinterfaces,whileobjects
alsomaintainasetofprivateinterfaces(anddata)thatareusedinternally
by the objects themselves. Objects typically encapsulate some logical
functionalityofasystemAsampleofobject-orientedlanguagesthroughfour
decadesisprovidedinTable13.3.
Table13.3: Sampleofobject-orientedlanguages.
Language YearIntroduced Focus
Simula67 1967 F irst object-oriented programming
system.
C++ 1970 C l a n g u a g e w i t h o b j e c t - o r i e n t e d
extensions.
Smalltalk 1970 Dynamicandreflectiveprogramming.
Ruby 1993 P u r e i n t e r p r e t e d o b j e c t - o r i e n t e d
language.
Aclassisanabstractentitythatdefinesbehaviors(theactionsthatit
canperform)andattributes(thecharacteristicsfortheclass).Objectsare
instantiatedfromaclass,goingfromanabstractentitytoaphysicalinstance
oftheclass.Take,forexample,aclassthatrepresentsanNPC(non-player
character)inagame.TheattributescouldspecifytheNPC’sdamageand
theweaponthatit’scarrying(whichitselfcouldberepresentedbyanother
object). The class behaviors could include the actions that the NPC may
perform(attack,move,defend,etc.).
AnexampleclassisshowninListing13.6.Inthissimpleexample,our
class exports two behaviors, set_value and area. The behavior set_value
(whichisalsocalledamethod)allowstheinitializationofaninternalvalue.
Thesecondbehavior,area,returnsthesquareoftheinternalvalue.Asample
mainfunctionisalsopresentedthatcreatestwoinstancesoftheCsquare
classanddemonstratestheirmethods.
Listing13.6: IllustratingaclassandtwoobjectswithC++.
#include<iostream.h>
usingnamespacestd;
classCsquare{
inth;
public:
voidset_value(int);
intarea(void);
};
voidCsquare::set_value(intin_h)
{
h=in_h;
return;
}
intCsquare::area(void)
{
returnh*h;
}
intmain()
{
Csquares,t;
s.set_value(5);
t.set_value(5);
cout<<“sarea=“<<s.area()<<endl;
440 ArtificialIntelligence
cout<<“tarea=“<<t.area()<<endl;
return0;
}
Inheritanceistheabilityforclassestoinheritthecharacteristicsofother
classes.Inthisway,wecanbuildbaseclassesthatdefinedefaultbehavior,
and then create subclasses that amend these behaviors. For example, in
designingagame,wemightspecifyaclassforanNPC.Thisclassprovides
thebasesetoffeaturesthatallNPCsshare.Then,wecancreatesubclasses
thatspecializethebaseclassformorespecificcharactersinthegame.
AsshowninListing13.7,wecreateanNPC_Baseclassthatprovides
ourbasesetofbehaviors.Fromthis,wecreateaBarbariansubclass,anda
Villagersubclass,thatrefinethebehaviorsforthecharactertype.Attheend
ofthelisting,wecreatetwoobjectsofthetwosubclassestodemonstrate
thespecialization.
Listing13.7: IllustratinginheritancewithRuby.
>classNPC_Base
>defmove
>puts“MovetheNPC”
>end
>
>defbehavior
>puts“Juststandthere”
>end
>end
>
>classVillager<NPC_Base
>defbehavior
>puts“Loiter”
>end
>end
>
>classBarbarian<NPC_Base
>defbehavior
>puts“Attack!”
>end
>end
>
TheLanguagesofAI 441
>fred=Villager.new
>hans=Barbarian.new
>fred.behavior
Loiter
>hans.behavior
Attack!
>
Noteherethatthebehaviormethodhasbeenreplacedfromthebase
classinthesubclasses.Thisisanexampleofpolymorphism,wherethesame
methodachievesdifferentresultsbasedontheparticularsubclassforwhich
anobjectisaninstance.
Finally,theconceptofencapsulation(otherwiseknownasinformation
hiding)allowsaclasstohidethedetailsofitsinternaloperations.Aclass
exportswhatisnecessaryasfarasmethodsandinstancedatagoesforproper
operation,buthideswhattheexternalworldisnotnecessarytoknow.
LogicProgramming
Logicprogramming,firstintroducedbyJohnMcCarthyin1958,isbased
on pattern-directed execution of rules to arrive at a goal. While logic
programmingisbasedonmathematicallogic,logicprogrammingismuch
more constricted than the capabilities of mathematical logic. Specifically,
logicprogrammingimplementswhatisknownashornclauses.Ahornclause
(specificallyadefiniteclause)canbeshownas:
X0ifX1andX2,and…Xn
whichmeansthatifallX1throughXnistrue,thenX0istrue.Aspecialcase
ofthehornclauseisasimplefact,whichissimplyspecifiedas:
X0.
Thehornclausecanthenbethoughtofasafactbeingtrueifallprerequisites
ofthefactaretrue.Forexample:
mortal(X)ifman(X)
whichmeansthatiftheatomidentifiedbyXisaman,thenXisalsomortal.
ThiscanbedemonstratedinPrologasfollows,withfirstthedefinitionofa
fact,andthenaquery,testingagivenquery:
442 ArtificialIntelligence
?-man(Socrates).
?-mortal(X):-man(X).
?-mortal(Socrates)?
Yes
?-
Inthisexample,wespecifyanewfact(Socratesisaman).Wethendefine
thatallmenaremortalwitharule.Finally,wechecktoseeifSocratesisa
mortal,whichevaluatesto‘Yes.’
Later in this chapter, we’ll review logic programming in more depth,
from the perspective of a couple of different languages. Recently, non-
functional languages have begun to integrate logic capabilities (such as
Mercury,showninTable13.4).
Table13.4: Samplelogiclanguages.
LANGUAGESOFAI
Consideringthespectrumofapplicationsofartificial-intelligenceapplications,
a single language would not provide all of the capabilities necessary. For
example,Cisidealfornumericalapplicationssuchasthoseillustratedby
neuralnetworksorgeneticalgorithms.Butthespecializedneedsofexpert
systemsmakeProloganidealchoice.Further,forapplicationsthatrequire
dynamicity(suchascreatingnewfunctionsonthefly),LISPprovidesthe
necessaryfunctionality.Somelanguage-to-applicationmappingsareprovided
inTable13.5.
Table13.5: Artificialintelligenceapplicationsandtheirlanguages.
Application Language
ExpertSystem POP-11,Prolog,LISP
TheLanguagesofAI 443
DynamicApplications LISP,Scheme,Dylan
ComputerVision C
NaturalLanguageSystems Prolog,Oz
EvolutionarySystems C,DSLs
In the remainder of this chapter, we’ll dig further into a number of
languages commonly used to build AI applications. This includes LISP,
Scheme, Prolog, Erlang, POP-11, Smalltalk, and a look at some domain-
specificlanguagesthatprovidefunctionalityusefulforAIapplications.
TheLISPLanguage
TheLISPlanguageisoneoftheoldestlanguagesstillinusetoday.LISP
shares this distinction with other ancient languages like FORTRAN
and COBOL (COmmon Business Oriented Language). But these other
languages,likeLISP,haveevolvedovertheyears,andlookverydifferent
thantheirinitialofferings.Havingsaidthat,thefundamentalattributesof
theselanguagesremain.Forexample,theFORTRANcontinuestousethe
imperativeapproach,andLISPisstillbasedonthefunctionalapproach.
LISPisoneofthemostuniquelanguages(withintheAIdomainand
outside).LISPstandsforLIStProcessing,asbothdataandLISPprograms
arerepresentedaslists.Further,dataandprogramscanbe(andusuallyare)
representedaslistsoflists.Withprogramsanddatarepresentedinidentical
ways(whichmakesLISPahomoiconiclanguage),it’seasytounderstandhow
LISPfunctionscangeneratedatatobeusedasanotherLISPfunction.
Let’snowdigfurtherintotheLISPlanguage,learnaboutitsorigin,and
thenexploreafewexampleprograms.
TheHistoryofLISP
JohnMcCarthy(nowatStanfordUniversity)introducedtheideasbehind
LISPwhileatMITin1958.In1960,hepublishedapaperwhichservedas
thedesignforLISP,andimplementationsbegantoappearshortlythereafter.
McCarthy’snotationusedwhatwascalledbracketedM-expressions,butthis
notationwasabandonedinearlyimplementationsforS-expressions,which
arestillinusetoday.
TIP
WhilemanyconsiderLISPtobeanarchaiclanguage,LISPisworthwhile
tolearnbecauseofthefundamentalideasuponwhichit’sbased.
LISP quickly consumed the early computers of the 1960s (beginning
withtheearlyvacuumtubecomputers),whichresultedinspecialtymachines
444 ArtificialIntelligence
in the 1970s that were designed for LISP. In particular, LISP required
specialized garbage collection. However, today LISP is able to run on
generallyavailablehardware.
OverviewoftheLISPLanguage
Let’snowdigintotheLISPlanguage,andexploresomeofthecoreconcepts
fromrepresentingdatatorepresentingprograms.Notethat,aswe’llsee,
dataandprogramsarerepresentedidentically.Infact,LISPprogramscan
becombinationsofprogramanddata.
DataRepresentation
As the language’s acronym suggests, everything in LISP is a list. The
followingareexamplesofdatainLISP,fromthesimple,tomorecomplex
representations.
‘() Emptylist.
‘(()) Listcontaininganemptylist.
‘(1) Listwithasingleatom.
‘(2481632) Listwithmultipleatoms.
‘((1357)(2468)) Listoflists.
‘(LISP(JohnMcCarthy)(MIT)(1958)) Listasastructure.
SimpleExpressions
Evaluating math expressions in LISP looks very similar to the data
representation example, except that math symbols are provided which
areknowninLISPtobemathoperations.LISPuseswhatiscalledprefix
notation, where an operation is specified, followed by the necessary
argumentsfortheoperation.Considerthefollowingexamples:
(+13) Evaluatesto4
(*57) Evaluatesto35
(*(-75)(-94)) Evaluatesto10
(/82) Evaluatesto4
Note above that each of the examples are examples of lists. As LISP
evaluatesthefunctions,itidentifiestheoperatorsymbol,andthenapplies
thesubsequentargumentstoit.
TIP
It’simportanttonotethateveryLISPexpressionreturnsavalue.Infact,
aLISPfunctioncanreturnanynumberofvalues.
TheLanguagesofAI 445
Predicates
LISPalsoincludespredicatesthatprovidetheabilityofintrospection.The
atompredicatereturnstrueiftheargumentisanatom.Forexample:
(atom5) EvaluatestoTrue
(atom()) EvaluatestoFalse
Variables
LISPalsosupportsvariablesthroughanumberofmeans.First,let’slookat
thesetqmeansofevaluatingasymbol.Thesetqpermitsbindingavalueto
asymbolofasequenceofvaluestoasequenceofsymbols,forexample:
(setqscore100) Evaluatesto100
(setqscore-195score-2100) Evaluatesto100
(setqthedate‘(“April”131968)) Evaluatesto(“April”131968)
Noteinthelastexampletheuseofthesinglequote(ortick)symbol.This
tellstheLISPevaluatornottoevaluatethelistthatfollows.Ifthequotehad
notbeenused,thenLISPwouldattempttoevaluatethelist(lookingfora
functioncalled“April”)inordertobindtheresultingvaluetothedate.
LISPalsoprovidesamorecomplexformofvariableassignmentcalled
theletform.Inthisform,anumberofbindingscanbetemporarilycreated.
Considerthefollowingexample:
(let((x5)
(y7))
(+(*xx)(*yy))) Evaluatesto74
Note that in this example x and y have no value once the evaluation is
complete.Wecouldbindtheresulttoavariableasfollows:
(setqvar
(let((x5)(y7))
(+(*xx)(*yy))) Evaluatesto74
ListProcessing
Let’snowlookathowLISPperformslistconstructionandprocessing.We’ve
alreadyseenthesimplestwaytocreatealist,throughtheuseofsetq.Now
we’lllookattwofunctionsthatcanbeusedforlistconstruction.Thefirst
446 ArtificialIntelligence
iscalledcons,whichconstructsalistfromtwoarguments(withthesecond
alwaysbeinganemptylist).
(cons1()) Evaluatesto(1)
(cons1(cons2())) Evaluatesto(12)
LISPalsoprovidesasimplerwaytoconstructlists,calledlist.Thisfunction
ismuchsimplerasshowninthefollowingexamples:
(list123) Evaluatesto(123)
(list1‘(23)) Evaluatesto(1(23))
(list‘(1)‘(2)) Evaluatesto((1)(2))
Nowthatwe’veexploredsomewaystoconstructlists,let’slookatafew
of the basic list-processing functions provided by LISP. The two basic
operationsarecalledcar(toretrievethefirstelementofalist)andcdr
(to retrieve the remainder of the list without the first element), for
example:
(car‘(123)) Evaluatesto1
(cdr‘(123)) Evaluatesto(23)
(car(cdr‘(123))) Evaluatesto2
LISPalsoprovidessomesimplificationsonthistheme,byprovidingfunctions
thatcombinetheseprimitivefunctionsforlistprocessing.Forexample,the
followingtwooperationsareidentical:
(car(cdr‘(123)) Evaluatesto2
(cadr‘(123)) Evaluatesto2
Thecadroperationisthereforeacombinationofcarandcdr.There’salso
acombinationofcdrandcdrcalledcddr.Thefollowingtwoexamplesare
alsoidentical:
(cdr(cdr‘(1234))) Evaluatesto(34)
(cddr‘(1234)) Evaluatesto(34)
TheLanguagesofAI 447
LISPprovidesanumberofthesecombinationoperators,someofwhichare
showninTable13.6.
Table13.6: LISPoperationcombinationsandtheirequivalents.
Operation Equivalent
(cadrx) (car(cdrx))
(cdarx) (cdr(carx))
(caarx) (car(carx))
(cddrx) (cdr(cdrx))
(caddrx) (car(cdr(cdrx)))
(caaddrx) (car(car(cdr(cdrx))))
Finally, we can append to lists using the append function. This function
takestwolistsandputsthemtogether:
(append‘(12)‘(34)) Evaluatesto(1234)
ProgramsasData
Recallthatprogramanddataarethesame.Here’sanexamplethatillustrates
this.Westartbycreatingavariablecalledexprandloadingitwithalist.We
thenusetheevalfunctiontoevaluatethedataasaLISPprogram.
(setqexpr‘(*55)) Evaluatesto(*55)
(evalexpr) Evaluatesto25
Nowlet’slookathowprogramflowisalteredinLISP.We’llstartwithareview
ofconditionalsinLISP,andthenlookatfunctioncreationanditeration.
Conditions
ControllingtheflowofaLISPfunctioncanbeachievedanumberofways.
We’lllookattwomethodshere,condandif.Thefirstmethodwe’llexplore
istheiffunction.Theiffunctionisverysimpleandismadeupofadistinct
setofexpressions.Theformatfortheiffunctionis:
(if(expr)
(print“expristrue”)
(print“exprisfalse”))
Asimpleexampleofthisformis:
448 ArtificialIntelligence
(if(>21)
(print“2isgt1”)
(print“1isgt2”))
Thefalseportionoftheformcanbeomittedifnotnecessary,asfollows:
(setqname“Elise”)
(if(equalname“Elise”)
(print“HiElise”))
Thecondformisslightlymorecomplicated,butpermitsgreaterflexibilityin
theconditionalstructure.Thecondformcanspecifyasmanytestclausesasare
necessary.Thefollowingexampleillustratesthecondformwithfourclauses:
(setqname“Elise”)
(setqsalutation
(cond
((equalname“Marc”)“HiMarc.”)
((equalname“Megan”)“HiMegan.”)
((equalname“Elise”)“HiElise.”)
((equalname“Jill”)“HIJill.”)
(t“Hiyou.”)))
(printsalutation) Evaluatesto“HiElise.”
Notethatintheendofthecondformisthe‘else’(ordefault)portionofthe
conditional.The‘t’simplyrepresentsTRUE,andasitalwaysrepresentstrue,
thevalue“Hiyou.”isreturnedifnootherconditionsaremet.
FunctionsinLISP
AfunctioniscreatedinLISPusingthe defunmacro.Theformatofthe
defunmacroisasfollows:
(defunfunc-name(parameter*)
“Optionalstringtodescribefunction.”
body-form*)
Thefunc-namecanbeasequenceofalphabeticcharacters(andhyphens),
aswellassomeotherspecialcharacters.Theparameter*representszeroor
moreparameters,whichisfollowedbyadocumentationstring(usedbythe
documentationfunction).Thebodyofthefunctionthenfollows.
TheLanguagesofAI 449
Let’s look at a simple example of a function. Recall the quadratic
discriminant(b2–4ac).ThiscanberepresentedinLISPverysimplyas:
(defunqd(abc)
“Calculatethequadraticdiscriminant”
(-(*bb)(*4.0ac)))
Anexampleuseofthisfunctionisshownas:
(setqval(qd542))
Now let’s look at a few examples that demonstrate some of the core
featuresofLISP.We’llbuildasetoffunctionsthatmanipulateaverysimple
database(alistoflists).Let’sbeginwithadefinitionofourdatabase.Our
simpledatabase(db)willrepresentalistofrecords,eachrecordbeingalist
containinganamefollowedbyanage.We’llusethedefvarfunctiontocreate
ourdatabasewithaninitialsetofrecords(defvarcreatesaglobalvariable).
(defvar*db*‘((TIM41)(JILL35)(MEGAN14)(ELISE9)(MARC6)))
Nowlet’screatethreefunctionsthatmanipulatethisdatabase.We’llcreate
LISPfunctionstoreturnthenumberofrecordsinthedatabase,emitthe
namesinthedatabase,andaddanewrecordtothedatabase.
Returning the number of records in the database (function num-
records) is a great function to explore recursion in LISP. The function
num-records takes as its input the database. The first thing that the
functiondoesistestforanulllist,andifso,itreturns0(aswe’vereached
theendofthelist).Otherwise,itaddsonetotheresultofarecursivecall
tonum-records.
(defunnum-records(x)
“Returnthenumberofrecordsinthelist.”
(if(nullx)0
(+(num-records(cdrx))1)))
Let’s say that our input to the function is a database that contains
fourrecords.Figure13.1illustratestherecursivecalltracealongwiththe
returnvalues.Noteattheend,thefunctionreachesthenulllist,andzero
isreturned.Atthispoint,eachfunctionaddsonetothereturnofthevalue
returnedbytherecursivecall.
450 ArtificialIntelligence
FIGURE13.1: Illustratingtherecursivenatureofthenum-recordsfunction.
Nowlet’slookatanotherexampleofaLISPfunctionthatisusedtoemitthe
namescontainedineachoftherecords(emit-names).Thiswillfollowthesame
approachandrecursivelyiteratethroughtherecords(sublists)ofthedatabase
(superlist).Thisfunctionoperatesasfollows.First,wetesttoseeiftheinputis
anemptylist(usingthenullfunction).Ifitis,wereturnnil(whichisthesame
as‘())andtheprocessiscomplete.Otherwise,wesetthevariablenametothe
firstatomofthefirstsublist.Withintheletform,weprintthisname,andthen
recursivelycallemit-names.Thisrecursivecallcontinuesuntiltheemptylist
results,atwhichpointthecallchainunwindsandtheprocessiscomplete.
(defunemit-names(x)
“Emitthenameportionofeachrecordinthedatabase.”
(if(nullx)
nil
(let((name(caarx)))
(printname)
(emit-names(cdrx)))))
Thefinalfunctionthatwe’llprovidedemonstratesthepuresimplicityofLISP’s
list-handlingfunctions.Inthisexample,we’lldemonstratehowtoaddanew
recordtothedatabase.Thisfunction, add-record,simplyappendsthenew
recordtothedatabase,andthenusessetqtosetthedatabasetothisnewlist.
(defunadd-record(x)
(setq*db*(append*db*x)))
Finally,let’slookatanexampleofcallingthesefunctions.Thefollowingthree
examplesillustratetheuseofthethreefunctions(num-records,add-record,
andemit-names).
TheLanguagesofAI 451
;;;
(formatt“~%Numrecords:~D~%”(num-records*db*))
(formatt“Currentrecordlist:”)
(emit-names*db*)
(formatt“~%~%Addinganewrecord~%”)
(add-record‘((MADDIE3)))
(formatt“Numrecords:~D~%”(num-records*db*))
(formatt“~%RecordList:”)
(emit-names*db*)
ThisLISPprogramresultsinthefollowingoutput:
Numrecords:5
Currentrecordlist:
TIM
JILL
MEGAN
ELISE
MARC
Addinganewrecord
Numrecords:6
RecordList:
TIM
JILL
MEGAN
ELISE
MARC
MADDIE
LISPSummary
ThisintroductiontoLISPprovidedatipoftheicebergofthecapabilities
providedbytheLISPlanguage.WhileLISPmaybeanancientlanguage
consideringthetimelineofsomeofthemorerecentpopularlanguages,it’s
wellworthlearningfortheuniqueconceptsandfeaturesthatitprovides.
TheSchemeLanguage
TheSchemelanguage,createdduringthe1970satMIT,isamoderndialect
oftheLISPlanguage.Butinsteadofsimplybeingafunctionallanguage,it
wasdesignedtosupportanumberofotherprogrammingparadigmssuch
asimperativeandobject-oriented.
452 ArtificialIntelligence
HistoryofScheme
The Scheme language was the result of a series of papers by Gerald Jay
SussmanandGuySteele,Jr.Thesepapersfocusedonideasinprogramming
languagedesign,specificallythedesignofasimplelanguagethatwasboth
efficient and unconstrained by arbitrary rules. The language that evolved
fromthesepapersillustratedasimplearchitecturewithveryfewprimitive
constructsandallotherelementsimplementedaslibraries.
The Scheme language is now standardized through an IEEE standard,
andalsothroughaformalreviewprocesscalledtheRxRS(RevisedReport
onScheme).Theentirelanguageisdescribedinaround50pages,with
another roughly 100 dedicated to library discussions. [R6RS] From this
specification, numerous implementations exist including PLT Scheme,
MITScheme,Scheme48,andaninteractiveshellthatallowsoperatorsto
writescriptsandinteractwithanoperatingsysteminSchemecalledSCSH
(SchemeShell).
OverviewoftheSchemeLanguage
Let’s now dig into the Scheme language, and explore some of the core
concepts.Asyou’llsee,Schemeisaverysimplelanguage,andwhileit’seasy
tolearnthecoreideasofprogramminginscheme,it’sverydifficulttomaster.
AsadialectofLISP,dataandprogramsarerepresentedidentically.
DataRepresentation
Like LISP, Scheme supports the basic data type of the list. Scheme also
supportsinteger,real,stringsandsymboltypes(amongothers).Consider
theseexamplesthatillustrateScheme’suseoflistsandatoms.
‘() Emptylist
‘(()) Listcontaininganemptylist.
‘(1) Listwithasingleatom.
‘(2481632) Listwithmultipleatoms.
‘((1357)(2468)) Listoflists.
‘(Scheme((GuySteeleJr.)(GeraldJSussman))(MIT)(1975))
Listasastructure.
SimpleExpressions
EvaluatingmathexpressionsinSchemeisidenticaltoLISP;mathsymbols
areprovided,whichareknowninSchemeasmathoperations(primitives).
Schemeuseswhatiscalledprefixnotation,whereanoperationisspecified,
followed by the necessary arguments for the operation. Consider the
followingexamples:
TheLanguagesofAI 453
(+13) Evaluatesto4
(*57) Evaluatesto35
(*(-75)(-94)) Evaluatesto10
(/82) Evaluatesto4
Each of the examples are examples of lists. As Scheme evaluates the
functions,itidentifiestheoperatorsymbol,andthenappliesthesubsequent
argumentstoit.
TIP
Schemerequiresnooperatorprecedencerulesbecauseoperatorsusethe
prefixnotationwithfullyparenthesizedoperands.
Predicates
Schemealsoincludesavarietyofpredicates,whichcanbeusedtodetermine
thetypeofanobject,ortheequivalenceoftwoobjects.Predicatesalways
returnaboolean,true(#t)orfalse(#t)value.Predicatesaredefinedasthe
typefollowedbythe‘?’symbol.
Forexample,wecanidentifyifanobjectisanull(orempty)listusing:
(null?‘()) Evaluatesto#t(true)
Wecanalsoidentifyifanobjectreferstoaprocedurewith:
(procedure?list) Evaluatesto#t(true)
Finally, we can identify if two objects are equivalent using the eqv?
procedure:
(eqv?5(+23)) Evaluatesto#t(true)
(eqv?carcdr) Evaluatesto#f(false)
Manyotherpredicatesexist,whichcanbefoundintheR6RS.
NOTE Ifthedesireistocheckiftwomemoryobjectsareidentical(refertothe
sameobjectinmemory),thentheeq?equivalenceoperationcanbeused.
Variables
Let’slookattwoexamplesofvariablesinScheme,thosethathaveglobal
scopeandthosethathavelocalscope.Variablesinglobalscopearecreated
usingdefine.Thisbindsavaluetoavariableasfollows:
(definepi3.1415926)
454 ArtificialIntelligence
Variablesoflocalscopecanalsobecreatedusingtheletform.Thisform
hasthestructure:
(let(varlist)(expr))
(let((pi3.1415926)(r5))
(*pi(*rr)))
Notethatasmultiplevariablebindingsarepossible,wecanalsoperform
multiple expressions. Additionally, let forms can be embedded in other
expressions(aswellasotherletforms).
ListProcessing
Schemeprovidesarichsetofoperationsforlistprocessing.Let’sbeginwitha
lookatScheme’slistconstructionoperations.Thetwobasiclistconstruction
proceduresareconsandlist.Theconsproceduretakestwoobjectsand
joinsthemtogether.Forexample:
(cons‘a‘(ab)) Evaluatesto(abc)
(cons‘(ab)‘(cd)) Evaluatesto((ab)cd)
Anotherwaytothinkaboutconsisthatthefirstargumentisthecarofthe
resultandthesecondargumentisthecdr.
Thelistprocedureismuchsimplerandsimplytakesasetofarguments
andturnsthemintoalist.Forexample:
(list‘a‘b‘c) Evaluatesto‘(abc)
(list‘a‘(bc)) Evaluatesto‘(a(bc))
(list‘a‘()‘(abc)) Evaluatesto‘(a()(abc))
ThefundamentallistmanipulationproceduresarethesameasLISP,carand
cdr.Thecarprocedurereturnsthefirstobjectfromalistwhilecdrreturns
thetail(allobjectsexceptthefirst).Forexample:
(car‘(abc)) Evaluatesto‘a
(cdr‘(abc)) Evaluatesto‘(bc)
(car‘((ab)(cd))) Evaluatesto‘(ab)
(cdr‘((ab)(cd))) Evaluatesto‘((cd))
TheLanguagesofAI 455
Schemealsosupportscombinationsoftheseoperators(suchascadr,for(car
(cdr))).SeeTable13.6formoreoptions.
Thelengthprocedurecanbeusedtoidentifythenumberofobjectsin
thelist.Notethatthisreferstothenumberofobjectsattherootofthelist,
andnotthemembersofthesublist.Forexample:
(length‘(abcd(ef))) Evaluatesto5
(length‘((ab)(cd))) Evaluatesto2
(length‘()) Evaluatesto0
(length‘(()())) Evaluatesto2
Finally,toretrieveaspecificobjectfromalist,onecouldusethecar/cdr
combinations,orsimplyuselist-ref.Thisfunctiontakesalistandanumeric
argumentandreturnsthatobject(usingthenumericargumentastheindex)
fromthelist.Notethattheindexisbase0,so0representsthefirstobject,
etc.Forexample:
(list-ref‘(abcd)2) Evaluatesto‘c
(list-ref‘((ab)(cd)(ef))1) Evaluatesto‘(cd)
AswasillustratedwiththeLISPlanguage,Schemeisapowerfullanguage
forlistprocessingandmanipulation.
Conditions
Schemesupportsanumberofconditionalforms.Thetwomostcommonare
theifformandthecondform.Theifformisthesimplest,supportingan
expressionforthetruecaseandanexpressionforthefalsecase,as:
(if(test-expr)
(expr-for-true-case)
(expr-for-false-case))
The if form can be demonstrated as follows. The return of the overall
expressionisthefirstobjectofmy-listifmy-variszero;otherwise,thereturn
objectistheremainderofthemy-list:
(if(equal?my-var0)
(carmy-list)
(cdrmy-list))
456 ArtificialIntelligence
If more complex conditionals are required, the cond form can be used.
Thecondformpermitsmultipletest-expressionsaswellasadefaultcase
(makingthisamoreflexibleversionoftheswitchstatementavailableinmany
imperativeandobject-orientedlanguages).
(cond(test-expr-1expr-1)
(test-expr-2expr-2)
...
(elseexpr-n))
Toillustratethecondform,weimplementthesignfunctionality:
(cond
((<my-var0)-1)
((>my-var0)1)
(else0))
Whiletheifformismuchsimpler,thecondfunctionalityisrecommended
forallbutthesimplestconditionalexpressions.
IterationandMaps
Scheme, as a functional language, can rely on recursion for looping and
iteration. But Scheme also provides some other forms for iteration. Let’s
startwithalookatthedoloopformthatpermitsiterationssimilartothefor
loopprovidedinmanyimperativelanguages:
(do
((variablevalueupdate)...)
(testdone-expr...)
cont-expr...)
Inthisform,wespecifyouriterationvariable,itsinitialvalue,andhowwe’ll
update the variable at each loop iteration. At each update, we’ll perform
ourtest,andifthetestistrue,theloopiscompletedandweperformany
done-expressionsthatareavailable.Ifthetestisfalse,theloopcontinues
andanycontinue-expressionsareprocessed.Let’slookatasimpleexample
thatillustratestheform:
(do((i0(+i1)))
((equal?i10)(write“done”)(newline))
(writei)(newline))
TheLanguagesofAI 457
Thisexamplesimplyiterates,usingtheivariable,from0to9.Duringthe
iteration,thevalueisemitted,andattheend,the“done”stringisemitted
andtheformiscomplete.
Iterationistypically performed to operate over an indexed object, or
toperformanactionsomenumberoftimes.Forcasesinwhichthegoalis
toperformanactionoverasetofdata,Schemeprovideswhatiscalledthe
mapoperation.Thisisacommonoperationinfunctionallanguages.Recall
that higher-order functions in functional languages support functions as
arguments to other functions. The map function is used to apply a user-
specifiedfunctiontoeveryelementofalist.
A simple example uses a lambda (or anonymous) function, which is
appliedtoeachelementofalist:
(map(lambda(x)(*xx))‘(0123)) Evaluatesto‘(0149)
Nowlet’slookatanexampleofauser-definedfunctionusedinthecontext
of map. Let’s return to our simple database application from the LISP
introduction. Our simple database contains a list of names and ages, and
our user-defined function (print-names) emits the name from one of the
recordsinthedatabase.
(definemy-list‘((Megan14)(Elise9)(Marc6)))
(define(print-namesx)
(write(carx))(newline))
Wecanthenusemaptoiterateourdatabasewiththeprint-namesfunction
as:
(mapprint-namesmy-list)
Themapfunctioniteratesthelist,andinvokestheuser-definedfunctionfor
eachobjectinthelist(whichiswhyweextractthenamewithcarinsteadof
needingtodoacaar).
ProceduresinScheme
Schemeprovidestwotypesoffunctions, primitivesand procedures.
Aprimitiveisabuilt-inprocedure(fundamentalelementsoftheScheme
environment), where procedures are user-defined (or provided through
libraries).Examplesofprimitivesincludelet,car,andthemathoperations,
such as ‘+’ ,and ‘-‘. Scheme is therefore built from a very basic set of
operations,andfromtheseoperations,morecomplexformsarederived.
458 ArtificialIntelligence
CreatingfunctionsinSchemeissimilartoLISP,butthesyntaxdiffers
slightly:
(define(procparameters*)
body)
Anexampleprocedure, square,takesasingleargumentandreturnsthe
squareoftheargument:
(define(squarex)
(*xx))
Nowlet’sreturntooursimpledatabaseimplementationandimplement
theproceduresthatallowustomanipulatetherecordsofthedatabase.The
simpledatabase(db)representsalistofrecords,whereeachrecordcontains
anameandanage.We’llusethedefineprimitivetodeclarethedatabase
anditsinitialsetofrecords.
(definedb‘((TIM41)(JILL35)(MEGAN14)(ELISE9)(MARC6)))
We’llimplementthesamesetoffunctionsasimplementedforLISP,butwe’ll
implementthemdifferentlytoexploresomeofScheme’sfeatures.
Thefirstprocedurewe’llimplementwillreturnthenumberofrecords
inthedatabase(procedurenum-records).ForLISP,weimplementedthis
as a recursive function. For Scheme, we’ll use the length procedure to
demonstrateanalternativemethodtoimplementthis.
(define(num-recordsx)
(lengthx))
Recall that the length procedure returns the number of objects in the
list.Objectsinthisscenarioarethenumberofsublists(orrecordsinthe
database).
Toimplementtheemit-namesprocedure,we’llusethemapprocedure.
Thisprocedureappliesafunctiontoeachobjectofalist.Forthisprocedure,
wesimplyemitthefirstelementofeachobject(whichwillrepresentthe
namefortherecord).
(define(emit-namesx)
(write“Databaseis”)(newline)
TheLanguagesofAI 459
(map(lambda(y)(write(cary))(newline))x)
(newline))
Our final procedure is used to add a new record to our database, called
add-record.Inthisprocedure,we’remodifyingthecontentsofanexisting
variable,soweusetheset!procedure.
(define(add-recordx)
(set!db(appenddbx)))
Finally, let’s have a look at how we’d invoke these procedures within a
Schemeprogram.ThefollowingSchemecodeillustratestheinitialcreation
ofthedatabaseandinvokeseachofthedefinedprocedures:
(definedb‘((TIM41)(JILL35)(MEGAN14)(ELISE9)(MARC6)))
(display‘”Numrecords:“)
(write(num-recordsdb))(newline)(newline)
(emit-namesdb)
(display‘”Addinganewrecord”)(newline)(newline)
(add-record‘((MADDIE3)))
(display‘”Numrecords:“)
(write(num-recordsdb))(newline)(newline)
(emit-namesdb)
ThisSchemeprogramresultsinthefollowingoutput:
Numrecords:5
CurrentRecordList:
TIM
JILL
MEGAN
ELISE
MARC
Addinganewrecord
Numrecords:6
CurrentRecordList:
TIM
JILL
MEGAN
ELISE
MARC
MADDIE
460 ArtificialIntelligence
SchemeSummary
LikeLISP,Schemeisausefullanguagetolearnbecauseoftheideasthatare
expressed.Whilenotexploredhere,Schemeprovidescontinuationswhich
allowprogramcontroltocontinueataprocedureatanypointintheprogram.
Schemealsoallowsprogrammerstodefinenewsyntacticformstoextend
thelanguage.Howmanylanguagespermitextendingthesyntaxofthecore
languagethroughthelanguageitself?Schemeisdefinitelynotthesimplest
languagetomaster,butit’sworththeefforttoexplore.
ThePOP-11Language
POP-11 is an interesting multi-paradigm language that shares many
characteristicsoffunctionallanguages.POP-11isdynamicallytypedwith
garbagecollectionlikefunctionallanguages,butsharesablockstructured
syntaxlikemanyimperativelanguages.POP-11isalsostack-oriented,sharing
arelativelyuniquefeatureinlanguagedesignwiththeForthlanguage.
HistoryofPOP-11
POP-11istheresultofaseriesofPOPlanguagesdevelopedforresearchin
ArtificialIntelligenceatEdinburghUniversity.POP-11wasimplementedin
themid-1970sonthePDP11/40computerontheUNIXoperatingsystem,and
wasdesignedasalanguagespecificallyforteachingpurposes.[Sloman1996]
POP-11hasadvancedasthestateoftheartinlanguagedesignandnowincludes
notonlyfunctionalaspects,butalsoobject-orientedfeaturesandagraphical
library.POP-11canbefoundformanyoperatingsystems,andwhileapowerful
language,isnotwidelyusedforproductionsoftwaredevelopment.
OverviewofthePOP-11Language
Let’snowexploresomeofthefeaturesofthePOP-11language.Duetothe
sizeandscopeofthelanguage,we’llrestrictthisdiscussiontosomeofthe
basicfeaturesthatwillhelpillustratethelanguageanditsuses.
DataRepresentation
POP-11 includes a variety of data objects, from numbers (integers and
decimals) to words, strings, and lists. Sharing a heritage from functional
languages,listscanbecomplex,containingotherdataobjects,andcollections
oftheseobjectsinembeddedlists.
41 IntegerNumber
0.707 DecimalNumber
7_/11 Ratio
TheLanguagesofAI 461
“Zoe” Word
‘asamplestring’ String
[asamplelist] List
[simple[embeddedlist]] Listcontainingalist
{POP-11{{RobinPopplestone}}{UnivsersityofSussex}{1975}}
VectorasaStructure
Vectordatatypesincludearangeofclassesofstandardoperations.Users
canfurthercreatetheirownvectorclasses.
Predicates
POP-11 provides a way to identify the data type given an object. In
demonstratingthis,we’llalsoshowtheprint-arrow(=>)ofPOP-11.This
instructstheinterpretertoemittheresultoftheexpression.Forexample:
dataword(5)=> Evaluatestointeger
dataword(1.1)=> Evaluatestodecimal
dataword(1_/2)=> Evaluatestoratio
dataword(“Zoe”)=> Evaluatestoword
dataword(‘teststring’)=> Evaluatestostring
dataword([atestlist]’)=> Evaluatestopair
dataword({a{testvector}})=> Evaluatestovector
dataword(dataword)=> Evaluatestoprocedure
SimpleExpressions
AsPOP-11isamulti-paradigmlanguage,expressionscanbedefinedand
evaluated in a number of ways. The developer can use infix notation (as
commonlyusedinimperativelanguages)ortheprefixnotation(aswe’veseen
demonstratedinfunctionallanguagessuchasSchemeorLISP).Thereare
somedifferenceswithinfixaswe’llseeinthefollowingexamples:
5+7=> Evaluatesto12
*(3,4)=> Evaluatesto12
10/3.0=> Evaluatesto3.33333
10/3=> Evaluatesto10_/3
10–7=> Evaluatesto3
NOTE An interesting aspect of POP-11 is the error-reporting mechanism
thatitprovides.POP-11providesverydetailedexplanationsoferrors
when they’re encountered, which can be very helpful for beginner
developers.
462 ArtificialIntelligence
Variables
WedeclarevariablesinPOP-11usingthevarskeyword.Thisallowsusto
createanynumberofvariables,comma-delimited,as:
varsx,y;
As variables are dynamically typed, they can be assigned any value, for
example:
[abc]->x; Assigns[abc]tovariablex
1.99->y; Assigns1.99tovariabley
Noteherethat->istheassignmentoperator,andthe=signrepresentsa
testofequality.
ListProcessing
Like the functional languages, POP-11 provides a rich set of procedures
forlistprocessing.Let’shavealookatsomeoftheimportantaspectsoflist
processinginPOP-11.
First, recall that we can declare a list (and in this case two lists)
simplyas:
varsclist1=[abcd],clist2=[efgh];
Concatenatinglistsisperformedusingthe<>operator.Wecancombine
thetwoliststoanewlistas:
clist1<>clist2->comblist;
pr(comblist); Evaluatesto[abcdefgh]
POP-11alsoprovidesawaytomergelistswithalittlemoreflexibilitythan
the<>operatorallows.Forexample,wecancombinethetwopreviouslists
withafewadditionalelementseasily:
[x^^clist1y^^clist2z]->newlist; Evaluatesto[xabcdyefghz]
pr(newlist);
Let’s look at one last useful operation that POP-11 provides for list
construction.Thesinglearrowoperationallowsalisttobeconstructedwith
valuesofvariables,forexample:
TheLanguagesofAI 463
varsx,y,z;
1->x;
2->y;
3->z;
[^x^y^z]->mylist;
mylist => Evaluatesto[123]
POP-11provideslist-manipulationproceduresthatmimicthoseprovidedby
LISPandScheme.RecallinLISP,thecarfunctionreturnedthefirstobject
fromthelistandcdrreturnedthelistwithoutthefirstobject.InPOP-11,
thesefunctionsarehd(toreturntheheadofthelist,likecar)andtl(toreturn
thetailofthelist,likecdr).
hd([[ab][cd][ef]])=> Evaluatesto[ab]
tl([[ab][cd][ef]])=> Evaluatesto[[cd][ef]]
hd(hd(tl([[ab][cd][ef]])))=> Evaluatestoc
POP-11 also provides a length function, which identifies the number of
objectsinalist.Thisisdemonstratedasfollows:
length([[ab][cd]])=> Evaluatesto2
Conditions
ConditionalsinPOP-11followanimperativeblock-structuredmodel.The
basicpatternforaconditionalinPOP-11is:
if<condition>then
<statements>
endif
The condition can be made up of a number of conditions, separated by
logical operators (such as and, or). Let’s look at a short example of the
conditional:
iflength(mylist)>0then
mylist=>
endif
Thissnippetsimplyteststhelengthofthelist(testsforanon-nulllist),andif
it’snotempty,printsthecontentsofthelist.Anelsecanalsobesupplied.
464 ArtificialIntelligence
IterationandMaps
POP-11 provides many of the looping constructs that are found in the
imperativelanguages.Thisincludesuntil-do,do-until,repeat,while-do,for,
andothers.Let’slookatacoupleofexamples.
Thewhile-doloopisoneofthesimplestandmostcommonlyusediterator.
The following example iterates over a list and emits each object. Note that
duringtheloop,wedestroymylistbysettingittoitstailasweemitthehead.
[abcdef]->mylist;
varsnum;
0->num;
whilenum<length(mylist)do
hd(mylist)=>
tl(mylist)->mylist;
endwhile;
Theforloopprovidesabitmoreflexibility,andisonlyslightlymorecomplicated.
Thefollowingexampleillustratesiteratingthroughtheobjectsofalist:
[abcdef]->mylist;
for0->num;stepnum+1->num;
tillnum=length(mylist)
do
hd(mylist)=>
tl(mylist)->mylist;
endfor;
Youcanseethepatternhereusedinotherlanguages(start,iterator,end).
ThisislesscompactthaninlanguageslikeC,butthesemanticsareroughly
thesame.
TIP
While POP-11 supports a variety of iteration methods, they are
fundamentally very similar, and in fact, some are simply syntactic
sugar.Syntacticsugar(coinedbyPeterJ.Landin)definesadditionstoa
language,butdonotextendtheexpressivenessofthelanguage.
PatternMatching
POP-11providesanicesetofpattern-matchingfeaturesthatcanbeusedfor
logicprogramming,ornatural-languageprogramming.Let’sexploresomeof
thepattern-matchingfeaturesthatareprovidedbyPOP-11.
Let’sfirstlookatsimplematchinginPOP-11.Let’ssaythatwewantto
identifyifanelementexistsinalist,andifitdoes,storeaparameterrelated
tothatelement.Forexample,ifourlistisdefinedas:
[[tim41][jill35][megan14]]=>mylist;
Thematcheskeywordcanbeusedtofindanelementinalist,andpotentially
storeanelementofthatsequence.Thesnippet:
varsage;
mylistmatches[==[jill?age]==]=>
returns true if the element ‘jill’ is matched in mylist. Further, the ?age
elementinstructsthematchertostorethefollowingelementafterthematch
inthedefinedvariable.Ifwenowemittedage,suchas:
age=>
we’d emit the value 35. The ‘==’ elements indicate that something may
beginorendinthelist,andtoignoretheseelementsofthelist.Thispattern
matchercanbeusefulinbuildingnaturallanguagesystems(forexample,
thefamousElizaprogramhasbeenwritteninPOP-11usingthematches
pattern-matcher).Forexample:
[Ihateyou]->inp_sentence;
ifinp_sentencematches[I?verbyou==]then
[whydoyou^verbme?]=>
endif;
Theresultofthissnippetis[whydoyouhateme?].You’llrecognizethisasa
typicalresponsefrom‘bot’programs,suchasEliza.Fromthissimplesnippet,
youcanseehoweasyitistoconstructnatural-languagesystemswithPOP-11.
ProceduresinPOP-11
Defining procedures in POP-11 is what you would expect from a block-
structurelanguage(suchasCorPascal).Theproceduredefinitionincludes
466 ArtificialIntelligence
adefineblock,theoptionalsetofargumentsthataretobepassedin,and
anyvaluethatisreturned.Forexample,thefollowingprocedureimplements
thesquare(returnstheproductofthesingleargument):
definesquare(num)->result;
num*num->result
enddefine;
Demonstratingacalltothisfunctionisdoneverysimplyas:
square(5)=> Evaluatesto25
Where it’s possible to pass multiple arguments to a procedure, it’s also
possibleforaproceduretoreturnmultipleresults.Thefollowingprocedure
illustratesmultipleargumentsandmultiplereturns:
definemulti(num1,num2)->(result1,result2);
num1->result1;
num2->result2;
enddefine
Callingthisprocedureas:
varsx,y;
multi(3,7)->(x,y);
resultsinxbeingboundto3andybeingboundto7.
Let’snowreturntooursimpledatabaseapplicationtoseehowthiscould
beimplementedinPOP-11.We’llbeginwiththecreationofthedatabase,
whichissimplyalistoflists.
[[TIM41][JILL35][MEGAN14][ELISE9][MARC6]]->db;
Identifying the number of records in our database can use the length
procedure.Thisreturnsthenumberofobjectsinthelist:
definenumrecords(thelist)->result;
length(thelist)->result;
enddefine;
Usingourpreviouslydefineddatabase,wecancallthiseasilyas:
TheLanguagesofAI 467
numrecords(db)=>
toemitthenumberofrecords.
The procedure to emit the names of the records is provided next
(emitnames).Forthisprocedure,we’veusedanimperativestyle,iterating
through the database list for a number of times defined by the length
procedure.
defineemitnames(thelist);
varsnum;
0->num;
whilenum<length(thelist)do
hd(hd(thelist))=>
tl(thelist)->thelist;
endwhile;
enddefine;
Thisfunctioncanbecalledasshownhere:
emitnames(db);
Let’s look at our final procedure, to add a new record to our database
(addrecord). This procedure simply combines two lists (using the <>
operator)andsetstheresultbacktotheoriginaldatabasevariable(db).
defineaddrecord(record);
db<>record->db;
enddefine;
Thisprocedureiscalledwithalistobject(actuallyalistcontainingourlist
record):
addrecord([[MADDIE3]]);
Finally, let’s look at the complete application (user calls to our database
procedures)toseetheexpectedoutputfromthePOP-11interpreter.First,
oursimpleapplicationemitsthecurrentnamesinthedatabase,addsanew
record, and then emits the names again. Finally, we emit the number of
recordsinthedatabase.
468 ArtificialIntelligence
[[TIM41][JILL35][MEGAN14][ELISE9][MARC6]]->db;
printf(‘Currentrecordlist\n’);
emitnames(db);
printf(‘Addinganewrecord\n’);
addrecord([[MADDIE3]]);
printf(‘Numrecords:‘);
numrecords(db)=>
printf(‘\n’);
printf(‘Currentrecordlist\n’);
emitnames(db);
Executing this POP-11 program with our previously defined functions
provides:
;;;DECLARINGVARIABLEdb
Currentrecordlist
**TIM
**JILL
**MEGAN
**ELISE
**MARC
Addinganewrecord
Numrecords:**6
Currentrecordlist
**TIM
**JILL
**MEGAN
**ELISE
**MARC
**MADDIE
POP-11Summary
POP-11 is an interesting language, first because while old it’s still
useful, and second because it’s an interesting perspective on multi-
paradigmprogramming.POP-11supportsimperativeprogramming,logic
programming,andfunctionalprogrammingwithinasinglelanguage.Alarge
task,butPOP-11doesitwell.
Prolog
Prolog (which stands for “Programming in Logic”) is one of the most
interesting languages in use today. Prolog is a declarative language that
TheLanguagesofAI 469
focusesonlogic.ProgramminginProloginvolvesspecifyingrulesandfacts
andallowingPrologtoderiveasolution.Thisdiffersfromtypicaldeclarative
programming, where a solution is coded. Building a Prolog application
typicallyinvolvesdescribingthelandscapeoftheproblem(usingrulesand
facts),andthenusinggoalstoinvokeexecution.
HistoryofProlog
Prologwascreatedaround1972byAlainColmerauerandRobertKowalski
asacompetitortotheLISPlanguage.LISPwasdominantatthetimefor
artificial-intelligenceapplications,butPrologthencame.Prolog,asthename
implies,focusesonlogicrathertypicalsequentialprogramming.
TheearliestPrologapplicationswereinthedomainofnatural-language
programming, as this was the object of research by its creators. Natural-
language programming (otherwise known as computational linguistics)
is supported by a unique feature of Prolog. Prolog includes a built-in
mechanismforparsingcontext-freegrammars,makingitidealforNLPor
parsinggrammars(suchastheProloglanguageitself).Infact,likeLISP,a
ProloginterpretercanbeeasilyimplementedinProlog.
Today,Prologremainsalanguageusedprimarilybyresearchers,butthe
languagehasadvancedtosupportmultipleprogrammingparadigms.Prolog
isidealfordevelopingknowledge-basedsystems(suchasexpertsystems)
and also systems for research into computer algebra. Prolog’s problem-
solvingcapabilitiesmaintainitasanimportantlanguageforproblemsinthe
knowledgedomain.
OverviewofthePrologLanguage
Prolog is comprised of two fundamental elements, the database and the
interpreter. The database contains the facts and rules that are used to
describetheproblem.Theinterpreterprovidesthecontrolintheformofa
deductionmethod.Fromthissimpledescription,it’sclearthatprogramming
inPrologdiffersfromprogramminginotherdeclarativelanguages.Forthis
reason, we’ll sharply focus on the unique features of Prolog for building
knowledge-centeredsystems.
DataRepresentation
Prologdoesn’tincludedatatypes,perse,butinsteadwhatcanbereferred
to as lexical elements (or sequences of characters in legal combinations).
SomeoftheimportantlexicalelementsofPrologincludeatoms,numbers,
variables,andlists.
Anatomisastringmadeupofcharacters(loweranduppercase),digits,
andtheunderscore.Numbersaresimplysequencesofdigitswithanoptional
470 ArtificialIntelligence
precedingminussign.Variableshavethesameformatasatoms,exceptthat
thefirstcharacterisalwayscapitalized.Alistisrepresentedasacomma-
delimitedsetofelements,surroundedbybrackets.
person Atom
f_451 Atom
‘Acomplexatom.’ Atom
86 Number
-451 Number
Person Variable
A_variable Variable
[a,simple,list] List
[a,[listoflists]] List
ListProcessing
Like the functional languages we’ve explored so far, Prolog provides the
typicalsetoflist-processingpredicates.Theseallowtheconstructionoflists,
manipulationoflists,andalsoextractinginformationaboutlists.
NOTE The following Prolog examples will show the dialog with a Prolog
interpreter.Commandsareshowninbold(followingthePrologcommand
prompt,?-).
Constructingalistisperformedsimplyasfollows:
|?-[a,b,c,d]=X.
X=[a,b,c,d]
yes
|?-
TheXvariablereferstothelist[a,b,c,d],whichisidentifiedbytheProlog
interpreter.Wecanalsoconcatenatetwolistsusingtheappendpredicate.
Thispredicatetakestwolistsandproducesanewlistforthevariablename
defined(thirdargument).
|?-append([[a,b],[c,d]],[[e,f]],Y).
Y=[[a,b],[c,d],[e,f]]
yes
|?-
TheLanguagesofAI 471
Prolog can also tell us how many objects are in a list, using the length
predicate.Theformofthispredicatetakestwoarguments,thefirstalist,
andthesecond,avariablewhichwillbeusedtorefertothelength:
|?-length([a,b,c,[d,e]],Length).
Length=4
yes
|?-
Prologalsoallowsmatchinganobjectfromalistusingthememberpredicate.
Usingthispredicate,weget
|?-member(elise,[marc,jill,megan,elise,marc]).
true?
yes
|?-
Finally,Prologalsopermitsthetypicalheadandtailmanipulationsoflists.
AsinLISPandScheme,theheadofalististhefirstelement,andthetailof
alististheremainder.Prologcanextractboththeheadandthetailofalist
atthesametime.Inthefollowingexample,wecreatealist,andthenextract
theheadtovariableHandthetailtovariableT.
|?-[a,b,c,d]=[H|T].
H=a
T=[b,c,d]
yes
|?-
Asillustratedhere,Prologprovidesarichsetoflist-processingpredicates
matchingthoseprovidedinthefunctionallanguages.Let’slooknowatthe
featuresthatmaketheProloglanguageunique.
Facts,Rules,andEvaluation
What separates Prolog from all other languages is its built-in ability of
deduction.Let’sstartwithalookatfacts,andhowwecandefinenewfacts
forthePrologdatabase.We’llbeginasimpleexample,andthenbuildupto
morecomplexfacts.
Let’sstartwithasetofrulesdescribingfruitsandvegetables.Afactis
knownasapredicate,andconsistsofaheadandsomenumberofarguments.
472 ArtificialIntelligence
Forexample,thefollowingfactsdefinetheheadasthetypeofproduct(fruit
orvegetable)andtheargumentisthenameoftheobject.
fruit(pear).
fruit(apple).
fruit(tomato).
vegetable(radish).
vegetable(lettuce).
vegetable(tomato).
Eachofthesepredicatesistrue,sowecanquerythePrologdatabase,such
as:
|?-fruit(pear).
yes
for which Prolog would reply with ‘yes’ (a pear is a fruit). We could also
querywhetherapearisavegetable,andPrologwouldreturn‘no’(apearis
notavegetable).
|?-vegetable(pear).
no
Nowlet’ssaywewanttoknowwhatthingsarefruits.Wecouldprovidea
variabletoProlog(astheargument)andgetthethingsthatarefruits,such
as:
|?-fruit(X).
X=pear
X=apple
X=tomato
yes
You’llalsonoticethatthetomatoisbothafruitandavegetable.Let’scall
thisanoddity,andcreatearuletodefineitassuch.Whatwewanttodois
tellPrologthatifathingisbothafruitandavegetable,thenwe’llclassifyit
asanoddity.InProlog,wewouldcreatethisruleasfollows:
oddity(X):-
fruit(X),
vegetable(X).
TheLanguagesofAI 473
Withthisnewrule,wecanaskPrologifanyodditiesexist,bythefollowing
query:
|?-oddity(X).
X=tomato
yes
Solet’slookatourruleinmoredetail.Theoddityrulesimplysaysthatan
objectisanoddityiftheobjectisbothafruitandavegetable(bindingtothe
variableX).The:-sequencecanbereadasIF,and‘,’canbereadasAND.
Finally,let’ssaythatwewanttoknowifathingisafruitoravegetable.
WecanencodeanORusingthesemicolon,asfollows:
fruit_or_vegetable(X):-
fruit(X);
vegetable(X).
Invokingthisruleas‘fruit_or_vegetable(X).’resultsinthelistofbothfruits
andvegetables(withtomatobeingprovidedtwice).
Let’s now look at a more complex example that includes a couple of
levelsofrules.AninterestingexampleofProlog’sdeductioncapabilitiescan
beillustratedusinggenealogy.AsimplefamilytreeisshowninFigure13.2.
Thistreeshowsfourgenerations,withparentsatthetopandgreat-grand
childrenatthebottom.
Intheend,we’llshowhowtoencoderulestodeducetheparentsofa
child,thegrandparentsofachild,thechildrenofparentsorgrandparents,
andothers.
Let’sstartwithfacts,andhowwe’llencodethefamilytreeinformation
inProlog.First,weencodethegenderoftheindividualsinthetreeusing
thepredicatemaleandfemale.Thefollowingfactsencodethegenderofa
person:
FIGURE13.2: AsamplefamilytreeforPrologdeduction.
474 ArtificialIntelligence
male(maurice).
male(gettis).
male(bud).
male(ernie).
male(tim).
male(ed).
male(marc).
male(greg).
female(katherine).
female(viola).
female(celeta).
female(lila).
female(jill).
female(allison).
female(maria).
female(megan).
female(elise).
Next, we identify the members of the tree that are married using the
marriedpredicate.Thisrelatesamalepersonandafemalepersoninholy
matrimony:
married(maurice,valerie).
married(gettis,viola).
married(bud,celeta).
married(ernie,lila).
married(tim,jill).
married(ed,maria).
married(greg,allison).
Thenextstepistoidentifytherelationshipsofthepeopletooneanother.
Theonlyrelationshipthat’snecessarytonoteisthesetofparentsforagiven
individual.Fromthis,wecandeduceotherrelationships,suchasgrandparent
orgrandchild(aswe’llseelater).
father(maurice,bud).
father(gettis,celeta).
father(bud,tim).
father(bud,ed).
TheLanguagesofAI 475
father(ernie,jill).
father(ernie,greg).
father(tim,megan).
father(tim,elise).
father(tim,marc).
mother(valerie,bud).
mother(viola,celeta).
mother(celeta,tim).
mother(celeta,ed).
mother(lila,jill).
mother(lila,greg).
mother(jill,megan).
mother(jill,elise).
mother(jill,marc).
Thefatherandmotherfactsspecifytheparentfollowedbythechild.The
predicate,fatherormother,definestheirspecificrelationship.
Nowthatwe’vedefinedourfacts,let’screateasetofrulesthatallow
ustodeducesomeadditionalrelationships.First,let’shavealookattwo
rulesthatcandeterminethehusbandorwifeofagivenperson.Tobea
husband,apersonofthemalegenderismarriedtoapersonofthefemale
gender.Similarly,awifeisafemalepersonthatisalsomarriedtoamale
person.
husband(Man,Woman):-
male(Man),married(Man,Woman).
wife(Woman,Man):-
female(Woman),married(Man,Woman).
Thenextruledefineswhetherapersonisaparentofachild.Inthisrule,we
useanOR(;)andsimplydefinethatapersonisaparentofachild,ifthat
personisamotherorfatherofthatchild.
parent(Parent,Child):-
father(Parent,Child);mother(Parent,Child).
Wecanthenusetheparentruletocreateanewruleforthechildtest.Inthis
rule,wedefinethatapersonisachildofpersonifthatpersonistheparent
ofthechild(usingtheparentrule).
476 ArtificialIntelligence
child(Child,Parent):-
parent(Parent,Child).
With the child rule, we can create two other rules to refine the child
relationship.First,wecreateasonrule,whichdefinesthatasonisamale
childofaparent.Similarly,adaughterisafemalechildofaparent.
son(Child,Parent):-
male(Child),child(Child,Parent).
daughter(Child,Parent):-
female(Child),child(Child,Parent).
Sofar,ourruleshavefocusedondirectrelationshipsbetweenpersonsin
thedatabase.Nowlet’slookattworulesthatdefineindirectrelationships
topersonsinthedatabase,grandchildrenandgrandparents.Agrandparent
canbedefinedastheparentofthechild’sparent.Conversely,agrandchild
isthechildofthegrandparent’schild.Wecanlookatthesebothasparent
relationships,asdefinedbelow:
grandparent(Gparent,Gchild):-
parent(Gparent,Parent),parent(Parent,Gchild).
grandchild(Gchild,Gparent):-
grandparent(Gparent,Gchild).
Inthegrandparentrule,wematchthefactsforthegrandparent,andfind
thechildren(whicharereferencedbytheParentvariable).Thenweusethe
parentruletoidentifyifthespecifiedGchildisachildofthatParent.The
grandchildruleisthenareverseofthegrandparentrule.
Let’snowhavealookatthisdatabaseinaction.Thisisillustratedbelow
usingtheGNUprologinterpreter(gprolog),(seeListing13.8).
Listing13.8: Queryingthefamilytreedatabasewithgprolog.
mtj@camus:~$gprolog
GNUProlog1.2.18
ByDanielDiaz
Copyright(C)1999-2004DanielDiaz
|?-consult(‘family.pl’).
compiling/home/mtj/family.plforbytecode...
/home/mtj/family.plcompiled,71linesread-5920byteswritten,51ms
(10ms)yes
TheLanguagesofAI 477
|?-grandchild(marc,X).
X=bud
X=ernie
X=celeta
X=lila
no
|?-married(bud,X).
X=celeta
yes
|?-daughter(X,jill).
X=megan
X=elise
no
|?-son(X,maurice).
X=bud
no
|?-mother(celeta,X).
X=tim
X=ed
yes
|?-grandparent(X,tim).
X=maurice
X=gettis
X=valerie
X=viola
(20ms)no
|?-
Alsoofinterestisthetracefacilityprovidedbygprolog.Thisallowsus
totracetheflowoftheapplicationofrulesforagivenquery.Forexample,the
followingquerycheckstoseeifapersonisagrandparentofanotherperson.
Noteherethatthevariablesprecededbyunderscoresaretemporaryvariables
createdbyPrologduringitsrulechecking.Notealsothecall/exitpairswhich
determinetherelationshipsalongthewaysasneededbythespecificrules.As
specifiedabove,agrandparentisapersonwhoistheparentoftheparent.
Listing13.9: Queryingthefamilytreedatabasewithgprologwithtraceenabled.
|?-trace.
Thedebuggerwillfirstcreep--showingeverything(trace)
478 ArtificialIntelligence
yes
{trace}
|?-grandparent(bud,marc).
11Call:grandparent(bud,marc)
22Call:parent(bud,_79)
33Call:father(bud,_103)
33Exit:father(bud,tim)
22Exit:parent(bud,tim)
42Call:parent(tim,marc)
53Call:father(tim,marc)
53Exit:father(tim,marc)
42Exit:parent(tim,marc)
11Exit:grandparent(bud,marc)
true?
yes
{trace}
|?-
Prolog can be used for much more complex relationships, but this
illustratesthepowerofdeductionprovidedbythePrologengine.
ArithmeticExpressions
WhilePrologsupportsthearithmeticoperators,theirusediffersfromwhat
youmightexpect.Forexample,thefollowingexpressionisfalse:
2*4=8.
This expression is false because ‘2 * 4’ is a compound term and ‘8’ is a
number.Arithmetically,theyarethesame,butPrologseesthisdifferently.
Ifwe’retestingthevalidityofanexpression,weinsteadusetheisoperator.
Forexample,thefollowingexpressionistrue,aswe’recheckingthevalidity
oftheexpressionusingis:
8is2*4.
We can relate our expression to a variable using the is operator as well.
The following final example illustrates the variety of operations that are
available.
|?-Xis(6*2+4**3)/4.
TheLanguagesofAI 479
X=19.0
yes
|?-
Finally,let’slookatanexamplethatcombinesarithmeticexpressionswith
logicprogramming.Inthisexample,we’llmaintainfactsaboutthepricesof
products,andthenarulethatcancomputethetotalcostofaproductgiven
aquantity(again,encodedasafact).
First,let’sdefineourinitialsetoffactsthatmaintainthepricelistofthe
availableproducts(inthiscase,fruits).
cost(banana,0.35).
cost(apple,0.25).
cost(orange,0.45).
cost(mango,1.50).
These facts relate (or create a mapping between) a fruit to a per-item
cost(forexample,mangosrunaround$1.50each).Wecanalsoprovidea
quantitythatweplantopurchase,usinganotherrelationforthefruit,for
example:
qty(mango,4).
Wecanthenaddarulethatcalculatesthetotalcostofanitempurchase:
total_cost(X,Y):-
cost(X,Cost),
qty(X,Quantity),
YisCost*Quantity.
Whatthisrulesaysisthatifwehaveaproductwithacostfact,andaquantity
fact,thenwecancreatearelationtotheproductcostandquantityvaluesfor
theitem.WecaninvokethisfromtheProloginterpreteras:
|?-total_cost(mango,TC).
TC=6.0
yes
|?-
Thistellsusthatthetotalcostforfourmangosis$6.00.
480 ArtificialIntelligence
PrologSummary
As we’ve demonstrated here, Prolog is unique language that offers
programming in a semi-traditional imperative paradigm, as well as logic
programming based on first-order predicate calculus. While restricted to
Hornclauses,Prologisapowerfullanguagethatpermitsthedevelopment
ofknowledge-basedapplications.Thisintroductionhasonlyscratchedthe
surfaceofProlog,andtheresourcesectionprovidesmoreinformationon
wheretogotolearnmore.
OTHERLANGUAGES
AsillustratedbythemanyexamplesinCprovidedbythisbook,artificial-
intelligence applications are by no means restricted to the traditional
languages of AI. Other popular languages provide some of the powerful
featuresthatweredemonstratedforLISPandScheme.Forexample,the
Rubylanguageprovidesdynamicfunctioncreationsimilartothelambda
functionavailableinLISPandScheme.Infact,thelambdakeywordremains.
ThefollowingexampleillustratesthelambdafunctioninRuby.
irb>mulby2=lambda{|x|x*2}
=>#<Proc>
irb>mulby2.call(5)
=>10
Ruby also provides the Procs method, which creates new procedure
objects:
irb>defmulby(x)
irb>returnProc.new{|n|n*x}
irb>end
=>nil
irb>mulby2=mulby(2)
=>#<Proc>
irb>mulby2.call(5)
=>10
ThePythonlanguagealsosupportssomeofthefunctionalfeaturesyou’ll
findinLISPorScheme.Recallthemapfunctionwhichallowsafunctionto
beappliedtoalistofdata.Usingmap,afunctionisprovided(whichistobe
TheLanguagesofAI 481
appliedtothelist),aswellasthelistitself.Thefollowingexampleillustrates
creatingafunctionthatmultiplestheargumentbytwo,andthenappliesthis
functiontoalist.
>>>defmulby2(val):
… returnval*2
…
>>>mylist=[1,2,3,4,5]
>>>newlist=map(mulby2,mylist)
>>>printnewlist
[2,4,6,8,10]
>>>
Astheseshortexamplesillustrate,functionalprogrammingisnotrestricted
solely to functional languages. The newer object-oriented scripting
languages,suchasRubyandPython,haveborrowedthekeyfeaturesfrom
theirancestorsandarebringingthemintothefuture.
CHAPTERSUMMARY
Languages are the vehicles by which developers create solutions to
problems. But unfortunately, languages are also religious artifacts that
become the centerpieces of heated debates. But ultimately, languages
are tools, and each has their strengths and weaknesses. This chapter
introducedthemajorclassesoflanguagesforAI,andalsodiscussedfour
particular languages that are used in AI application development. The
chapterconcludedwithareviewofsomeofthefeaturesthatstartedinthe
olderfunctionallanguages,buthavenowbeenappliedtomoderndomain-
specificlanguages.
REFERENCES
[R6RS]Sperber,Michael,etal“RevisedReportontheAlgorithmLanguage
Scheme,”9/5/2006.
Availableonlineathttp://www.r6r2.org/r6r2_91.pdf
[Sloman1996]Sloman,Aaron“AnOverviewofPOP-11,”SecondEdition.
Available online at http://www.cs.bham.ac.uk/research/projects/poplog/
primer/
482 ArtificialIntelligence
RESOURCES
Dybvig,R.Kent.“TheSchemeProgrammingLanguage,”2003.TheMIT
Press.
GNUProlog
Availableonlineathttp://gnu-prolog.inria.fr/
McCarthy,John.“RecursiveFunctionsofSymbolicExpressionsandTheir
ComputationbyMachine.”1960.
Sloman,Aaron.“TeachPrimer–AnOverviewofPOP-11,”1996.
Availableonlineathttp://cs.bham.ac.uk/research/projects/poplog/primer/
Steele, Guy and Sussman, Gerald. “The Original Lambda Papers,” 1975-
1980.
Availableonlineathttp://library.readscheme.org/page1.html
EXERCISES
1. Historically,whatwasthefirstAIlanguage,andwhatfeaturesmadeit
idealforAIprogrammingoftheday?
2. WhatdoesitmeanforalanguagetobeTuringcomplete?
3. Describetheprimaryfeaturesoffunctionallanguages,asdescribedin
thistext.
4. Describethepurposeofthemapfunction,whichcanbefoundinthe
olderfunctionallanguages,aswellasthenewdomain-specificlanguages
suchasRubyandPython.
5. Describehowfirst-classfunctionsareimportantforAIprogramming.
6. Definetheimportantcharacteristicsofimperativeprogramming.
7. Describe what is meant by polymorphism for object-oriented
languages.
8. Defineoneprimarydifferenceoflogicprogramminglanguages,suchas
Prolog,tootherlanguages.
9. WhatfeatureofLISPintroducedtheconceptofdynamicfunctions?
10. Whatistheresultoftheoperation(car(cdr‘(a,(b,c),d)))in
LISP?
11. Whatistheresultoftheoperation(cadr ‘(a, (b, c), d, e)))in
LISP?
12. DescribethepurposeoftheevalfunctioninLISP.
13. WriteafunctioninLISPtoreturntheagegivenanameforthedatabase
functions.
14. Describe some of the primary differences between Scheme and its
predecessor,LISP.
TheLanguagesofAI 483
• IncludedontheCD-ROMaresimulations,code,videos,figuresfrom
thetext,thirdpartysoftware,andotherfilesrelatedtotopicsinartificial
intelligence.
• See the “README" files for any specific information/system
requirements related to each file folder, but most files will run on
Windows2000orhigherandLinux.
INDEX
1NN,185-192 AdaptiveResonanceTheory(ART),
2001:ASpaceOdyssey,2 257,313-322
4-Queensproblem,76-80 Adjacencylists,29-30,68-70
apriori,305-307 Adjacencymatrix,28-29
Affectivecomputing,430-432
A Synthesizingemotion,431-433
A*searchalgorithm,123 Agency,12
A*Search,57-63 Agentarchitectures,366-382
EightPuzzle,59-66 Agentcommunication,385-389
Implementation,61-64 Agentenvironments,353-356
Applicationsof,65 Agentlanguages,382-385
A*variants,65 AgentpropertiesandAI,352-353
ACL(FIPAAgentCommunicative Agentsystems,12
Language),388 Agenttaxonomies,356-366
ACOalgorithm,424 AgentTCL,384-385
ACO,10,423-430 Agents,351-353,404-405
Activationfunctions,285 Aglets,363-364,379-380,383-384
Activenetworking,364 AIandgames,15
ActiveNodeTransferSystem(ANTS), AIeffect,10
364 AI@50,6
Actuationwitheffectors,338 AIML,361-362
Ada,437 AIS,11
ADALINE,250,265 ALGOL,437
488 ArtificialIntelligence
ALife,11 Automaticcomputingsystems,400
Alphaparameter,62 AutomaticMathematician(AM),165-
Alpha-betapruning,101-106 168
AM,166-167 Avoidancebehavior,341
Ananova,358 Axons,251
Anatomyofanagent,350-351
And-orgraphs,115 B
Annealing,66-68 Backgammon,116-117
AntColonyOptimization(ACO),10, Backpropagation,250,257,265-270
199,423-430 Algorithm,250,267-268
Travelingsalesmanproblem,423- Implementation,268-270
424 Tuning,274
ACOalgorithm,424 Trainingvariants,274
ACOparameters,430 Weightadjustmentvariants,274-
ANTS,364 275
Anytimeplanning,343 Backtracking,84-85
API,363,380,383,400 Backwarderrorpropagation,265
Arcs,27 BACONSystem,165-167
ART,172,257,313-322 BASIC,437
ART-1,313-322 BB1,377
ART-2,313-322 BBH,200-201
Artificiallife,402-410 BBS,374
Echo,403 Beam-search,56
Tierra,403 Behaviorarchitectures,373-374
Simulatedevolution,403-404 Behavior-BasedSystems(BBS),374
Variationsofartificiallife,408 Belief-Desire-Intention(BDI)
Lindenmayersystems,408 architectures,370-371
ArtificialImmuneSystems(AIS),11, Best-FirstSearch(BFS),50-57
398-402 BestFS,51-54
Self-managementcapabilities,399- Betaparameter,62
400 BFS,39-47,123
ArtificialIntelligenceMarkup BidirectionalSearch,40-41
Language(AIML),361-362 Bitboard,110
ART-implementation,316-322 Blackboardarchitectures,369-370
Asimov,Isaac,2 Blanksheets,4
A-staralgorithm,57-63 Bottom-upapproach,7
ATLANTIS,375-376 Braitenburgvehicles,334-335
ATN,9 Breadth-FirstSearch(BFS),39-47,123
Atomicsentences,153 Brooks,Rodney,13,10,132
AugmentedTransitionNetwork(ATN),9 Bug,403-408
Index 489
Building-BlockHypothesis(BBH), Constraint-satisfactionalgorithm,84-86
201-202 Generateandtest,84
Backtracking,84-85
C Forwardchecking,85-86
Clanguage,437 Lookahead,85-86
CA,393-398 ConstraintSatisfactionProblems
CCD,337 (CSP),81-85
Celldecomposition,343-345 Graphcoloring,81-83
CellularAutomata(CA),393-398 Scheduling,83-84
One-dimensionalCAs,394-395 Constraints,81
Two-dimensionalCAs,394-396 Content-AddressableMemory(CAM),
Conwayapplication,396-398 322-323
Turingcompleteness,398 Conway,John,11,395
Emergenceandorganization,398 CornellAeronauticalLaboratory,7
centroid,305-311 Costfunctions,58-62
CG,358 Crossoverprobability,230
Charge-Couple-Device(CCD),337 CSP,81-85
ChatterBots,360 Cubeprogram,214
Checkers,2,4,15,90,106-109 Cybernetics,291
Checker-boardrepresentation,107 Cycle,27
Chess,2,4,15,25,90,95,109-112,354
Chess-boardrepresentation,110 D
ChildMachine,3 DAI,353
Chinook,107-109 DARPA,332
Chunking,382 DartmouthAIConference,4
ClassicalGameAI,106-122 Dataminingapplication,2
Checkers,106-109 DEalgorithm,228-230
Chess,109-112 DE,227-235
Clark,ArthurC.,2 Decisiontrees,172-173
Classesofsearch,22 Creating,174-176
Class-nodeactivation,276 Decision-treelearning,173-176
Clustering,296 Declarativeknowledge,144
Colmeraur,Alain,9 Decomposition,330
Commonsense,168 Deductivereasoning,151-152
Communicationofknowledge,167 DeepBlue,110
Compoundsentences,154 Deepspace1,12
Computationknowledgediscovery, Deliberativearchitectures,368-369
165-167 Deltarule,262
Conjunctions,149 Demons,147
Constraintrelaxation,81 DendralProject,7
490 ArtificialIntelligence
Dendrites,251 EURISKO,167
Depth-FirstSearch(DFS),31-32,93, EvolutionaryComputation,16,195-244
123 Strategies,196
Depth-LimitedSearch(DLS),33-34 Programming,197
DFS,31-32,93,123 Geneticalgorithms,197-198
Diagraph,27-28 Geneticprogramming,198
DifferentialEvolution(DE),227-235 Biologicalmotivation,199-200
Algorithm,228-230 Geneticalgorithms,200-220
Implementation,230 GeneticProgramming(GP),212-
Discoveryspaceship,2 220
Disjunctions,149 EvolutionaryStrategies(ES),220-
Disk-squaretables,112 227
DistributedArtificialIntelligence DifferentialEvolution(DE),227-
(DAI),353 235
DistributedProblemSolving(DPS),353 ParticleSwarmOptimization
DistributedSequentialComputing (PSO),236-244
(DSC),381-382 EvolvableHardware,244
Diversification,81 Evolutionaryneuralnetworks,416-422
DLS,33-34 Geneticallyevolvedneural
Dorigo,Marco,11 networks,416-422
DPS,353 EvolutionaryStrategies(ES),220-227
DSC,381-382 Algorithm,221-223
DUPinstruction,214 Implementation,223-227
Evolutionarystrategiesalgorithm,221-
E 223
Echo,403 Evolvablehardware,244
EightPuzzle,58-65
DemonstrationwithA*,64-65 F
Effectors,338,351 Feedforward,256,276,301
ElizaandParry,360 Field-of-View(FOV),125,130-131
Eliza,6,9 Fifteenpuzzle,59
EMACS,8 FifthGenerationComputerSystems
Emailfiltering,364-365 Project,156-157
End-gamedatabase,109 FiniteStateMachine(FSM),130-131
End-games,113 FIPA,388
Engelman,Carl,7 First-OrderandProlog,148,155
Entertainmentagents,358 First-OrderLogic(PredicateLogic),
Entropy,174 152-162
ES,220-227 First-personshooter(FPS),13,172-
Euclideantour,70-71 173,355-356
Index 491
Fitness-proportionateselection,201 Implementation,215-220
FOL,152-163 Geneticrecombination,202
FORTRAN,437 Go,114-115
Forwardchecking,83-85 Goalgeneration,115
Forwardpropagation,265 Go-Boardrepresentation,112
FourColorTheorem,83 GOFAI,9
FOV,127 Goldblach’sconjecture,166
FPS,13,172-173,355-356 Good-old-fashioned-AI,9
Frames,146-148 GP,212-220
Generic,146 Graphalgorithms,32
Instance,146 Graphcoloring,81-83
Longbowman,146 Graphs,27
Pikeman,147 Groupordistributedrobotics,345
FSM,131-132,360
Functionalprogramming,434-435 H
Fuzzycontrol,415-416 HAL,2-3
Fuzzylogic,410-416 Hart,Tim,6
Fuzzylogicmapping,411-414 Haskell,435
Fuzzylogicoperators,414-415 Hebb,Donald,7
Fuzzysystems,410-416 Hebb’srule,291-296
Fuzzylogicmapping,411-414 Implementation,292-296
Fuzzylogic,410-416 Hebbianlearning,7,172,290-291
Fuzzylogicoperators,414-415 HelperAPIs,31-32
Fuzzycontrol,415-416 Hidden-nodeactivation,277
Hill-climbingsearch,65-66
G HistoryofAI,1
GA,199-200,200-220 Holland,John,9,403
Gameagents,358-359 Homer,376-377
GameofLife,11,395-396 Hopfieldauto-associatoralgorithm,
Gametheory,90 323-324
GeneralProblemSolver(GPS),4 Hopfieldauto-associativemodel,322-
Generalsearchparadigms,31 327
Generalstatespacesearch,22 Content-AddressableMemory
Generalization,268,273 (CAM),322-323
GenerateandTestmethod,31-32,84 Hopfieldauto-associatoralgorithm,
Geneticalgorithms,197-199,200-220, 323-324
212-220 Hopfieldimplementation,324-327
Implementation,204-211 Hopfieldimplementation,324-327
GeneticProgramming(GP),198,212- Hornclauses,9
220 HTML,163-164
492 ArtificialIntelligence
HTTP,350,353 Karel++,346
Hybridagents,366 Kismet,331
Hybridarchitectures,371 k-Means,304
Hybridmodels,17 k-Meansalgorithm,305-307
Hyperplane,258 k-Meansclustering,257,304-313
k-Meansalgorithm,305-307
I k-Meansimplementation,307-313
ID3,36-37,173,176, k-Meansimplementation,307-313
Imperativeprogramming,437 KnowledgeQueryandManipulation
Informationgatheringandfiltering,365 Language(KQML),385-387
InformationretrievalandKR,157 KnowledgeRepresentation(KR),15,
Informedsearch,15,41-87 143-169
Best-FirstSearch(BFS),50-57 Typesofknowledge,144
A*Search,57-63 Roleofknowledge,144-145
Hill-climbingsearch,65-66 Semanticnetworks,145-146
IntegratedManagementConsole,401- Frames,146-147
403 Propositionallogic,149-152
Intelligentagents,349-389 First-OrderLogic(Predicate
Anatomyofanagent,350-351 Logic),152-163
AgentpropertiesandAI,351-353 Computationknowledgediscovery,
Agentenvironments,353-356 165-167
Agenttaxonomies,356-366 Ontology,167
Agentarchitectures,366-382 Communicationofknowledge,167
Agentlanguages,382-385 Commonsense,168
Agentcommunication,385-389 Kohonenself-organizingmaps,257
Intelligentagents,17,132 Koza,John,198
Intensification,80 KQML,385-387
InterdisciplinaryR&D,12 KQMLperformatives,387
Interfaceagents,356-357 KR,143-159
iProlog,148-149
IterativeDeepeningSearch(IDS), L
36-37 LAMA,346
Languagetaxonomy,433-442
J LanguagesofAI,17
Java,363,379-380 Last-In-First-Out(LIFO),32
JavaVirtualMachine(JVM),379-380 Layeredbehaviorarchitectures,130-131
JVM,379-380 Least-Mean-Squares(LMS)Learning,
250,257,262-265
K Learningalgorithm,262-263
Karel,346 Implementation,263-265
Index 493
Lenat,Doug,168 MarkI,Ferranti,4
Levin,Mike,6 Markovmodels,177
LIFOqueue,32 Markovchains,177-181
LIFOstack,32 Wordgenerationwith,179-180
Lindenmayersystems,408-410 Implementation,180-181
Lineardiscriminants,257-258 Martin,William,7
Lineargeneticprogramming,212 Maximsagent,365
Linearseparableproblems,257 McCarthy,John,6,9
LISP,6-8,167,200,365,385-386,435- MechanicalIntelligence,2
436,443-451 Messengers,380-381
Historyof,443-444 MessyandScruffyapproachestoAI,10
Datarepresentation,444 Meta-knowledge,144
Simpleexpressions,444 Min-conflictssearch,86
Predicates,445 Minimaxalgorithm,92-106
Variables,445 ImplementationforTic-Tac-Toe,
Listprocessing,445-446 98-101
Conditions,447-448 Alpha-betapruning,101-106
Functions,448-449 Minimaxsearchwithalpha-beta
LMS,262-266 pruning,111
Logicprogramming,441-442 Minsky,Marvin,8,146
LOGO,346 MIT,7
Loki,120-121 ML,435
Lookahead,84-85 MLP,254-256,265
Loop,27 Mobileagents,362-363
L-systems,408-410 Mobilearchitectures,371-372
Luna2,331 Mobility-basedevaluation,112
ModusPonens,149
M ModusTollens,149
Machinelearning,16,172-192 Moses,Joel,7
Machine-learningalgorithms,171- Motionplanning,342-343
172 Movementandpath-finding,121-122
Supervisedlearning,172-173 Movementplanning,342-345
Unsupervisedlearning,176-181 MSE,262
NearestNeighborClassification MULinstruction,214
(1NN),185-192 Multi-layerneuralnetwork,273
Machine-learningalgorithms,171-172 Multi-layerperceptron(MLP),250,
Maclisp,7 254-256
Macsyma,7 Multi-Prob-Cut(MPC),113
Managementconsole,399 Mutation,232-233
MarkI,259 MYCIN,8
494 ArtificialIntelligence
N N-puzzle,60,91
NASA,12 N-Queensproblem,50-56
NaturalLanguageProcessing(NLP),9 N-Queensproblem,77-78
NaturalLanguageUnderstanding
(NLU),9 O
NearestNeighborClassification(1NN), OAA,377
185-192 Objectavoidancebehavior,341
NeatandScruffyapproachestoAI,9 Object-orientedprogramming(OOP),
Neuralnetworktopology,269 438
NeuralNetworksI,16,249-285 Obliq,382
Shorthistory,249-250 One-dimensionalCAs,392-393
Biologicalmotivation,250-251 Ontology,165
Fundamentals,251-252 OOP,438
Perceptron,257-261 OpenAgentArchitectures(OAA),377
Least-Mean-Squares(LMS) Opendynamicsengine,346
Learning,262-265 Openingbookdatabase,110-111
Learningwithbackpropagation, Openingknowledge,112
265-270 Othello,112-113
ProbabilisticNeuralNetworks
(PNN),276-281 P
OtherNeuralNetwork Papert,Seymour,8
Architectures,281-283 ParticleSwarmOptimization(PSO),
NeuralNetworksII,17,289-327 236-244
Unsupervisedlearning,289-290 Algorithm,236-238
Hebbianlearning,290-291 Implementation,238-244
SimpleCompetitiveLearning,296-313 Patternrecognition,273-274
k-Meansclustering,304-305 Pattern-basedevaluation,112
AdaptiveResonanceTheory(ART), PDP-6,6
313-322 Perceptron,7
HopfieldAuto-associativemodel, Perceptronimplementation,260-261
322-327 Perceptronlearningalgorithm,259
Neurogammon,116-117 “Perceptrons”paper,8,250
Newell,Alan,4 Perceptronrule,259
Nim,26-27 Perceptronwithsensors,337-338
NLP,9 Perceptron,257-261
NLU,9 Phenotypicalgorithm,222
Non-Player-Character(NPC),124-134 Planner,136
Non-zero-sumgame,90 PNN,276-281
NPCbehavior,129-130 PNNclassifierfunction,277-279
NPC,124-134,352-354,356-358 Poker,90,109,118-121
Index 495
POP-11,361,437,460-468 RAPT,346
Historyof,460 Ray,Tom,403
Datarepresentation,460-462 RBF,277
Variables,462 RBS,136-139
Listprocessing,462-463 RDF,164
Conditions,463-464 Reactiveagent,358
Iterationandmaps,464 Reactivearchitectures,367-368
Patternmatching,465 Reactivecontrolsystemarchitecture,
Procedures,465-468 340
Potentialfields,344-345 Real-timestrategyAI,123,136
Predicatelogic,152-163 Recombination,232-233
PrincipalVariationSearch(PVS),108 Recurrentneuralnetwork,283
Prinz,Dietrich,4 Remoteagent,12
ProbabilisticNeuralNetworks(PNN), Replacement,232-233
275-281 ResourceDescriptionFramework
Algorithm,275-277 (RDF),164
Proceduralattachment,147-148 Reversi,112
Proceduralknowledge,144 Robotprogramminglanguages,346
ProceduralReasoningSystem(PRS), Robotsimulators,346
378-379 Robotics,329-346
Prodigy,144 Introduction,329-334
Prolog,9,155-157,160-161,468-481 Braitenburgvehicles,334-335
Historyof,469 Naturalsensingandcontrol,336-
Datarepresentation,469-470 337
Listprocessing,470-471 Perceptronwithsensors,337-338
Facts,rules,andevaluation,471-480 Actuationwitheffectors,338
Proof-numbersearch,115 Roboticcontrolsystems,338-339
Propositionallogic,149-152 Simplecontrolarchitectures,339-
Propositions,149 342
PRS,378-379 Movementplanning,342-345
PSO,236-244 Groupordistributedrobotics,345
PSOalgorithm,236-238 Robotprogramminglanguages,346
PSOimplementation,238-244 Robotsimulators,346
Pythonmapfunction,435 Roboticcontrolsystems,338-339
Rosenblatt,Frank,7
Q Rossum’sUniversalRobots,2
Quantifiers,155 Roulettewheelselection,178-179,201-
202,208-211
R Roussel,Phillipe,9
Radial-BasisForm(RBF),277 Ruby,436
496 ArtificialIntelligence
Ruletraverse,161 Tabusearch,75-81
Rule-basedprogramming,136 Min-conflictssearch,86
Rule-BasedSystems(RBS),136-139 PrincipalVariationSearch(PVS),108
Multi-Prob-Cut(MPC),113
S Proof-NumberSearch,115
SA,66-68 Searchfunctions(commonorders),30
Samuel,Arthur,5,106 Searchinadversarialgamespace,25
SchedulingasaCSP,83-84 Searchinapuzzlespace,23
Scheme,435,451-459 TowersofHanoipuzzle,23-25
Historyof,452 Searchinphysicalspace,22
Simpleexpressions,452-453 Seekpowerbehavior,343
Predicates,453 Semanticnetworks,145-146
Variables,453 Semanticweb,163-164
Iterationandmaps,456-457 S-expressions,198,200,212
Conditions,455 Shaftencoder,339
Procedures,457-459 Shannonnumber,4
Scrabble,120-121 Shannon,Claude,4
Search,22-23,29-45,50-67,75-81,86 Shaw,J.C.,4
Classesofsearch,22 Shortliffe,8
Generalstatespacesearch,22 SHRDLU,6
Searchinphysicalspace,22 Sigmoidfunction,270
Searchinapuzzlespace,23 Sigmoidsquashingfunction,267-268
Searchinadversarialgamespace,25 Simbad,346
Uninformedsearch,29-45 Simon,Herbert,4
IterativeDeepeningSearch(IDS), Simplecompetitivelearning,296-313
36-37 Vectorquantization,297-298
Searchinadversarialgamespace,25 Vectorquantization
Depth-FirstSearch(DFS),31-32 implementation,297-305
Searchfunctions(commonorders),30 Simplecontrolarchitectures,339-342
Depth-LimitedSearch(DLS),33-34 SimulatedAnnealing(SA),66-68
IterativeDeepeningSearch(IDS), Simulatedannealingalgorithm,67-68
36-37 Simulatedannealingdemonstration,
Breadth-FirstSearch(BFS),38-39 70-75
Bidirectionalsearch,40-41 Simulatedevolution,403-404
Uniform-CostSearch(UCS),41-42 Single-layerperceptron(SLP),250,
Best-FirstSearch(BFS),50-57 252-254
Beam-search,56 Slidingwindow,281
A*Search,57-58 SLP,254-256
Informedsearch,41-87 Smalltalk,383
Hill-climbingSearch,65-66 SMTP,350,353
Index 497
SOAR,382 Tesla,Nikola,330
SolarSystem,157-159 Testquery,162
Speechrecognition,281 TheLogicTheorist,4
StanfordUniversity,7-8 ThresholdLogicUnit(TLU),258
Staticboardevaluation,111-112 Tic-Tac-Toe,90-96
Staticevaluationfunction,108 Tierra,403
Staticstatemachine,129-130 Time-seriesProcessingArchitecture,
Strachey,Christopher,4 282
StrategicAI,133-134 TLU,258
StrongAI,5-7,15,143 Touchpoint,399-401
Subsumptionarchitectures,131,372- Touchpointautonomicmanagers,400-
373 401
Subsumptioncontrolsystem TowersofHanoiproblem,23-25,91,
architecture,340-342 204-211
Supervisedlearning,16,172-173,257 TravelingSalesmanProblem(TSP),68-
Supervisedlearningalgorithm,260 75,423-424
Supervisedneuralnetwork Trees,27
algorithms,16 Truthtable,149-151
Swarmintelligence,11,237 Truthvalues,152
Synapticjunctions,251 TSP,68-75,423-429
Synthesizingemotion,431-432 Turing,Alan,3-4
Syntheticagents,357-358 Turingcomplete,393
Systemsapproach,12-15 Turingcompleteness,398
Turingmachine,3
T Turingtest,360
Tablelookup,124-125 Two-dimensionalCAs,395-396
Tabulist,75 Two-playergames,89-91
Tabusearch,75-81
Variants,80-87 U
Tabulist,75 UCS,41-45
Tabusearchalgorithm,77-79 UML,146
Tabusearchalgorithm,77-79 UnifiedModelingLanguage(UML),
Taxonomyofrobotics,332-334 146
TCL,384-385 Uniform-CostSearch(UCS),41-42
TD,115 UninformedSearch,15,29-45
TD-Gammon,116-117 HelperAPIs,31-32
TeamAI,132-133 Generalsearchparadigms,31
Telescript,382-383 Depth-FirstSearch(DFS),31-32
TemporalDifference(TD)learning, GenerateandTestmethod,31-32
115 UnrealEngine,358
498 ArtificialIntelligence
UnrealScript,358 Viking11,331
Unsupervisedlearning,16,176-181, Virtualcharacteragents,357-358
257,289-290
UseNet,357 W
Userassistanceagent,364-366 Webspider,350-351
Emailfiltering,364-365 Weiner,Norbert,291
Informationgatheringandfiltering, Weizenbaum,Joseph,6-7
365 Werbos,PaulJohn,9
Otheruser-assistanceapplications, Widrow-Hoffrule,262
365-366 Winner-takes-all,268,273,276,300
Woods,Bill,9
V Word-formlearning,177-180
VanMelles,Bill,8 XML,389-399
Variables,81,101,154-155 XORproblem,8
Variationsofartificiallife,408 Zero-addressarchitecture,213
Vectorquantization,305 Zero-sumgame,89-90
VideogameAI,121-139 Zobristhashing,109
Movementandpath-finding,123-
124