我是NetLogo的新手,并试图为后续殖民者的家庭范围选择建模。该模型应遵循简单的步骤:
我想不出该怎么让这件事起作用。我可以让第一只乌龟来挑选一个家乡。但后代却没有。以多种方式编写代码只实现了两种意想不到的结果。要么是无穷无尽的新个体同时孵化出来,要么是在第一只海龟有了归宿范围之前,而新的海龟却没有选择一个家园。或者,第一只海龟选择它的家园,孵化出一只新的海龟,但这只新海龟不会选择一个家的范围。结果都不是我想要的。
我如何设置它来按照预期运行,这样雏鸟也可以选择回家的范围?下面是我的代码的一个简化版本:
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因此,在这最后一部分,一旦家庭范围建成,我想要一只新的海龟孵化从它的父母。然后我想让那只乌龟重复选择本垒打的程序。怎么会发生这种情况呢?我已经试着用我所能想到的每一种方式来写这篇文章;没有什么是可行的。谢谢您的帮助!
发布于 2017-03-05 02:23:53
要做到这一点,一种方法是让每个补丁都等于一个“食物价值”,让海龟生长他们的家园,直到他们的家园提供足够的食物。我会把它设置成这样,让海龟“知道”它们属于哪只海龟,让海龟知道它们需要多少食物,哪些补丁是它们家园的一部分,以及它们的家乡提供的食物。例如,修补程序和海龟变量将是:
patches-own [
owned_by
]
turtles-own [
food_required
my_homerange
homerange_food
]然后,您的海龟可以添加补丁到他们的家庭范围,直到他们达到他们的"food_required",无论你设置的。为了简单起见,在这个例子中,我假设海龟是属地的,因此不会“共享”家园范围。对步骤的进一步解释在下面的代码中有注释。这只是为了让您开始-例如,如果您运行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)
]
]
endhttps://stackoverflow.com/questions/42602727
复制相似问题