Netlogo-聚集到agentset中的其他人(与所有海龟相反)

Netlogo-聚集到agentset中的其他人(与所有海龟相反),netlogo,Netlogo,很抱歉,这个问题可能有一个简单的答案,但我对Netlogo比较陌生,已经花了几天的时间来解决这个问题,但都没有用,所以欢迎您提出任何建议-谢谢 有一种很好的鱼类和鸟类群集方法,请参见MacLennan,2007年,基于Huth和Wissel 1992,我想采用这种群集方法,这样,您就不会群集到您最近的三个邻居,他们都是同一个群体,您只会群集到您代理集中的人,或者更具体地说,那些在你自己的小组忽略那些在它之外 我知道一个建议可能是在开始时创造两个不同的品种,例如鹿和驼鹿,然后你只会聚集到属于同一品

很抱歉,这个问题可能有一个简单的答案,但我对Netlogo比较陌生,已经花了几天的时间来解决这个问题,但都没有用,所以欢迎您提出任何建议-谢谢

有一种很好的鱼类和鸟类群集方法,请参见MacLennan,2007年,基于Huth和Wissel 1992,我想采用这种群集方法,这样,您就不会群集到您最近的三个邻居,他们都是同一个群体,您只会群集到您代理集中的人,或者更具体地说,那些在你自己的小组忽略那些在它之外

我知道一个建议可能是在开始时创造两个不同的品种,例如鹿和驼鹿,然后你只会聚集到属于同一品种的品种。然而,在我的示例中,我有代理在每次勾选时重新评估哪个组最适合归属,并在通过代理集定义的组之间切换

针对同质人群的原始代码MacLennan,2007,如下所示:

TO GO ;--------------------------------------------------------------------------------------------

set i 0                                            ; initializes variables used in finding neighbors
set distance_1 1000
set distance_2 1000
set distance_3 1000

   ; (below) determines ID numbers and distances for three closest neighbors; each turtle is
   ;   first checked to be visible (i.e., out of blind spot and in front of obstacles), and then
   ;   its distance is compared with that of neighbor_3.  If a bird is closer than neighbor_3,
   ;   it is then compared with neighbor_2 and (if it's closer than neighbor_2) with neighbor_1;
   ;   variables are then re-assigned to accomodate the new neighbor as one of the closest three.

repeat count turtles
  [ifelse not ((i = who) or in_blind_spot? or behind_opaque_obstacle?)
     [set dist distance turtle i
      ifelse dist < distance_3
        [ifelse dist < distance_2
           [set neighbor_3 neighbor_2
            set distance_3 distance_2
            ifelse dist < distance_1
              [set neighbor_2 neighbor_1
               set distance_2 distance_1
               set neighbor_1 i
               set distance_1 dist]               
              [set neighbor_2 i
               set distance_2 dist
               repeat 2 [fd 0]]]                 ; (see note at end for explanantion of "fd 0")
           [set neighbor_3 i
            set distance_3 dist
            repeat 4 [fd 0]]]
        [repeat 6 [fd 0]]]
     [repeat 7 [fd 0]]
   set i i + 1]

set acceleration 0                                ; sets acceleration to zero

set target_1 calc_target neighbor_1 distance_1    ; calculates targets (see procedure below) based on
set target_2 calc_target neighbor_2 distance_2    ;   the headings and positions of each neighbor;
set target_3 calc_target neighbor_3 distance_3    ;   acceleration is set by the calc_target procedure

   ; (below) calculates a bird's composite target, weighting each individual target by the inverse
   ;   of distance to that neighbor

set target atan ((sin target_1) * 100 / distance_1 + (sin target_2) * 100 / distance_2 + (sin target_3) * 100 / distance_3)
                ((cos target_1) * 100 / distance_1 + (cos target_2) * 100 / distance_2 + (cos target_3) * 100 / distance_3)

   ; (below) turns bird towards target by a fraction specified by "flexibility"; random motion is also
   ;   included to an extent governed by "noise".  The "... + 540) mod 360) - 180" clause is necessary to
   ;   ensure that the bird turns in the right direction.  For example, if a bird is oriented at 0 degrees
   ;   and its target is 300 degrees, it should consider a 60 [ = ((0 - 300 + 540) mod 360) - 180] degree
   ;   turn to the left instead of a 300 [ = 300 - 0] degree turn to the right.

fd 1 + acceleration                               ; moves birds forward

end
   to flock
set id-within-group 0                                            ; initializes variables used in finding neighbors
set distance_1 1000
set distance_2 1000
set distance_3 1000

set group-members turtle-set turtles with [group-id = [group-id] of myself] ; set those sharing a group id as my group-members
 (foreach (sort group-members) (n-values count group-members [?]) [ ; set each turtle in my agentset (group) with an ID no. 0 - n?
    ask ?1 [ set id-within-group ?2 ]
  ])
repeat count group-members ; repeat the next step as many times as there are group members
  [ifelse not (id-within-group = who)
     [
       set dist distance turtle id-within-group
       print "turtle id-within-group"
       print turtle id-within-group
       print dist
      ifelse dist < distance_3
        [ifelse dist < distance_2
           [set neighbor_3 neighbor_2
            set distance_3 distance_2
            ifelse dist < distance_1
              [set neighbor_2 neighbor_1
               set distance_2 distance_1
               set neighbor_1 id-within-group
               set distance_1 dist]               
              [set neighbor_2 id-within-group
               set distance_2 dist
               repeat 2 [fd 0]]]                 
           [set neighbor_3 id-within-group
            set distance_3 dist
            repeat 4 [fd 0]]]
        [repeat 6 [fd 0]]]
     [repeat 7 [fd 0]]
   print "_____________________"
   print "ID" print self
   print "group-id" print group-id
   ]

;]
set acceleration 0                                ; sets acceleration to zero

