NetLogo: distance refering agent - distance

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!

Related

How to specifically change a variable of an agent in the agentset?

I am trying to change a variable (score) of a particular agent in the agent set if it meets the specific condition of a patch. This is called by an another agent. To be more clear. My idea is for instance if there is a breed (horse) and it sees the patch (grass) and it is standing on another breed (vertices - since horse move along a path connected by nodes represented by vertices) - a score variable to added to vertices-own where if the grass quality <=3, it would add a score to the vertex on which it stands.
ask horses[
ask patches in-cone 50 60 [
if grass-quality <= 3 ask vertices with [min-one-of vertices in-radius 0 [distance myself] [set vertex-score vertex-score + 1 ]]]]
I know something is wrong with this code logic. I am trying to convert my mentioned thought into codes. Kindly suggest me.
Thank you all.
Regards,
Heng wah
NetLogo agent (turtle) positions are continuous numbers so it is generally wrong to try and say something like 'if another turtle is where I am'. While you may have got there using move-to, it's probably safer to have the horse identify a vertex that is very close to it rather than in the exact position. You have used radius 0 but I'm going to change that to 0.001 to allow for potential errors in position.
ask horses
[ if any? patches in-cone 50 60 with [ grass-quality <= 3 ]
[ let my-vertex min-one-of vertices in-radius 0.001 [distance myself]
ask my-vertex
[ set vertex-score vertex-score + 1 ]
]
]
]
This is not tested, but I have simply reorganised your code. You had some bracketing issues and you were also asking vertices to find the closest vertex (which would have been itself), rather than having the horse find the closest vertex.
It's also not necessary to separate the let and the ask but I thought that would be easier for you to see how it works.

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...

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 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.

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.