« The Subsumption Architecture | Main | Papers of possible interest »
December 02, 2004
Getting started with NetLOGO
This post assumes you were around for the presentation. It's just a few steps that bootstrap us into doing interesting things with NetLOGO.
- Create a button; call it 'Setup' and the code should be 'setup'
- Distribute some turtles randomly
to setup ca ;; clear all crt 10 ;; create 10 turtles end
- Add a button; call it 'Go' and the code should be 'go'. Check the 'Forever' box.
- Give the turtles something to do
to go ask turtles [ fd 1 rt random 10 lt random 10 ] end
- They move kinda fast; ask the turtles to
wait .2
. - Let's give our turtles the ability to remember things; how about their
age
? We do this by saying thatturtles-own [ age ]
somewhere at the top-level of our program (that is, not inside a procedure). - Hm. Let's make it visual regarding how old our turtles are. First, set all their colors (by default) to white by changing the setup.
to setup ca ;; clear all crt 10 ;; create 10 turtles ask turtles [ set color white ] end
Then, we need to modify their color based on their age. We can do this in the 'go' method.to go ask turtles [ fd 1 rt random 10 lt random 10 wait .2 set color ((color - 1) mod 10) ] end
- We haven't done anything cool with these turtles yet, but I'd like to add some types to the discussion:
breeds
. In particular, hunters and gatherers. At the top of the program, addbreeds [ hunter gatherer ]
. Now, modify our 'setup' procedure:to setup ca ;; clear all create-gatherer 10 ;; create 10 gatherers create-hunter 10 ;; create 10 hunters ask hunters [ set color 19 ] ask gatherers [ set color 59 ]
end
And, while we're at it, modify our 'go' method:
to go
ask hunter [
fd 1
rt random 10
lt random 10
wait .2
set color (20 + ((color - 1) mod 10))
]
end - We now have to races of turtle--two types, if you will, and we can control them seperately. Indeed, run the simulation again, and you'll see that only the ten hunters are wandering around. The colors (for reference) are from a table half-way down this page.
- We have two breeds; we need to break this program up.
to go
do-hunters
do-gatherers
endto do-hunters
ask hunter [
fd 1
rt random 10
lt random 10
wait .2
set color (20 + ((color - 1) mod 10))
]
endto do-gatherers
end
- Let's make our gatherers wander randomly.
to do-gatherers
ask gatherer [
fd 1
rt random random 120
lt random random 120
]
end - Now, can we make the hunters follow the nearest gatherer? This would involve detecting who is near us; to do this, we make our hunter look around to see how many turtles of another breed are in a given radius, say. We can also have them 'sniff', which I believe lets us detect properties of a patch and other agents... but you'll have to look that up.
to do-hunters
locals [ num-in-radius ]
ask hunter [
fd 1
rt random 10
lt random 10
wait .2
;;set color (20 + ((color - 1) mod 10))
set num-in-radius (count gatherer in-radius 4)
ifelse (num-in-radius = 0) [
set color 14
] [
set color (num-in-radius + 5) * 5
]
]
end
And that's where I start to run out of steam (or time). In particular, the question is: how can we make a hunter close in on a gatherer? What combination of NetLOGO commands will do the trick?
I can see that distancexy
might be useful; however, it could take some doing to figure out how to point ourselves at the nearest gatherer. At this point, however, it might be good to learn the way all great programmers learn:
At this point, I'd like to recommend that we take a little time to explore the built-in models library, and then come back to seeing if we can add some things to our model:
- Can we make a hunter actually close in on the nearest gatherer?
- When the hunter shares a space with a gatherer, can we tell that particular gatherer to die?
- Can we give the hunters some kind of energy property, and make each movement cost them some energy? When they run out, they should probably die.
- If we're going to take the previous step, then eating should replenish our energy.
Other things that we might do past this point that are more or less complex:
- If we wanted to jump directly to something complex, we could add signalling. This would be where a hunter within some distance of a gatherer broadcasts a it's location, and all other hunters would move towards that space for some number of steps. This way, hopefully, they'll close in on a single gatherer.
- We could put food on the patches, and require the gatherers to eat to survive.
- We could make hunters and gatherers breed (split) if they get enough energy.
- We could set the thresholds for breeding (the energy cost) and value of food sources (patch-food (grass) for gatherers and gatherers (for hunters)) tied to sliders.
- We could allow hunters to eat hunters.
- We could attach plotting widgets to the populations, and see how different parameters affect the populations.
(Note: Upon looking at some of the models, it looks like they typically use random motion of things like our hunters and gatherers. So, we could ignore the notion of tracking...?)
Basically, this is a real-time, evolving dynamical system. As we heard last week, it is very difficult to reason formally about this kind of system; as a result, we have to think about the "world" from the agent's point of view, and then run simulations or (in the case of systems we want to run in the real world) build things and see what happens.
Hopefully, there's enough here to get you started. I personally just like poking around with these things; hopefully, you've enjoyed it a bit as well.
Actually, I think NetLOGO is really cool shit. And I really wish that we had introductory classes in CS that used software like NetLOGO; it is a completely different, but very valuable, paradigm.
Hm. Perhaps there is a way we could work it into our classes...
Posted by mjadud at December 2, 2004 05:50 PM
Comments
I don't have the answer to your question but I appreciate the positive comments about Redcone Robot News in an earlier posting. Thanks
I added your site to my blogroll. I like what you are doing here!!
Posted by: Kevin at December 12, 2004 07:38 AM