如何在 NetLogo 中一次在本地设置海龟的概率等变量
How to set variables such as probabilities to turtles one at a time locally in NetLogo
在 NetLogo 中,我想一次遍历每只海龟,有两个品种;大小事。在观察一只乌龟时,我想分配它的邻居概率,然后将这些概率放入一个列表中,将列表相乘,然后使用该值来决定是否应该移动乌龟。然后,一旦循环完成对一只海龟的处理,这些值就应该丢失,这样它们就不会影响下一个中央海龟邻域。我一直在使用这段代码,但我现在意识到它似乎正在覆盖值,因为 'ask smalls' 在 Probability_Product 中排在最后,但我不确定如何修复它。这里的大多数未定义变量都位于 GUI 中的滑块上。
谢谢!
breed [ bigs big ]
breed [ smalls small ]
bigs-own [ probability]
smalls-own [ probability]
to setup
clear-all
set-default-shape bigs "square"
set-default-shape smalls "square"
ask n-of bigs-number patches with [ abs (min-pxcor - pxcor) > 2]
[sprout-bigs 1 [ set color red ]]
ask n-of smalls-number patches with [not any? bigs-here]
[sprout-smalls 1 [ set color blue ]]
reset-ticks
end
to go
ask turtles with [count turtles-on neighbors4 < 4 ][
;; so only turtles will a space in the neighbours 4 can move
let vacant-patches neighbors4 with [not any? turtles-here ]
;print Probability_Product
if count turtles-on neighbors4 < 4 [
if random 1001 <= Probability_Product * 1000 ;;if random number in the probability range, the following happens
[ move-to one-of vacant-patches ]]
]
tick
end
to-report Probability_Product
if ( count turtles-on neighbors4 < 4 ) or ( count turtles-on neighbors4 = 0 ) [
ifelse breed = bigs
[ ask bigs-on neighbors4 [set probability Prob_big_big_breaking] ask smalls-on neighbors4 [set probability Prob_small_big_breaking]
let prob-list ( sentence ([probability] of turtles-on neighbors4))
print prob-list
ifelse prob-list != []
[ report reduce * prob-list ] ;; multiplies all the probabilities together
[report 1 ]]
[ ask smalls-on neighbors4 [set probability Prob_small_small_breaking] ask bigs-on neighbors4 [set probability Prob_small_big_breaking]
let prob-list ( sentence ([probability] of turtles-on neighbors4))
print prob-list
ifelse prob-list != []
[ report reduce * prob-list ] ;; multiplies all the probabilities together
[report 1 ]]]
end
这不是一个真正的答案(编辑:也许现在是?),但这是一个必要的回应,不适合发表评论。我有其他问题在评论。
我仍然不确定我是否已经在评论中回答了你的问题。我认为首先需要做一些事情来阐明您的代码应该做什么。首先,我编辑了您问题中的代码以对其进行格式化以使其更清晰(同时尝试尽可能保留您的风格)。其次,我在 ask bigs
和 ask smalls
之后的行中添加了右括号。最重要的是,我认为如果您能提供一个 Minimal Working Example (MWE) 会有所帮助——您的程序的最小、最简单的版本仍然包含 (a) 运行的代码,并且 (b) 说明您遇到的问题正在努力解决。构建 MWE 需要一些工作,因为你必须弄清楚你可以去掉什么(因为留在里面只会让读者感到困惑),以及你必须留下什么,因为它对于产生问题至关重要。 (但是,有时您会在尝试创建 MWE 时自行找出问题的答案。)
例如,这里是一个 MWE,在它运行的意义上,但您必须更改它以说明您的问题。
breed [bigs big]
breed [smalls small]
bigs-own [probability]
smalls-own [probability]
globals [Prob_bigs_bigs_breaking Prob_smalls_smalls_breaking]
to setup
reset-ticks
ask n-of 20 patches [sprout-bigs 1 []]
ask n-of 20 patches [sprout-smalls 1 []]
end
to go
ask turtles with [count turtles-on neighbors4 < 4 ][
;; so only turtles will a space in the neighbours 4 can move
move-turtle
]
tick
end
to-report Probability_Product
ask bigs with [count turtles-on neighbors4 < 4 ] [
;; self is each big in turn
ask bigs-on neighbors4 [set probability Prob_bigs_bigs_breaking]
]
ask smalls with [count turtles-on neighbors4 < 4 ][
;; self is each small, in turn
ask smalls-on neighbors4 [set probability Prob_smalls_smalls_breaking]
]
;; Here self is the turtle that called move-turtle, which called Probability_Product.
if any? turtles with [count turtles-on neighbors4 < 4 ][
let prob-list ( sentence ([probability] of turtles-on neighbors4))
if prob-list != []
[ report reduce * prob-list ] ;; multiplies all the probabilities together
]
report 0
end
to move-turtle
if random 1001 <= Probability_Product * 1000 [
;.....
]
end
接下来,我建议 ask bigs
和 ask smalls
行应该在与包含 if any? ...
的函数分开的过程中完成。将所有这些都放在一个过程中是令人困惑的,因为在 Probability_Product
中的 ask
块中,我们引用由那些 ask
定义的海龟,但在 if any?
中,我们引用由 ask
两个过程 "above" 定义的乌龟,即由 go
中的 ask
定义,然后调用 move-turtle
,然后调用 Probability_Product
.当我们到达 if any?
时,很难弄清楚 turtle neighbors4
是相对的,因为它在上面定义了两个过程,并且因为 Probability_Product
也引用了所有 bigs
和所有 smalls
.
除了令人困惑之外,我不确定 Probability_Product
是否按照您的意愿行事。对于每只海龟,此过程会要求所有 bigs
和所有 smalls
做某事。所以,如果只有大乌龟和小乌龟,Probability_Product
要求所有乌龟做某事,然后对于下一只乌龟,再次要求所有乌龟做同样的事情,依此类推。
编辑:
在我看来,Probability_Product
应该在 MWE 中执行的操作可以使用以下函数更简单地完成。也许我误解了您的意图,并且可能在整个程序中还有更多工作要做,因为下面的函数根本没有设置或使用 probability
变量。即使这不是您想要的,也许这个例子会帮助您思考您需要做什么。
to-report new-Probability_Product
let neighbor-count count turtles-on neighbors4
let big-neighbor-count count bigs-on neighbors4
let small-neighbor-count count smalls-on neighbors4
if-else neighbor-count < 4 and neighbor-count > 0
[ if-else is-big? self ; self is turtle from ask in go procedure
[ report (Prob_big_big_breaking ^ big-neighbor-count) * ; if self is a big
(Prob_small_big_breaking ^ small-neighbor-count) ]
[ report (Prob_small_big_breaking ^ big-neighbor-count) * ; if self is a small
(Prob_small_small_breaking ^ small-neighbor-count) ] ]
[ report 1]
end
此函数背后的想法是,probability
变量在 Probability_Product
中所做的唯一事情就是保存滑块中设置的值。然后将这些值相乘。但是我们可以直接将这些值相乘。
(我的一些其他评论似乎仍然适用。)
在 NetLogo 中,我想一次遍历每只海龟,有两个品种;大小事。在观察一只乌龟时,我想分配它的邻居概率,然后将这些概率放入一个列表中,将列表相乘,然后使用该值来决定是否应该移动乌龟。然后,一旦循环完成对一只海龟的处理,这些值就应该丢失,这样它们就不会影响下一个中央海龟邻域。我一直在使用这段代码,但我现在意识到它似乎正在覆盖值,因为 'ask smalls' 在 Probability_Product 中排在最后,但我不确定如何修复它。这里的大多数未定义变量都位于 GUI 中的滑块上。 谢谢!
breed [ bigs big ]
breed [ smalls small ]
bigs-own [ probability]
smalls-own [ probability]
to setup
clear-all
set-default-shape bigs "square"
set-default-shape smalls "square"
ask n-of bigs-number patches with [ abs (min-pxcor - pxcor) > 2]
[sprout-bigs 1 [ set color red ]]
ask n-of smalls-number patches with [not any? bigs-here]
[sprout-smalls 1 [ set color blue ]]
reset-ticks
end
to go
ask turtles with [count turtles-on neighbors4 < 4 ][
;; so only turtles will a space in the neighbours 4 can move
let vacant-patches neighbors4 with [not any? turtles-here ]
;print Probability_Product
if count turtles-on neighbors4 < 4 [
if random 1001 <= Probability_Product * 1000 ;;if random number in the probability range, the following happens
[ move-to one-of vacant-patches ]]
]
tick
end
to-report Probability_Product
if ( count turtles-on neighbors4 < 4 ) or ( count turtles-on neighbors4 = 0 ) [
ifelse breed = bigs
[ ask bigs-on neighbors4 [set probability Prob_big_big_breaking] ask smalls-on neighbors4 [set probability Prob_small_big_breaking]
let prob-list ( sentence ([probability] of turtles-on neighbors4))
print prob-list
ifelse prob-list != []
[ report reduce * prob-list ] ;; multiplies all the probabilities together
[report 1 ]]
[ ask smalls-on neighbors4 [set probability Prob_small_small_breaking] ask bigs-on neighbors4 [set probability Prob_small_big_breaking]
let prob-list ( sentence ([probability] of turtles-on neighbors4))
print prob-list
ifelse prob-list != []
[ report reduce * prob-list ] ;; multiplies all the probabilities together
[report 1 ]]]
end
这不是一个真正的答案(编辑:也许现在是?),但这是一个必要的回应,不适合发表评论。我有其他问题在评论。
我仍然不确定我是否已经在评论中回答了你的问题。我认为首先需要做一些事情来阐明您的代码应该做什么。首先,我编辑了您问题中的代码以对其进行格式化以使其更清晰(同时尝试尽可能保留您的风格)。其次,我在 ask bigs
和 ask smalls
之后的行中添加了右括号。最重要的是,我认为如果您能提供一个 Minimal Working Example (MWE) 会有所帮助——您的程序的最小、最简单的版本仍然包含 (a) 运行的代码,并且 (b) 说明您遇到的问题正在努力解决。构建 MWE 需要一些工作,因为你必须弄清楚你可以去掉什么(因为留在里面只会让读者感到困惑),以及你必须留下什么,因为它对于产生问题至关重要。 (但是,有时您会在尝试创建 MWE 时自行找出问题的答案。)
例如,这里是一个 MWE,在它运行的意义上,但您必须更改它以说明您的问题。
breed [bigs big]
breed [smalls small]
bigs-own [probability]
smalls-own [probability]
globals [Prob_bigs_bigs_breaking Prob_smalls_smalls_breaking]
to setup
reset-ticks
ask n-of 20 patches [sprout-bigs 1 []]
ask n-of 20 patches [sprout-smalls 1 []]
end
to go
ask turtles with [count turtles-on neighbors4 < 4 ][
;; so only turtles will a space in the neighbours 4 can move
move-turtle
]
tick
end
to-report Probability_Product
ask bigs with [count turtles-on neighbors4 < 4 ] [
;; self is each big in turn
ask bigs-on neighbors4 [set probability Prob_bigs_bigs_breaking]
]
ask smalls with [count turtles-on neighbors4 < 4 ][
;; self is each small, in turn
ask smalls-on neighbors4 [set probability Prob_smalls_smalls_breaking]
]
;; Here self is the turtle that called move-turtle, which called Probability_Product.
if any? turtles with [count turtles-on neighbors4 < 4 ][
let prob-list ( sentence ([probability] of turtles-on neighbors4))
if prob-list != []
[ report reduce * prob-list ] ;; multiplies all the probabilities together
]
report 0
end
to move-turtle
if random 1001 <= Probability_Product * 1000 [
;.....
]
end
接下来,我建议 ask bigs
和 ask smalls
行应该在与包含 if any? ...
的函数分开的过程中完成。将所有这些都放在一个过程中是令人困惑的,因为在 Probability_Product
中的 ask
块中,我们引用由那些 ask
定义的海龟,但在 if any?
中,我们引用由 ask
两个过程 "above" 定义的乌龟,即由 go
中的 ask
定义,然后调用 move-turtle
,然后调用 Probability_Product
.当我们到达 if any?
时,很难弄清楚 turtle neighbors4
是相对的,因为它在上面定义了两个过程,并且因为 Probability_Product
也引用了所有 bigs
和所有 smalls
.
除了令人困惑之外,我不确定 Probability_Product
是否按照您的意愿行事。对于每只海龟,此过程会要求所有 bigs
和所有 smalls
做某事。所以,如果只有大乌龟和小乌龟,Probability_Product
要求所有乌龟做某事,然后对于下一只乌龟,再次要求所有乌龟做同样的事情,依此类推。
编辑:
在我看来,Probability_Product
应该在 MWE 中执行的操作可以使用以下函数更简单地完成。也许我误解了您的意图,并且可能在整个程序中还有更多工作要做,因为下面的函数根本没有设置或使用 probability
变量。即使这不是您想要的,也许这个例子会帮助您思考您需要做什么。
to-report new-Probability_Product
let neighbor-count count turtles-on neighbors4
let big-neighbor-count count bigs-on neighbors4
let small-neighbor-count count smalls-on neighbors4
if-else neighbor-count < 4 and neighbor-count > 0
[ if-else is-big? self ; self is turtle from ask in go procedure
[ report (Prob_big_big_breaking ^ big-neighbor-count) * ; if self is a big
(Prob_small_big_breaking ^ small-neighbor-count) ]
[ report (Prob_small_big_breaking ^ big-neighbor-count) * ; if self is a small
(Prob_small_small_breaking ^ small-neighbor-count) ] ]
[ report 1]
end
此函数背后的想法是,probability
变量在 Probability_Product
中所做的唯一事情就是保存滑块中设置的值。然后将这些值相乘。但是我们可以直接将这些值相乘。
(我的一些其他评论似乎仍然适用。)