好海龟和坏海龟:如何更新它们的动作?

The good and the bad turtles: how to update theirs actions?

我正在尝试建立一个包含好乌龟和坏乌龟的网络,以研究它们的行为(做得好、做错、什么都不做)。 好龟可能易感、被感染或自私;坏龟只会感染,反而。 在每个滴答声中,都会创建其他海龟(好的和坏的)。当模拟开始时,每只海龟都可以(随机地)根据其邻居决定做一些好事、坏事或什么都不做。

我编写有关海龟动作的代码部分的方法如下:

  1. 检查随机选择的海龟 G 的 link;
  2. 如果海龟没有links,那么它可以选择做好事或什么都不做;否则,如果它至少有一个 link,那么我们需要检查它的邻居是好乌龟还是坏乌龟,以便决定乌龟应该采取哪种行动。 乌龟必须选择是跟随一只好乌龟还是跟随一只坏乌龟并采取一些行动(做好、做错或什么都不做)。
  3. 如果乌龟的邻居是好的(这意味着没有被感染),以一定概率选择的动作可以是做好、做错或什么都不做。龟龟会一直好好的
  4. 如果乌龟的邻居是坏的(这意味着被感染),选择的动作将有一定的概率可以是做好,做错或不做。只有当乌龟选择做错事时,它才会被感染。

这是我为回答这个问题而编写的部分代码:

    ifelse one-of goods with [any? link-neighbors]  ;; check neighbours and decide action to take
     [ ifelse out-link-neighbors with [infected? true] ;; if neighbor is infected, it should have three options: do-well, do-wrong, do-nothing.
        [ 
           ifelse random-float 1 < probability
           [do-well][do-wrong]
        ] ;; but there should be another possible action, do-nothing... and maybe I should use an if condition with probability split in 0.3, 0.4, 0.3 for example
      [
        ifelse random-float 1 < probability ;; this if condition is for turtle linked with only good turtles 
            [do-well][do-nothing]
]

 ] [ifelse random-float 1 < probability [do-well][do-nothing]]

但我知道这段代码由于错误而无法工作,所以我想问你我做错了什么,最后,你能不能解释一下原因。

(如果您需要编辑概率,为了向我展示如何在 if/ifelse 条件下设置三个不同的 options/actions,请这样做。我使用概率作为我正在尝试做的事情的例子)

你有很多问题。

  • ifelse one-of goods with [any? link-neighbors] 没有意义。您的描述说您想随机选择一只乌龟。这不会选择海龟,它会检查随机海龟是否有任何邻居。但随后的任何代码都不会使用同一个乌龟。
  • 你的描述在第 2 步有逻辑问题。如果选择的乌龟有两个邻居——一好一坏,会发生什么?我编写下面代码的方式让乌龟随机 select 它的邻居之一,然后检查该特定邻居是好是坏。但是你也可以检查是否有任何坏的,坏的比例超过某个阈值,或者全部都是坏的。您需要决定您想要的实现方式。
  • 如果有三种可能的动作,那么您需要某种方式来指示每种动作的概率,不能只使用一种概率。

所以我做了一些猜测,这是一种可能性。当然,它未经测试,因为我还没有用 'goods' 或网络等建立模型。希望它能帮助你弄清楚你想要什么以及如何去做。

ask one-of goods ; select a random turtle
[ ifelse [any? link-neighbors]  ; check if it has neighbours
  [ let choice one-of out-link-neighbors   ; pick one of the available neighbours
    [ let roll random-float 1     ; get a random number
      ifelse  [infected?] of choice
      ; condition if the chosen neighbour is infected
      [ ifelse roll < probability1
        [ do-well ]
        [ ifelse roll < probability1 + probability2 [do-wrong] [do-nothing] ]
      ]
      ; condition if the chosen neigbour not infected 
      [ ifelse roll < probability1   
        [do-well]
        [do-nothing]
      ]
    ]
  ]
  [ print "No neighbours to select" ]
]