Netlogo
2021年9月14日Download here: http://gg.gg/vz0a1
The following material explains some important features of programmingin NetLogo.
You can use the netlogo testing extension and/or put print statements. – mattsap Apr 23 ’17 at 21:18 I have kind of broken it up, but the problem is with the optimization procedure. I will ask questions about that seperately. – user7335938 Apr 23 ’17 at 21:44. If you are looking for the other way around, i.e. Embed R into NetLogo models, don’t hestitate to try the R-Extension for NetLogo and/or the RServe-Extension. The latter is designed to work if you’re using RNetLogo. The first one won’t work in conjunction with RNetLogo. An introduction to the NetLogo programming language. We build a little ’random walk’ agent-based model. This video is best seen at 720 resolution: click the. Launch What’s New Documentation About NetLogo. Search the Models Library. These videos are from the Fundamentals of NetLogo tutorial on Complexity Explorer (complexityexplorer.org) taught by Prof. This course will explor.
(Note: If you are already familiar with StarLogo or StarLogoT, thenthe material in the first four sections may already be familiar toyou.)
The Code Example models mentioned throughout can be found in theCode Examples section of the Models Library.
*Agents
*Procedures
*Variables
*Colors
*Ask
*Agentsets
*Breeds
*Synchronization
*Buttons
*Lists
*Math
*Random Numbers
*StringsAgents
The NetLogo world is made up of agents. Agents are beings that canfollow instructions. Each agent can carry out its own activity, allsimultaneously.
In NetLogo, there are three types of agents: turtles, patches, andthe observer. Turtles are agents that move around in the world. Theworld is two dimensional and is divided up into a grid of patches.Each patch is a square piece of ’ground’ over which turtles can move.The observer doesn’t have a location -- you can imagine it as lookingout over the world of turtles and patches.
When NetLogo starts up, there are no turtles yet. The observer canmake new turtles. Patches can make new turtles too. (Patches can’tmove, but otherwise they’re just as ’alive’ as turtles and theobserver are.)
Patches have coordinates. The patch in the center of the world hascoordinates (0, 0). We call the patch’s coordinates pxcorand pycor. Just like in the standard mathematical coordinateplane, pxcor increases as you move to the right andpycor increases as you move up.
The total number of patches is determined by the settingsscreen-edge-x and screen-edge-y. When NetLogostarts up, both screen-edge-x and screen-edge-y are17. This means that pxcor and pycor both range from-17 to 17, so there are 35 times 35, or 1225 patches total. (You canchange the number of patches by editing NetLogo’s Graphics window.)
Turtles have coordinates too: xcor and ycor. Apatch’s coordinates are always integers, but a turtle’s coordinatescan have decimals. This means that a turtle can be positioned at anypoint within its patch; it doesn’t have to be in the center of thepatch.
The world of patches isn’t bounded, but ’wraps’ -- so when a turtlemoves past the edge of the world, it disappears and reappears on theopposite edge. Every patch has the same number of ’neighbor’ patches-- if you’re a patch on the edge of the world, some of your’neighbors’ are on the opposite edge.Procedures
In NetLogo, commands and reporters tell agents what to do.Commands are actions for the agents to carry out.Reporters carry out some operation and report a result eitherto a command or another reporter.
Commands and reporters built into NetLogo are calledprimitives. The PrimitivesDictionary has a complete list of built-in commands and reporters.
Commands and reporters you define yourself are calledprocedures. Each procedure has a name, preceded by the keywordto. The keyword end marks the end of thecommands in the procedure. Once you define a procedure, you can useit elsewhere in your program.
Many commands and reporters take inputs -- values thatthe command or reporter uses in carrying out its actions.
Examples: Here are two command procedures:Note the use of semicolons to add ’comments’ to the program.Comments make your program easier to read and understand.
In this program,
*setup and go are user-defined commands.
*ca (’clear all’),crt (’create turtles’),ask,lt (’left turn’), andrt (’right turn’)are all primitive commands.
*random and turtles are primitivereporters. random takes a single number as an input andreports a random integer that is less than the input (in this case,between 0 and 9). turtles reports the agentset consisting ofall the turtles. (We’ll explain about agentsets later.)
setup and go can be called by other procedures orby buttons. Many NetLogo models have a once-button that calls a procedurecalled setup, and a forever-button that calls a procedure calledgo.
In NetLogo, you must specify which agents -- turtles, patches, orthe observer -- are to run each command. (If you don’t specify, thecode is run by the observer.) In the code above, the observer usesask to make the set of all turtles run the commands betweenthe square brackets.
ca and crt can only be run by the observer.fd, on the other hand, can only be run by turtles. Someother commands and reporters, such as set, can be run bydifferent agent types.
Here are some more advanced features you can take advantage of whendefining your own procedures.
Procedures with inputs
Your own procedures can take inputs, just like primitives do.To create a procedure that accepts inputs, include a list ofinput names in square brackets after the procedure name. For example:
Elsewhere in the program, you could ask turtles to each draw anoctagon with a side length equal to its ID-number:
Reporter procedures
Just like you can define your own commands, you can define your ownreporters. You must do two special things. First, use to-report instead ofto to begin your procedure. Then, in the body of theprocedure, use report toreport the value you want to report.Variables
Variables are places to store values (such as numbers). A variablecan be a global variable, a turtle variable, or a patch variable.
If a variable is a global variable, there is only one value for thevariable, and every agent can access it. But each turtle has its ownvalue for every turtle variable, and each patch has its own value forevery patch variable.
Some variables are built into NetLogo. For example, all turtleshave a color variable, and all patches have a pcolorvariable. (The patch variable begins with ’p’ so itdoesn’t get confused with the turtle variable.) If you set thevariable, the turtle or patch changes color. (See next section fordetails.)
Other built-in turtle variables including xcor,ycor, and heading. Other built-in patch variablesinclude pxcor and pycor. (There is a complete listhere.)
You can also define your own variables. You can make a globalvariable by adding a switch or a slider to your model, or by using theglobals keyword at thebeginning of your code, like this:
You can also define new turtle and patch variables using the turtles-own and patches-own keywords,like this:
These variables can then be used freely in your model. Use the set command to set them. (Ifyou don’t set them, they’ll start out storing a value of zero.)
Global variables can by read and set at any time by any agent. Aswell, a turtle can read and set patch variables of the patch it isstanding on. For example, this code:
causes every turtle to make the patch it is standing on red.(Because patch variables are shared by turtles in this way, youcan’t have a turtle variable and a patch variable withthe same name.)
In other situations where you want an agent to read or set adifferent agent’s variable, you put -of after the variable nameand then specify which agent you mean. Examples:
Local variables
A local variable is defined and used only in the context of aparticular procedure or part of a procedure. To create a localvariable, use the let command.You can use this command anywhere. If you use it at the top of aprocedure, the variable will exist throughout the procedure. If youuse it inside a set of square brackets, for example inside an ’ask’, then it will exist only inside those brackets.Colors
NetLogo represents colors as numbers in the range 0 to 140, withthe exception of 140 itself. Below is a chart showing the range ofcolors you can use in NetLogo.
The chart shows that:
*Some of the colors have names. (You can use these names in your code.)
* Every named color except black and white has a number ending in 5.
* On either side of each named color are darker and lighter shades of the color.
* 0, 10, 20, and so on are all black. 9.9999, 19.9999, 29.9999 and so on are all white.Code Example: The color chart was made in NetLogo with theColor Chart Example model.
Note: If you use a number outside the 0 to 140 range, NetLogo willrepeatedly add or subtract 140 from the number until it is in the 0 to140 range. For example, 25 is orange, so 165, 305, 445, and so on areorange too, and so are -115, -255, -395, etc. This calculation isdone automatically whenever you set the turtle variablecolor or the patch variable pcolor. Should you needto perform this calculation in some other context, use thewrap-color primitive.
If you want a color that’s not on the chart, more can be foundbetween the integers. For example, 26.5 is a shade of orange halfwaybetween 26 and 27. This doesn’t mean you can make any color inNetLogo; the NetLogo color space is only a subset of all possiblecolors. A fixed set of discrete hues is available, and you can eitherdecrease the brightness (darken) or decrease the saturation (lighten)of those hues to get your desired color, but you cannot decreaseboth the brightness and saturation.
There are a few primitives that are helpful for working with colorshades. The scale-color primitiveis useful for converting numeric data into colors. And shade-of? will tell youif two colors are ’shades’ of the same basic hue. For example,shade-of? orange 27 is true, because 27 is a lightershade of orange.Code Example: Scale-color Example shows you how to usethe scale-color reporter.
For many models, the NetLogo color system is a convenient way ofexpressing colors. But sometimes you’d like to be able to specifycolors the conventional way, by specifying HSB(hue/saturation/brightness) or RGB (red/green/blue) values. The hsb and rgb primitives let you dothis. extract-hsband extract-hsb letyou convert colors in the other direction.
Since the NetLogo color space doesn’t include all hues,hsb and rgb can’t always give you the exact color youask for, but they try to come as close as possible.Code Example: You can use the HSB and RGB Example model toexperiment with the HSB and RGB color systems.Ask
NetLogo uses the askcommand to specify commands that are to be run by turtles or patches.All code to be run by turtles must be located in a turtle’context’. You can establish a turtle context in any of three ways:
*In a button, by choosing ’Turtles’ from the popup menu. Any code you put in the button will be run by all turtles.
*In the Command Center, by choosing ’Turtles’ from the popup menu. Any commands you enter will be run by all the turtles.
*By using ask turtles.
The same goes for patches and the observer, except that code to berun by the observer must not be inside any ask.
Here’s an example of the use of ask syntax in a NetLogoprocedure:
The models in the Models Library are full of other examples.A good place to start looking is in the Code Examples section.
Usually, the observer uses ask to ask all turtles or allpatches to run commands. You can also use ask tohave an individual turtle or patch run commands. Thereportersturtle,patch, andpatch-atare useful for this technique. For example:
Every turtle created has an ID number. The first turtle createdhas ID 0, the second turtle ID 1, and so forth. The turtleprimitive reporter takes an ID number as an input, and reports theturtle with that ID number. The patch primitive reportertakes values for pxcor and pycor and reports the patch with thosecoordinates. And the patch-at primitive reporter takesoffsets: distances, in the x and y directions, from thefirst agent. In the example above, the turtle with ID number 0 isasked to get the patch east (and no patches north) of itself.
You can also select a subset of turtles, or a subset of patches,and ask them to do something. This involves a concept called’agentsets’. The next section explains this concept in detail.Agentsets
An agentset is exactly what its name implies, a set of agents.An agentset can contain either turtles or patches, but not both atonce.
You’ve seen the turtles primitive, which reports theagentset of all turtles, and the patches primitive, whichreports the agentset of all patches.
But what’s powerful about the agentset concept is that you canconstruct agentsets that contain only some turtles orsome patches. For example, all the red turtles, or the patcheswith pxcor evenly divisible by five, or the turtles in the firstquadrant that are on a green patch. These agentsets can then be usedby ask or by various reporters that take agentsets as inputs.
One way is to use turtles-here orturtles-at, to makean agentset containing only the turtles on my patch, or only theturtles on some other particular patch. There’s also turtles-on so you canget the set of turtles standing on a given patch or set of patches,or the set of turtles standing on the same patch as a given turtleor set of turtles.
Here are some more examples of how to make agentsets:
Once you have created an agentset, here are some simple things youcan do:
*Use ask to make the agents in the agentset do something
*Use any? to see if the agentset is empty
*Use count to find out exactly how many agents are in the set
And here are some more complex things you can do:
*Pick a random agent from the set using random-one-of. For example, we can make a randomly chosen turtle turn green: Or tell a randomly chosen patch to sprout a new turtle:
*Use the max-one-of or min-one-of reporters tofind out which agent is the most or least along some scale. Forexample, to remove the richest turtle, you could say
*Make a histogram of the agentset using the histogram-from command.
*Use values-fromto make a list of values, one for each agent in the agentset. Thenuse one of NetLogo’s list primitives to do something with thelist. (See the ’Lists’ section below.) For example, to find out how rich therichest turtle is, you could say
*Use turtles-fromand patches-fromreporters to make new agentsets by gathering together the resultsreported by other agents.This only scratches the surface -- see the Models Library for manymore examples, and consult the Primitives Guide and PrimitivesDictionary for more information about all of the agentset primitives.
More examples of using agentsets are provided in the individualentries for these primitives in the NetLogo Dictionary. In developingfamiliarity with programming in NetLogo, it is important to begin tothink of compound commands in terms of how each element passesinformation to the next one. Agentsets are an important part of thisconceptual scheme and provide the NetLogo developer with a lot ofpower and flexibility, as well as being more similar to naturallanguage.Code Example: Ask Agentset ExampleBreeds
NetLogo allows you to define different ’breeds’ ofturtles. Once you have defined breeds, you can go on and make thedifferent breeds behave differently. For example, you could havebreeds calledsheep and wolves, and have the wolves try to eat thesheep.
You define breeds using the breeds keyword, at the topof your model, before any procedures:
The order in which breeds are declared is also the order inwhich they are drawn in the graphics window. So breeds definedlater will appear on top of breeds defined earlier; in thisexample, sheep will be drawn over wolves.
When you define a breed such as sheep, an agentset forthat breed is automatically created, so that all of the agentsetcapabilities described above are immediately available withthe sheep agentset.
The following new primitives are also automatically available onceyou define a breed:create-sheep,create-custom-sheep (cct-sheep for short),hatch-sheep,sprout-sheep,sheep-here, andsheep-at.
Also, you can use sheep-own to define newturtle variables that only turtles of the given breed have.
A turtle’s breed agentset is stored in the breed turtlevariable. So you can test a turtle’s breed, like this:
Note also that turtles can change breeds. A wolf doesn’t have toremain a wolf its whole life. Let’s change a random wolf intoa sheep:
The set-default-shapeprimitive is useful for associating certain turtle shapes with certainbreeds. See the section on shapes below.
Here is a quick example of using breeds:Code Example: Breeds and Shapes ExampleButtons
Buttons in the interface tab provide an easy way to control themodel. Typically a model will have at least a ’setup’ button, to setup the initial state of the world, and a ’go’ button to make the modelrun continuously. Some models will have additional buttons thatperform other actions.
A button contains some NetLogo code. That code is run when youpress the button.
A button may be either a ’once-button’, or a ’forever-button’. Youcan control this by editing the button and checking or unchecking the’Forever’ checkbox. Once-buttons run their code once, then stop andpop back up. Forever-buttons keep running their code over and overagain, until either the code hits the stop command, or youpress the button again to stop it. If you stop thebutton, the code doesn’t get interrupted. The button waits until thecode has finished, then pops up.
Normally, a button is labeled with the code that it runs. Forexample, a button that says ’go’ on it usually contains the code ’go’,which means ’run the go procedure’. (Procedures are defined in theProcedures tab; see below.) But you can also edit a button and entera ’display name’ for the button, which is a text that appears on thebutton instead of the code. You might use this feature if you thinkthe actual code would be confusing to your users.
When you put code in a button, you must also specify which agentsyou want to run that code. You can choose to have the observer runthe code, or all turtles, or all patches. (If you want the code to berun by only some turtles or some patches, you could make an observerbutton, and then have the observer use the ask command to askonly some of the turtles or patches to do something.)
When you edit a button, you have the option to assign an ’actionkey’. This makes that key on the keyboard behave just like a buttonpress. If the button is a forever button, it will stay down until thekey is pressed again (or the button is clicked). Action keys areparticularly useful for games or any model where rapid triggering ofbuttons is needed.Netlogo 2d
Buttons and display updates
When you edit a button, there is a checkbox called ’Force displayupdate after each run’. Below the c
https://diarynote.indered.space
The following material explains some important features of programmingin NetLogo.
You can use the netlogo testing extension and/or put print statements. – mattsap Apr 23 ’17 at 21:18 I have kind of broken it up, but the problem is with the optimization procedure. I will ask questions about that seperately. – user7335938 Apr 23 ’17 at 21:44. If you are looking for the other way around, i.e. Embed R into NetLogo models, don’t hestitate to try the R-Extension for NetLogo and/or the RServe-Extension. The latter is designed to work if you’re using RNetLogo. The first one won’t work in conjunction with RNetLogo. An introduction to the NetLogo programming language. We build a little ’random walk’ agent-based model. This video is best seen at 720 resolution: click the. Launch What’s New Documentation About NetLogo. Search the Models Library. These videos are from the Fundamentals of NetLogo tutorial on Complexity Explorer (complexityexplorer.org) taught by Prof. This course will explor.
(Note: If you are already familiar with StarLogo or StarLogoT, thenthe material in the first four sections may already be familiar toyou.)
The Code Example models mentioned throughout can be found in theCode Examples section of the Models Library.
*Agents
*Procedures
*Variables
*Colors
*Ask
*Agentsets
*Breeds
*Synchronization
*Buttons
*Lists
*Math
*Random Numbers
*StringsAgents
The NetLogo world is made up of agents. Agents are beings that canfollow instructions. Each agent can carry out its own activity, allsimultaneously.
In NetLogo, there are three types of agents: turtles, patches, andthe observer. Turtles are agents that move around in the world. Theworld is two dimensional and is divided up into a grid of patches.Each patch is a square piece of ’ground’ over which turtles can move.The observer doesn’t have a location -- you can imagine it as lookingout over the world of turtles and patches.
When NetLogo starts up, there are no turtles yet. The observer canmake new turtles. Patches can make new turtles too. (Patches can’tmove, but otherwise they’re just as ’alive’ as turtles and theobserver are.)
Patches have coordinates. The patch in the center of the world hascoordinates (0, 0). We call the patch’s coordinates pxcorand pycor. Just like in the standard mathematical coordinateplane, pxcor increases as you move to the right andpycor increases as you move up.
The total number of patches is determined by the settingsscreen-edge-x and screen-edge-y. When NetLogostarts up, both screen-edge-x and screen-edge-y are17. This means that pxcor and pycor both range from-17 to 17, so there are 35 times 35, or 1225 patches total. (You canchange the number of patches by editing NetLogo’s Graphics window.)
Turtles have coordinates too: xcor and ycor. Apatch’s coordinates are always integers, but a turtle’s coordinatescan have decimals. This means that a turtle can be positioned at anypoint within its patch; it doesn’t have to be in the center of thepatch.
The world of patches isn’t bounded, but ’wraps’ -- so when a turtlemoves past the edge of the world, it disappears and reappears on theopposite edge. Every patch has the same number of ’neighbor’ patches-- if you’re a patch on the edge of the world, some of your’neighbors’ are on the opposite edge.Procedures
In NetLogo, commands and reporters tell agents what to do.Commands are actions for the agents to carry out.Reporters carry out some operation and report a result eitherto a command or another reporter.
Commands and reporters built into NetLogo are calledprimitives. The PrimitivesDictionary has a complete list of built-in commands and reporters.
Commands and reporters you define yourself are calledprocedures. Each procedure has a name, preceded by the keywordto. The keyword end marks the end of thecommands in the procedure. Once you define a procedure, you can useit elsewhere in your program.
Many commands and reporters take inputs -- values thatthe command or reporter uses in carrying out its actions.
Examples: Here are two command procedures:Note the use of semicolons to add ’comments’ to the program.Comments make your program easier to read and understand.
In this program,
*setup and go are user-defined commands.
*ca (’clear all’),crt (’create turtles’),ask,lt (’left turn’), andrt (’right turn’)are all primitive commands.
*random and turtles are primitivereporters. random takes a single number as an input andreports a random integer that is less than the input (in this case,between 0 and 9). turtles reports the agentset consisting ofall the turtles. (We’ll explain about agentsets later.)
setup and go can be called by other procedures orby buttons. Many NetLogo models have a once-button that calls a procedurecalled setup, and a forever-button that calls a procedure calledgo.
In NetLogo, you must specify which agents -- turtles, patches, orthe observer -- are to run each command. (If you don’t specify, thecode is run by the observer.) In the code above, the observer usesask to make the set of all turtles run the commands betweenthe square brackets.
ca and crt can only be run by the observer.fd, on the other hand, can only be run by turtles. Someother commands and reporters, such as set, can be run bydifferent agent types.
Here are some more advanced features you can take advantage of whendefining your own procedures.
Procedures with inputs
Your own procedures can take inputs, just like primitives do.To create a procedure that accepts inputs, include a list ofinput names in square brackets after the procedure name. For example:
Elsewhere in the program, you could ask turtles to each draw anoctagon with a side length equal to its ID-number:
Reporter procedures
Just like you can define your own commands, you can define your ownreporters. You must do two special things. First, use to-report instead ofto to begin your procedure. Then, in the body of theprocedure, use report toreport the value you want to report.Variables
Variables are places to store values (such as numbers). A variablecan be a global variable, a turtle variable, or a patch variable.
If a variable is a global variable, there is only one value for thevariable, and every agent can access it. But each turtle has its ownvalue for every turtle variable, and each patch has its own value forevery patch variable.
Some variables are built into NetLogo. For example, all turtleshave a color variable, and all patches have a pcolorvariable. (The patch variable begins with ’p’ so itdoesn’t get confused with the turtle variable.) If you set thevariable, the turtle or patch changes color. (See next section fordetails.)
Other built-in turtle variables including xcor,ycor, and heading. Other built-in patch variablesinclude pxcor and pycor. (There is a complete listhere.)
You can also define your own variables. You can make a globalvariable by adding a switch or a slider to your model, or by using theglobals keyword at thebeginning of your code, like this:
You can also define new turtle and patch variables using the turtles-own and patches-own keywords,like this:
These variables can then be used freely in your model. Use the set command to set them. (Ifyou don’t set them, they’ll start out storing a value of zero.)
Global variables can by read and set at any time by any agent. Aswell, a turtle can read and set patch variables of the patch it isstanding on. For example, this code:
causes every turtle to make the patch it is standing on red.(Because patch variables are shared by turtles in this way, youcan’t have a turtle variable and a patch variable withthe same name.)
In other situations where you want an agent to read or set adifferent agent’s variable, you put -of after the variable nameand then specify which agent you mean. Examples:
Local variables
A local variable is defined and used only in the context of aparticular procedure or part of a procedure. To create a localvariable, use the let command.You can use this command anywhere. If you use it at the top of aprocedure, the variable will exist throughout the procedure. If youuse it inside a set of square brackets, for example inside an ’ask’, then it will exist only inside those brackets.Colors
NetLogo represents colors as numbers in the range 0 to 140, withthe exception of 140 itself. Below is a chart showing the range ofcolors you can use in NetLogo.
The chart shows that:
*Some of the colors have names. (You can use these names in your code.)
* Every named color except black and white has a number ending in 5.
* On either side of each named color are darker and lighter shades of the color.
* 0, 10, 20, and so on are all black. 9.9999, 19.9999, 29.9999 and so on are all white.Code Example: The color chart was made in NetLogo with theColor Chart Example model.
Note: If you use a number outside the 0 to 140 range, NetLogo willrepeatedly add or subtract 140 from the number until it is in the 0 to140 range. For example, 25 is orange, so 165, 305, 445, and so on areorange too, and so are -115, -255, -395, etc. This calculation isdone automatically whenever you set the turtle variablecolor or the patch variable pcolor. Should you needto perform this calculation in some other context, use thewrap-color primitive.
If you want a color that’s not on the chart, more can be foundbetween the integers. For example, 26.5 is a shade of orange halfwaybetween 26 and 27. This doesn’t mean you can make any color inNetLogo; the NetLogo color space is only a subset of all possiblecolors. A fixed set of discrete hues is available, and you can eitherdecrease the brightness (darken) or decrease the saturation (lighten)of those hues to get your desired color, but you cannot decreaseboth the brightness and saturation.
There are a few primitives that are helpful for working with colorshades. The scale-color primitiveis useful for converting numeric data into colors. And shade-of? will tell youif two colors are ’shades’ of the same basic hue. For example,shade-of? orange 27 is true, because 27 is a lightershade of orange.Code Example: Scale-color Example shows you how to usethe scale-color reporter.
For many models, the NetLogo color system is a convenient way ofexpressing colors. But sometimes you’d like to be able to specifycolors the conventional way, by specifying HSB(hue/saturation/brightness) or RGB (red/green/blue) values. The hsb and rgb primitives let you dothis. extract-hsband extract-hsb letyou convert colors in the other direction.
Since the NetLogo color space doesn’t include all hues,hsb and rgb can’t always give you the exact color youask for, but they try to come as close as possible.Code Example: You can use the HSB and RGB Example model toexperiment with the HSB and RGB color systems.Ask
NetLogo uses the askcommand to specify commands that are to be run by turtles or patches.All code to be run by turtles must be located in a turtle’context’. You can establish a turtle context in any of three ways:
*In a button, by choosing ’Turtles’ from the popup menu. Any code you put in the button will be run by all turtles.
*In the Command Center, by choosing ’Turtles’ from the popup menu. Any commands you enter will be run by all the turtles.
*By using ask turtles.
The same goes for patches and the observer, except that code to berun by the observer must not be inside any ask.
Here’s an example of the use of ask syntax in a NetLogoprocedure:
The models in the Models Library are full of other examples.A good place to start looking is in the Code Examples section.
Usually, the observer uses ask to ask all turtles or allpatches to run commands. You can also use ask tohave an individual turtle or patch run commands. Thereportersturtle,patch, andpatch-atare useful for this technique. For example:
Every turtle created has an ID number. The first turtle createdhas ID 0, the second turtle ID 1, and so forth. The turtleprimitive reporter takes an ID number as an input, and reports theturtle with that ID number. The patch primitive reportertakes values for pxcor and pycor and reports the patch with thosecoordinates. And the patch-at primitive reporter takesoffsets: distances, in the x and y directions, from thefirst agent. In the example above, the turtle with ID number 0 isasked to get the patch east (and no patches north) of itself.
You can also select a subset of turtles, or a subset of patches,and ask them to do something. This involves a concept called’agentsets’. The next section explains this concept in detail.Agentsets
An agentset is exactly what its name implies, a set of agents.An agentset can contain either turtles or patches, but not both atonce.
You’ve seen the turtles primitive, which reports theagentset of all turtles, and the patches primitive, whichreports the agentset of all patches.
But what’s powerful about the agentset concept is that you canconstruct agentsets that contain only some turtles orsome patches. For example, all the red turtles, or the patcheswith pxcor evenly divisible by five, or the turtles in the firstquadrant that are on a green patch. These agentsets can then be usedby ask or by various reporters that take agentsets as inputs.
One way is to use turtles-here orturtles-at, to makean agentset containing only the turtles on my patch, or only theturtles on some other particular patch. There’s also turtles-on so you canget the set of turtles standing on a given patch or set of patches,or the set of turtles standing on the same patch as a given turtleor set of turtles.
Here are some more examples of how to make agentsets:
Once you have created an agentset, here are some simple things youcan do:
*Use ask to make the agents in the agentset do something
*Use any? to see if the agentset is empty
*Use count to find out exactly how many agents are in the set
And here are some more complex things you can do:
*Pick a random agent from the set using random-one-of. For example, we can make a randomly chosen turtle turn green: Or tell a randomly chosen patch to sprout a new turtle:
*Use the max-one-of or min-one-of reporters tofind out which agent is the most or least along some scale. Forexample, to remove the richest turtle, you could say
*Make a histogram of the agentset using the histogram-from command.
*Use values-fromto make a list of values, one for each agent in the agentset. Thenuse one of NetLogo’s list primitives to do something with thelist. (See the ’Lists’ section below.) For example, to find out how rich therichest turtle is, you could say
*Use turtles-fromand patches-fromreporters to make new agentsets by gathering together the resultsreported by other agents.This only scratches the surface -- see the Models Library for manymore examples, and consult the Primitives Guide and PrimitivesDictionary for more information about all of the agentset primitives.
More examples of using agentsets are provided in the individualentries for these primitives in the NetLogo Dictionary. In developingfamiliarity with programming in NetLogo, it is important to begin tothink of compound commands in terms of how each element passesinformation to the next one. Agentsets are an important part of thisconceptual scheme and provide the NetLogo developer with a lot ofpower and flexibility, as well as being more similar to naturallanguage.Code Example: Ask Agentset ExampleBreeds
NetLogo allows you to define different ’breeds’ ofturtles. Once you have defined breeds, you can go on and make thedifferent breeds behave differently. For example, you could havebreeds calledsheep and wolves, and have the wolves try to eat thesheep.
You define breeds using the breeds keyword, at the topof your model, before any procedures:
The order in which breeds are declared is also the order inwhich they are drawn in the graphics window. So breeds definedlater will appear on top of breeds defined earlier; in thisexample, sheep will be drawn over wolves.
When you define a breed such as sheep, an agentset forthat breed is automatically created, so that all of the agentsetcapabilities described above are immediately available withthe sheep agentset.
The following new primitives are also automatically available onceyou define a breed:create-sheep,create-custom-sheep (cct-sheep for short),hatch-sheep,sprout-sheep,sheep-here, andsheep-at.
Also, you can use sheep-own to define newturtle variables that only turtles of the given breed have.
A turtle’s breed agentset is stored in the breed turtlevariable. So you can test a turtle’s breed, like this:
Note also that turtles can change breeds. A wolf doesn’t have toremain a wolf its whole life. Let’s change a random wolf intoa sheep:
The set-default-shapeprimitive is useful for associating certain turtle shapes with certainbreeds. See the section on shapes below.
Here is a quick example of using breeds:Code Example: Breeds and Shapes ExampleButtons
Buttons in the interface tab provide an easy way to control themodel. Typically a model will have at least a ’setup’ button, to setup the initial state of the world, and a ’go’ button to make the modelrun continuously. Some models will have additional buttons thatperform other actions.
A button contains some NetLogo code. That code is run when youpress the button.
A button may be either a ’once-button’, or a ’forever-button’. Youcan control this by editing the button and checking or unchecking the’Forever’ checkbox. Once-buttons run their code once, then stop andpop back up. Forever-buttons keep running their code over and overagain, until either the code hits the stop command, or youpress the button again to stop it. If you stop thebutton, the code doesn’t get interrupted. The button waits until thecode has finished, then pops up.
Normally, a button is labeled with the code that it runs. Forexample, a button that says ’go’ on it usually contains the code ’go’,which means ’run the go procedure’. (Procedures are defined in theProcedures tab; see below.) But you can also edit a button and entera ’display name’ for the button, which is a text that appears on thebutton instead of the code. You might use this feature if you thinkthe actual code would be confusing to your users.
When you put code in a button, you must also specify which agentsyou want to run that code. You can choose to have the observer runthe code, or all turtles, or all patches. (If you want the code to berun by only some turtles or some patches, you could make an observerbutton, and then have the observer use the ask command to askonly some of the turtles or patches to do something.)
When you edit a button, you have the option to assign an ’actionkey’. This makes that key on the keyboard behave just like a buttonpress. If the button is a forever button, it will stay down until thekey is pressed again (or the button is clicked). Action keys areparticularly useful for games or any model where rapid triggering ofbuttons is needed.Netlogo 2d
Buttons and display updates
When you edit a button, there is a checkbox called ’Force displayupdate after each run’. Below the c
https://diarynote.indered.space
コメント