Netlogo: Ask directed link "my-in-turtle" and "my-out-turtle" - netlogo

I've been working in netlogo for some time, but there is one thing I can't really figure out. You can ask a turtle what its out- and in-links are, but is there a way to ask a link the equivalent using a simple construction of both-ends and/or other-end?
right now my code is
if one-of both-ends [firing = true] and one-of both-ends [time-not-fired = 1] [do-stuff]
but what I'd like to do is
if my-in-turtle [firing = true] and my-out-turtle [time-not-fired = 1] [do-stuff]
is there an easier/faster way than to iterate over all turtles with [firing = true] and asking all their links if the other end has [time-not-fired = 1] ?

I think you are looking for
in-link-neighbors
It reports the set of turtles with links to the turtle calling it.
ask turtles with [firing]
[
ask in-link-neighbors with [time-not-fired = 1][do-stuff]
]
Now be cautious this code my ask some turtles to do stuff more than once. so put in safeguards.
I realized you want to ask links not turtles.
"My-in-turtle" (the source) is called end1 "my-out-turtle"(the destination) is end2 on directed breeds.

Related

How do you "reference" a particular turtle within as ask turtles block?

Whenever we use the "ask" command for all agents of a particular kind or breed, the Netlogo program goes through each agent one by one in random order. What I want is really simple: I would like to access the turtle whose turn it is at that moment.
I can't seem to find an appropriate command for this.
My code for reference purposes is as follows:
to surfer-visits-source
ask surfers [
if ([quality] of one-of [out-link-st-neighbors] of one-of out-link-ss-neighbors) < expected-quality
[
let temp ([who] of out-link-ss-neighbors)
create-link-ss-to one-of sources with [who != temp]
ask links-ss with [end1 = [who] of surfer][
ask links-ss with [[who] of turtle temp] [
die
]
]
]
]
We can use the self command to do this.

Is there a way to create a modification of Breed-On that includes specific characteristics?

I want my forager turtles to follow a path laid down by leader turtles, and this path consists of a turtle breed called trail-markers. The forager turtles also lay down trail-markers (to reinforce the trail), so I would like them to check for leader trail-markers on some patch/patches. I know Netlogo includes breeds-on to check for the presence of members of a breed, but I need the foragers to check for blue leader trail-markers. Is there a way to do this?
Here is what I had in mind:
if count (trail-markers with [color = blue]) on patch-ahead 1 > 0 [fd 1]
But I got, error:nothing named on has been defined, which makes sense, since the Netlogo primitive is breeds-on, so I modified it to:
if any? (trail-markers with [color = blue])-on patch-ahead 1 [fd 1]
However, I simply received: error:nothing named -on has been defined instead.
See the code below. I think you just want to use trail-markers-on, but you can simplify by using any? a bit. And you might want to have the custom reporter so you can just check for blue-markers-on if you find that simpler.
breed [ trail-markers trail-marker ]
to example
; I think this is what you want
if count ((trail-markers-on patch-ahead 1) with [color = blue]) > 0 [ forward 1 ]
; this is slightly simpler, using `any?` instead of `count ... > 0`
if any? (trail-markers-on patch-ahead 1) with [color = blue] [ forward 1 ]
; you could also write a custom reporter if you'll need to get this info a lot
if any? blue-markers-on patch-ahead 1 [ forward 1 ]
end
to-report blue-markers-on [p]
report (trail-markers-on p) with [ color = blue ]
end

Delete links between turtles with different breeds

I would like to drop links between one turtle and another one with different breed if a condition is satisfied.
I am using the following code to do this:
ask one-of turtle1 [
if any? my-out-links with [breed = turtle2]
[ask one-of out-link-neighbors with [breed = turtle2 and value < 0.7] ;; value should refer to a neighbour's characteristics and it represents my condition
[die]
]
]
But the links seems to be kept. No link has been deleted.
Could you please tell me what it is wrong in my code and how to fix it accordingly?
I'm assuming that turtle1 and turtle2 are two breeds of turtles. If so,
if any? my-out-links with [breed = turtle2]
will never find any such out-links as you are checking to see if any of my-out-links are of breed turtle2, and of course no links are of that breed. What you want is to know if the turtle at the other end of the link is of breed turtle2. Since these are directed links, the turtle you want to check is at end2.
if any? my-out-links with [[breed] of end2 = turtle2]
will let you know if the any of the turtles at the other end of my-out-links are of breed turtle2.
The line
ask one-of out-link-neighbors with [breed = turtle2 and value < 0.7] [die]
may do more than you intend. It asks a link-neighbor turtle of breed turtle2 and with value < 0.7 to die. That will indeed kill the link as well (since it has lost its end2), but if you want to kill the link and not the turtle at the other end you could do all this in a single line
ask one-of turtle1 [
ask one-of my-out-links with [[breed = turtle2 and value < 0.7] of end2] [die]
]
If there are no such out-links, then you will be asking nobody and the ask will do nothing.

how to sprout around a turtle in netlogo?

I have been looking to spread around turtles and I don't get it.
The idea is that I have an insect population (a type of turtle) and this insect population check around if there's a nesting patch available. If there's one and there's no other insect population I would like this patch to generate a new insect population. So far I have come with this idea :
ask insect-populations
[
ask patches in-radius 2
[
if lay? = 1
[
if not any? insect-populations [ask self [sprout-insect-populations 1]]
]
]
]
Thanks in advance for any tip
ask insect-populations
[
ask patches in-radius 2 with [lay? = 1 and not any? insect-populations-here]
[sprout-insect-populations 1]
]
should be what you want if I understand your intent correctly.
The trick is in the [with]. It takes a true/false block. So any agent for which the boolean statement inside the square brackets is included in the set.

Netlogo one-to-one relation

I have a netlogo problem for which I can't seem to find a solution, but yet it feels very basic.
I have two types of breeds:
breed [individuals individual]
breed [cars car]
I want to create a link from one individual to one car. So, its a one-one relation. I use this code to do that:
to setup-individuals
create-individuals individuals-number [
set ID 2
set shape "person"
set color yellow
setxy random-xcor random-ycor
set activity ""
set activity_time 0
let rand random 2
ifelse rand = 0
[
set owns-car false
]
[
set owns-car true
create-link-to one-of cars ;; here is the issue
]
]
end
The problem is that if i use "create-link-to one-of cars" there are more than one individuals linked to one car, but I want each individual to have a distinct car. When trying the following command: "create-link-to one-of cars with [my-in-links = 0]" its giving me the following ERROR: "CREATE-LINK-TO expected input to be a turtle but got NOBODY instead." I tried many variations of this command, but its not working.
Your attempted solution of create-link-to one-of cars with [my-in-links = 0] is on the correct path. However, if you look at the NetLogo dictionary, you will see that my-in-links returns an agentset, not an integer giving the number of members of that agentset. So you need to compare to empty rather than compare to the number 0.
This is the code that is syntactically closest to what you have: create-link-to one-of cars with [count my-in-links = 0].
What you really want though is something more like create-link-to one-of cars with [not any? my-in-links]