如何检查两个不同海龟品种的变量是否匹配?

发布于 2025-01-11 11:44:03 字数 1240 浏览 3 评论 0 原文

我正在尝试根据共享变量将乌龟移向它的后代。在我最初的代码中,我要求具有展示但没有凉亭的雄性(品种)通过孵化来建立凉亭(不同品种)。我使用 create-link-with myself 来让雄性和凉亭共享一个名为 Bird-id 的变量。

to establish-bower
  ask males[
   if has-bower? = false[
     if has-display? = true[
        hatch 1 [
        set breed bowers
        set color yellow
        set bower-id who
        set shape "house"
        create-link-with myself
        ]
           ]
      ]
    ]
end

在我的代码的第二部分中,我要求拥有凉亭和展示台的雄性返回凉亭。如果它们与自己的凉亭的距离大于 0,则雄性面向该凉亭并向前移动 1。如果距离为 0,则调整其他两个变量和能量,并且雄性会稍微随机移动。

to return-to-bower
  ask males[
    if has-bower? = true[
      if has-display? = true[
        (ifelse
        distance one-of bowers with [bird-id = (bird-id of myself)] > 0[
          face one-of bowers with [bird-id = (bird-id of myself)]
          fd 1]
        distance one-of bowers with [bird-id = (bird-id of myself)] = 0[
          set has-display? false
          set nr-display nr-display + 1
          rt random 50
          lt random 50
          fd 1
          set energy energy - 3])
      ]
    ]
  ]
end

我在编码识别凉亭属于制造它的男性的部分时遇到了困难。到目前为止,

bowers with [bird-id = (bird-id of myself)]

这显然不起作用,但它传达了我想做的事情。

我该如何写这部分?

I am trying to move a turtle towards it's offspring based on a shared variable. In my initial code I ask a male (breed) that has a display, but not a bower, to establish a bower (different breed) by hatching. I use create-link-with myself to so that the male and the bower share a variable called bird-id.

to establish-bower
  ask males[
   if has-bower? = false[
     if has-display? = true[
        hatch 1 [
        set breed bowers
        set color yellow
        set bower-id who
        set shape "house"
        create-link-with myself
        ]
           ]
      ]
    ]
end

In the second part of my code, I ask males that have a bower and a display to return-to-bower. If their distance to THEIR OWN bower is greater than 0, the male faces that bower and moves forward one. If the distance is 0, two other variables and the energy are adjusted and the male moves a bit randomly.

to return-to-bower
  ask males[
    if has-bower? = true[
      if has-display? = true[
        (ifelse
        distance one-of bowers with [bird-id = (bird-id of myself)] > 0[
          face one-of bowers with [bird-id = (bird-id of myself)]
          fd 1]
        distance one-of bowers with [bird-id = (bird-id of myself)] = 0[
          set has-display? false
          set nr-display nr-display + 1
          rt random 50
          lt random 50
          fd 1
          set energy energy - 3])
      ]
    ]
  ]
end

I have trouble coding the part that identifies a bower as belonging to the male that made it. So far I have

bowers with [bird-id = (bird-id of myself)]

This obviously doesn't work, but it conveys what I want to do.

How can I write this part?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2025-01-18 11:44:03

认为你的bird-id没有按预期在你的雄性和凉​​亭之间共享 - 在external-bower中你有一行设置 Bower-id who,但其中的 who 应该在凉亭的上下文中,而不是男性的上下文中。因此,male 0 可能会要求 Bower 4 将其 Bird id 设置为 4,而不是 0。如果您想使用该选项(这当然是可行的),我认为您需要更改为 set Bower-id [who] of myself - 这反映了您对 create-link-with myself 行所做的操作。

但是,如果您已经在使用链接,您可能需要使用现有链接的替代方案。 other-end 原语返回代理在提问海龟拥有的链接的另一端(在该链接中有更好的解释)。因此,只要让乌龟弄清楚哪个凉亭是它自己的,就让它查询关联链接。例如:

breed [ males male ]
breed [ bowers bower ]

males-own [ has-bower? go-time? ]

to setup
  ca
  ask n-of 10 patches [
    sprout-males 1 [
      set go-time? true
      set has-bower? false
    ]
  ]
  ask males [
    establish-bower
  ]
  reset-ticks
end

to establish-bower
  if not has-bower? [
    hatch-bowers 1 [
      set shape "house"
      create-link-with myself
    ]    
    set has-bower? true
  ]  
end

to go 
  ask males with [has-bower?] [
    ifelse go-time? [
      rt random 60 - 30 
      fd 1
      if random-float 1 < 0.05 [
        set go-time? false
      ]
    ] [
      let my-bower [other-end] of one-of my-links
      ifelse distance my-bower > 1 [
        face my-bower
        fd 1
      ] [
        move-to my-bower
        if random-float 1 < 0.02 [
          set go-time? true
        ] 
      ]
    ]
  ]
  tick 
end

我在上面使用 let 来让 bowers 仅由 males 临时检查 - 您也可以通过使用males-own 变量类似于 my-bower 并将凉亭直接分配给该变量,然后雄性将永久跟踪它。

I think your bird-id is not being shared as expected between your males and bowers- in establish-bower you have the line set bower-id who, but the who there should be in the context of the bower instead of the context of the male. So, male 0 may ask bower 4 to set its bird id to 4, rather than 0. If you want to use that option (which is certainly doable), I think you'd need to alter to set bower-id [who] of myself- this mirrors what you did with the create-link-with myself line.

However, if you are already using links, you might like an alternative that makes use of the link already in place. The other-end the primitive returns the agent on the other end of the link owned by the asking turtle (better explained in that link). So whenever it makes sense to have the turtle figure out which bower is its own, have it query the associate link. For example:

breed [ males male ]
breed [ bowers bower ]

males-own [ has-bower? go-time? ]

to setup
  ca
  ask n-of 10 patches [
    sprout-males 1 [
      set go-time? true
      set has-bower? false
    ]
  ]
  ask males [
    establish-bower
  ]
  reset-ticks
end

to establish-bower
  if not has-bower? [
    hatch-bowers 1 [
      set shape "house"
      create-link-with myself
    ]    
    set has-bower? true
  ]  
end

to go 
  ask males with [has-bower?] [
    ifelse go-time? [
      rt random 60 - 30 
      fd 1
      if random-float 1 < 0.05 [
        set go-time? false
      ]
    ] [
      let my-bower [other-end] of one-of my-links
      ifelse distance my-bower > 1 [
        face my-bower
        fd 1
      ] [
        move-to my-bower
        if random-float 1 < 0.02 [
          set go-time? true
        ] 
      ]
    ]
  ]
  tick 
end

I use let in the above to have the bowers only be temporarily checked by the males- you could also simplify this by having a males-own variable like my-bower and assign the bower directly to that variable and then the males would permanently track it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文