Home
NetLogo heatbugs
Contents
1. Heatbugs with NetLogo Introduction Heatbugs is a modeling and simulation exercise which has become firmly established in the canon of agent based modeling and complexity science The model serves as a demonstration of self organization in a population of very simple agents who are only able to sense their immediate surroundings and to interact indirectly only with those agents in the immediate surroundings The agents in this model the heatbugs were reportedly inspired by actual biological organisms although their behavior doesn t closely match that of any specific organism Nonetheless the behavioral rules employed by the agents in the model serve as a basic introduction to the kinds of rules employed in many agent based models including many that attempt to deal more thoroughly with some aspect of the behavior of biological organisms Behavior Each heatbug is born with an ideal temperature generally not all heatbugs have the same ideal temperature Over the course of the simulation the heatbug seeks out in its very short sighted way territory with a temperature matching its ideal At the same time the heatbug radiates some of its own body heat into the environment warming it the amount radiated in each unit of time is also an attribute of each individual heatbug The further the temperature of the local environment is from a heatbug s ideal temperature the more unhappy the heatbug is Thus the central aim o
2. assign them to ideal temp and radiant heat 4 Check your code syntax by clicking the Check button in the toolbar at the top of the Procedures tab If there are errors an error message will appear just under the toolbar and the cursor will be placed on the line where the error was found Try to use the error message and the knowledge of where the error occurred to fix your code 5 When your code is free of syntax errors return to the Interface tab Heatbugs with NetLogo Page 8 6 Create a button to run your setup procedure The Button properties dialog should look something like this Display name and Action key are optional the other values and selections are required E Button observer E Make sure that you have Observer selected in the Agent s drop down and that the Forever checkbox is not checked 7 Click OK 8 Once you ve corrected any error conditions save your model 9 Click the Setup button You should see a bunch of red circles distributed randomly around on the NetLogo world Task 3 Making the Heatbugs Move Now we will implement the heatbug behavior described at the start There are many ways to write this behavior in NetLogo code we will only have time to explore one approach but you are enouraged to experiment with others after completing this tutorial First we must remember that we need to allow for the heatbug moving randomly a user specifiable fraction of the time This sounds li
3. are required either to force a desired order of operations on a computation or to help NetLogo make sense of certain kinds of operations which can have a variable amount of information associated with them Heatbugs with NetLogo Page 11 However parentheses are sometimes used primarily to make explicit for the programmer s benefit the logical structure of an expression m Since many punctuation characters are allowed in NetLogo variable and procedure names we have to be careful when we want to use those characters for their mathematical logical or structural purpose For example a b is an expression that NetLogo will try to evaluate by taking the value of a adding it to the value of b and returning the result However a b without spaces around the plus sign is interpreted by NetLogo to refer to a variable with the name a b and not to the sum of the variables a and b 5 Use the above summaries along with the NetLogo User Manual to help you read and understand the move procedure Feel free to ask questions Task 4 Dissipating and Diffusing Heat Several pages back we hinted that we would need to have the patches do some processing in our model and not simply serve as heat buckets Let s consider two simple questions 1 What would happen if every heatbug kept radiating heat into the environment and that heat simply accumulated in the patches 2 In the absence of some insulating structure should we expect that heat
4. environment and we know they are adding heat to the environment but so far we are able to observe the heat in the environment only indirectly through the heatbugs actions Let s do something about that now One frequently used way to visualize data that varies across a landscape is to color that landscape according to the data NetLogo supports this allowing us not only to set the color of a turtle agent but also to set patch colors For our model let s use shades of blue with the darkest shades representing little or no heat and the lightest shades nearly white representing a lot of heat But how much is a lot We generally won t know in advance how hot the hottest patches might get How should we calibrate our shading so that we use the full range of the shades of blue without having some patches that turn out to be hotter than the temperature corresponding to the lightest shade of blue The most direct way to solve this problem would be to get the temperature of every patch after each tick get the largest of those values and then use that largest value as the temperature corresponding to the lightest shade of violet scaling everything else from there In fact the NetLogo code for this approach is very simple the expression max temperature of patches uses just a few characters to get the key piece of information we need the highest temperature on the terrain Heatbugs with NetLogo Page 14 After we know the highest tempe
5. plot a single measure there s no need to modify or create plot pens or to change other plot settings Plot Average Unhappiness O O O l pig fo mato se 11 Click OK 12 Switch back to the Procedures tab 13 Create a new procedure after the end of the existing code as follows to update plot set current plot Average Unhappiness plot mean unhappiness of heatbugs end In general when there is only one plot in a model s Interface tab there s no need to use set current plot However it s a good habit to get into since your code will require fewer changes if you should add additional plots 14 Modify the go procedure by adding a single line just before the tick statement Type update plot in this new line 15 Check save and run 16 What do you observe about the average unhappiness 17 What happens to the average unhappiness if you change one or more of random move prob dissipation rate and diffusion rate while the model is running Heatbugs with NetLogo Page 17 Discussion How would you characterize the type s of aggregate behavior the heatbugs displayed Did the behavior exhibit any qualitative changes with different values of the input parameters Did displaying the temperature in the patches via color coding make it easier for you to observe the heatbugs behavior or was it a distraction Did the plot of average unhappiness show you what you expected to see In other words did you anticipate
6. radiated into one patch will remain there and not spread by convection or conduction to the neighboring patches Obviously we need to do something so that the heat doesn t continue to build up without limit and so that the heat can flow between the patches Fortunately NetLogo makes it relatively easy to take care of both of these concerns First the diffuse statement can be used to spread some quantity stored in a patch variable around between the patches with the end effect being that the differences between the patches tends to smooth out Second patches are themselves stationary agents we can ask them to perform tasks in the same way that we ask turtles or in the current instance heatbugs to perform tasks But before we write code for this we have a complication there are some things we need to know that we simply don t How quickly should heat diffuse between patches How quickly should heat dissipate out of the system altogether In the real world the answers to these would depend on all sorts of factors none of which are addressed in our model In this case the easy answer is also a pretty good one let the user decide We will add two more sliders to our model these sliders will control the rate of heat diffusion and the rate of heat dissipation The user will be able to set the slider values to what he or she considers to be appropriate and our heatbugs will just have to deal with it 1 Switch to the Interface t
7. a variable without that we have nothing to use for computing the average So we ll need to fix that as our first order of business in plotting the average unhappiness of the heatbug population 1 Find the heatbugs own statement near the top of the Procedures tab To the two variables already listed within the square brackets add a third unhappiness 2 Insert a new line at the start of the move procedure just after the line that reads to move In the new line insert the following code set unhappiness abs temperature ideal temp 3 In the i statement that immediately follows the new line change if temperature ideal temp to if unhappiness gt 0 Check your code Save and test your model the aggregate behavior of the heatbugs should be unchanged Switch to the Interface tab N Ps U A Add a plot to the user interface via the same technique you used to add sliders and buttons simply select Plot instead of Slider or Button from the drop down or pop up menu 8 Name the plot Average Unhappiness Note that the name of a plot is treated like the name of a shape it is not a variable or procedure name and the plot is always referred to by name with the name in quotes Thus this type of name can have spaces in it 9 Check the Autoplot checkbox 10 Uncheck Show legend Heatbugs with NetLogo Page 16 The Plot properties dialog should look something like this since we are only going to
8. ab Heatbugs with NetLogo Page 12 2 Create two more sliders with the following attributes Global variable Minimum Increment Maximum Value dissipation rate 0 0 01 1 00 0 10 diffusion rate 0 0 05 1 00 0 70 3 Now we need to write some more code As it turns out this is a good time to write the main go procedure for our model This procedure will use NetLogo s built in diffuse statement to diffuse the heat across the patches then it will ask the patches to let some of their heat dissipate away finally it will ask the heatbugs to move Try to start writing the go procedure If you get stuck or confused or want to check your work proceed to the next step A Switch to the Procedures tab 5 Below the end of the move procedure add the following code CORGO diffuse temperature diffusion rate ask patches set temperature temperature 1 dissipation rate ask heatbugs move erek end 6 You should be able to read this procedure and match it virtually line for line against the description of what the procedure would do found in 3 above The only part that wasn t mentioned before was the tick statement tick updates the NetLogo time tick counter and depending on the model settings tells NetLogo to update the display 7 Save your model Task 5 Running the Simulation 1 Switch to the Interface tab 2 Create a Go button This button should call the go procedur
9. e and should be a forever button Based on the contents of the go procedure do you think the Go button should be an Observer button a Turtles button a Patches button or a Links button 3 Make sure the View updates checkbox in the toolbar at the top of the Interface tab is checked 4 Select on ticks from the drop down below View updates Heatbugs with NetLogo Page 13 o N OS O Save your model Click the Setup button Click the Go button What happens While the model is running experiment with different values of the random move prob dissipation rate and diffusion rate sliders Do you notice any changes in the aggregate behavior of the heatbugs Try setting up and re running the simulation with different values of the ideal and radiant heat range sliders What changes in aggregate behavior do you notice Task 6 Making Heat Visible Sometimes we choose to add strictly visual elements to a model even though they don t have any effect on agent behavior and even if they don t present output data in a form we can easily collect and analyze Such elements can be very useful in helping us as modelers and programmers understand what is going on as the simulation is running They can also be useful to enrich the explanatory power of a model that will be shown to others In this model we don t have as of yet an easy way to see what s going on in the environment We know that the heatbugs are reacting to the heat in the
10. e of the workspace e Right click in the white space of the workspace and select Slider from the pop up menu that appears Using your method of choice do the following 1 Create a slider This slider will be used to specify the desired number of heatbugs 2 Make the following changes in the Slider dialog a In Global variable type number heatbugs 1 Notice that there s a dash between the two words but no spaces When creating a slider you are declaring a global variable that can be referenced in the procedures of the model and in other user interface elements NetLogo variable and procedure names can t contain spaces but they can contain letters numbers and some punctuation characters The fact that many punctuation characters have mathematical or other special meanings is often a source of confusion and programming errors for new NetLogo programmers Thus apart from dash and which have well established usage conventions you should generally avoid using punctuation characters in this way Heatbugs with NetLogo Page 5 b For Minimum specify O c For Increment specify 10 d Set Maximum to 500 According to the restrictions on heatbug placement and movement described previously and given the dimensions we specified for the NetLogo world what is the absolute maximum number of heatbugs that can be created e Value is where the initial value of the slider is specified For now specify 100 here The Slide
11. etLogo Page 18
12. f each heatbug is to be as happy as possible by repeatedly attempting to move to spaces which are closer to that heatbug s ideal temperature than the space the heatbug currently occupies Of course there are some constraints on what the heatbug senses and to how it moves Specifically in every tick the smallest discrete interval of time that elapses in the simulation the heatbug does the following 1 Assesses its own happiness level based on the absolute difference between its ideal temperature and the local temperature i e the temperature of the patch the heatbug is on 2 If there is any difference i e if the heatbug at least a little unhappy the heatbug tries to move in this fashion a Some given percent of the time this will be essentially equivalent to flipping a coin that has a stated percentage of turning up heads The heatbug selects a neighboring patch at random as its intended destination NetLogo Tutorials The rest of the time If the current patch is warmer than the ideal temperature The heatbug selects the coldest patch in its immediate neighborhood as its intended destination Otherwise The heatbug selects the warmest patch in its immediate neighborhood as its intended destination If the intended destination is not an improvement over the current patch The heatbug decides not to move after all b If the heatbug intends to move but its intended destination is currentl
13. for Global variable or incorrectly spelling one of the variable names referred to in Minimum then you re ready to write the setup procedure code 5 Switch to the Procedures tab and write the following code breed heatbugs heatbug heatbugs own ideal temp radiant heat patches own temperature CO SETUD clear all set default shape heatbugs circle ask n of number heatbugs patches sprout heatbugs 1 set color red set ideal temp min ideal temp random float max ideal temp min ideal temp set radiant heat min radiant heat random float max radiant heat min radiant heat end 3 Let s review the code a First we declare that we will be using an agent breed called heatbugs in addition to or instead of the default turtles breed For those NetLogo statements that expect a singular form of the breed name the second name specified heatbug will be used b The heatbugs own statement tells NetLogo that each agent in the heatbugs breed will have two variables in addition to those defined automatically by NetLogo ideal temp and radiant heat c patches own is similar to heatbugs own in that it declares new agent variables However the variables declared in patches own belong to the patches In this case we are telling NetLogo that each patch will have a temperature variable Heatbugs with NetLogo Page 7 d Next we have the setup procedure itself which includes the following instruct
14. gent in a specified agentset and returns the agent in the agentset that has the largest value for the computed expression e min one of returns the agent in the agentset that has the smallest value for the computed expression f one of simply returns a single agent selected at random from a specified agentset In the setup procedure we used n of to select a given number of agents at random from a specified agentset we can think of one of as a special case of n of where n 1 g nobody is a special value which is returned by some reporters to indicate that no agent satisfied the specified criteria We can also assign the value nobody to our own variables in our code to indicate that such a variables does not refer to any agent h any isa reporter which returns a value of true if there are any agents in the specified agentset and a value of false if there are none i with is an operator that filters a specified agentset by some criteria in the brackets that follow with and returns a new agentset containing all of the agents in the first agentset that satisfy the criteria j here and on are suffixes that filter an agentset returning only those on the current patch or on a specified patch respectively k Indentation is not required in NetLogo nor is it interpreted in any fashion by NetLogo However it can be useful to the programmer in keeping clear the logical structure of code In some instances parentheses
15. ions i The clear all1 command which is one of the instructions NetLogo already knows how to do removes any turtle or custom breed agents and clears all variable values including patch variables like the temperature variable we created When such variables are cleared their values are set to zero 0 this is fine for our purposes now but in many cases we would need to include code to set the initial values of our patch variables ii set default shape is used to specify that the default shape for the heatbugs breed will be the shape named circle You can see the shapes that are available and modify those shapes or create new ones via the Tools Turtle Shapes Editor menu command iii Rather than create some number of agents and scatter them randomly and then have to check to make sure that no two landed on the same patch this setup procedure turns the logic around a bit it uses n of number heatbugs patches to select number heatbugs of the patches agentset at random Then it asks each of these selected patches to sprout a single agent of the heatbugs breed on the center of the patch This guarantees that we will have the correct number of distinct patches each with exactly one heatbug agent with no possibility of two heatbugs starting out on the same patch iv Each of the newly created heatbugs sets its color to red and then uses the current values of the sliders we previously created to generate random values and
16. ke a job for another slider 1 Create a slider in the Interface tab using the following attribute values Global variable Minimum Increment Maximum Value random move prob 0 0 01 1 00 0 20 Heatbugs with NetLogo Page 9 Now we need to create a move procedure Before copying the code that follows try to create at least some portion of it yourself Remember to create a new procedure preceding the procedure name with the to keyword and following it one or two lines down with the end keyword And remember procedures cannot be nested in NetLogo so make sure you don t start your new procedure inside the setup procedure you just wrote If you get stuck or confused or have written as much of your procedure as you feel comfortable writing without help check your work against the following and fill in the gaps or fix as necessary 2 Click on the Procedures tab 3 Below the end that closes your setup procedure add the following code to move if temperature ideal temp let destination nobody ifelse random float 1 lt random move prob set destination one of neighbors ifels temperature lt ideal temp set destination max one of neighbors temperature if temperature of destination lt temperature set destination nobody set destination min one of neighbors temperature if temperature of destination gt temperature set destination nobody if des
17. nt to capture each data point in detail so that we can crunch the numbers with statistical software we might simply want to grab frames from the simulation display and string them together in a QuickTime movie In any case the general question is this In what way s do we most want to examine and or present the behavior of the simulated world we have built NetLogo has the capability to capture simulation output in a wide variety of ways One of the most powerful of these is also one of the most direct ways of viewing output data plotting it on a graph That s what we ll do next But what will we plot What sort of measurements do you think would be interesting What do you think is possible Heatbugs with NetLogo Page 15 In the fundamental description of a heatbug s behavior we find something that might have seem a little frivolous or a bit strange heatbugs are motivated by happiness more accurately they re always attempting to reduce their unhappiness Given that the average unhappiness of the entire population of heatbugs might be an interesting piece of data to capture and to plot over time Just as we were able to find the maximum heat level in the terrain with a single line of code the average value of a patch or turtle variable is equally easy to compute But here we have a problem we used the concept of unhappiness in an implicit fashion in the move procedure but we aren t storing each heatbug s level of unhappiness in
18. procedure needs to do 1 Clear any existing heatbugs from memory 2 Set the temperature of every patch to some default level 3 Create a user specifiable number of heatbugs with the following attributes a An ideal temperature randomly generated from a user specifiable range b A heat radiation rate i e how much heat the heatbug will put into the environment every tick randomly generated from a user specifiable range 4 Place each heatbug on a random patch in the NetLogo world with the additional condition that no two heatbugs can be on the same patch In case you weren t keeping count there were three uses of the phrase user specifiable in that description And two of those were for a user specifiable range i e the user will specify a minimum and a maximum value Clearly we need a way to let the user specify the values of interest The easiest way to do this in NetLogo is with a slider At the top of the NetLogo Interface tab there s a toolbar that includes a button labeled Add and a drop down menu containing a number of user interface devices One of these devices is a slider you can create a new slider in one of these three ways e Click the Add button then select Slider from the drop down menu and then click somewhere in the white space of the workspace below the toolbar e Select Slider from the drop down menu notice that the Add button is automatically pressed and click somewhere in the white spac
19. r dialog should now look something like this E 3 Click OK One slider down four more to go for now anyway 4 Create four more sliders using the settings given in the table that follows Global variable Minimum Increment Maximum Value min ideal temp 0 1 200 100 max ideal temp min ideal temp 1 200 110 min radiant heat 0 1 100 20 max radiant heat min radiant heat 1 100 30 Notice that min ideal temp appears as the Global variable name for one slider and the Minimum value for another the same is true for min radiant heat In each of these cases one slider is taking its minimum value from the current value of another slider In fact Minimum Increment and Maximum can be specified as numeric literals variable names where those variables are assumed to hold numeric values or more complicated expressions that return numeric results If this seems a bit confusing experiment with moving the min ideal temp and min radiant heat sliders i e changing the values of those variables and see what happens to the max ideal temp and max radiant heat sliders when you do so this should make things a bit more clear Heatbugs with NetLogo Page 6 You should now have five sliders If you do and if none of the slider names and values are appearing in red which would indicate that there is an error in one or more of the slider settings probably due to inadvertently including a space in the value typed
20. rature there s another built in NetLogo reporter that makes it easy to scale colors according to data values With the scale color reporter we specify a color a minimum value and a maximum value and a value to be converted into a scaled color Based on where that value falls between the minimum and maximum a color somewhere between the darkest and lightest shades of the specified color will be returned Let s try it 1 Create a new procedure containing the following code OmUup ces eC orllon let high temp max temperature of patches ask patches set pcolor scale color blue temperature 0 high temp end 2 Modify your go procedure by inserting a new line above the line that has the tick statement and typing update color in the new line 3 Check your code A Save your model 5 Run the simulation What do you see Task 7 Plotting Output Data Some models are purely exploratory with no real expectation of useful results some are even built for amusement We may not invest much effort into capturing or presenting output data from these types of models at least not at first But in general we should be thinking starting fairly early in the modeling process about the kind of output that we would like to capture from our simulation models Capture is being used in a pretty broad sense here we might simply want to generate a plot with no intention to analyze the underlying data any further we might wa
21. the corner selection pull down b c d e Make sure the World wraps vertically checkbox is checked Set the max pxcor value to 99 Set the max pycor value to 99 Make sure the World wraps horizontally checkbox is checked f Set the Patch size value to 5 0 Make sure the Turtle shapes checkbox is checked With patches this small it s debatable as to whether it makes sense to show turtle shapes But we can use a simple circle shape and it will show up well even at this size g h Make sure the Show tick counter checkbox is checked Heatbugs with NetLogo Page 3 The World amp View window should now look like something like the following image comer Bottom Lert E TNT Sa KGGF MiB GGF 3 Click the OK button 4 Adjust the size of the NetLogo window as necessary so you can see the entire world 5 Save your model Task 2 Setting Up the Model As usual we ll need a way to clear one simulation run and prepare our model for the next By convention the procedure that does this is usually called setup but it s important to remember that this is simply convention setup is not a magic name and NetLogo has no idea what setup means until we explain what it means i e by writing the procedure in NetLogo code and what NetLogo should do when we instruct it fo run the setup procedure Heatbugs with NetLogo Page 4 So before writing it let s first review what our setup
22. tination nobody and any heatbugs on destination set destination one of neighbors with not any heatbugs here if destination nobody move to destination set temperature temperature radiant heat end A Rather than review the code line by line let s review some key NetLogo language statements and structures seen in the code a if is used to execute a block of statements only if a stated condition is true The if keyword is always followed immediately by a conditional expression which must evaluate to true or false parentheses are optional around the conditional expression After the conditional expression comes a block of statements enclosed in Heatbugs with NetLogo Page 10 a pair of square brackets these statements will be executed only if the conditional expression is true b ifelse is similar to if with one key difference instead of one statement block after the conditional expression there are two The statements in the first block are executed if the conditional expression is true the statements in the second are executed if the condition expression is false c neighbors is a built in reporter i e a statement that computes and returns a value which returns the agentset consisting of the eight patches adjacent to or diagonal to the patch where the current agent is located d max one of is a reporter that computes the value of an expression given in brackets for each a
23. well what this plot would look like At every tick the model is reviewing the temperature in all 10 000 patches to find the hottest spot What do you think will happen if we expanded the model to 500 patches X 500 patches Can you think of any other way to find the highest temperature in the terrain No heatbugs is allowed to move onto a patch where another heatbug is already resident However it is possible that the other heatbug will actually move from that patch before processing of that tick is complete How should we deal with this kind of issue in our models Keep in mind that NetLogo shuffles the agentset before processing an ask thus the order in which the heatbugs move changes with each tick Optional Tasks 1 What do you think will happen if you perturb the heatbugs system while the model is running by suddenly heating or cooling the entire terrain by a large amount How do you think that will show up in the average unhappiness plot Add a Heat Up and or a Cool Down button with the necessarily additions or changes to the Procedures tab contents to test your hypotheses Report on the outcome Can you think of any ways to make the heatbugs movement a bit smarter while still limiting visibility and interaction to their immediate neighborhoods What change s do you think this will produce in the aggregate behavior Implement and test your modified heatbug movement behavior Report on the outcome Heatbugs with N
24. y occupied If there are other unoccupied patches in the immediate neighborhood The heatbug selects a new destination at random from the unoccupied patches in its immediate neighborhood Otherwise The heatbug decides not to move after all c If it hasn t decided not to move the heatbug moves to its selected destination patch 3 The heatbug radiates some heat to the patch where it is currently located Discussion 1 How is the heatbugs behavior affected by their environment 2 How do the heatbugs affect their environment 3 How do the heatbugs interact with each other 4 Have we described any behavior of the patches Should we In fact patches are stationary agents in NetLogo and there are two important things that patches will have to do as the simulation runs What might these be Hint think about heat and what happens to heat over time Task 1 Getting Started 1 Launch NetLogo v4 0 2 2 For this model we ll need a fairly large terrain On the other hand our heatbugs don t need to look like real bugs so we can use pretty small patches Let s go with a NetLogo Heatbugs with NetLogo Page 2 world that is a torus of size 100 X 100 patches with each patch measuring 5 pixels on a side To do this click the Settings button near the top of the NetLogo window and make these changes a Change the Location of origin set to corner why do we need to do this and select Bottom Left from
Download Pdf Manuals
Related Search
Related Contents
Craftsman 917.289251 Lawn Mower User Manual IWC 740-750 fre 2 True Fitness Touchscreen Elliptical User's Manual Bentley Trust Licensing Conversion Guide GRANPOWER5000 モデル 580 取扱説明書 追記・訂正事項 GPNET : MODE D`EMPLOI Valueline VLMB37000W20 médiathèque mode d`emploi - Médiathèque communautaire de マキシム-X取扱説明書 Copyright © All rights reserved.
Failed to retrieve file