How to create links based on the node's in-degree? - netlogo

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

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.

Netlogo: randomly transform breed

I'm working on a rebellion simulation and I'm trying to use Netlogo but I'm new to it so I'm running into some issues. I'm working off a modified wolf sheep model and I have three turtle types: police, rebels, and civilians. I have the general set up working but I'm trying to incorporate a civilian collateral damage element.
I want to model the police imperfectly differentiating rebels and civilians. When police mistakenly arrest a civilian, that causes n other number of random civilians to then become rebels. Where I get lost is interacting the police's actions with the civilian breed type. Below is my police procedure for civilian arrests:
to collateral-damage ;police procedure
let prey one-of civilians-here
if prey != nobody and random 100 < prob-collateral
[ ask prey [ die ] ]
Any help is super appreciated!
Breed can be set like any other turtle owned variable, with a set statement. There's not quite enough detail to give you a definitive answer, but I think you want something like:
let prey one-of civilians-here
if prey != nobody and random 100 < prob-collateral
[ ask prey [set breed rebels]
ask n-of min (list 5 count civilians) [set breed rebels]
]

How can attribute of one node of a link be copied from the other node of the same link?

I would like to create a link between a turtle of one breed and another turtle of another breed, and then to copy the value of an attribute of the turtle at one node of the link into an attribute of the other turtle at the other node of the same link.
I tried:
to go
ask one-of apples [
create-link-to one-of oranges
ask-mood]
end
to ask-mood
set others-mood [my-mood] of other-end
end
And (obviously) got the error message "Only a link can get the OTHER-END from a turtle."
Please could you advice the code that I need? Thank you.
It's difficult to give an answer without a working example. You have a bit of a conceptual problem - if two edges link to the same turtle, which turtle is supposed to provide the value for the others-mood? In the following code I have ignored that and just randomly selected the one to provide the value.
You need to change contexts by selecting a link and then the other end of the link is available. See the following for a full model that you can adapt to your code.
turtles-own [mood others-mood]
to setup
clear-all
create-turtles 10
[ setxy random-xcor random-ycor
set mood random-float 1
]
ask turtles
[ create-link-to one-of other turtles
set others-mood [mood] of [other-end] of one-of my-links
]
end

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.