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

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.

Related

How to check if 2 different breeds of turtles are on the same patch

The final part of my design involves me recording down anytime a car breed drives into or in netlogo terms, is on the same patch or X and Y coordinate as the people breed as they navigate across the edge of the screen. Had this been java I could've done something like
if Car.xPostion == Person.xPostion
(Do something...)
But unfortunately I do not know how to do the same in NetLogo, all I've been able to do so far is just ask the two breeds by giving every turtle a boolean variable called movable and setting them to true and the rest to false, is there anyway I can check the two coordinates of two different turtles on Netlogo? This is all I 've been able to do so far.
to record-accidents
ask turtles with [movable? = true]
[
]
If you tried something like your java approach, it would fail because turtle positions are continuous and floating numbers are nearly always not equal.
If I have understood your question correctly, you have given a boolean variable called movable? set to true for cars and false for all other breeds. You don't need to do this, turtles know their own breed so you can do ask cars.
To answer your specific question, there are several ways to approach it depending on the perspective (sort of, which agent is in charge).
You could identify patches where there are accidents:, which is the answer to your question in the title (about identifying patches with two breeds).
let accident-locations patches with [any? people-here and any? cars-here]
if any? accident-locations
[ ask accident-locations
[ <do something>
But you can also take a turtle perspective. You could start from pedestrians who have been hit. This takes advantage of the fact that turtles can automatically access the patch variables (like turtles-here) for the patch where they are located:
let hit people with [any? cars-here]
if any? hit
[ ask hit...
or from cars:
let hitters cars with [any? people-here]
if any? hitters
[ ask hitters...

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.

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
]

NetLogo two kinds of self-reference

I want to add an agenset of turtles to the variable TurtlesICanSee of a certain turtle that depends on that turtles properties. For instance, in one application I want to add only the turtle itself to TurtlesICanSee, in another application I want to add the two turtles (if there are any) with adjacent who-numbers (the turtle's own who-number + or - 1).
If I can figure out the first application by using who-numbers I think I can extend that to second application. However, I cannot figure out the first.
I tried
ask turtles [
set TheTurtlesICanSee turtles with [who = ([who] of self)]
]
but this fills the TheTurtlesICanSee of each turtle with every turtle.
I think I understand why; NetLogo thinks that I want every turtle x that has the same who-number as itself (x), i.e. every turtle. But I don't. For every turtle x I want every turtle y that has the same who-number as x.
Can anyone help me with this? Note that the solution that I need to the first application is one that can be generalized to the second. So not any way of adding a turtle to one of its own variables will do. I need a form of self-reference involving who (or a good argument against doing it this way I guess, but preferably the former).
Your code needs only a slight alteration to work, as follows:
ask turtles [ set TheTurtlesICanSee turtles with [who = [who] of myself] ]
Note the substitution of myself for self; http://ccl.northwestern.edu/netlogo/docs/dictionary.html#myself has an explanation of the difference.
But actually there's no need to involve who numbers. It's almost never necessary to use who numbers in NetLogo; there's almost always a simpler, more direct solution. A simpler solution is:
ask turtles [ set TheTurtlesICanSee turtles with [self = myself] ]
But actually it isn't necessary to use with at all. We can use turtle-set to build the desired agentset directly:
ask turtles [ set TheTurtlesICanSee (turtle-set self) ]
This is the solution I would recommend, for clarity and brevity, but also because it will run faster, since it doesn't involve iterating over the set of all turtles, as the with-based solutions do.