Netlogo: randomly transform breed - netlogo

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

Related

How can I address the attributes of other turtles in the "procedure-calling" turtle's radius and how long they stay there? In Netlogo

I am building a model in Netlogo where I am simulating the spread of a virus. I have looked at other virus models but so far haven't found a solution to my problem.
I would like each agent interaction to have a specific transmission-probability, based on the susceptible agent's susceptibility (agent attribute), the infected agent's (can be more than one) probability of transmitting the virus (agent attribute), as well as the time spent in proximity. Basically P(getting infected)= infectivity * time in proximity * susceptibility. So for each tick that a susceptible turtle is close to a infected one the probability of getting infected should increase. So far I have only managed to use susceptibility when creating the procedure with susceptible agents as callers.
Here is my code for this procedure so far:
to transmit; procedure for transmitting the disease to nearby people, inspired by epiDEM
let caller self ; adress the susceptible turtle by the name caller
if susceptible? = TRUE
[
let nearby-infected turtles in-radius 2 with [infected? = TRUE]
if nearby-infected != nobody
[
let transmission-risk age-susceptibility ;here I want to include protective-measures (attribute of the possibly several infected turtles) and time-exposed
if random-float 1 < transmission-risk[
set infected? TRUE set susceptible? FALSE]
]
]
end
I am really having a hard time on how to go about this, I am not sure how to address the attributes of the turtles within the radius and how I could measure the time of exposure. I was thinking of creating links that have exposure time as an attribute that will die when the infected is no longer in the radius. But I am not sure how to ask the links to die when the turtles are further away from each other than a 2 patch radius. Also I would prefer to not use links to keep the model runnning faster so if there is another solution I would be very happy to hear it :)
For the option with the links I wanted to try something like this:
to transmit; procedure for transmitting the disease to nearby people
ifelse link-neighbors (in-radius 2 = FALSE)
[ask link [die]]
[set link-age link-age + 1]
let caller self ; adress the susceptible turtle by the name caller
if susceptible? = TRUE
[
let nearby-infected turtles in-radius 2 with [infected? = TRUE]
if nearby-infected != nobody
[create-links-with nearby-infected
let transmission-risk age-susceptibility ;include protective-measures and time-exposed using links to address the turtles
if random-float 1 < transmission-risk[
set infected? TRUE set susceptible? FALSE]
]
]
However I for starters just don't understand how to address the links of the turtles further away to ask them to die, I have changed it after the error messages I've gotten but just can't seem to get it to work.
Is it even possible to do what I want in Netlogo?
Very thankful for any hints!

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

How to model spawning after kill/miss in Predator-Prey model in Netlogo?

I'm working on a Predator-Prey model but I have a unique spawning mechanic that I'm trying to work in.
The idea is that when a wolf encounters sheep, there is a probability of the sheep escaping (I already have this part coded in). If the sheep is killed, n number of lambs are created. If the sheep escapes, k number of lambs are created. After t periods, the lambs become prey. n, k, and t will all be sliders on the interface side.
I'm pretty new to agent-based modeling and Netlogo coding, so any advice would be really appreciated.
This is the current code for the hunting:
to catch-sheep ;; wolf procedure
let prey one-of sheep-here
if prey != nobody and random 100 < kill-probability
[ ask prey [ die ]
Presumably k>n, so you can have all adult sheep hatch n lambs, and then catch-sheep, and then have all remaining adult sheep hatch k-n lambs. This requires distinguishing between lambs and adults, so you may want to add an age attribute.
However, I suspect you will want to switch to probabilities of hatching a lamb each tick, rather than a number of lambs hatched each tick.
Using your code to add some code and see if that works for you.
When a sheeps is killed create a random number of new sheeps, in the code below use your slide variable instead of 10.
I use the primitive function hatch to do all the work.
to catch-sheep ;; wolf procedure
let prey one-of sheep-here
if prey != nobody and random 100 < kill-probability
[ ask prey [ die ]
;;Here we create new sheeps upon prey death
let rd-num random 10 ;; 1 to 10 sheeps will appear
hatch-sheep rd-num [ set color blue
set xcor 9 ;; Initialize your new sheeps ]
]
Let us know of any questions you might have! Happy coding.