Netlogo:要求乌龟在完成一个程序后重现,并要求新乌龟为自己重复该程序

时间:2017-03-04 23:48:00

标签: netlogo

我是NetLogo的新手,正在尝试为后续殖民者的家庭范围选择建模。该模型应遵循简单的步骤:

  1. 个人1选择一个家庭范围(补丁的子集)。
  2. 当个人1完成选择其原定范围时,它会变成新的 个人2.
  3. 个人2选择一个家庭范围,然后孵化个人3.
  4. 个人3选择一个家庭范围,依此类推。
  5. 我无法弄清楚如何让它发挥作用。我可以得到第一只乌龟选择一个家庭范围。但是后代没有。以多种方式编写代码只能实现两个意想不到的结果。在第一只乌龟有一个家庭范围之前,无论是无尽的新人都同时孵化,而新的乌龟未能选择一个家庭范围。或者,第一只乌龟选择它的家乡范围并孵化一只新的乌龟,但是这只新乌龟并没有选择一个家庭范围。这两种结果都不是我想要的。

    如何将其设置为按预期运行,以便孵化器也选择家庭范围?这是我的代码的一个简化版本:

    to setup-turtles
         crt 1  
         [setxy random-xcor random-ycor]
    end
    
    to go
         ask turtles [pick-homerange]
         tick
    end
    
    to pick-homerange
         while [food-mine < food-required]  ;; if not enough food, keep picking patches for home range
         [;; code to pick home range until it has enough food; this is working okay
         ]
         [;; when enough food, stop picking home range 
          hatch 1 fd 20 ;; now hatch 1, move new turtle slightly away
         ]
    end
    

    所以在最后一部分,一旦建立了家庭范围,我想要一只新的龟从其父母孵化出来。然后我希望那只乌龟重复拾取范围的程序。怎么可能编码发生?我试过用我能想到的每一种方式写这个;什么都没有用。提前感谢您的任何帮助!

1 个答案:

答案 0 :(得分:1)

这样做的一种方法是让每个补丁等于食物价值&#34;,并让乌龟长大到他们的家庭范围,直到他们的家庭范围为他们提供足够的食物。我会设置它,以便补丁&#34;知道&#34;他们属于哪只乌龟,以便乌龟知道他们需要多少食物,哪些补丁是他们家乡的一部分,以及由他们的家乡提供的食物。然后是示例补丁和乌龟变量:

patches-own [
  owned_by
]

turtles-own [
  food_required
  my_homerange
  homerange_food
]

然后,你的海龟可以添加补丁进入他们的家庭范围,直到他们击中他们的&#34; food_required&#34;,无论你设置为什么。为简单起见,在这个例子中,我假设乌龟是地域性的,所以不会分享&#34;分享&#34;家庭范围。下面的代码中对步骤的进一步说明进行了评论。这只是为了让您入门 - 例如,如果您多次运行pick-homerange,它将会挂起。

to setup-turtles
  crt 1 [
        set size 1.5
        setxy random-xcor random-ycor
        set food_required 5 + random 5
        set homerange_food 0
        set my_homerange []
      ]
  end

to pick-homerange

  ask turtles [
    ;; Check if the current patch is owned by anyone other than myself
    if ( [owned_by] of patch-here != self ) and ( [owned_by] of patch-here != nobody ) [
      ;; if it is owned by someone else, move to a new patch that is not owned
      let target one-of patches in-radius 10 with [ owned_by = nobody ]
      if target != nobody [
        move-to target
      ]
    ]

    ;; Now add the current patch into my homerange
    ask patch-here [
      set owned_by myself
    ]
    set my_homerange patches with [ owned_by = myself ]

    ;; calculate the number of patches currently in my homerange
    set homerange_food count patches with [owned_by = myself]

    ;; Now grow the homerange until there are enough patches in the homerange 
    ;; to fulfill the "food_required" variable
    while [ homerange_food < food_required ] [
      let expander one-of my_homerange with [ any? neighbors with [ owned_by = nobody ] ]
      if expander != nobody [
        ask expander [
          let expand_to one-of neighbors4 with [ owned_by = nobody ] 
          if expand_to != nobody[
            ask expand_to [
              set owned_by [owned_by] of myself
            ]
          ]
        ]
      ]
      ;; Reassess homerange food worth
      set my_homerange patches with [ owned_by = myself ]
      set homerange_food count patches with [owned_by = myself]
    ]

    ask my_homerange [ 
      set pcolor [color] of myself - 2
    ]

    ;; Now that my homerange has been defined, I will hatch a new turtle
    hatch 1 [
      set color ([color] of myself + random 4 - 2)
    ]     
  ]
end