How to select specific link and make it die - netlogo

I'm trying to make a carrier collect sheet metal from delivery and drop it off at the cutter. The cutter will cut the metal sheet into one of the two bodies and will output it to be collected by a carrier. Is creating and destroying links such as below the best approach to simulating the carriers carrying, dropping off and collecting the items? If so, how do I select the specific link to die? I've marked where NetLogo expected 2 inputs with "X X" as I am unsure what to write there. Cheers
globals[
metal-sheets
cutter-closest-free-carrier
s-welder-closest-free-carrier
p-welder-closest-free-carrier
skeletons
standard-skeletons
]
breed[carriers carrier]
breed[cars car]
cars-own [
body; standard, prestige
]
to cut
let free-carriers carriers with [laden = false]
let cutter patches with [machine-type = "cutter"]
let delivery patches with [area = "delivery"]
set cutter-closest-free-carrier nobody
ask cutter [
if status = "import" [
set cutter-closest-free-carrier min-one-of free-carriers[distance myself]] ]
if cutter-closest-free-carrier != nobody[
ask cutter-closest-free-carrier [
set target delivery
face one-of delivery
fd 0.01
set metal-sheets cars-here
if any? metal-sheets [
create-link-to one-of metal-sheets [tie]
set laden true
set target cutter
face one-of cutter
fd 0.01
ask cutter[
set metal-sheets cars-here
if any? metal-sheets [
ask link X X [die]
set status "pending"
create-link-to one-of metal-sheets [tie]]
]
]
]
]
end

This is not going to answer your question because I think you have got yourself in a real tangle with timing here. The whole thing needs to be reorganised, which will end up with a completely different question.
This code has the cutter finding a sheet, delivering it etc all within one procedure. The implication is that this all happens in the same tick. However, it is only moving forward 0.1. You need to think about the process in an entirely different way.
What happens during one time step? Some cutters find and cut a sheet. Other cutters that already have a sheet move toward the delivery point. Other cutters deliver their sheet. These all need to be different procedures. The cutters delivering their sheet are the ones where the link dies, but you won't have to identify them (what you have as X X) because only cutters doing deliveries will be implementing the procedure.
Have a look at the go procedure in the NetLog0 Models Library model called Shepherds. That has a similar structure to what you need here.

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!

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

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.

Updating Simple Preferential Attachment Model in NetLogo

There is a model in NetLogo based on simple preferential attachment. Agents create links thusly:
to go
if count turtles > num-nodes [ stop ]
let partner one-of [both-ends] of one-of links
create-turtles 1 [
set color red
move-to partner
fd 1
create-link-with partner
]
layout
tick
end
In this model, a node's chance of being selected is directly proportional to the number of connections it already has. I want to edit this code so that there is a variable, prestige, which also determines the probability that a node will be selected. I understand that I need to create a variable for turtles called prestige, but I'm stuck on how to make it so that the probability that a link will be created is also proportional to the amount of prestige a turtle has.

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