Two agents of the same breed - netlogo

I was wondering how you would refer to two agents of the same breed without specifically stating the who/number of the turtles. What I'm trying to do is when two players are on the same patch, the larger player consumes the smaller player. But there are players constantly spawning, so how do I refer to them without specifically stating the who? Any help is appreciated! This is what I have so far:
'''
ask players
[if any? other players-here
[if size player
'''

You can use max-one-of to find the largest player and give them a temporary name (eg "biggest"). Then you can address the player directly. It would look like this:
to consume ; patch procedure
ask patches
[ let biggest max-one-of players-here [size]
ask biggest
[ if any? other players-here
[ ... ] ; code to get eaten
]
]
end

Related

NetLogo : getting turtles to direct-link if a condition is true

I'm trying to implement YOYO leader election algorithm in netlogo
first step in this algorithm is to orient links ( direct link )from the minimum to the maximumbut between neighbors only !
I tried the command
[`ask turtles with [ [ who ] of self < [who] of one-of link-neighbors ]
create-direct-to turtle [who] of one-of link-neighbors ]`
this creates direct link from min to max ( neighbors ) but also creates a direct link from max to min ( neighbors )
and I don't know what's wrong :(
here's a screenshot , if you notice theres' a direct link from 0 to 2 and also from 2 to 0 and my goal is to have only from 0 to 2
Your problem is that every time you do one-of, it randomly selects. So you test against a random link-neighbor in the first line, find it's true and then randomly select a link-neighbor to connect to.
[ ask turtles with [ [ who ] of self < [who] of one-of link-neighbors ]
create-direct-to turtle [who] of one-of link-neighbors
]
More generally, this seems an odd way to achieve your goal. To start with, link-neighbors are the turtles that the turtle is already linked to. link is the generic name for all link breeds (I think you have created a breed called direct-link).
I am not entirely clear what you mean by minimum and maximum since your code is just from smaller to larger who value, regardless of what other who values are available. If you want to create a link from every turtle to every turtle with a higher who value, here is some code:
ask turtles
[ let targets turtles with [who > [who] of myself]
create-links-to targets
]
In general, it is bad practice to use who in NetLogo code. who is a completely arbitrary identifier that simply tracks the order that turtles are created. If you have turtles that die, then your code may crash because it is referring to a turtle that no longer exists. Or perhaps at some point you will have two breeds of turtles - who doesn't care if your turtle is a person or a dog or a factory or...
This may be one of the very few exceptions, but you might want to think about what you are intending who to mean. For example, as this is a leadership model, perhaps you could have a variable called 'charisma' and all the links are from turtles with lower values of charisma to higher values of charisma.

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 create links based on the node's in-degree?

I am having some difficulties to create links from two different breeds, humans and zombies.
These links should be created in such a way that turtles from the first breed (humans) with more links (in-degree) have a higher probability of linking the second breeds (zombies) - i.e.,the probability of a turtle from the humans being selected is proportional to the node's in-degree k_in.
At each tick, I add a zombie in the network.
By considering the preferential attachment model, I wrote:
let thisZombie one-of [both-ends] of one-of links
create-link-with thisZombie
but it has returned me the following error:
LINKS is a directed breed.
error while zombie 10 running CREATE-LINK-WITH
called by procedure ADD-ZOMBIE
called by procedure GO
called by Button 'go'.
This is the whole code for this part:
create-zombies 1
[
if targeting = "first model"
[
let thisZombie self
ask one-of humans
[
ifelse random-float 1 < p
[create-link-from thisZombie [set color red]]
[ask thisZombie [die]]
]
]
if targeting = "second model (preferential_attachment)"
[
let thisZombie one-of [both-ends] of one-of links
create-link-with thisZombie
]
]
I have the two following questions:
How can I select the human based on its in-degree?
Is it correct to use create-link-with one-of both-ends in case of two different breeds?
Thanks
You have the correct general approach (which I suppose you've lifted from the Preferential Attachment model in the NetLogo Models Library) but there are two things that are causing you problems:
You correctly using one-of links to pick a link, but by then picking one-of [both-ends] of that link, you could be picking either the human or the zombie, which is not what you want. If your links are always from a zombie to a human (like they seem to be based on the code for your "first model", then the human will always be end2 of the link, you can write: [end2] of one-of links. If you didn't know the direction of the link, you could write [one-of both-ends with [breed = humans]] of one-of links, but that would be less efficient.
You cannot mix directed and undirected "unbreeded" links. (See the user manual for more details on this.)
Supposing, again, that your links are always from zombie to human, your code should look something like this:
if targeting = "second model (preferential_attachment)" [
let thisZombie self
; pick the human end of a random link
ask [end2] of one-of links [ create-link-from thisZombie ]
]

Choosing patch analysing neighboors netlogo

I am trying to model an community that engages in shifting cultivation. For that I want each household to change the patch every year. Each household can have a different crop area, depending on time and number of people working. I want them to be able to choose a patch that has the amount of forest patch need to open their crop. For example, one household has a crop area of 3, so the new location needs to be a forest patch with two other forest patch neighbors. Any idea how can I specify that?
Thanks
Here is a possible solution:
patches-own [ patch-type ]
breed [ households household ]
to setup
clear-all
ask patches [ set patch-type one-of ["forest" "rock" "sand"] ]
let forest-neighbors-needed 2
create-households 100 [
let candidate-locations patches with [
not any? households-here and
patch-type = "forest" and
count neighbors with [ patch-type = "forest" ] >= forest-neighbors-needed
]
ifelse any? candidate-locations [
move-to one-of candidate-locations
] [
error "No suitable location found!"
]
]
end
This method is not the most efficient, because it rebuilds the set of possible location for each household it creates, but if your model is not two big, it shouldn't make much of a difference.
Note that you don't give us a lot of detail about how your model is organized, so I had to make a few assumptions. Next time, please tell us bit more: what breeds to you have, what are their variables, etc. Ideally, post a bit of code showing what you already tried.

Count neighbors turtles of a specific patch and report true or false

Hello i will try to be quick
I have a room with a fire that expands , and i have two exits , all i want to do is say to agents that if a door is blocked by fire then to go to the other one. i came up with something like this but not result.
to doorblock
show count neighbors with [pcolor = 77] ;; the patch color of the two doors
end
;;to go
ask smarts [ ;;smarts are the agents inside the room that need to get oout
if [ doorblock > 5 ]
[ set target one-of sexits]] ;;sexits is the other door
Anyone got a better idea? Thanks
OK, so if I understood correctly, you want your agents to take a look at the door that is their current target, check if that door has more than 5 fire agents around it, and choose another target door if that is the case.
If your fire agents are just red turtles (with no specific breed), you probably want something like this:
ask smarts [
if count ([ turtles-on neighbors ] of target) with [ color = red ] > 5 [
if-else ([ breed ] of target = sexits )
[ set target one-of nexits ]
[ set target one-of sexits ]
]
]
The key primitives here are:
neighbors, that will give you the patches around a turtle (the patches around target, in this case)
turtles-on, that will give you the turtles that are on members of a patch set (here, that will be the turtles that are on the patches that are the neighbors of target)
and finally, with allows you to get only the turtles from an agentset that satisfy some condition (here, we use it to get only the red turtles that represent the fires).
You should also try to understand the of primitive.
And I guessed you wanted to assign a new target that was of a different breed than the previous one (south if north, north if south) but that's up to you.