turtles-own[
nearby-classmates ;; agentset of turtles within some specified radius
stopped? ;; if the turtle hasn't moved
]
to setup
clear-all
set-default-shape turtles "circle"
make-turtles
reset-ticks
end
to make-turtles
;初始化时让海龟聚集在patch 0 0 周围,由于程序后续会限制每个patch 上只允许同时有一个海龟,所以初始化的这个圈内的patch 数量要大于海龟数量,否则弹框提示。
if [count patches in-radius initial-radius] of patch 0 0 < num-open-min[
user-message (word "There aren't enough patches in initial-radius to create the number "
"of turtles you've asked for. Please make initial-radius larger or decrease "
"the number of turtles.")
stop
]
ask patch 0 0 [
create-initial-turtles num-open-min blue
]
ask turtles[
set stopped? false
]
end
to create-initial-turtles [n turtle-color]
ask n-of n (patches in-radius initial-radius with [not any? turtles-here])[;从“圈”内取出n 个patch
sprout 1[;每个patch 生成一个海龟并且为部分属性赋值
set color turtle-color
]
]
end
to go
ifelse bees-pen-down? [
ask turtles [pen-down]
][
ask turtles [pen-up]
]
if all? turtles [stopped?] [ stop ]
ask turtles [
move-open-min
]
tick
end
; set heading towards the largest open space, stopping when all other turtles are at least "too-close" away
to move-open-min ;; turtle procedure
set nearby-classmates other turtles in-radius too-close
ifelse any? nearby-classmates[
facexy (mean [xcor] of nearby-classmates) (mean [ycor] of nearby-classmates)
rt 180
avoid-walls
fd step-size
set stopped? false
][
set stopped? true
]
end
;如果乌龟遇到墙,转身
to avoid-walls ;; turtle procedure
if not can-move? 1[
rt 180
]
end
参考自:Netlogo 库中的模型 Sample Models/Social Science/Scatter 里的open-min 算法