Why am I getting an error when I disengage world wrap in NetLogo? - netlogo

I downloaded a working model and I'm adjusting it to fit criteria I have. I want to disable the world wrap so the turtles are constrained in their movement to the initial screen but when I do I get an error telling me: OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.

Welcome to StackOverflow! For future reference, your question is a bit incomplete as it's hard for others to help you without seeing the code you are using. Try to include enough code for others to answer your question. I cannot directly help resolve your issue, but I can provide some general information.
In NetLogo the behavior of many of the language primitives changes when world wrapping is enabled and when it is not. As an example using patch-ahead, in a world with wrapping in both directions, this code will always succeed:
; turtles will color themselves the color of the patch 1 unit away
ask turtles [
set color ([pcolor] of patch-ahead 1)
]
But if wrapping is disabled in one or both directions, the patch-ahead of a turtle can return nobody when there is no patch in front of the turtle because the turtle is at the edge of the world. In that case you are asking NetLogo to evaluate [pcolor] of nobody, but that is not possible, and you get an error like the one you described.
To resolve this issue you'll have to look through the code and see where these errors are made and handle them appropriately. For my example:
ask turtles [
if patch-ahead 1 != nobody [
set color ([pcolor] of patch-ahead 1)
]
]
You can read more about the world topology of NetLogo models in the docs.

Related

How to have patches "belong" to turtle

I am brand new to Netlogo and am coding a world with caching animals. They will go to their caches (I set them as blue patches) if their energy value falls below 10. They can find these caches based on a random 'memory' value given to them which is used as an in-radius value. That way, they will face and go towards a cache within their in-radius memory if they are about to die. I am starting to incorporate a home-base system where the turtle remains in a smaller area with their own caches. Does anyone know how I can make a patch belong to an individual turtle? This would allow turtles to have their specific caches in their territory. I have looked into using links and breeds, but links are only between turtles and making the individual breeds for the 50+ turtles at a time seems ineffective and complex. I apologize I don't have any code attempting to have a cache (patch) belong to a turtle, I don't know where to start. Any ideas?
If you want a turtle to remember anything (patches or income or anything else), then you need to assign a variable in a turtles-own statement and then set the value appropriately. Here's some example code fragments. They won't work, and you actual code would likely look a lot different because you will need some design about the conditions under which a cache will be assigned, but they show you what a variable solution looks like.
turtles-own
[ my-caches
]
set my-caches (patch-set my-caches patch-here) ; code when a turtle finds a new cache site
If you want a patch that belongs to a turtle to make that patch unavailable to other turtles, then also set up a patch variable to store its owner.
patches-own
[ my-owner
]
ask turtles
[ if [my-owner] of patch-here = nobody [set my-caches (patch-set my-caches patch-here)]
ask patch-here [set my-owner myself]
]
I suggest you do several NetLogo tutorials, then look at some library models (and understand them) before starting your own model. You need to understand basic concepts like turtles/patches, variables, ticks before trying to build a model.

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!

Constraining Movement of Agents to a Home Range in Netlogo

I'm relatively new to NetLogo, and I'm working to model moose density in New Hampshire and its correlation to winter tick parasitism.
I'd like to program my moose agents to move randomly within a set home range (~5km2), that originates from the randomly chosen patch they first enter the model on.
I'm not really sure how to bound agents based on area, rather than just patch color... Any suggestions on how to do this would be most appreciated!
Thank you!
General stackoverflow tip: typically, stackoverflow encourages specific programming questions. So including the code you've actually tried so far is generally preferred.
Alright, on to your problem.
One really simple way to do this is, first, store the mooses' starting patch. Second, when the moose is moving around, check the distance to the starting patch. If the distance exceeds the starting amount, have the moose towards the starting patch. Here's some template code to give you ideas:
breed [ mooses moose ]
mooses-own [
starting-patch
]
to setup
clear-all
;; only using one moose as it's easier to see the behavior
create-mooses 1 [
setxy random-xcor random-ycor
set starting-patch patch-here
]
reset-ticks
end
to go
ask mooses [
move
]
tick
end
to move
;; If farther than 10 patches from starting patching, take a step towards starting patch, otherwise, move randomly
ifelse distance starting-patch > 10 [
face starting-patch
] [
rt random 90
lt random 90
]
fd 1
end

NetLogo: distance refering agent

I'm new in NetLogo and this may be a too obvious question, but I don't see how to test if what I am doing is right.
I'm making a selection of an agent of breed1 (turtles) based on its distance to breed2 (crocodiles). I would like the crocodile to choose one turtle randomly from the ones around it, but with a higher probability to be chosen the closer the turtle is. Thus, I am using rnd extension and distance command.
My question is if the distance command is referring to the right agent (i.e. distance between the crocodile and the turtles):
ask crocodiles [
let potential_preys turtles in-radius max_distance
let selected_prey rnd:weighted-one-of potential_preys [ (1 - ( distance ? / max_distance ) ) ]
ask selected_prey [
scape
]
]
Before I get to your question, there is another problem I noticed with your code.
I had never realized this before, but NetLogo's semantics can make it tricky to model actual turtles! (At least when other breeds are involved.)
What I mean by this is that turtles refer to all turtles in the model, regardless of their breed. It means that, in your case, crocodiles are included in turtles, so when you say:
let potential_preys turtles in-radius max_distance
...crocodiles can be included in the potential preys!
Getting around this is easy enough, though: just choose another name for the breed that represent actual turtles in your model, e.g.:
breed [ tortoises tortoise ]
And then you can write:
let potential_preys tortoises in-radius max_distance
And now, for your question regarding distance, I think what you want is the distance to myself, where myself would be the crocodile that is selecting its prey. The myself primitive refers to the agent in the "outer" context of the block where you use it, i.e., the "calling" agent.
This gives you something like:
let selected_prey rnd:weighted-one-of potential_preys [
1 - (distance myself / max_distance)
]
Haha, I didn't thought about the turtles detail, indeed... ^^
Anyway it was an example, not the actual name of my breeds, so no problem, but thanks for noticing it!
Regarding the question itself, I also think myself will do, so I'll keep it like this, but now with higher confidence :D
Thanks Nicolas!

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
]