代码之家  ›  专栏  ›  技术社区  ›  MoMo

NetLogo:随机但平等地将海龟分配给不同的群体

  •  0
  • MoMo  · 技术社区  · 6 年前

    我使用下面的代码创建了50只海龟,并将它们随机分配到四种不同策略(即a、b、c和d)中的一种:

    问题是,当我减少海龟数量或增加策略数量时,我会面临至少一种策略没有被任何海龟采用的情况。

    turtles-own [ my_strategy ]
    
    to setup
      ;; create 50 turtles and assign them randomly
      ;; to one of four different strategies
      create-turtles 50 [
        set my_strategy one-of [ "a" "b" "c" "d" ]
      ]
    end
    

    我需要你的帮助: 1.确保我不会面临任何海龟不采取一种或多种策略的情况。 2.确保分配给每个策略的海龟数量大致相等。

    我试图用下面的代码解决这个问题,但没有成功:

    turtles-own [ my_strategy ]
    
    to setup
      let strategies [ "a" "b" "c" "d" ]
      let turtles-num 51
      let i 0
    
      create-turtles turtles-num 
    
      while [ not any? turtles with [  my_strategy = 0 ] ] [
        ifelse i < length strategies - 1 [ set i i + 1 ] [ set i 0 ]
        ask n-of ceiling  ( turtles-num / length strategies ) turtles with [ my_strategy = 0 ] [
          set my_strategy item i strategies
        ]
      ]
    

    谢谢你的帮助。

    1 回复  |  直到 6 年前
        1
  •  3
  •   JenB    6 年前

    一般来说,你不应该使用 who NetLogo中任何内容的数字。然而,这是极少数合适的场合之一。

    从评论中,你实际上希望每组的数字相等(或尽可能接近相等),所以你不需要计算每组的数字。什么时候 turtles 都是创建的,它们都是按顺序创建的 数字。所以你可以使用 mod 操作员依次将其分配给每个策略。

    turtles-own [ my_strategy ]
    
    to setup
      ;; create 50 turtles and assign them equally
      ;; to one of four different strategies
      create-turtles 50 [
        set my_strategy item (who mod 4) [ "a" "b" "c" "d" ]
      ]
    end