Netlogo:如何在海龟之间建立连接,条件是它们的变量在同一个列表中?

时间:2018-04-19 02:49:26

标签: netlogo

我正在尝试在NetLogo中构建网络模型。每只乌龟都有一代g,她有child属于代g+1,父属属于代g-1。每只乌龟都有一个名为parent的变量,它等于turtle who + NN是一代中乌龟的数量。因此,例如,如果N = 10,乌龟5是乌龟15的父母,乌龟3是乌龟13的父母。

我根据他们的财富水平对父母龟进行了分类,并将这个列表分成了10个子列表。在N = 20的例子中,第一个子列表包含最差的2个父龟,而最后一个包含最丰富的两个龟。我想建立在同一子列表中的两只乌龟之间以及这些乌龟的孩子之间的链接。但是我的代码似乎有问题。我连接孩子的代码如下:

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]]]] 

我的父母联系代码是:

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]]]]

其中C是我理想建立的连接数,pdist是子列表的列表,即包含具有相似财富水平的父母的代理集。

创建plist并正确地将父母分配给海龟没有问题,如果N = 20,则每个子列表包含2个父项,每个海龟都有一个包含一个成员的candidates代理集。我不知道怎么看那个经纪人。当我尝试show candidates时,输出就像(turtle 24): (agentset, 1 turtle)。所以我的第一个问题是如何看到那只1只乌龟。我想这就是错误所在。我怀疑(member? parent ?)可能是错误的部分。如果是这样,有人可以告诉我如何正确地写这个条件?

如果您想重现它并查看结果,我将在下面提供我的全部代码。与问题相关的唯一功能是update-links-with-seg,你不需要担心其余的问题,是的,我确信这个问题与代码的其余部分无关:)。如果有人能告诉我什么是错的,我真的很感激。这是我的学期论文,我被困在这里,因为几天和我的截止日期临近。非常感谢提前。

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]

0 个答案:

没有答案