Unwatching an agent without resetting perspective - watch

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.

Related

In a Netlogo network, how can turtles "see" properties of other turtles?

I am trying to build a model in which turtles decide to change colour depending on their environment in a network.
The approach would be to "check" the colour of the surrounding turtles and then set an if statement for the turtle in question to switch colour (there will only be 2 colours).
Specifically I would like to know how can a turtle "see" or check other turtles' colour (or other properties).
If possible I would also like to create a slider for "how many links away" can turtles see their neighbouring turtles' (or neighbours of neighbours, etc) colour.
I am new to both Netlogo and Stackoverflow, so please let me know if I should make any modifications to my model and/or question.
Thanks!
Welcome to Stack Overflow! Typically you'll want to stick to a single question per post, both for simplicity and for the benefit of future users with similar questions. As well, in cases where its applicable you should try to include some code to show what you've tried so far, as well as any setup necessary- you want to make a minimal, complete, and verifiable example. In this case, I think you're okay since your questions are clear and well explained, but if you have more complex questions in the future you will be more likely to get useful answers by following those guidelines.
For your first question, it looks like you want the of primitive- check out the dictionary entry for details. of can be used in a few ways, including allowing agents to check the value of a variable (such as color) of another agent. Check out this example code:
to setup
ca
reset-ticks
crt 10 [
setxy random 30 - 15 random 30 - 15
create-link-with one-of other turtles
]
end
to go
ask turtles [
set color [color] of one-of link-neighbors
]
end
Every time the go procedure is called, one of the turtles changes its color to the color of one of its link-neighbors. If you run it long enough, all connected turtles should end up with the same color.
For your second question, I suggest you check out the Nw extension, which is an extension built to deal more easily with Netlogo networks. Specifically, have a look at nw:turtles-in-radius, which should work with your slider approach. To get it working, include the extension using
extensions [ nw ]
at the start of your code. Then, assuming the same setup as above, you can play around with something like
to network-radius
ask one-of turtles [
set color red
ask other nw:turtles-in-radius 2 [
set color white
]
]
end
When you call the network-radius procedure above, you should see one turtle turn red, and any turtles within 2 links of that turtle turn white. To switch to a slider, just swap the "2" out for your slider variable. Hope that helps!

How do I delete turtle pen lines in netlogo?

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

Query about hidden turtles

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
]

When using the forward command is the movement specified carried out after each tick?

I am trying to make turtles move along fixed paths that the user can draw in the u.i. The forward command can make turtles move a certain fraction of a patch forward per tick I assume, however to instigate smooth movement would it be possible to specify a fixed movement per tick in the setup commands for turtles? If this is possible what would be the basic structuring of the code I would use to achieve this?
The fd command (bk as well) accept floating-point inputs. I.e.
Ask turtles [ fd .01 ]
Makes each turtle move forward 1/100th of a patch. This movement happens at the time of the command.
Tick does not have any connection to when commands are carried out. If you set view updates to on ticks it can effect when you see updates otherwise it is usually a scheme for keeping track of how many times go has run.
A sample model of turtles moving at different speeds.
Turtles-own [speed]
To setup
Crt 100[
Set speed random-float 1
]
End
To go
Ask turtles[ rt 1 fd speed]
End
Copy and paste that into a new model make setup and go buttons. Mess with it for a while.

Efficient access to a Turtle's variable which is a patch address, or How to filter patches that are not assigned to turtles?

In my simulation each turtle has a my-home variable which is the patch agent family lives in, so agents with same Family-ID have same my-home until one of agents moves out or family grows to more than 7 agents.
when an agent wants to move out , I have to check if there is any patch nearby which is not another's agent my-home, what I have done is to store all my-homes in a list and check if any selected possible next home is not a member of this list, but I believe there should be better way to do this:
let all-homes [my-home] of agents with [belongs_to = BS]
set my-home min-one-of patches with [not member? self all-homes and label_ = BS][distance m]
m is current home address
min-one-of patches with ... assesses every patch in the entire world before picking a winner. That's going to be slow. I'd suggest searching nearby patches first, then farther patches, and so forth. Most of the time the turtle will find a new home after only a very brief search, so you'll have replaced code that's O(n) in the number of patches with code that's O(1)-ish. That should fix the main performance problem with this code.
The simplest such search routine I can think of is for the turtle to simply head in a random direction and keep moving fd 1 until it lands on a free patch. But if you want to do something more rigorous that always results in finding the closest possible new home, you could do that too; you'll just have more code to write.
Building the all-homes list is probably only a secondary performance problem here, but it's fixable too. The simplest fix I can think of is to add:
patches-own [home?]
initialize it with ask patches [ set home? false ], and then make sure that whenever a turtle adopts a patch as its home, it does ask my-home [ set home? true ]. Now you can replace member? self all-homes with simply home?. After all, you don't really need to know what all of the home patches are; you only need to know whether some particular patch is a home patch.