从 NetLogo 中的列表创建矩阵
Creating a matrix from lists in NetLogo
我正在使用 NetLogo 来模拟人群中的病毒感染。受感染的个体是红色的,他们还存储一个数值来表示他们感染的病毒株。我想制作一个矩阵,对于每个时间步长,只存储人群中受感染个体的菌株标签(并可能使用一些占位符来表示未感染的个体)。
我的人口中有 100 个人,每个模拟运行 365 个时间步,所以我知道矩阵将有 100 列和 365 行——我只是不知道如何将其转换为 NetLogo 语法。例如:
to track-strain-diversity
report list ([tflu-strain] of turtles with [color = red])
how do I store this list as a row for a single timestep in a matrix?
how do I then fill in each subsequent row with the flu strains found in each timestep?
如有任何帮助,我们将不胜感激!
您可能想查看该扩展中的 matrix extension if you're hoping to use indexing to access some of the values, or if you want to do some matrix math in the model after you've collected your strain info. If you want to go this route, check out the matrix:set-row
原语。
如果您只是记录这些值,然后将它们输出到其他地方进行分析,您可能会满足于只使用列表列表来存储每个刻度的菌株,然后在最后使用 csv extension。
以下示例使用此设置:
extensions [ csv ]
globals [ strains strain_list_list ]
turtles-own [ infected? flu-strain ]
to setup
ca
set strains [ 1 2 3 ]
crt 5 [
ifelse random 2 = 0 [
set infected? true
set flu-strain one-of strains
] [
set infected? false
]
]
set strain_list_list ( list t-sorted-strain-list )
print strain_list_list
reset-ticks
end
使用此选项,您仍然需要像您提到的那样使用某种占位符,以便您的行的长度都相同。你可以让记者这样做,比如:
to-report my-strain ; turtle reporter
ifelse infected? [
report flu-strain
] [
report 0
]
end
那个记者(被一只乌龟叫)报告说,如果乌龟有流感毒株数,如果没有,则为零。为了整洁起见,您可以将其报告为按海龟排序的列表,以便每个 "column" 对应于一只海龟:
to-report t-sorted-strain-list
report map [ i -> [flu-strain] of i ] sort turtles
end
然后只需 运行 构建您的模型并将 t-sorted-strain-list
作为列表列表 (strain-list-list
) 中的新条目记录下来。当您的模型一直 运行 时,您可以将整个列表列表输出为 csv 文件,以便使用 csv:to-file
进行分析。在此示例中,模型将 go
直到所有海龟都被感染,此时模型将 strain_list_list
输出到名为 "strains_each_tick.csv" 的 csv 文件,然后模型将 stop
.
to go
if not any? turtles with [ not infected? ] [
csv:to-file "strains_each_tick.csv" strain_list_list
stop
]
ask turtles with [ not infected? ] [
if random 5 = 0 [
set flu-strain one-of strains
set infected? true
]
]
set strain_list_list lput t-sorted-strain-list strain_list_list
tick
end
加载到 R 中的输出文件类似于:
> df
V1 V2 V3 V4 V5
1 2 0 0 3 2
2 2 0 0 3 2
3 2 0 2 3 2
4 2 0 2 3 2
5 2 3 2 3 2
我正在使用 NetLogo 来模拟人群中的病毒感染。受感染的个体是红色的,他们还存储一个数值来表示他们感染的病毒株。我想制作一个矩阵,对于每个时间步长,只存储人群中受感染个体的菌株标签(并可能使用一些占位符来表示未感染的个体)。
我的人口中有 100 个人,每个模拟运行 365 个时间步,所以我知道矩阵将有 100 列和 365 行——我只是不知道如何将其转换为 NetLogo 语法。例如:
to track-strain-diversity
report list ([tflu-strain] of turtles with [color = red])
how do I store this list as a row for a single timestep in a matrix?
how do I then fill in each subsequent row with the flu strains found in each timestep?
如有任何帮助,我们将不胜感激!
您可能想查看该扩展中的 matrix extension if you're hoping to use indexing to access some of the values, or if you want to do some matrix math in the model after you've collected your strain info. If you want to go this route, check out the matrix:set-row
原语。
如果您只是记录这些值,然后将它们输出到其他地方进行分析,您可能会满足于只使用列表列表来存储每个刻度的菌株,然后在最后使用 csv extension。
以下示例使用此设置:
extensions [ csv ]
globals [ strains strain_list_list ]
turtles-own [ infected? flu-strain ]
to setup
ca
set strains [ 1 2 3 ]
crt 5 [
ifelse random 2 = 0 [
set infected? true
set flu-strain one-of strains
] [
set infected? false
]
]
set strain_list_list ( list t-sorted-strain-list )
print strain_list_list
reset-ticks
end
使用此选项,您仍然需要像您提到的那样使用某种占位符,以便您的行的长度都相同。你可以让记者这样做,比如:
to-report my-strain ; turtle reporter
ifelse infected? [
report flu-strain
] [
report 0
]
end
那个记者(被一只乌龟叫)报告说,如果乌龟有流感毒株数,如果没有,则为零。为了整洁起见,您可以将其报告为按海龟排序的列表,以便每个 "column" 对应于一只海龟:
to-report t-sorted-strain-list
report map [ i -> [flu-strain] of i ] sort turtles
end
然后只需 运行 构建您的模型并将 t-sorted-strain-list
作为列表列表 (strain-list-list
) 中的新条目记录下来。当您的模型一直 运行 时,您可以将整个列表列表输出为 csv 文件,以便使用 csv:to-file
进行分析。在此示例中,模型将 go
直到所有海龟都被感染,此时模型将 strain_list_list
输出到名为 "strains_each_tick.csv" 的 csv 文件,然后模型将 stop
.
to go
if not any? turtles with [ not infected? ] [
csv:to-file "strains_each_tick.csv" strain_list_list
stop
]
ask turtles with [ not infected? ] [
if random 5 = 0 [
set flu-strain one-of strains
set infected? true
]
]
set strain_list_list lput t-sorted-strain-list strain_list_list
tick
end
加载到 R 中的输出文件类似于:
> df
V1 V2 V3 V4 V5
1 2 0 0 3 2
2 2 0 0 3 2
3 2 0 2 3 2
4 2 0 2 3 2
5 2 3 2 3 2