I have an "ifelse" command like this:
ifelse [label] of [x] of end1 < [label] of [x] of end2
[ask [x] of end1 [hide-link]]
[ask [x] of end2 [hide-link]]
x is defined as such:
set x max-one-of my-out-links [label]
The purpose of this code is: Out of two opposite links, have the link with the lower value hidden. But, no matter the changes I make to the operator (change it to ">") in proportionate to changes I also make to end1 and end2 to keep the same logic, it always hides the link with higher value.
This is the entire code in the event that the problem isn't in the code provided above.
ask turtles
[
set x max-one-of my-out-links [trust]
set y one-of my-in-links
]
ask links
[
set test1 [[x] of end1] of self
set test2 [[y] of end2] of self
]
set max-links sort-on [(- label)] link-set [max-one-of my-out-links [label]] of turtles
foreach max-links
[
the-links -> ask the-links
[
if [[who] of end1 ] of test1 = [[who] of end2] of test2
and [[who] of end2] of test1 = [[who] of end1] of test2
[
ifelse [trust] of [x] of end1 < [trust] of [x] of end2
[ask [x] of end1 [hide-link]]
[ask [x] of end2 [hide-link]]
]
]
]
Thank you.
Related
I want to check if target patches fulfil a condition. If a patch is found that fulfils the condition,
then the turtles should move there. If 'nobody' fulfils this condition, then an error message should be printed.
The condition is that a patch should have in radius 10 2 turtles of the same breed.
I try to achieve this with ifelse and nobody. However, at the moment I always get the error message, even though the
target variable is not empty (you can check this with the if loop).
breed [ breed1s breed1 ]
breed [ breed2s breed2 ]
globals [target1 target2]
to setup
ca
create-breed1s 1000 [
setxy random-xcor random-ycor
]
create-breed1s 1000 [
setxy random-xcor random-ycor
]
end
to go
ask turtles [
set target1 ( count turtles in-radius 10 with [breed = breed1s] ) >= 2
set target2 ( count turtles in-radius 10 with [breed = breed2s] ) >= 2
new-position
]
end
to new-position
ifelse target1 != nobody
[ if (breed = breed1s) [ move-to one-of patches with [ target1 ] ] ]
[ print "Not enough agents in the neighborhood" ]
ifelse target2 != nobody
[ if (breed = breed2s) [ move-to one-of patches with [ target2 ] ] ]
[ print "Not enough agents in the neighborhood" ]
; if (breed = breed1s)
; [ move-to one-of patches with [ target1 ] ]
end
A remark to the efficiency of the model: as I want to add turtles later in every tick, target has to be reevaluated
in every tick (therefore it is in "go" and not in "setup").
Another question: is there a possibility to do something like [ breed = myself ] instead of [ breed = breed1s ], so
I don't have to type the breed for every breed?
Edit:
the turtles that move to the target patch should have the same breed that is also addressed in the target patch.
The problem is actually how you are creating target1, not the check as to whether it is nobody. You have:
set target1 ( count turtles in-radius 10 with [breed = breed1s] ) >= 2
This line first identifies all the nearby turtles with the appropriate breed and counts them. If the count is 2 or higher, then the variable target1 is set to true and to false if the count is 0 or 1. So you are comparing a boolean true or false to nobody (a special type of agent). That will always be a mismatch and therefore print the error.
Just a note on debugging - when you get this sort of problem, it's always useful to have a print statement for each side of the check just before doing the check. You would have immediately spotted that target1 wasn't what you thought it was.
Since you are asking to move to one-of the patches, you probably really want to store the available patches that are within 10 distance (I think) and have enough of the right type of turtles. So, you need something like:
to go
ask turtles [
set target1 patches in-radius 10 with [count breed1s-here >= 2]
set target2 patches in-radius 10 with [count breed2s-here >= 2]
new-position
]
end
Then your emptiness test is any?
to new-position
ifelse any? target1
[ move-to one-of target1 ]
[ print "Not enough agents in the neighborhood" ]
ifelse any? target2
[ move-to one-of target2 ]
[ print "Not enough agents in the neighborhood" ]
end
Assuming I have correctly interpreted that you want patches within 10 of the asking turtle (as compared to any patch with sufficient turtles within a distance of 10) and all you care about is the number of turtles of its own breed, then:
to go
ask turtles [
let target-breed [breed] of myself
set targets patches in-radius 10 with [count turtles-here with [breed = target-breed] >= 2]
new-position
]
end
to new-position
ifelse any? targets
[ move-to one-of targets ]
[ print "Not enough agents in the neighborhood" ]
end
On the efficiency, it depends on how many turtles you have. If you have quite a lot of turtles, then asking each to count its own neighbourhood will be expensive. Instead, you could have patchsets for each breed. That is, set up target1 as patches with [count breed1s-here >= 2] at the beginning of the go procedures. Then you can just do:
to go
let targets1 patches with [count breed1s-here >= 2]
let targets2 patches with [count breed2s-here >= 2]
ask turtles
[ set targets targets1 in-radius 10
new-position
]
end
However, you can no longer use the breed of the turtle and the myself trick to pick the correct patchset. There are ways to get around this (for example, using a list with two items, breed at first position and the patchset as second position) but that's getting well off track for this answer.
to new-position
ifelse any? targets
[ move-to one-of targets ]
[ print "Not enough agents in the neighborhood" ]
end
I have a complete directed weighted graph. Also, I have a max-links list where the maximum out-link of all turtles are in there. But sometimes two turtles give opposite links as their maximum out-links such as link (2 3) and link (3 2). I've managed to delete the link with the lower value and update the list
ask turtles
[
set x max-one-of my-out-links [trust]
set y one-of my-in-links
]
ask links
[
set test1 [[x] of end1] of self
set test2 [[y] of end2] of self
foreach max-links
[
the-links -> ask the-links
[
if [[who] of end1 ] of test1 = [[who] of end2] of test2
and [[who] of end2] of test1 = [[who] of end1] of test2
[
set max-links sort-on [(- label)] link-set [max-one-of my-out-links [label]] of turtles
ifelse [trust] of [x] of end1 < [trust] of [x] of end2
[ask [x] of end1 [die]]
[ask [x] of end2 [die]]
if one-of max-links = nobody
[set max-links sort-on [(- label)] link-set [max-one-of my-out-links [label]] of turtles]
show max-links
]
]
set max-links sort-on [(- label)] link-set [max-one-of my-out-links [label]] of turtles
]
However, when the if turns out true, it does indeed delete the link with the lower value but it gives the error of "that link is dead"
and shows max-links with a nobody in the list:
[(link 0 5) nobody (link 4 5) (link 2 4) (link 1 0) (link 3 2)]
I tried to get rid of the nobody with this section:
if one-of max-links = nobody
[set max-links sort-on [(- label)] link-set [max-one-of my-out-links [label]] of turtles]
show max-links
but it has no effect.
When an agent or link dies, it is removed from any turtle-set or link-set in which contained it, but references to the now dead link in lists or other variables are set to nobody. So, your code is doing exactly what one would expect. If you want to remove it from the list, then JenB's suggestion is the way to go. If you want to delete dead links from max-links, then
set max-links filter is-link? max-links
'nobody' is not a link, so it will be filtered out. Then, your if should work.
I am writing simulation where i am trying to simlate recruiting process for terrorost organization. In this model turtles have groups of friends i.e other turtles they are connected to with links. The model includes the forming of new bonds(links) with turtles they meet if their world view is similar and is supposed to have a mechanism for disconectiong from friends with world view most different from them among their friends.
Tried to solve the issue with following block of code which does not seem to work properly, often get the error message
"OF expected input to be a turtle agentset or turtle but got NOBODY instead."
related to value of friend_dif
ask turtles with [(connections > 0) and (color = blue)][
let friends_inverse ( 1 / connections )
if friends_inverse > random-float 1[
let friend_dif abs([world_view] of self - [world_view] of one-of other link-neighbors)
ask max-one-of links [friend_dif][
die
]
]
set connections count link-neighbors
]
Below is the whole code for the mentioned simulation. The aim is to comparetwo strategies one where recriters focus on turtles with most radical world view, the second where they first targets the most central turtles in the net.
turtles-own [connections world_view]
to setup
ca
crt potential_recruits [setxy random-xcor random-ycor set color blue]
ask turtles with [color = blue][
let przypisania random max_start_recruits_connections
;; 0-0.4 non interested, 0.4-0.7 moderate, 0.7-0.9 symphatizing, >0.9 radical - can be recrouted
set world_view random-float 1
if count my-links < 10 [
repeat przypisania [
create-link-with one-of other turtles with [(count link-neighbors < 10) and (color = blue)]
]
]
show link-neighbors
set connections count link-neighbors
]
crt recruiters [setxy random-xcor random-ycor set color orange]
ask turtles with [color = orange][
set world_view 1
if strategy = "world view"[
recruit_view
]
if strategy = "most central"[
recruit_central
]
]
;;show count links
reset-ticks
setup-plots
update-plots
end
to go
;;creating new links with turtles they meet and movement which is random
ask turtles [
rt random-float 360
fd 1
if any? other turtles-here[
let world_view1 [world_view] of one-of turtles-here
let world_view2 [world_view] of one-of other turtles-here
let connection_chance abs(world_view1 - world_view2)
if connection_chance <= 0.2 [
;;show connection_chance
create-links-with other turtles-here
]
]
;;show link-neighbors
set connections count link-neighbors
]
;;how recruiting works in this model
ask turtles with [world_view > 0.9][
if count in-link-neighbors with [color = orange] > 0[
set color orange
set world_view 1
]
]
;; friend's influence on turtles
ask turtles with [(count link-neighbors > 0) and (color = blue)][
let friends_view (sum [world_view] of link-neighbors / count link-neighbors)
let view_dev (friends_view - world_view)
;;show world_view show view_dev
set world_view world_view + (view_dev / 2)
]
;; removing turtles from with most different opinion from our colleagues
ask turtles with [(connections > 0) and (color = blue)][
let friends_inverse ( 1 / connections )
if friends_inverse > random-float 1[
let friend_dif abs([world_view] of self - [world_view] of one-of other link-neighbors)
ask max-one-of links [friend_dif][
die
]
]
set connections count link-neighbors
]
;show count links
tick
update-plots
end
to recruit_view
ask max-n-of start_recruiters_connections turtles with [ color = blue][world_view][
repeat start_recruiters_connections[
create-link-with one-of other turtles with [ color = orange]
]
]
ask turtles with [color = orange][
set connections count link-neighbors
]
end
to recruit_central
ask max-n-of start_recruiters_connections turtles with [ color = blue][count my-links][
repeat start_recruiters_connections[
create-link-with one-of other turtles with [ color = orange]
]
]
ask turtles with [color = orange][
set connections count link-neighbors
]
end
to batch
repeat 50 [
go
]
end
Your problem is that you aren't switching contexts (that is, whether the code is 'currently' in the perspective of a turtle or a link) correctly.
You start with ask turtles - pretend you are now the first turtle being asked. First a value is calculated and then compared to a random number - assume that the if is satisfied. The code is still in the turtle context, so the code inside the [] is applied to this first turtle.
The code creates a variable called friend_dif and assigns its value as the difference in worldviews between itself and one randomly selected network neighbours. In your code, you then have max-one-of links [friend_dif]. However, that only selects the link with the maximum value of friend_dif if (1) friend_dif is a links-own attribute and (2) the value of friend_dif has been set for all links. Neither is true. Furthermore, by asking for max-one-of links [friend_dif], you are asking for the link with the highest value from all links in the model, not just the ones with the turtle of interest at one end.
So you need to get your turtle to calculate the difference for all its link-neighbors and then switch contexts to the link that connects the two turtles, before asking that link to die.
This is not tested. What it is supposed to do is identify the network neighbour that returns the biggest difference in worldview values and then use the name of the link (which is given by the two ends) to ask it to die.
ask turtles with [ count my-links > 0 and color = blue]
[ if random-float 1 < 1 / count my-links
[ let bigdif max-one-of link-neighbours [abs ([worldview] - [worldview] of myself)
ask link self bigdif [die]
]
]
Alternatively (and easier to read), you can create a link attribute that stores the value of the differences in worldviews (called dif below), then do something like:
ask links [ set dif abs ([worldview] of end1 - [worldview] of end2) ]
ask turtles with [ count my-links > 0 and color = blue]
[ if random-float 1 < 1 / count my-links
[ ask max-one-of my-links [dif] [die]
]
]
my model
of netlogo
globals [ mejor-recorrido
coste-mejor-recorrido ]
breed [ nodos nodo ]
breed [ hormigas hormiga ]
links-own [ coste
feromona ]
hormigas-own [ recorrido
coste-recorrido ]
to setup
__clear-all-and-reset-ticks
set-default-shape nodos "circle"
ask patches [set pcolor white]
crea-nodos
crea-aristas
crea-hormigas
set mejor-recorrido camino-aleatorio
set coste-mejor-recorrido longitud-recorrido mejor-recorrido
end
to crea-nodos
ask n-of num-nodos patches
[
sprout-nodos 1
[
set color blue + 2
set size 2
set label-color black
set label (word "Ciudad-" who)
]
]
end
to crea-aristas
ask nodos
[
create-links-with other nodos
[
; hide-link
set color red
set coste link-length
set feromona random-float 0.1
]
]
ask links [
if(feromona < 0.05) [die] ]
let max-coste max [coste] of links
ask links
[
set coste coste / max-coste
]
end
to crea-hormigas
create-hormigas num-hormigas [
hide-turtle
set recorrido []
set coste-recorrido 0
]
end
to reset
ask hormigas [die]
ask links [
hide-link
set feromona random-float 0.1
]
crea-hormigas
set mejor-recorrido camino-aleatorio
set coste-mejor-recorrido longitud-recorrido mejor-recorrido
clear-all-plots
end
to go
no-display
ask hormigas [
set recorrido generar-recorrido
set coste-recorrido longitud-recorrido recorrido
if coste-recorrido < coste-mejor-recorrido [
set mejor-recorrido recorrido
set coste-mejor-recorrido coste-recorrido
]
]
actualiza-feromona
tick
display
end
to-report camino-aleatorio
let resp [self] of nodos
report lput (first resp) resp
end
to-report generar-recorrido
let origen one-of nodos
let nuevo-recorrido (list origen)
let resto-nodos [self] of nodos with [self != origen]
let nodo-actual origen
while [not empty? resto-nodos] [
if (self = origen) [
ask hormigas [die]
]
let siguiente-nodo elige-siguiente-nodo nodo-actual resto-nodos
set nuevo-recorrido lput siguiente-nodo nuevo-recorrido
set resto-nodos remove siguiente-nodo resto-nodos
set nodo-actual siguiente-nodo
]
set nuevo-recorrido lput origen nuevo-recorrido
report nuevo-recorrido
end
to-report elige-siguiente-nodo [nodo-actual resto-nodos]
let probabilidades calcula-probabilidades nodo-actual resto-nodos
let rand-num random-float 1
report last first filter [first ? >= rand-num] probabilidades
end
to-report calcula-probabilidades [nodo-actual resto-nodos]
let pt map [([feromona] of ? ^ alpha) * ((1 / [coste] of ?) ^ beta)]
(map [arista nodo-actual ?] resto-nodos)
let denominador sum pt
set pt map [? / denominador] pt
let probabilidades sort-by [first ?1 < first ?2]
(map [(list ?1 ?2)] pt resto-nodos)
let probabilidad-normalizada []
let ac 0
foreach probabilidades [
set ac (ac + first ?)
set probabilidad-normalizada lput (list ac last ?) probabilidad-normalizada
]
report probabilidad-normalizada
end
to actualiza-feromona
;; Evapora la feromona del grafo
ask links [
set feromona (feromona * (1 - rho))
]
ask hormigas [
let inc-feromona (100 / coste-recorrido)
foreach aristas-recorrido recorrido [
ask ? [ set feromona (feromona + inc-feromona) ]
]
]
end
to-report aristas-recorrido [nodos-recorrido]
report map [arista (item ? nodos-recorrido)
(item (? + 1) nodos-recorrido)] (n-values num-nodos [?])
end
to-report arista [n1 n2]
report (link [who] of n1 [who] of n2)
end
to-report longitud-recorrido [nodos-recorrido]
report reduce [?1 + ?2] map [[coste] of ?] (aristas-recorrido nodos-recorrido)
end
I added
print aristas-recorrido nodos-recorrido
as the first line after to-report longitud-recorrido [nodos-recorrido]. This shows that aristas-recorrido nodos-recorrido returns a list in which some of the elements are nobody. That's what's processed by one of the lines that is generating the error you mentioned.
I believe that the problem is that in the definition of aristas-recorrido,
to-report aristas-recorrido [nodos-recorrido]
report map [arista (item ? nodos-recorrido)
(item (? + 1) nodos-recorrido)]
(n-values num-nodos [?])
end
arista attempts to report the link between subsequent nodo turtles in the list nodos-recorrido, but some of the pairs of nodos are not linked.
It looks to me like the procedure crea-aristas links every nodo with every other nodo, but then removes links with feromona < 0.05. After that, not all nodos are linked. Since nodos-recorrido above is just a randomly-ordered list of nodos (is this right?), some of the pairs of nodos are not linked, and thus arista returns nobody rather than a link for some pairs. Then this leads to the error in longitud-recorrido.
(I didn't investigate the other line that's generating the error, but I think you'll have enough information to track down that error now.)
I am growing animal territories. Animal territories may cleave parts of other animal territories during the process of expand. So instead of being one contiguous territory, the territory may include more than one cluster (i.e., unattached clusters). This is what happens in the model below. I'd like to have the territory recognize this and release whichever cluster of cells (or a single unattached cell) is smallest so that the territory remains one contiguous cluster. I'm not sure where to start with this. Any help would be great.
breed [animals animal]
breed [homeranges homerange]
animals-own
[
Name
orig
territory
food
status
]
patches-own
[
owner
prey
]
to setup
clear-all
random-seed 2234
ask patches
[
set owner nobody
set prey 2
set pcolor scale-color (black) prey 1 4
]
let $colors [brown orange violet sky lime]
let $Name ["t6" "t7" "t8" "t9" "t10"]
let $status [0 0 0 0 5]
ask n-of 5 patches
[
sprout-animals 1
[
set shape "circle"
set orig patch-here
set territory patch-set orig
set status item who $status
set size 0.3 + 0.1 * status
set color item who $colors
set pcolor color
set Name item who $Name
set owner self
]
]
reset-ticks
end
to go
if all? animals [food >= 350] [ stop ]
if ticks = 70 [ stop ]
expand
tick
end
to expand ; animals procedure
repeat 10
[
ask animals
[
let vacant no-patches
let subord no-patches
let target nobody
let new-patches no-patches
let status-of-calling-tiger status ;
let calling-tiger self ;
; If territory not yet good enough:
if food < 500
[
ask territory
[
; Add unoccupied neighbor patches as potential targets:
set vacant (patch-set vacant neighbors with [owner = nobody])
; Add occupied neighbor patches as potential targets if their tiger has a lower status than me:
set subord (patch-set subord neighbors with [owner != nobody and [status] of owner < status-of-calling-tiger])
]
ask subord [ set pcolor red ]
; Set of all potential targets:
set new-patches (patch-set new-patches vacant subord)
; Choose as target the one potential target with highest prey:
if any? new-patches
[
ask new-patches
[ifelse any? vacant
[ifelse any? subord
[ifelse [prey] of max-one-of vacant [prey] = [prey] of max-one-of subord [prey]
[set target max-one-of vacant [prey]]
[set target max-one-of new-patches [prey]]
]
[set target max-one-of vacant [prey]]
]
[set target max-one-of subord [prey]]
]
move-to target
if-else member? target subord
[ set shape "triangle" ] ; so you can see that the target patch was from "subord"
[ set shape "circle" ] ; or from "vacant"
]
;ifelse any? target with [owner != nobody]
if target != nobody
[
; Add target patch to territory of the current animal:
set territory (patch-set territory target) ; this is the territory of the calling tiger
let old-owner [owner] of target; this needs to be memorized
; Tell target patch that is has new owner:
ask target [ set owner calling-tiger ]
; Tell the original owner of the target patch to remove the target patch from its territory:
if old-owner != nobody ;
[
ask old-owner
[
set territory territory with [ owner != calling-tiger ]
]
]
]
set food sum [prey] of territory
]
]
]
ask animals
[
ask territory
[
set pcolor [color] of myself
set plabel (word [status] of owner [status] of myself)
]
if food < 10 [die]
]
end
Patch Clusters Example, in the Code Examples section of NetLogo's Models Library, has code for identifying a contiguous cluster of patches. The core code is as follows:
patches-own [cluster]
to setup
...
ask patches [ set cluster nobody ]
...
end
to grow-cluster ;; patch procedure
ask neighbors4 with [(cluster = nobody) and
(pcolor = [pcolor] of myself)]
[ set cluster [cluster] of myself
grow-cluster ]
end
But see the rest of the example as well.
In your use case, instead of placing the "seeds" randomly for growing the clusters, you'll start growing the clusters in the patches where the animals are standing.