首先,让我描述一下我在做什么,以及为什么我要问这个问题。
我有74个城市包含在模型中,我想模拟它们之间的运动。我有一个OD概率矩阵,其中行是起点,列是目的地。Matrix看起来像这样:
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范围内的值,然后它会一个接一个地检查它的值是否小于或等于这个常量值。从这个意义上说,我的随机创伤永远不会到达"sw“部分(0总是小于0.5),并且有更多的机会获得概率为0.5的"nw”部分,而不是概率更高的"ne“部分-0.8。而且只是因为它没有列在第一位。我不确定这是不是正确的方法,我也不确定哪一个是正确的(我不能对我的概率进行排序,因为它们的位置代表城市id(见下文))。还是我理解错了?
下面是我的代码的一部分。我导入了没有标题/城市ID的矩阵,因为它们等同于NetLogo索引。ID为的城市已经导入到模型中。此外,在每个点/城市中,我创建了从CSV文件中为每个城市读取的相应代理数量。在创建代理期间,我使用与当前城市/来源相对应的矩阵中的行,并像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我知道这有点长,但我想解释清楚。如果能提供一些指导和解释,我们将不胜感激!
发布于 2020-09-11 18:05:54
您的问题表明您不理解该部分中的代码在做什么。这是该位的校正代码:
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将为false,代码将继续测试else部分,这是真的,因为0.6 <= 0.8。
该代码之所以有效,是因为初始绘制是均匀的。也就是说,10%的时间(平均)它返回0到0.1范围内的数字,10%返回0.1到0.2范围内的数字,依此类推。因此,50%的情况下,它返回0到0.5范围内的数字,第一行返回true。30%的时间,它返回一个介于0.5到0.8之间的数字,这对于第一个测试为false,对于第二个测试为true。10%的时间返回0.8到0.9范围内的数字,10%的时间返回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%的部分。
https://stackoverflow.com/questions/63832251
复制相似问题