我试图在Netlogo中建立一个有两个品种的世界,但每个补丁只有一只乌龟:
breed [supras supra]
breed [subs sub]
turtles-own [age]
subs-own [status]
to setup
clear-all
;; Color the patches so they're easier to see
ask patches [ set pcolor random-float 2 ]
;; num-turtles patches will sprout one turtle each
ask n-of (num-turtles / 2) patches [
if not any? turtles-on patch-set self [
sprout-subs 1
]
]
ask n-of (num-turtles / 2) patches [
if not any? turtles-on patch-set self [
sprout-supras 1
]
]
;; Set breed colors and own-variables
ask subs [
set color blue
set shape "dot"
]
ask supras [
set color pink
set shape "dot"
]
reset-ticks
end
to go
ask turtles [
fd 1
]
tick
end
这似乎有效,但我不能确定它在技术上是否正确。什么是一个很好的测试,以确保我没有在初始化时有多个海龟补丁?
答案 0 :(得分:3)
我实际上会提出一个不同的方法;而不是为一个品种随机选择一些补丁而为另一个选择一些补丁并试图避免彼此,你可以选择最初萌芽的全部补丁,然后将你的一半龟转换成另一个品种。
globals [num-turtles]
breed [supras supra]
breed [subs sub]
turtles-own [age]
subs-own [status]
to setup
clear-all
set num-turtles 99
ask n-of num-turtles patches [sprout-subs 1]
ask n-of (num-turtles / 2) subs [set breed supras]
<procedures to set colours etc>
end
答案 1 :(得分:2)
尝试将代码剥离到完整示例所需的内容。
globals [num-turtles]
breed [supras supra]
breed [subs sub]
turtles-own [age]
subs-own [status]
to setup
clear-all
set num-turtles 99
;; num-turtles patches will sprout one turtle each
ask n-of (num-turtles / 2) patches [sprout-subs 1]
ask n-of (num-turtles / 2) patches with [not any? turtles-here] [
sprout-supras 1
]
end
to test-setup
if (int (num-turtles / 2) != count supras) [error "setup error: supras"]
if (int (num-turtles / 2) != count subs) [error "setup error: subs"]
if any? patches with [count turtles-here > 1] [error "setup error: patches"]
end