set target_1 calc_target neighbor_1 distance_1    ; calculates targets (see procedure below) based on
set target_2 calc_target neighbor_2 distance_2    ;   the headings and positions of each neighbor;
set target_3 calc_target neighbor_3 distance_3    ;   acceleration is set by the calc_target procedure


   ; (below) calculates a bird's composite target, weighting each individual target by the inverse
   ;   of distance to that neighbor

set target atan ((sin target_1) * 100 / distance_1 + (sin target_2) * 100 / distance_2 + (sin target_3) * 100 / distance_3)
                ((cos target_1) * 100 / distance_1 + (cos target_2) * 100 / distance_2 + (cos target_3) * 100 / distance_3)

   ; (below) turns bird towards target by a fraction specified by "flexibility"; random motion is also
   ;   included to an extent governed by "noise".  The "... + 540) mod 360) - 180" clause is necessary to
   ;   ensure that the bird turns in the right direction.  For example, if a bird is oriented at 0 degrees
   ;   and its target is 300 degrees, it should consider a 60 [ = ((0 - 300 + 540) mod 360) - 180] degree
   ;   turn to the left instead of a 300 [ = 300 - 0] degree turn to the right.

lt (flexibility / 100 * (((heading - target + 540) mod 360) - 180)) + (random noise) - (random noise)

fd 1 + acceleration                               ; moves birds forward

end
“海龟”指的是世界上任何一只海龟,而不仅仅是我们这群海龟

为了做到这一点,我考虑将我的代理集“group members”临时转换成一个品种,这样我就可以用GroupMemberi替换turtle I这个词。。e、 g

