您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > NetLogo-4-0-QuickGuide
NETLOGO4.0–QUICKGUIDELuisR.Izquierdo()AgentsTheNetLogoworldismadeupofagents.Agentsarebeingsthatcanfollowinstructions.Therearefourtypesofagents:Turtles.Turtlesareagentsthatmovearoundintheworld.Patches:Theworldistwodimensionalandisdividedupintoagridofpatches.Eachpatchisasquarepieceofgroundoverwhichturtlescanmove.Links:Linksareagentsthatconnecttwoturtles.Linkscanbedirected(fromoneturtletoanotherturtle)orundirected(oneturtlewithanotherturtle).Theobserver:Theobserverdoesn'thavealocation-youcanimagineitaslookingoutovertheworldofturtles,linksandpatches.InstructionsInstructionstellagentswhattodo.Therearethreecharacteristicsthatareusefultorememberaboutinstructions:a)Whethertheinstructionisimplementedbytheuser(procedures),orwhetheritisbuiltintoNetLogo(primitives).Onceyoudefineaprocedure,youcanuseitelsewhereinyourprogram.TheNetLogoDictionaryhasacompletelistofbuilt-ininstructions(i.e.aPrimitivesDictionary).tosetup;;commentsarewrittenaftersemicolon(s)clear-all;;cleartheworldcreate-turtles10;;make10newturtlesend;(onesemicolonisenough,butIliketwo)Inthisprogram,setupisaprocedure(sinceitisimplementedbyus),whereasclear-allandcreate-turtlesarebothprimitives(theyarebuiltintoNetLogo).b)Whethertheinstructionproducesanoutput(reporters)ornot(commands).oAreportercomputesaresultandreportsit.Mostreportersarenounsornounphrases(e.g.“average-wealth”,“most-popular-girl”).Thesenamesareprecededbythekeywordto-report.Thekeywordendmarkstheendoftheinstructionsintheprocedure.to-reportaverage-wealth;;thisreporterreturnsthereportmean[wealth]ofturtles;;averagewealthintheend;;populationofturtlesoAcommandisanactionforanagenttocarryout.Mostcommandsbeginwithverbs(e.g.create,die,jump,inspect,clear).Theseverbsareprecededbythekeywordto(insteadofto-report).Thekeywordendmarkstheendoftheinstructionsintheprocedure.togoaskturtles[forward1;;allturtlesmoveforwardonesteprightrandom360];;...andturnarandomamountendc)Whethertheinstructiontakesaninput(orseveralinputs)ornot.Inputsarevaluesthattheinstructionusesincarryingoutitsactions.to-reportabsolute-value[number];;numberistheinputifelsenumber=0;;ifnumberisalreadynon-negative[reportnumber];;returnnumber(anon-negativevalue).[report0-number];;Otherwise,returntheopposite,whichend;;isthennecessarilypositive.1/6VariablesVariablesareplacestostorevalues(suchasnumbers).Avariablecanbeaglobalvariable,aturtlevariable,apatchvariable,alinkvariable,oralocalvariable(localtoaprocedure).Tochangethevalueofavariableyoucanusethesetcommand(Ifyoudon'tsetthevariabletoanyvalue,itstartsoutstoringavalueofzero).a)Globalvariables:Ifavariableisaglobalvariable,thereisonlyonevalueforthevariable,andeveryagentcanaccessit.Youcanmakeaglobalvariablebyaddingaswitch,aslider,achooseroraninputboxtoyourmodel,orbyusingtheglobalskeywordatthebeginningofyourcode,likethis:globals[number-of-trees]b)Turtle,patch,andlinkvariables:Eachturtlehasitsownvalueforeveryturtlevariable,eachpatchhasitsownvalueforeverypatchvariable,andeachlinkhasitsownvalueforeverylinkvariable.Turtle,patch,andlinkvariablescanbebuilt-inordefinedbytheuser.Built-invariables:Forexample,allturtlesandalllinkshaveacolorvariable,andallpatcheshaveapcolorvariable.Ifyousetthisvariable,thecorrespondingturtle,linkorpatchchangescolor.Otherbuilt-inturtlevariablesarexcor,ycor,andheading.Otherbuilt-inpatchvariablesincludepxcorandpycor.Otherbuilt-inlinkvariablesareend1,end2,andthickness.YoucanfindthecompletelistintheNetLogoDictionary.User-definedturtle,patchandlinkvariables:Youcanalsodefinenewturtle,patchorlinkvariablesusingtheturtles-own,patches-own,andlinks-ownkeywordsrespectively,likethis:turtles-own[energy];;eachturtlehasitsownenergypatches-own[roughness];;eachpatchhasitsownroughnesslinks-own[strength];;eachlinkhasitsownstrengthc)Localvariables:Alocalvariableisdefinedandusedonlyinthecontextofaparticularprocedureorpartofaprocedure.Tocreatealocalvariable,usetheletcommand.Youcanusethiscommandanywhere.Ifyouuseitatthetopofaprocedure,thevariablewillexistthroughouttheprocedure.Ifyouuseitinsideasetofsquarebrackets,forexampleinsideanask,thenitwillexistonlyinsidethosebrackets.toswap-colors[turtle1turtle2];;turtle1andturtle2areinputslettemp([color]ofturtle1);;storethecolorofturtle1intempaskturtle1[setcolor([color]ofturtle2)];;setturtle1’scolortoturtle2’scoloraskturtle2[setcolortemp];;nowsetturtle2’scolortoturtle1’s(original)colorend;;(whichwasconvenientlystoredintemp).SettingandreadingthevalueofvariablesGlobalvariablescanbereadandsetatanytimebyanyagent.Everyagenthasdirectaccesstoherownvariables,bothforreadingandsetting.Sometimesyouwillwantanagenttoreadorsetadifferentagent'svariable;todothat,youcanuseask(whichisexplainedindetailabitlater):askturtle5[showcolor];;printturtle5’scoloraskturtle5[setcolorblue];;turtle5becomesblueYoucanalsouseoftomakeoneagentreadanotheragent’svariable.ofiswritteninbetweenthevariablenameandtherelevantagent(i.e.[reporter]ofagent).Example:show[color]ofturtle5;;sameasthefirstlineinthecodeaboveFinally,aturtlecanreadandsetpatchvariablesofthepatchitisstandingondirec
本文标题:NetLogo-4-0-QuickGuide
链接地址:https://www.777doc.com/doc-4963464 .html