限制代理可以创建的链接数 V2

Limit the number of links an agent can make V2

我知道,这里也是。问题是......我一直在尝试实施它,但它似乎对我不起作用。我的模拟是 运行,但即使 agentessight-radius 处面对 activities,也进行了 0 links。这是代码:

to participate
  ask activities [
    if count my-links < n-capacity [
      ask other agentes in-radius sight-radius with
      [shared-culture = [shared-culture] of other agentes] [
        create-participations-with n-of n-capacity agentes
        ]
        ask links [
          set color blue]
      ]
    ]
end

如果代码不够清晰,我希望活动能够: 1- 知道他们可以拥有多少 agentes。 2- 如果他们有相同的 shared-culture 并且他们是 in-radius,则接受他们。 3- 用蓝色链接表示此 "acceptance" 和 "participation"。

我尝试了与 while 类似的操作,但结果为 0。

如果没有看到更多您的代码,很难说,但有一些问题可能会导致您的问题。根据您的总结,我的理解是您本质上希望 activities 代理在 sight-radius 内与 agentes 形成链接,最多达到 n-capacity 定义的数量。但是,在您的代码中,您正在检查 if count my-links < n-capacity 活动,让其他 代理人 在 activity 的视野范围内创建与其他 [=38] 的参与链接=]agents 而不是原来的 activity agent,我知道你想要一些链接。

假设 n-capacity 是一个 activities-own 变量,您只需切换

就可以更接近您想要的结果
ask other agentes in-radius sight-radius with
      [shared-culture = [shared-culture] of other agentes] [
        create-participations-with n-of n-capacity agentes
        ]

与 [shared-culture = [shared-culture] myself] [create-participation-with myself] 询问 n-of n-capacity agents in-radius sight-radius

编辑:忘了原来的of

ask n-of n-capacity agentes in-radius sight-radius with
  [shared-culture = [shared-culture] of myself] [
    create-participation-with myself
    ]

但是,由于我没有您的设置和其他代码,因此无法对此进行测试,因此我将向您展示一个我知道有效并且 可能 的不同示例成为你所追求的。下面是所有需要的代码,为 setup 制作一个按钮,为 go 制作一个永久按钮,然后观察狼慢慢地与具有相同颜色的绵羊代理建立最多三个链接:

breed [ wolves wolf ]
breed [ sheeps sheep ]
undirected-link-breed [ participations participation ]

to setup
  ca
  reset-ticks

  create-wolves 3 [
    set shape "wolf"
    setxy random 32 - 16 random 32 - 16
    set color one-of [ blue red ]
    set size 2
  ]

  create-sheeps 25 [
    set shape "sheep"
    setxy random 32 - 16 random 32 - 16
    set color one-of [ blue red ]
  ]

end


to go
  ask turtles [
    rt random 90 - 45 
    fd 0.1
  ]
  links-with-if
  tick  
end


to links-with-if
  ask wolves [
    if count my-links < 3 [
      ; Make sure the links a wolf tries to form
      ; does not exceed the max number of links it can make
      ; or the number of sheep available
      let n-to-link ( 3 - count my-links)      
      let n-sheep-in-radius count ( sheeps with [ color = [color] of myself ] in-radius 5 )
      if n-sheep-in-radius < n-to-link [
        set n-to-link n-sheep-in-radius
      ]

      ; Ask the appropriate sheeps to form a link with
      ; the asking wolf
      ask n-of n-to-link sheeps with [ color = [color] of myself ] in-radius 5  [
        create-participation-with myself [ set color red ]
      ]
    ]
  ]
end