So in this case it retrieves the color of the patch 1 unit ahead of the turtle.The nlrx package provides tools to setup and execute NetLogo simulations from R. The of operator gets the value of the property for the particular agent. There are also a range of keywords that let you retrieve the patch in front of a turtle and at given positions and so on. Different colored patches can also be used to signify food or resources for the agents. You can use the patch color to form barriers and allow agents to bounce off for kinetics simulations and so on. The if statement checks the patch color that the turtle is on and if it is green it turns the turtle through 180 degrees. As a simple example suppose we have three random green patches then we could make the turtles bounce off: The whole point of patches is that they provide an environment which the turtles can interact with. However, once you know that agent sets exist and what they are for the documentation is quite good at explaining things. This uses the one-of operator which selects a random member of an agent set.Īs you might guess, there are lots of way of creating and manipulating agent sets and it would take far too long to cover them all here. Uses a patch set to select just three patches at positions 1,1,2,2 and 3,3 and sets them to green.Īs another example, this sets three random patches to green: For exampleĪsk (patch-set patch 1 1 patch 2 2 patch 3 3)[ So far we have used complete agent sets like turtles or patches but you can build smaller subsets of agents and patches and work with then using ask. There are a number of ways to do this but one interesting one uses an agent set. not all, to display as green how could you do this as all of the patches are going to obey the same code.
You can also ask the patches to execute some code and this is the difficult part.įor example to turn all the patches green you could use: You can set the number of patches and hence their size using commands in the setup function. They form a rectangular grid that the turtles move over. You don't create patches like you create turtles. Patches are parts of the simulation that don't move and they can be the most difficult idea for the beginner to come to terms with. In this way turtles can be people, atoms, ants - whatever you need within the simulation.
They don't have to look like turtles, you can create any graphic you want for them. Turtles provide the all-purpose moving agent. simulation time doesn't advance until all of the moves have been completed. You can change this so that each turtle obeys one instruction and then gives the next turtle a turn - this is ask-concurrent.Īlso notice that the tick doesn't move on. By default each agent runs the complete list of instructions in the ask before the next turtle gets a go. This can be important in some algorithms. The simulation is not a true parallel algorithm where all the turtles move at the same time - they each take a turn. If you slow down the program you can also see the turtles each taking a turn to move. The result is quite a pretty pattern for such a simple program. You can control the rate at which the go function is called using the speed slider at the top. By manually clicking the go button we can step thought the simulation but by changing the buttons type to Forever - simply click the Forever box - then the go function is called repeatedly until the button is clicked a second time.
When we click the go button a step of the simulation is computed and displayed. the simulation, is reset and then then turtles are created. Now we place two buttons on the interface and set the first to the setup function and the second to the go function. The reset-ticks command sets the count to zero and tick increments it - you don't have to use this but it is very useful in most simulations. The system keeps count of each step of the simulation and shows it at the top of the window. Notice that we have now got two extra commands - reset-ticks and tick. However this is such a standard requirement for a simulation that NetLogo has a special built in mechanism.įirst we separate out the setup and go functions: Now you could do this by writing the code for a single step of the simulation and enclosing it in a loop so that it repeats. The go part is a bit more complicated because generally what we have is a single step of the simulation that has to be run more than once to allow things to develop. The setup part is generally easy in that it just has to happen once and so you can associate it with a button exactly as we have been doing.
The setup routine initializes everything ready for the simulation to start and the go does the actual running of the simulation. In most cases there are two distinct parts to a simulation - setup and go.
So far we have just used a single button to setup and run our program.