In this example I am trying to make two directed-link-breed links based on turtles-own Value. The condition is as follows ask turtles to link to other turtles who have a smaller Value than me.
directed-link-breed [active-links active-link]
turtles-own [ Value ]
to setup
crt 100 [setxy random-xcor random-ycor set value random 500]
ask turtles [ create-active-links-to min-n-of 2 other turtles [Value < myself] ][set links to have xxxxx ]
end
when I do the following:
ask turtles [ create-active-links-to min-n-of 2 other turtles with [value < [ value ] of myself ] [set links to have xxxxx ]
I am getting the following error:
Requested 3 random agents from a set of only 2 agents.
error while turtles 8 running N-OF
called by procedure GO
called by Button 'go'
That message is telling you that (for the particular lettuce), there were only two that satisfied the criteria, but you wanted to link with 3 of them. Looking at your question, you want turtles to send links to turtles with lower values of a specific variable. What do you want the turtle with the lowest value of that variable to do? Clearly it can't link to lower valued turtles because there aren't any.
I'm also not clear why you are using min-n-of. Do you want to link to the lowest valued turtles (in which case every turtle will send links to the same few turtles) or do you want to link to randomly selected turtles with lower values (in which case turtles with higher values will have more choices)?
There are a couple of ways you can handle this once you have the logic sorted out. If you definitely want the lowest value, then use min-n-of first to find the candidates, then link to any with a lower value than the asking turtle. If you want to randomly select from a potentially larger group, use up-to-n-of instead of n-of. Or you could count the number found before trying to link to make sure there's enough.
Related
I have a patch variable set as visitantBees [].
The intention is that each patch has a record of which agents were in it.
The method for registering the "visiting agents" is as follows:
to computing Frequency
ask patches
[
if any? turtles-here
[
set visitantBees lput [who] of turtles-here visitantBees
]
]
end
However, this way a list of list is returned ([[3 1 0 2 4]], for example).
Would anyone know how I can add only the who number to the visitantBees list?
Maybe a way to extract all the items from the turtles-here.
The reason why you get a list of lists is that both visitantBees and [who] of turtles-here are lists.
While visitantBees is a list because you set it as a list, why is [who] of turtles-here a list? Because turtles-here is an agentset - and the only way to report a variable of an agentset is to create a list. For example, if you wanted to know [color] of turtles, the only way NetLogo has to give you this information is to put all the turtles' colors in a list.
So, why is turtles-here an agentset? Because, even if sometimes turtles-here can contain 0 or 1 turtle, it can also contain multiple turtles. And anything that is fit to contain multiple agents has to be an agentset.
On the other hand, when you ask a single agent to report one of its variables, you get the value as such, i.e. not a list (unless that value is a list in itself, of course). For example, [color] of turtle 0 is just its color, not a list containing a color.
Therefore, you can achieve your goal by individually asking every turtle on the patch to append their who to visitantBees:
to computingFrequency
ask patches [
ask turtles-here [
set visitantBees lput who visitantBees
]
]
end
Or, given that turtles can automatically read and change the patches-own variables of the patch they are standing on, you can make it even simpler:
to computingFrequency
ask turtles [
set visitantBees lput who visitantBees
]
end
Which is also faster because it will only engage with turtles (who, by definition, are standing on a patch) rather than engaging with every patch even if there are no turtles on it.
I have a complete directed graph with each link a weight of it's own. I've managed to select the max-out-link of every turtle. But, sometimes the max-out-link of two turtles are opposite of each other resulting in both links opposite of one another being selected. if this happens i want the link with the lower value to die.
i have created the two lists with this:
set max-end1 [[end1] of max-one-of my-out-links [trust]] of turtles
set max-end2 [[end2] of max-one-of my-out-links [trust]] of turtles
and by setting an x and y parameter like so:
ask turtles
[
set x max-one-of my-out-links [label]
set y my-in-links
]
i was hoping to compare each item of the two lists like so:
if [x] of max-end2 = any? [y] of max-end1
[
ifelse x X y
[ask x [die]]
[ask y [die]]
]
but i have no idea how to combine the foreach command with the if command
can someone help me?
I can't actually figure out how your code is supposed to work. Lists seems like an awkward way to approach this and the way you are using any? is not going to work. So, I have instead started again and written a standalone model (put it in an empty NetLogo session) to do what I think you are trying to do.
links-own [ weight ]
to testme
clear-all
create-turtles 15
ask turtles
[ create-links-to other turtles
[ set weight random 100
]
]
layout-circle turtles 10
kill-lowers
end
to kill-lowers
; first remove any that's not a max weight
ask turtles
[ let big-link max-one-of my-out-links [weight]
let dying my-out-links with [not member? self (link-set big-link)]
ask dying [die]
]
; find pairs and make lower turn red
ask links
[ let opp-links links with [end1 = [end2] of myself and end2 = [end1] of myself ]
if any? opp-links
[ ask opp-links [set color red]
]
]
end
The testme procedure just creates a complete network. It then calls the kill-lowers procedure to do the link removal. The first section has each turtle identify its out-link with the largest weight (or randomly selects one if two equally high) and then creates the link-set of all the other links and gets them to die. I think you already have that happening in your code.
So the interesting bit is the section that starts ask links. It randomly runs through all the links (think of it as a foreach except operating on a set of links rather than a list). In turn, for the particular link, the code checks if there is a link in the opposite direction and sets that to the variable named opp-links. The way it checks is to simply see if there is a link that has end2 to be its own end1 and also the other way. end1 is the source of a directed link and end2 is the target.
If there is such a link, it becomes red. That's so you can check the code is doing what you want. After you have tested it, then you have it die instead.
I have n turtles which all have links with one another. That means I posses a complete directed weighted graph. I've managed to locate the links with maximum value per each turtle, but now I want to delete all other links without maximum value, again per each turtle.
I'm using the [die] function but in order to distinguish the set of links per each turtle and then clear links from each set
This is the line of code I'm using:
> ask turtles
[
> ask my-in-links with [trust < max [trust] of links with [other-end] = ] [die]
]
However, I thought instead of using other-end function I could also use this line of code
> ask turtles [
ask my-in-links with [trust < max [trust] of links with [out-link-to] = ] [die]
]
My initial thought was by using one of the two functions (other-end/ out-link-to), I could create a common attribute between the set of links.
My main problem is
I'm not sure whether the functions (other-end/ out-link-to) are correct
I don't know what to write after the "=" expression
I don't know which turtle the ask turtle function begins to work with
Here's a complete model that does what you are trying to achieve.
links-own [trust]
to testme
clear-all
; create complete directed network
create-turtles 10
ask turtles
[ create-links-to other turtles
[ set trust random 50
]
]
; display complete network briefly
layout-circle turtles 10
type "average trust value is: " print mean [trust] of links
wait 5
; delete all except highest trust for each turtle
ask turtles
[ let keeper my-in-links with-max [trust]
ask my-in-links with [not member? self keeper][die]
]
type "average trust value is: " print mean [trust] of links
end
I have it showing the initial network and printing average trust values so you can see what's going on.
The important concept here is that my-links is an agentset (in particular, it's a linkset because the agents in the set are links). Since what you're trying to do is just look through the links that are connected to one turtle, then it doesn't matter which turtles are at the other end and you don't need to refer to the other end at all.
You can simply look at the set of links that attach to the particular turtle (my-in-links or my-links or my-out-links) and then look at the values of trust for those links. I have used with-max to find the one with the highest value and then used set membership. But you could also use
ask turtles
[ let upper max [trust] of my-in-links
ask my-in-links with [trust < upper] [die]
]
which is closest to the code you wrote in your question.
I'd like to have patches count the number of turtles that have stood on them. What would be ideal is a event such as:
if turtle-lands-on-me [add one to count]
because a turtles could leave and come back and be counted twice (which is what I want) and it would avoid counting turtles who stand still twice or more (which I don't want). Is there any way to achieve this?
Thank you!
What you need is a variable for each patch (I am calling it 'landed' below). The following code assumes you want to know about the patch it lands on each time step, but ignores the ones it passes over. It also updates the counts only where the turtle changes the patch, as requested, and labels the patch with the count.
patches-own [landed]
to setup
create-turtles 20
[ setxy random-xcor random-ycor
]
end
to go
ask turtles
[ let old-patch patch-here
set heading random 360
forward one-of [0 0.5 1 3]
if old-patch != patch-here
[ ask patch-here
[ set landed landed + 1
]
]
]
ask patches [set plabel landed]
end
The problem is that a turtle can pass over multiple patches during one time step. You can see this in the example model for those turtles that move 3. If you also want them, you will need to do something like the 'Line of Sight' model in the NetLogo models library.
I have a landscape where each patch contains a cost value.
I placed a turtle within each patch according to the following code :
to create-turtles
ask neighbors [ sprout 1 [
set shape "dot"
set size 0.5 ] ]
end
Then, I built a link between each turtle according to the following code :
to create-link-turtles
ask turtles [ create-links-with turtles-on neighbors ]
end
As each patch contains a cost value, I would like to assign a cost value to links between turtles.
For example,
If the link intersects two patches (patches 1 and 2) that have two different costs, the link would be equal to cost in patch 1 + cost in patch 2.
If the link intersects two patches (patches 1 and 2) that have the same cost, the link would be equal to cost in patch 1.
How can I assign a cost value to links between turtles in this way ?
After this, I would like to apply the dijkstra' s algorithm.
Thank you for your help.
Have a good day
Assuming that:
your patches have a cost variable
your links have a link-cost variable
turtles are always connected to turtles on neighboring patches (like in the code you posted)
You can simply :
ask links [ set link-cost sum [ cost ] of both-ends ]
This will just add the costs of the two patches under the turtles at both ends of the link. (If you had links traversing more than two patches, this approach would not work and things would get much more complicated.)
For calculating distances afterwards, I'd suggest you take a look at the NW extension. Its weighted-distance-to primitive uses Dijktra's algorithm internally.