I am trying to build a network model in NetLogo. Each turtle has a generation g
and she has a child
who belongs to generation g+1
and a parent who belongs to generation g-1
. Each turtle has a variable called parent
which is equal to turtle who + N
, N
is the number of turtles in a generation. So for example, if N = 10, turtle 5 is the parent of turtle 15 and turtle 3 is the parent of turtle 13.
I sorted the parent turtles according to their wealth level and divided this list into 10 sublist. In an N=20 example, the first sublist contains the poorest 2 parent turtle while the last one consists the richest two turtle. I want to build links between the two turtles who are in the same sublist and also between the children of these turtles. But there seems to be something wrong with my code. My code for connecting children is the following:
ask turtles with [generation = g] [
foreach pdist [
if member? parent ? [
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = g) and (member? parent ?)]
create-links-with n-of min (list new-links count candidates) candidates]]]]
And my code for connecting parents are:
ask turtles with [generation = g - 1] [
foreach pdist [
if member? turtle self ? [
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = g - 1) and (member? self ?)]
show candidates
create-links-with n-of min (list new-links count candidates) candidates]]]]
where C
is the number of connections I'd ideally like to build, pdist
is the lists of sublists i.e. agentsets containing parents with similar wealth levels.
I have no problem with creating plist
and assigning parents to turtles correctly, if N = 20, each sublist contains 2 parents and each turtles have a candidates
agentset containing one member. I don't know how to see that agent though. When I try show candidates
, the output is like (turtle 24): (agentset, 1 turtle)
. So my first question is how to see that 1 turtle. I guess this is where the mistake is. I suspect that (member? parent ?)
might be wrong part. If so, could somebody please tell me how to correctly write this condition?
I am providing my whole code below if you would like to reproduce it and see the results. The only function related to the question is update-links-with-seg
, you don't need to worry about the rest, and yes I am sure that the problem has nothing to do with the rest of the code :) . I'd really appreciate a lot if somebody could tell me what is wrong. This is my term paper and I am stuck here since days and my deadline is approaching. Many thanks in advance.
turtles-own
[
wealth
wage
experience
rank
age
offers
generation
bestoffer
choiceset
from_friends
from_parent
random_arrival
child
parent
]
globals [
g
r
b
al
ah
Nmax
rmax
wealth_dist
sorted_parents
con_break_prob
pdist
]
to setup
clear-all
set rmax T
set Nmax N
set al 0.2
set ah 0.8
set b 0.1
set g 0
set con_break_prob 1
set wealth_dist []
set sorted_parents[]
set pdist []
reset-ticks
create-new-generation
update-links-no-seg
end ;;to setup
to create-new-generation
crt N
[set wealth 0 set experience 0 set rank 0 set age 0 set generation g set xcor random 30 set ycor random 30 set color red]
if g != 0 [
ask turtles with [generation = g] [set parent turtle (who - N)]
ask turtles with [generation = g - 1] [set child turtle (who + N)]]
end
to update-links-no-seg
ask links [if con_break_prob < random 1 [die]]
ask turtles [
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = [generation] of myself)]
create-links-with n-of min (list new-links count candidates) candidates]]
end
to update-links-with-seg
if g != 0 [
set sorted_parents sort-on [wealth] turtles with [generation = g - 1]
set pdist []
let p 0
while [p <= 9][
let minlist (p * N) / 10
let maxlist ((p + 1) * N) / 10
let a sublist sorted_parents minlist maxlist
show a
set pdist lput a pdist
set p p + 1]
show pdist]
if C > 0 [
ifelse g = 0
[ask turtles with [generation = g][
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = g)]
create-links-with n-of min (list new-links count candidates) candidates]]]
[ ask turtles with [generation = g] [
foreach pdist [
if member? parent ? [
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = g) and (member? parent ?)]
show candidates
create-links-with n-of min (list new-links count candidates) candidates]]]]
ask turtles with [generation = g - 1] [
foreach pdist [
if member? turtle self ? [
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = g - 1) and (member? self ?)]
show candidates
create-links-with n-of min (list new-links count candidates) candidates]]]]]
ask links [show end1 show end2]]
end
to go
if stop-ticking? [stop]
if any? turtles with [age = T] [
set wealth_dist []
let sorted_turtles sort-on [who] turtles with [age = T]
foreach sorted_turtles [set wealth_dist lput [wealth] of ? wealth_dist]
file-open "wealth.csv"
file-print wealth_dist
file-close
ask turtles with [age = T] [die]]
if any? turtles with [age = T / 2 ][
set g g + 1
create-new-generation]
update-links-with-seg
ask turtles [ set offers [0] set choiceset [0] set from_friends [0] set from_parent[0] set random_arrival [0]]
set r 1
while [r <= rmax] [
ask turtles [
ifelse r <= rank[
if random-float 1 < ah [set offers lput r offers]]
[if r <= experience + 1[
if random-float 1 < al [
set offers lput r offers]]]]
set r r + 1]
ask turtles with [age < T / 2] [pass-unwanted-offers-young]
ask turtles with [age >= T / 2] [pass-unwanted-offers-old]
apply-job-and-work
ask turtles [if random-float 1 < b [set rank 0 set wage 0]]
tick
end
to pass-unwanted-offers-young
foreach offers [
ifelse (? - 1 > experience) or (? <= rank)[
if any? link-neighbors with [(? - 1 <= experience) and (? > rank)] [
ask one-of link-neighbors with [(? - 1 <= experience) and (? > rank)][set from_friends lput ? from_friends]]]
[set random_arrival lput ? random_arrival]]
end
to pass-unwanted-offers-old
foreach offers [
ifelse (? - 1 > experience) or (? <= rank) [
ifelse (? - 1 <= [experience] of child) and (? > [rank] of child)
[ask child [set from_parent lput ? from_parent]]
[if any? link-neighbors with [(? - 1 <= experience) and (? > rank)] [
ask one-of link-neighbors with [(? - 1 <= experience) and (? > rank)][set from_friends lput ? from_friends]]]]
[set random_arrival lput ? random_arrival]]
end
to apply-job-and-work
ask turtles [
;if generation > 0[
;if is-turtle? parent[
;show [wealth] of parent
;show from_parent]]
;show random_arrival
;show from_friends
set choiceset []
foreach random_arrival[set choiceset lput ? choiceset]
foreach from_parent[set choiceset lput ? choiceset]
foreach from_friends[set choiceset lput ? choiceset]]
set r 1
while [r <= rmax] [
let vacancies ((Nmax / rmax) * (1 + rmax - r)) - count turtles with [rank = r]
let applicants count turtles with [member? r choiceset]
ask n-of min(list vacancies applicants) turtles with [member? r choiceset] [set rank r]
set r r + 1]
ask turtles[
set wage exp rank
if rank > experience [set experience rank]
set wealth wealth + wage
set age (age + 1)]
end
to-report stop-ticking?
ifelse g = 3 [report true] [report false]