在运动中包含随机性的最合适方法是什么 - NetLogo
What is the most appropriate way to include randomness in the movement - NetLogo
首先,让我描述一下我在做什么以及我为什么问这个问题。
模型中包含 74 个城市,我想模拟它们之间的移动。我有一个 OD 概率矩阵,其中行是起点,列是终点。矩阵看起来像这样:
0 1 ..... 73
----------------------
0 |0.5 0.1 .... 0.0
...| . . .. . .
73 | 0.1 0.2 .. 0.3
请注意:如果我们看第一行,这意味着来自索引为 0 的城市的代理人有概率留在其中 0.5,移动到 idx=1 的城市 0.1 等等。 . 我想做的是用最好的统计方法来分配代理商的目的地。对于起源于城市 0 的代理人,我想保留大约 50% 的代理人(不是确切的 50%),但我也想给概率为 0% 的城市一些机会,比如 0-73 对。
我已经根据这个问题的答案对随机性进行了编码:Netlogo: How can send agents from "area x" to "area y" using an O/D matrix?
但是这个问题对我的回答是不合逻辑的,具体是这部分:
ask turtles with [residency = "nw"]
[ let myrandom random-float 1
ifelse myrandom <= 0.5 [ set destination "nw" ] [
ifelse myrandom <= 0.8 [ set destination "ne" ] [
ifelse myrandom <= 0.0 [ set destination "sw" ] [
set destination "se" ]]]
如果我理解的好的话,myrandom会取0-1范围内的一个值,然后它会去一个条件检查它的值是否小于或等于这个常量值。
从这个意义上说,myrandom wound 永远不会到达“sw”部分(0 总是小于 0.5)并且有更多机会获得概率为 0.5 的“nw”部分而不是概率更高的“ne”部分-0.8。而且只是因为它没有首先列出。我不确定这是正确的方法,而且我也不确定哪个是(我无法对我的概率进行排序,因为它们的位置代表城市 ID(见下文))。还是我理解错了?
这里展示了我的部分代码。我导入了没有 headings/cities ID 的矩阵,因为它们等同于 NetLogo 索引。具有 ID 的城市已导入到模型中。此外,在每个 point/city 中,我创建了我从 CSV 文件中为每个城市读取的相应数量的代理。在创建代理期间,我使用矩阵中与当前 city/origin 相对应的行,并像 JenB 在他上面的回答中所做的那样检查概率。
breed [city cities]
breed [inhabitant inhabitants]
;; part of the setup
;; open the csv file that contains population per city
file-open path
while [ not file-at-end? ] [
let data csv:from-row file-read-line
let city-id item 0 data
let population item 1 data
to add-inhabitants
create-inhabitants population [
set city-home one-of cities with [id = city-id] ;; set the origin
move-to city-home
set-destination(city-id) ;; for this origin chose the destination
]
]
to set-destination [row] ;; row from the matrix which represent the movement from the current city/origin to other cities
let row-probabilities matrix:get-row od-matrix row ;; use the row that correspondents to the city
let random-value random-float 1
let i 0 ;; index counter
foreach row-probabilities [ ;; for each probability in row
p ->
if random-value <= p ;; this part is coded as in the JenB's answer
[
set destination one-of cities with [id = i] ;; i is column index which is actually index of destination city
stop ;; if you set city -> stop
]
if i = 73 [set destination one-of cities with [id = i]
stop] ;; the last city -> there is no more option/reason to check
set i i + 1
]
end
我知道有点长,但我想解释清楚。非常感谢一些指南和解释!
您的问题表明您不理解该部分中的代码的作用。这是该位的更正代码:
ask turtles with [residency = "nw"]
[ let myrandom random-float 1
ifelse myrandom <= 0.5 [ set destination "nw" ] [
ifelse myrandom <= 0.8 [ set destination "ne" ] [
ifelse myrandom <= 0.9 [ set destination "sw" ] [
set destination "se" ]]]
是的,块中的第一行抽取了一个 0 到 1 范围内的随机数。假设抽取的是 0.4。那么第一个 ifelse
将为真,目的地将设置为“nw”。现在想象平局是 0.6,那么第一个 ifelse
将是假的,代码将继续测试 else
部分,这是真的,因为 0.6 <= 0.8.
该代码有效,因为初始绘制是统一的。也就是说,10% 的时间(平均)returns 是 0 到 0.1 范围内的数字,10% 的时间是 0.1 到 0.2 范围内的数字,依此类推。所以 50% 的时间它 returns 一个 0 到 0.5 范围内的数字,第一行 returns 为真。 30% 的时间 returns 是 0.5 到 0.8 范围内的数字,第一次测试为假,第二次测试为真。 10% 的时间 returns 是 0.8 到 0.9 范围内的数字,10% 的时间是 returns 0.9 到 1 范围内的数字。
因此,在 0.5、0.8、0.9(其余部分为 1)处打破检查点的区间会得到 0.5、0.3(=0.8-0.5)、0.1(=0.9-0.8)和 0.1( =1=0.9) 的长度。并且一个统一的随机数会落入概率分布为50%、30%、10%、10%的抽奖区间
首先,让我描述一下我在做什么以及我为什么问这个问题。
模型中包含 74 个城市,我想模拟它们之间的移动。我有一个 OD 概率矩阵,其中行是起点,列是终点。矩阵看起来像这样:
0 1 ..... 73
----------------------
0 |0.5 0.1 .... 0.0
...| . . .. . .
73 | 0.1 0.2 .. 0.3
请注意:如果我们看第一行,这意味着来自索引为 0 的城市的代理人有概率留在其中 0.5,移动到 idx=1 的城市 0.1 等等。 . 我想做的是用最好的统计方法来分配代理商的目的地。对于起源于城市 0 的代理人,我想保留大约 50% 的代理人(不是确切的 50%),但我也想给概率为 0% 的城市一些机会,比如 0-73 对。 我已经根据这个问题的答案对随机性进行了编码:Netlogo: How can send agents from "area x" to "area y" using an O/D matrix? 但是这个问题对我的回答是不合逻辑的,具体是这部分:
ask turtles with [residency = "nw"]
[ let myrandom random-float 1
ifelse myrandom <= 0.5 [ set destination "nw" ] [
ifelse myrandom <= 0.8 [ set destination "ne" ] [
ifelse myrandom <= 0.0 [ set destination "sw" ] [
set destination "se" ]]]
如果我理解的好的话,myrandom会取0-1范围内的一个值,然后它会去一个条件检查它的值是否小于或等于这个常量值。 从这个意义上说,myrandom wound 永远不会到达“sw”部分(0 总是小于 0.5)并且有更多机会获得概率为 0.5 的“nw”部分而不是概率更高的“ne”部分-0.8。而且只是因为它没有首先列出。我不确定这是正确的方法,而且我也不确定哪个是(我无法对我的概率进行排序,因为它们的位置代表城市 ID(见下文))。还是我理解错了?
这里展示了我的部分代码。我导入了没有 headings/cities ID 的矩阵,因为它们等同于 NetLogo 索引。具有 ID 的城市已导入到模型中。此外,在每个 point/city 中,我创建了我从 CSV 文件中为每个城市读取的相应数量的代理。在创建代理期间,我使用矩阵中与当前 city/origin 相对应的行,并像 JenB 在他上面的回答中所做的那样检查概率。
breed [city cities]
breed [inhabitant inhabitants]
;; part of the setup
;; open the csv file that contains population per city
file-open path
while [ not file-at-end? ] [
let data csv:from-row file-read-line
let city-id item 0 data
let population item 1 data
to add-inhabitants
create-inhabitants population [
set city-home one-of cities with [id = city-id] ;; set the origin
move-to city-home
set-destination(city-id) ;; for this origin chose the destination
]
]
to set-destination [row] ;; row from the matrix which represent the movement from the current city/origin to other cities
let row-probabilities matrix:get-row od-matrix row ;; use the row that correspondents to the city
let random-value random-float 1
let i 0 ;; index counter
foreach row-probabilities [ ;; for each probability in row
p ->
if random-value <= p ;; this part is coded as in the JenB's answer
[
set destination one-of cities with [id = i] ;; i is column index which is actually index of destination city
stop ;; if you set city -> stop
]
if i = 73 [set destination one-of cities with [id = i]
stop] ;; the last city -> there is no more option/reason to check
set i i + 1
]
end
我知道有点长,但我想解释清楚。非常感谢一些指南和解释!
您的问题表明您不理解该部分中的代码的作用。这是该位的更正代码:
ask turtles with [residency = "nw"]
[ let myrandom random-float 1
ifelse myrandom <= 0.5 [ set destination "nw" ] [
ifelse myrandom <= 0.8 [ set destination "ne" ] [
ifelse myrandom <= 0.9 [ set destination "sw" ] [
set destination "se" ]]]
是的,块中的第一行抽取了一个 0 到 1 范围内的随机数。假设抽取的是 0.4。那么第一个 ifelse
将为真,目的地将设置为“nw”。现在想象平局是 0.6,那么第一个 ifelse
将是假的,代码将继续测试 else
部分,这是真的,因为 0.6 <= 0.8.
该代码有效,因为初始绘制是统一的。也就是说,10% 的时间(平均)returns 是 0 到 0.1 范围内的数字,10% 的时间是 0.1 到 0.2 范围内的数字,依此类推。所以 50% 的时间它 returns 一个 0 到 0.5 范围内的数字,第一行 returns 为真。 30% 的时间 returns 是 0.5 到 0.8 范围内的数字,第一次测试为假,第二次测试为真。 10% 的时间 returns 是 0.8 到 0.9 范围内的数字,10% 的时间是 returns 0.9 到 1 范围内的数字。
因此,在 0.5、0.8、0.9(其余部分为 1)处打破检查点的区间会得到 0.5、0.3(=0.8-0.5)、0.1(=0.9-0.8)和 0.1( =1=0.9) 的长度。并且一个统一的随机数会落入概率分布为50%、30%、10%、10%的抽奖区间