create-groupies-from group-members
[set dist distance group-member i
然而,当我尝试使用create-from代码时,我收到了一个错误,即“没有定义任何名为create-GROUPIES-from的内容,即使这个品种已经或没有包含在品种[]中

因此,我尝试修改代码只考虑群组成员的分组:

TO GO ;--------------------------------------------------------------------------------------------

set i 0                                            ; initializes variables used in finding neighbors
set distance_1 1000
set distance_2 1000
set distance_3 1000

   ; (below) determines ID numbers and distances for three closest neighbors; each turtle is
   ;   first checked to be visible (i.e., out of blind spot and in front of obstacles), and then
   ;   its distance is compared with that of neighbor_3.  If a bird is closer than neighbor_3,
   ;   it is then compared with neighbor_2 and (if it's closer than neighbor_2) with neighbor_1;
   ;   variables are then re-assigned to accomodate the new neighbor as one of the closest three.

repeat count turtles
  [ifelse not ((i = who) or in_blind_spot? or behind_opaque_obstacle?)
     [set dist distance turtle i
      ifelse dist < distance_3
        [ifelse dist < distance_2
           [set neighbor_3 neighbor_2
            set distance_3 distance_2
            ifelse dist < distance_1
              [set neighbor_2 neighbor_1
               set distance_2 distance_1
               set neighbor_1 i
               set distance_1 dist]               
              [set neighbor_2 i
               set distance_2 dist
               repeat 2 [fd 0]]]                 ; (see note at end for explanantion of "fd 0")
           [set neighbor_3 i
            set distance_3 dist
            repeat 4 [fd 0]]]
        [repeat 6 [fd 0]]]
     [repeat 7 [fd 0]]
   set i i + 1]

set acceleration 0                                ; sets acceleration to zero

set target_1 calc_target neighbor_1 distance_1    ; calculates targets (see procedure below) based on
set target_2 calc_target neighbor_2 distance_2    ;   the headings and positions of each neighbor;
set target_3 calc_target neighbor_3 distance_3    ;   acceleration is set by the calc_target procedure

   ; (below) calculates a bird's composite target, weighting each individual target by the inverse
   ;   of distance to that neighbor

set target atan ((sin target_1) * 100 / distance_1 + (sin target_2) * 100 / distance_2 + (sin target_3) * 100 / distance_3)
                ((cos target_1) * 100 / distance_1 + (cos target_2) * 100 / distance_2 + (cos target_3) * 100 / distance_3)

   ; (below) turns bird towards target by a fraction specified by "flexibility"; random motion is also
   ;   included to an extent governed by "noise".  The "... + 540) mod 360) - 180" clause is necessary to
   ;   ensure that the bird turns in the right direction.  For example, if a bird is oriented at 0 degrees
   ;   and its target is 300 degrees, it should consider a 60 [ = ((0 - 300 + 540) mod 360) - 180] degree
   ;   turn to the left instead of a 300 [ = 300 - 0] degree turn to the right.

fd 1 + acceleration                               ; moves birds forward

end
   to flock
set id-within-group 0                                            ; initializes variables used in finding neighbors
set distance_1 1000
set distance_2 1000
set distance_3 1000

set group-members turtle-set turtles with [group-id = [group-id] of myself] ; set those sharing a group id as my group-members
 (foreach (sort group-members) (n-values count group-members [?]) [ ; set each turtle in my agentset (group) with an ID no. 0 - n?
    ask ?1 [ set id-within-group ?2 ]
  ])
repeat count group-members ; repeat the next step as many times as there are group members
  [ifelse not (id-within-group = who)
     [
       set dist distance turtle id-within-group
       print "turtle id-within-group"
       print turtle id-within-group
       print dist
      ifelse dist < distance_3
        [ifelse dist < distance_2
           [set neighbor_3 neighbor_2
            set distance_3 distance_2
            ifelse dist < distance_1
              [set neighbor_2 neighbor_1
               set distance_2 distance_1
               set neighbor_1 id-within-group
               set distance_1 dist]               
              [set neighbor_2 id-within-group
               set distance_2 dist
               repeat 2 [fd 0]]]                 
           [set neighbor_3 id-within-group
            set distance_3 dist
            repeat 4 [fd 0]]]
        [repeat 6 [fd 0]]]
     [repeat 7 [fd 0]]
   print "_____________________"
   print "ID" print self
   print "group-id" print group-id
   ]

;]
set acceleration 0                                ; sets acceleration to zero

set target_1 calc_target neighbor_1 distance_1    ; calculates targets (see procedure below) based on
set target_2 calc_target neighbor_2 distance_2    ;   the headings and positions of each neighbor;
set target_3 calc_target neighbor_3 distance_3    ;   acceleration is set by the calc_target procedure


   ; (below) calculates a bird's composite target, weighting each individual target by the inverse
   ;   of distance to that neighbor

set target atan ((sin target_1) * 100 / distance_1 + (sin target_2) * 100 / distance_2 + (sin target_3) * 100 / distance_3)
                ((cos target_1) * 100 / distance_1 + (cos target_2) * 100 / distance_2 + (cos target_3) * 100 / distance_3)

   ; (below) turns bird towards target by a fraction specified by "flexibility"; random motion is also
   ;   included to an extent governed by "noise".  The "... + 540) mod 360) - 180" clause is necessary to
   ;   ensure that the bird turns in the right direction.  For example, if a bird is oriented at 0 degrees
   ;   and its target is 300 degrees, it should consider a 60 [ = ((0 - 300 + 540) mod 360) - 180] degree
   ;   turn to the left instead of a 300 [ = 300 - 0] degree turn to the right.

lt (flexibility / 100 * (((heading - target + 540) mod 360) - 180)) + (random noise) - (random noise)

fd 1 + acceleration                               ; moves birds forward

end
在我修改后的示例中,虽然这些组确实正确地分配了单个ID组成员,但它们不会蜂拥而至。此外,邻居、距离和目标_1-_3都具有相同的值,这是错误的


我想知道是否有人对我所遇到的一些问题有任何见解?任何解决方案或相关文献都将不胜感激-谢谢

首先,让你知道,海龟可以通过简单地要求相关海龟将其品种属性设置为新品种来改变品种:询问。。。[设置繁殖…]有关示例,请参阅NetLogo字典中的繁殖原语

尽管如此,我认为您使用“group”属性并使用agentsets做您想做的事情是正确的。这有点像你想要的,但并不正确。希望其他人能够改进它

turtles-own
[ groupID ]

to setup

  create-turtles 20
  [ set groupID random 2
    set xcor random-xcor
    set ycor random-ycor
  ]

  ask turtles
  [ let close-friends min-n-of 3 turtles with [groupID = [groupID] of myself] [distance myself]
    ask close-friends
    [ face myself
      forward 1
    ]
  ]

end

我认为这段代码可以在同一组中找到三只最近的海龟。然而,它将这三个移动到询问的海龟,而不是将询问的海龟移动到这三个。如果不使用列表,我无法找到另一种方法。

好的,这里有一个更正的版本,我没有编辑上面的内容,因为这会把评论搞得一团糟。仍然没有错误检查,以确保组中还有3个其他成员等,但这使用列表来绕过第一个建议中的问题。它创建agentset close friends以包含三个最近的好友,这是一行,而不是所有嵌套的距离检查。该agentset被转换为列表,然后代码在列表中循环并移向列表成员。我仍然认为一定有更好的方式直接通过agentset进行循环,但至少这是可行的

turtles-own
[ groupID ]

to setup
  clear-all
  create-turtles 20
  [ set groupID random 2
    set xcor random-xcor
    set ycor random-ycor
    set color 155 - 10 * groupID
  ]
  reset-ticks
end

to go
  ask turtles
  [ let close-friends min-n-of 3 turtles with [groupID = [groupID] of myself] [distance myself]
    let friend-list sort-on [distance myself] close-friends
    foreach friend-list
    [ face ?
      forward 1
    ]
  ]
  tick
end

嗨,詹布。非常感谢你的回复——不幸的是,正如你正确地陈述自己的观点一样,你的建议并不能帮助我解决目前的问题。不过,非常感谢你的答复。我仍在不顾一切地寻求解决这个问题的办法。我绝对喜欢MacLennan,2007植绒方法,我的总体目标是尽可能忠实于此代码,同时植绒于我代理集中的代码-因此,如果其他人有任何建议,请让我知道,但谢谢JenB.Hi。谢谢你回复我,不过问题有些不同。我不想像你建议的方法那样将代理分组,而是希望我的代理组代理集对齐、连贯和分离,就像我上面引用的MacLennan模型中的一群成群的鸟一样。对McLeNeN植绒方法的唯一改变,我想做的是在计算群集标题/距离时考虑自己的代理集,而不是考虑所有的海龟而不考虑群组成员。我希望这是有意义的,并且感谢MacLennan模型中的群集行为,您需要以相同的方式计算如何移动-所有关于加速和距离的内容。这与从同一组中选择最近的3无关,而不是从整体上选择最近的3无关,因此我没有将其包含在示例代码中,我只是让它们移动,以便您可以看到它是有效的。