Trying to determine a link with specific nodes - netlogo

I'm trying to call upon an existing link but with specific nodes. keep in mind this command is inside a foreach. So for now I have this:
foreach max-links
[
the-links -> ask the-links
show [label] of [one-of my-out-links] of [end2] of the-links with [other-end = [end1] of the-links]
]
the "with" command doesn't work because it expects an agent set. I also tried this with the "and" command which doesn't work because it requires a true or false.
Can someone please tell me how to add the:
[other-end = [end1] of the-links]
section to the code?

here is your code with slightly different spacing
foreach max-links
[ the-links ->
ask the-links
show [label] of [one-of my-out-links] of [end2] of the-links with [other-end = [end1] of the-links]
]
Problem 1: you need to have brackets around what the 'the-links' is being asked to do
Problem 2: You have much too much convoluted logic in one line of code - there are no brackets to help. If you want to debug, break it into several steps and then combine only when it works. Also, I suspect 'the-links' is a single link (since it's in a foreach) rather than a link-set so you should give it a singular name to help keep your thinking clear.
So: show [label] of [one-of my-out-links] of [end2] of the-links with [other-end = [end1] of the-links]
Becomes
let chosen-turtle [end2] of the-link with [other-end = [end1] of the-link]
let chosen-link [one-of my-out-links] of chosen-turtle
show [label] of chosen-turtle
Having broken it into steps like this, it is clear that the first line makes no sense. If the other-end of the-link is [end1] of the-link then you are simply asking for [end2] of the-link.
Having spotted the (likely) problem, the fixed code is:
foreach max-links
[ the-link ->
ask the-link
[ let chosen-turtle [end2] of the-link
let chosen-link [one-of my-out-links] of chosen-turtle
show [label] of chosen-turtle
]
]
And, if that works (I can't test it) then you can put it back into one line if you like. But start it in multiple lines because NetLogo will point to the line with the problem, making it easier to debug.
foreach max-links
[ the-link ->
ask the-link
[ show [label] of [one-of my-out-links] of [end2] of the-link
]
]

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.

Deleting the opposite of a link in a list in netlogo

i have a list of links but sometimes within the list two links that are opposite of each other appear in the list. All links have values and the list is organized from highest value to lowest. what i want to do is to make the opposite link with the lower value die.
does any one have any ideas?
not that it would help a great deal but i got my list from this line of code:
set max-links sort-on [(- label)] link-set [max-one-of my-in-links [label]] of turtles
I'm assuming that these are directed links as there can not be two undirected links between any two turtles. The following code is not elegant, but I think it will do what you want. I've embedded it within a working model.
to test
clear-all
create-turtles 10 [fd random 10]
ask turtles [ create-links-to n-of 8 other turtles ]
ask links [set label random 100 ]
let link-list sort links
let to-die []
let remaining link-set link-list
foreach link-list [[m] ->
let opposite (link [who] of [end2] of m [who] of [end1] of m)
if opposite != nobody and member? opposite remaining [
ifelse ([label] of opposite < [label] of m) [
set to-die lput opposite to-die
]
[
set to-die lput m to-die
]
set remaining remaining with [self != m]
]
]
foreach to-die [m ->
set link-list remove m link-list
]
ask link-set to-die [die]
end
For each link in the list, it looks to see if there is an opposite link in the linkset remaining, originally made up of the links in the list. If so, it marks the proper link for deletion and then takes itself out of remaining so that when the opposite link is tested it won't be found. Once all links to be deleted are found, they are removed from the list and asked to die.
Hope this helps,
Charles

Netlogo - identifying a subset of an agentset

I spent all afternoon trying to work out with a part of my code and I don't seem to be getting anywhere. Basically, I'm trying to create a social network on model setup. Each person in the model starts off with a set of people that are nearby to them people-nearby. It is from this set that people choose who to connect with:
create-people population-size
[
set people-nearby turtle-set other people in-radius neighborhood-radius
]
to create-network
let num-links round (average-node-degree * population-size) / 2
while [ count links < num-links and count people with [length sort people-nearby > 0] > 0 ]
[ ask one-of people
[ *... initiate probabilistic link creation process...*
create-unlink-with chosen-friend
Once person A has connected to someone (ie. person B), person B is removed from person A's people-nearby set. I'm having trouble with this portion of the code where the people-nearby set is updated by excluding all nearby people that are members of the unlink-neighbors set (i.e., those to whom person A is already connected - this set including person B):
ifelse count turtle-set people-nearby > 1
[ let nearby-people-not-linked-to-me ( turtle-set people-nearby with [ not member? self [ turtle-set unlink-neighbors ] of myself ] )
set people-nearby nearby-people-not-linked-to-me ]
[ set people-nearby [ ] ]
For some reason this error keeps popping up:
"WITH expected input to be an agentset but got the list [(person 0) (person 1) (person 3) (person 4)] instead." whenever
people-nearby with [ not member? self [ turtle-set unlink-neighbors ] of myself is called.
I looked up so many posts but can't seem to get the form of the argument right so that it stops showing this error.
Can anyone help me fix this please? (Oh and it's my first post so apologies if I haven't set up the issue properly)
When you submit code, try to submit what is needed to recreate your problem- check out the asking help page, and specifically the section on helping others reproduce your problem. As is, I think your problem comes from using turtle-set. That primitive is mostly used to combine agentsets, not to query them. So in your line:
( turtle-set people-nearby with [ not member? self [ turtle-set unlink-neighbors ] of myself ] )
there is an syntax issue related to turtle-set. The error itself is saying that you have not returned an agentset but a list of agents, which behave differently.
If I understand correctly, you want all people to have a variable that contains all people within a radius of themselves: "people-nearby". Then, you want the people to form a link with one of their "neighbor" turtles. Finally, you want the people to update their "people-nearby" variable to exclude the person to whom they just formed a link. Below is some code with comments where I tried to follow those steps- obviously your variables will be different, but it may get you started. Let me know if I need to clarify anything or if I missed a step.
breed [ people person ]
turtles-own [ people-nearby ]
to setup
ca
reset-ticks
create-people 70 [
setxy (random 30 - 15) (random 30 - 15)
]
; do this after all turtles have spawned
ask people [
set people-nearby other people in-radius 3
]
end
to create-links
let num-links 10
;; Create a temporary agentset out of turtles that have people nearby
let turtles-with-neighbors turtles with [ any? people-nearby ]
; ask some number of the temporary agentset:
ask n-of num-links turtles-with-neighbors [
;; This just makes it easy to identify the turtle that causes the link
ask patches in-radius 3 [
set pcolor white
]
; create a link to one of the nearby people
create-link-to one-of people-nearby
; newly set people-nearby to only include turtles in radius
; that are not linked-to from the currently acting turtle
set people-nearby other people in-radius 3 with [ not member? self [ out-link-neighbors ] of myself ]
ask people-nearby [ set size 0.5 ]
]
end

Netlogo if statement with myself and other

I'm fairly new to Netlogo and struggling with how to set up a somewhat complicated if statement. The statement is for turtles and the condition is that other turtles live in the same region and have a house.
I've tried the iterations of the following, but have not yet been successful:
if (one-of other turtles with [region = [region] of myself and house? = True]) []
if (other turtles with [region = [region] of myself and house? = True]) []
Thank you for any insights!
If you need to insert code in a question, check out the "Code Sample" button in the toolbar. You can highlight your code and click the button- super handy.
Your second try is extremely close. The quick fix is to add the any? primitive in order to tell Netlogo that you want to evaluate the agentset "other turtles" in this case. As it was, you weren't actually evaluating anything with if- kind of like saying "If the turtles in my region who have a house," as opposed to "If there are any turtles in my region who have a house."
to check-region
ask one-of turtles [
if any? other turtles with [ region = [region] of myself and house? = true ] [
set color white
]
]
end
If you need to evaluate more specific numbers, you can use something like count to set a threshold - for example:
to check-region-count
ask one-of turtles [
if count other turtles with [ region = [region] of myself and house? = true ] > 3 [
set color white
]
]
end

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

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.