I have a complete weighted directed graph. My goal is to allow each turtle to only have one in-link and one out-link (which will result in a directed cycle graph) whilst making sure the sum total of the weights are maximum.
To do this, I have listed the maximum in-link (or out-link) of every turtle (without consideration of the above rules) and now I'm trying to delete all out-links of end1 of the maximum link and all in-links of end2 of the link. When deleted the initial list of maximum links has to be updated. This process continues until there are no more links to choose from.
This is the code I have so far:
foreach sort-on [(- label)] link-set [max-one-of my-in-links [label]] of turtles
[
ask the-links [ask end1 [ask my-out-links with [not member? myself x] [die]]]
ask the-links [ask end2 [ask my-in-links with [not member? myself x] [die]]]
]
I think until I don't update the maximum links the code cannot run properly because it keeps saying "the link has died". However, even if i manage to update the maximum links I'm not sure the "myself x" part is correct.
what can I do solve this problem?
Related
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.
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.
Model Details I'm attempting to code territory selection in NetLogo. Turtle 0 selects a territory center, and builds a territory. It select patches based on value of food, discounted by the distance away (food / # patches away from center of the territory), selecting patches in order of discounted value. It stops when it meets total food requirements. Patches selected don't have to be connected to the territory, but should become connected as the turtle walks to the selected patch. These patches crossed are added as travel corridors so that the territory remains contiguous. After Turtle 0 has finished building its territory, Turtle 1 sprouts and repeats the process to select its own territory, then Turtle 2, and so on. Patches belonging to a turtle's territory are unavailable to other turtles.
The Problem Existing territories should be seen as obstacles. Turtles need to identify and go around these obstacles to reach their destination patch using the shortest path possible.
In my current code, turtles will select patches on the other side of an existing territory because they just travel across it. They won't have added a travel corridor to get there, however, because the patches on the way there were already owned.
What are some ways I might tell turtles that if they encounter owned patches (existing territories), they must go around using the shortest distance possible so they can build a travel corridor to the selected patch? I realize this is a complex question, made even more so if actual distances are calculated in (connected question: NetLogo: measure real travel distances around obstacles before choosing destination). I've been racking my brain on this one, so any ideas are helpful. Thanks in advance!
patches-own [
benefit ;; ranges 0.1-1 and represents food available in the patch
owner ] ;; patches are owned once selected for a territory
turtles-own
[ sum-value ] ;; sum of values of patches in my territory
globals [threshold = 25]
to setup
ask patches [ set owner nobody ]
end
to go
ask turtles [build-territory]
tick
end
to build-territory
if sum-value > threshold [stop] ;; keeps picking patches for territory until I've met my threshold
pick-patch
reproduce ;; sprouts a new hatchling when done building territory
end
to pick-patch
let _destination highest-value ;; this is calculated in reporters, below
ifelse _destination != nobody [
face _destination forward 1
if patch-here = _destination ;; add the patch once reached
[ claim-patch _destination ]
if patch-here != _destination
[ if owner = nobody [claim-travel-patch ]] ;; add a travel patch while walking to selected patch
[ if owner != nobody [avoid-obstacle]] ;; or avoid the obstacle of existing territories
]
[stop] ;; if there is no _destination
end
to claim-patch [_patch]
ask _patch [set owner myself]
set sum-value sum-value + (benefit / (distance start-patch))
set pcolor [color] of self
;; etc....
end
to claim-travel-patch [_patch]
ask _patch [set owner myself]
set pcolor [yellow]
;; etc....
end
to avoid-obstacle
;; need to identify some means of going around owned patches
;; ideally this will use the shortest route possible....
;; but even if not shortest route, any ideas for identifying a new path
;; to avoid the obstacle and reach the destination patch?
end
to reproduce
if sum-value > threshold [ ask patch 75 75 [sprout 1 ] ]
end
;;;; --reporters for calculations:--
to-report highest-value ;; calculates value, benefit / cost.
let available-destinations patches with [owner = nobody]
report max-one-of available-destinations [benefit-to-me / cost-to-me]
end
to-report benefit-to-me
report mean [benefit] of patches in-radius 1 ;; this is basically a moving windows analysis to find a cluster of high-benefit patches.
end
to-report cost-to-me
report distance myself ;; later, this will hopefully account for actual travel costs (non-euclidean distance), including around other territories. I think this is complicated, so ignoring for now in this question.
end
I've been trying to write a model where turtles create links with a certain number of other turtles, and for those turtles to create links with the link-neighbors of the turtle that linked to it. Right now, I can get turtles to create links with other turtles, but they don't share the same link-neighbors. I want to end up with little sub-groups of turtles of a certain group size. However, at the moment I can only tell turtles to create a certain number of links, but they don't end up in sub-groups because their link-neighbors don't necessarily share the same link-neighbors. I thought I could ask each turtle to ask their link-neighbors to create-link-with [link-neighbors] of myself. I think this would work, except I get an error saying a turtle can't link with itself. I've tried to change the code so it says to a turtle ask link-neighbors [create-link-with [link-neighbors] of myself with [who != self]], but this doesn't work either.
Here is some of my code:
;group size is 1 + (count link-neighbors)
;preferred-size is a slider, used to alter what group size I want turtles to be in
ask turtles
[if (preferred-size > group-size) and (any? other turtles in-radius 1 with [preferred-size > group-size])
[create-link-with one-of other turtles in-radius 1 with [preferred-size > group-size]
ask link-neighbors
[create-links-with [link-neighbors] of myself]
]
Also, is there a term like link-neighbors but referring to all the turtles on a string of connections?
Any help would be greatly appreciated!
You were very close! All you need is other:
create-links-with other [ link-neighbors ] of myself
You cannot compare who to self: who is a number and self is a turtle. And in the context of with [ who != self ]], they would always be variables of the same turtle. In any case, it's usually better to avoid dealing with who numbers anyway: there's almost always a better way to do things.
is there a term like link-neighbors but referring to all the turtles on a string of connections?
I'm not entirely sure that's what you mean, but maybe nw:turtles-on-path-to? Or perhaps you will find some other useful thing in the nw extension.