I need to learn concurrently when a turtle changes its heading. Namely, when the turtle changes its direction, a procedure or a reporter will change the value of a boolean. But this reporter won't be called by any other procedures, it will be always running (checking turtle's heading) while the turtle is moving. Is there any way of this in NetLogo?
I think you can achieve something similar to what you want with a "forever" button: that is, a button that runs a procedure constantly. (People usually have at least one button like that, typically named "go", in their models.)
Assuming the heading you want to track is that of turtle 0, you can have code like this:
globals [
current-heading
heading-has-changed
]
to check-heading-changes
if [ heading ] of turtle 0 != current-heading [
set heading-has-changed true
set current-heading [ heading ] of turtle 0
]
end
To have the check-heading-changes code run constantly, you just need to call it from a "forever" button:
You have to remember to click the forever button when you want to start the monitoring. Now, of course, the code above also assumes that you will have some other procedures running that controls the turtle, and also that will actually do something (and reset the variable) when heading-has-changed becomes true.
Related
I am running my Netlogo model in behavior space. In my model, I created a turtles-own variable called consumption-rate. I want to export the consumption-rate of each turtle for every tick of my run. From my understanding of behavior space, I would somehow put consumption-rate in the box that says "Measure runs using these reporters" in order for it to be exported, but I keep getting different errors every time I try. For instance I mostly get an error that says "Experiment aborted due to syntax error: Expected reporter". I also need to export a patches-own variable I created called quality for every patch in my model at each tick, and I have the same issue. All the examples for this part of behavior space online just show "count turtles" or something similar. Am I able to export a turtle or patch variable there? If so, what code would I use?
I took a shot into the blue and tried using primitives like "show consumption-rate" or "report consumption-rate". I am unsure of the format of code I would even begin to use to give me those exports. Any advice or help? I also tried just typing in "consumption-rate" or "quality" in the "Measure runs using these reporters" box, but got an error saying I can't use a turtle or patch variable in an observer context, how can I make those into the observer context? Anyway around that?
patches-own [ quality ]
turtles-own [ consumption-rate ]
to setup-patches
ask patches
[set quality (2 + random 8)
set pcolor scale-color green quality 1 10 ]
end
to Go
ask turtles
[ calculate-consumption ]
end
to calculate-consumption
set consumption-rate ( [ quality ] of patch-here ) / ( strength-of-competition * count turtles-here )
end
You have a conceptual mismatch. There is no problem in exporting a turtle or patch variable in BehaviorSpace, but you haven't told NetLogo which variable to export. You need to specify whether it's that variable for all turtles, or only some turtles or whatever.
Here's a modified version of your code so that it is complete and self-contained.
globals [strength-of-competition]
patches-own [ quality ]
turtles-own [ consumption-rate ]
to setup
set strength-of-competition 0.4
ask patches
[ set quality (2 + random 8)
set pcolor scale-color green quality 1 10
]
create-turtles 300 [setxy random-xcor random-ycor]
end
to go
ask turtles
[ calculate-consumption ]
end
to calculate-consumption
set consumption-rate quality / ( strength-of-competition * count turtles-here )
end
Run this with a BehaviourSpace setup that has [consumption-rate] of turtles as the reporter. Also put 2 in the time limit. You will get the output you requested.
A good tip for working with BehaviorSpace when you are new to it, is to set up a monitor on your interface for each value that you want to save in your output. Get the monitors showing what you want to export and then simply take the code that you ended up with and put it in the reporter box. The advantage of doing the monitor step is that it forces you to get your thinking right without going down the rabbit hole of whether it is a BehaviorSpace issue.
I want to delete only turtle pen lines from a halfway point in a model run. The "clear-drawing" primitive seems to achieve that, but my problem is that I can't run it directly from an agent, or use "ask observer [clear-drawing]". Is there a way to trigger this observer command from an agent context (I expect not), or is there another way of erasing turtle pen lines? My solution to re-draw using pens having the background color is rubbish.
Instead of redrawing using the background color, use pen-erase. If that's equally “rubbish”, perhaps you want something more like the answers here? NetLogo turtles leaving a trail that fades with time
About clear-drawing being observer-only though, that seems like it shouldn't be too hard to work around, something like:
to go
let clear? false
ask turtles [
...
if ... [
set clear? true
]
...
]
if clear? [ clear-drawing ]
tick
end
What actually happens to hidden turtle? I mean after we hide the turtle it continue to live in invisible mode occupying memory as I guess.
I hide few turtles but did not ask them to be shown back and when I inspected the hidden turtles continuing simulation their attribute were changing as per my commands. So, what exactly hiding a turtle sense for.
In one of my simulations, turtles represent people making decisions about whether to protect themselves during an epidemic. There are tens of thousands of these turtles, with potentially hundreds on some patches. The turtles don't move, but they each make their own decision based on personal characteristics like attitude and environmental perception such as how close the epidemic is.
Having these turtles visible would just clutter up the screen. Instead, I hide them and colour the patch based on what proportion have adopted protective behaviour. This is much more informative.
In my most recent simulation, though, I make the turtles size 0 instead of hiding them. This still makes them disappear, but I can still right-click on the world view to access the list of turtles where I have clicked.
Another reason to hide turtles is when you are simulating an infinite plane and turtles outside the view should simply be hidden.
Note that if you are moving turtles using setxy rather than forward you should test to make sure the patch you are about to move to exists since setxy throws a runtime error if it is given coordinates outside the world. From NetLogo documentation:
ifelse patch-at (new-x - xcor) (new-y - ycor) = nobody
[ hide-turtle ]
[
setxy new-x new-y
show-turtle
]
I am 'watch'-ing an agent and now want to programmatically 'unwatch' the agent (so that no agents are being watched).I am running the model using the 3D view (in NetLogo 5.0.4). It seems from the User Manual that the only way of unwatching is to use 'reset-perspective', but this has the undesirable side effect of resetting the perspective in the 3D view. Is there a better way of unwatching that does not disturb the view?
As far as I know, there is no way to call reset-perspective in NetLogo 3D without returning the observer to its default position.
There is, however, a way to work around this: you can ask the subject currently being watched to hatch a hidden "dummy" turtle at its current position and watch this dummy instead while your original subject keeps on moving. (It's not seamless, though, because the "spotlight" circle stays around the dummy.)
Here is a full example, in which there is a travelers breed for the regular turtles and a cameras breed for the dummies:
breed [ travelers traveler ]
breed [ cameras camera ]
to setup
clear-all
create-travellers 10
watch one-of travelers
end
to go
ask travelers [ fd 0.1 ]
end
to stop-watching
if subject != nobody [
ask subject [
hatch-cameras 1 [
set hidden? true
watch-me
ask other cameras [ die ]
]
]
]
end
This assumes that stop-watching is called from an interface button.
Support for programmatic control of the observer in NetLogo 3D is... spotty.
You could store the values of __oxcor, __oycor, and __ozcor and then restore them using setxyz after calling reset-perspective.
But there's also pitch and roll. You can read them with __opitch and __oroll, but it appears to me they are not directly settable. I imagine you could use facexyz to restore the pitch at least, but not the roll? 3D stuff confuses me.
Note that NetLogo's extensions API could be used to write an extension that does what you want.
In my go function I have a few conditions which might be true and then call an action, if more than one condition are true does it mean in one tick agents can do more than one action ? or in another case I have a function that agents have to move to a target and after facing the target agent should find the distance and fd 1 until reaches the target, does it mean it should take n ticks to complete?
to move [t]
face t
let n distance self t
repeat n
[fd 1]
end
to go
action 1 = > [move]
action 2
action 3
tick
end
I need the agents to do only one task at the time, and I am not sure how to make sure that for example not all the agents have the same target! sorry if these questions are out of context but I am new to Multi-agent modeling.
This is a great question!
First, specific answers:
if more than one condition are true does it mean in one tick agents can do more than one action ?
Just to be clear, are you talking about a situation like this?
to go
ask turtles [ move ]
tick
end
to move
if xcor > 5 [ fd 1 ]
if ycor > 7 [ rt 15 ]
if color = red [ bk 2 ]
end
If so, then yes, if xcor > 5, ycor > 7, and color = red are all true, the turtle will move forward 1, turn right 15, and move backward 2. The main way to prevent this is by using a sequence of ifelses:
to move
ifelse xcor > 5 [
fd 1
] [
ifelse ycor > 7 [
rt 15
] [
if color = red [ bk 2 ]
]
]
end
That way, the second condition (ycor > 7) will only be looked at if the first condition (xcor > 5) is false. Similarly, the third condition will only be looked at if the first two fail.
I have a function that agents have to move to a target and after facing the target agent should find the distance and fd 1 until reaches the target, does it mean it should take n ticks to complete?
I would recommend taking n ticks to complete it. Otherwise, only one turtle will move to its target at a time! However, repeat n [ fd 1 ] will make the turtle go forward n all at once. In fact, it's the same as fd n. The repeat block will finish before the turtle is done performing the move procedure.
You can edit your move function as follows to get it to take n ticks:
to move [t]
face t
fd 1
end
Then, just have a condition to look for when the turtle gets to its target (for example, distance t < 1) and then do something accordingly.
I recommend just messing around with these various techniques with multiple turtles using them so you can get a feel for the differences.
I am not sure how to make sure that for example not all the agents have the same target!
This depends quite a bit on context. Supposing there is a turtles-own variable called target that stores each turtle's target, you could do something like this:
to-report get-available-target [ possible-targets ]
report one-of possible-targets with [ not any? turtles with [ target = myself ] ]
end
get-available-target will report a random agent from possible-targets that is not anyone's target. one-of just gets random agent from an agentset.
How can I schedule tasks in netlogo, I have conditions and actions but how can I make the agents do one task at the time?
Often this just takes care of itself. For example, say, when an agent is hungry, it should walk over to a food supply and eat. As it's walking over, you don't want it to go off and do anything. However, it will still be hungry, so if you've used an ifelse, it will just keep going to the food supply. Thus, your agent stays focused for free! In general, it's best to design your conditions such that they remain in effect until the agent addresses them. You can order them in an ifelse chain or something similar to establish the general priority of actions (for instance, if the agent walking to the food supply is threatened by a predator, it should still run away).
If you really want to the agent to do something for several ticks, you'll basically just extend this same idea, but in a more artificial way. For instance, if an agent should walk forward three times, have a turtles-own variable set to three. If that variable is greater than zero, the agent walks forward and decreases that variable, and doesn't do anything else.
#Bryan Head already answered your question, I had the same problem when I started Netlogo and agent based modeling, So I have added a current-task variable and in my Go Procedure I have added a function to check all the conditions and set the task of agents and when the task is completed the variable is set to "" again. This might be useful for your case as well.
if current-task = "move" [move]
if current-task = "A1" [A1]
if current-task = "..." [...]
if current-task = "" [select-current-task]