核心内容摘要
《星球大战》:星辰大海中的不朽史诗,点亮你心中的满天星光
NetLogo编程入门
NetLogo基础概念NetLogo 是一个强大的多代理仿真软件主要用于模拟复杂系统中的个体行为及其相互作用。
它最初由美国西北大学的 Uri Wilensky 开发现在由 Northwestern University 的 Center for Connected Learning and Computer-Based Modeling 维护。
NetLogo 提供了一个直观的界面使得用户可以轻松地创建和运行自己的模型同时也支持通过编程来实现更复杂的仿真逻辑。
1 代理Agents在 NetLogo 中仿真系统的核心是由多个代理Agents组成的。
代理可以是以下几种类型之一Turtles表示移动的个体可以有自己的位置、方向、颜色等属性。
Patches表示环境中的一个固定位置可以有自己的颜色、属性等。
Links表示代理之间的连接可以用于模拟社会网络中的关系。
2 世界World世界World是代理活动的环境。
它由一个二维的网格Grid组成网格中的每一个单元称为一个Patches。
代理Turtles可以在这些 Patches 上移动。
3 界面InterfaceNetLogo 提供了一个图形用户界面GUI用户可以通过这个界面来设置模型参数、运行模型、观察模型运行结果。
界面的主要组成部分包括按钮Buttons用于启动模型的初始化、运行等操作。
滑块Sliders用于设置模型参数的值。
监控器Monitors用于显示模型运行过程中的某些变量的值。
图表Plots用于绘制模型运行过程中的数据变化趋势。
输出区域Output Area用于输出模型运行过程中的日志信息。
4 语言和语法NetLogo 使用一种基于 Logo 的编程语言这种语言简单易学但功能强大。
NetLogo 的语法主要包括以下几部分命令Commands用于执行某个操作。
报告Reports用于返回某个值。
变量Variables用于存储数据。
过程Procedures用于定义一系列操作的集合类似于函数。
创建第一个模型在这一节中我们将通过一个简单的例子来介绍如何使用 NetLogo 创建一个模型。
我们将创建一个模型模拟一群人在一个二维环境中随机移动。
1 初始化模型首先我们需要初始化模型。
这包括设置世界参数、创建代理、设置代理的初始状态。
globals [ num-turtles ] to setup clear-all set num-turtles 100 create-turtles num-turtles [ set color blue set shape person setxy random-xcor random-ycor ] reset-ticks end
2 代理移动接下来我们定义一个过程让代理在每一步中随机移动。
to go ask turtles [ right random 360 forward 1 ] tick end
3 设置界面为了使模型更直观我们需要在界面上添加一些控件。
按钮用于启动setup和go过程。
滑块用于调整num-turtles的值。
输出区域用于输出模型运行过程中的日志信息。
在界面中添加这些控件的步骤如下按钮在界面编辑器中选择“Button”工具点击界面空白处添加按钮。
在按钮的设置中选择setup和go过程。
滑块选择“Slider”工具点击界面空白处添加滑块。
在滑块的设置中选择num-turtles变量并设置其范围。
输出区域选择“Output”工具点击界面空白处添加输出区域。
4 运行模型保存模型并点击setup按钮来初始化模型。
然后点击go按钮来运行模型。
你将看到一群蓝色的小人Turtles在环境中随机移动。
代理之间的交互在社会网络仿真中代理之间的交互是非常重要的。
我们可以使用 NetLogo 的Links功能来模拟这种交互。
1 创建链接假设我们想要模拟一个简单的社交网络其中每个人可以随机选择一个朋友。
to setup clear-all set num-turtles 100 create-turtles num-turtles [ set color blue set shape person setxy random-xcor random-ycor ] create-links reset-ticks end to create-links ask turtles [ create-link-with one-of other turtles ] end
2 链接属性链接也可以有自己的属性例如亲密度closeness。
links-own [ closeness ] to create-links ask turtles [ create-link-with one-of other turtles [ set closeness random 100 ] ] end
3 链接可视化我们可以通过链接的颜色来可视化亲密度。
to show-links ask links [ set color scale-color red closeness 0 100 ] end在go过程中调用show-links过程使链接的颜色在每一步更新to go ask turtles [ right random 360 forward 1 ] show-links tick end
模型参数和动态变化在社会网络仿真中模型参数的动态变化可以模拟各种现实场景例如人口增长、资源变化等。
1 动态增加代理假设我们想要在模型运行过程中动态增加代理。
to add-turtle create-turtles 1 [ set color blue set shape person setxy random-xcor random-ycor ] create-links show-links end在界面上添加一个按钮关联add-turtle过程用户可以通过点击这个按钮来增加一个新的代理。
2 动态改变链接属性假设我们想要在模型运行过程中动态改变链接的亲密度。
to update-closeness ask links [ set closeness closeness random 10 - 5 if closeness 0 [ set closeness 0 ] if closeness 100 [ set closeness 100 ] ] show-links end在go过程中调用update-closeness过程to go ask turtles [ right random 360 forward 1 ] update-closeness tick end
模型分析和数据输出在仿真过程中我们通常需要分析模型的运行结果并将数据输出到外部文件或图表中。
1 数据输出NetLogo 提供了output-print命令来输出数据到输出区域也可以使用file-write和file-print命令将数据写入到文件中。
to output-data output-print (word Number of turtles: count turtles) output-print (word Average closeness: mean [closeness] of links) end to go ask turtles [ right random 360 forward 1 ] update-closeness output-data tick end
2 数据可视化NetLogo 的图表功能可以用来绘制模型运行过程中的数据变化趋势。
to setup clear-all set num-turtles 100 create-turtles num-turtles [ set color blue set shape person setxy random-xcor random-ycor ] create-links reset-ticks setup-plots end to setup-plots create-plot Number of Turtles [ set-plot-x-range 0 100 set-plot-y-range 0 150 create-plot-pen Turtles [ set-pen-color blue ] ] create-plot Average Closeness [ set-plot-x-range 0 100 set-plot-y-range 0 100 create-plot-pen Closeness [ set-pen-color red ] ] end to go ask turtles [ right random 360 forward 1 ] update-closeness update-plots tick end to update-plots plotxy ticks count turtles plotxy ticks mean [closeness] of links end
高级编程技巧NetLogo 提供了许多高级编程技巧可以帮助用户更高效地开发复杂的模型。
1 使用列表和集合在 NetLogo 中列表Lists和集合Sets是非常有用的工具可以用来存储和处理多个值。
to setup clear-all set num-turtles 100 create-turtles num-turtles [ set color blue set shape person setxy random-xcor random-ycor set friends (list) ] create-links reset-ticks end to create-links ask turtles [ let new-friend one-of other turtles create-link-with new-friend set friends lput new-friend friends ] end to go ask turtles [ right random 360 forward 1 move-to one-of friends ] update-plots tick end
2 使用表格TablesNetLogo 的table扩展库可以用来存储键值对类似于 Python 中的字典。
extensions [table] globals [ turtle-table ] to setup clear-all set num-turtles 100 set turtle-table table:make create-turtles num-turtles [ set color blue set shape person setxy random-xcor random-ycor table:put turtle-table who random 100 ] create-links reset-ticks end to go ask turtles [ right random 360 forward 1 let friend one-of friends if friend ! nobody [ move-to friend let new-closeness (table:get turtle-table [who] of friend) table:put turtle-table [who] of friend (new-closeness
] ] update-plots tick end
3 使用 NetLogo 的随机数生成器NetLogo 提供了多种随机数生成器可以用来模拟不同的随机过程。
to setup clear-all set num-turtles 100 create-turtles num-turtles [ set color blue set shape person setxy random-xcor random-ycor set heading random 360 ] create-links reset-ticks end to go ask turtles [ right random-normal 0 20 forward 1 ] update-plots tick end
4 使用 NetLogo 的并行计算NetLogo 支持并行计算可以显著提高模型的运行效率。
to setup clear-all set num-turtles 100 create-turtles num-turtles [ set color blue set shape person setxy random-xcor random-ycor ] create-links reset-ticks end to go ask turtles with [color blue] [ right random 360 forward 1 ] ask turtles with [color red] [ right random 360 forward 2 ] update-plots tick end
社会网络仿真案例在这一节中我们将通过一个具体的案例来展示如何使用 NetLogo 进行社会网络仿真。
我们将模拟一个简单的疾病传播模型。
1 初始化模型首先我们需要初始化模型包括设置世界参数、创建代理、设置代理的初始状态。
globals [ infection-prob recovery-prob num-susceptible num-infected num-recovered ] turtles-own [ status ] to setup clear-all set infection-prob
05 set recovery-prob
05 set num-turtles 100 set num-susceptible num-turtles set num-infected 0 set num-recovered 0 create-turtles num-turtles [ set color blue set shape person setxy random-xcor random-ycor set status susceptible ] infect-initial create-links reset-ticks setup-plots end to infect-initial ask one-of turtles [ set color red set status infected set num-susceptible num-susceptible - 1 set num-infected num-infected 1 ] end
2 代理行为接下来我们定义代理的行为包括疾病传播和恢复。
to go ask turtles with [status infected] [ spread-disease recover ] ask turtles with [status susceptible] [ right random 360 forward 1 ] update-plots tick end to spread-disease let neighbors other turtles in-radius 1 ask neighbors with [status susceptible] [ if random-float 1 infection-prob [ set color red set status infected set num-susceptible num-susceptible - 1 set num-infected num-infected 1 ] ] end to recover if random-float 1 recovery-prob [ set color green set status recovered set num-infected num-infected - 1 set num-recovered num-recovered 1 ] end
3 设置界面为了使模型更直观我们需要在界面上添加一些控件。
按钮用于启动setup和go过程。
滑块用于调整infection-prob和recovery-prob的值。
图表用于绘制疾病传播过程中各状态代理的数量变化。
在界面中添加这些控件的步骤如下按钮在界面编辑器中选择“Button”工具点击界面空白处添加按钮。
在按钮的设置中选择setup和go过程。
滑块选择“Slider”工具点击界面空白处添加滑块。
在滑块的设置中选择infection-prob和recovery-prob变量并设置其范围。
图表选择“Plot”工具点击界面空白处添加图表。
在图表的设置中创建两个图表一个用于绘制num-susceptible、num-infected和num-recovered的变化趋势另一个用于绘制infection-prob和recovery-prob的变化趋势。
4 模型分析和数据输出我们可以通过输出数据和绘制图表来分析模型的运行结果。
to setup-plots create-plot Disease Spread [ set-plot-y-range 0 100 create-plot-pen Susceptible [ set-pen-color blue ] create-plot-pen Infected [ set-pen-color red ] create-plot-pen Recovered [ set-pen-color green ] ] create-plot Probabilities [ set-plot-y-range 0 1 create-plot-pen Infection Probability [ set-pen-color red ] create-plot-pen Recovery Probability [ set-pen-color green ] ] end to update-plots plotxy ticks num-susceptible plotxy ticks num-infected plotxy ticks num-recovered plotxy ticks infection-prob plotxy ticks recovery-prob end
扩展和优化在这一节中我们将介绍如何扩展和优化模型使其更符合现实场景。
1 扩展模型功能我们可以扩展模型增加更多的功能例如隔离措施、疫苗接种等。
8.
1 隔离措施假设我们想要在某些情况下隔离感染的代理。
to isolate-infected ask turtles with [status infected] [ if random-float 1
1 [ set color gray set status isolated set num-infected num-infected - 1 ] ] end to go ask turtles with [status infected] [ spread-disease recover ] isolate-infected ask turtles with [status susceptible] [ right random 360 forward 1 ] update-plots tick end
8.
2 疫苗接种假设我们可以在某些情况下给代理接种疫苗使其不再感染。
to vaccinate ask turtles with [status susceptible] [ if random-float 1
05 [ set color yellow set status vaccinated set num-susceptible num-susceptible - 1 ] ] end to go ask turtles with [status infected] [ spread-disease recover ] isolate-infected vaccinate ask turtles with [status susceptible] [ right random 360 forward 1 ] update-plots tick end
2 优化模型性能在进行大规模仿真时模型的性能优化是非常重要的。
以下是一些优化技巧
8.
1 减少代理数量在某些情况下减少代理的数量可以显著提高模型的运行效率。
可以通过调整num-turtles的值来实现。
to setup clear-all set infection-prob
05 set recovery-prob
05 set num-turtles 50 ; 减少代理数量 set num-susceptible num-turtles set num-infected 0 set num-recovered 0 create-turtles num-turtles [ set color blue set shape person setxy random-xcor random-ycor set status susceptible ] infect-initial create-links reset-ticks setup-plots end
8.
2 使用并行计算NetLogo 支持并行计算可以显著提高模型的运行效率。
通过使用ask命令的并行版本ask-concurrent可以实现并行计算。
to go ask-concurrent turtles with [status infected] [ spread-disease recover ] isolate-infected vaccinate ask-concurrent turtles with [status susceptible] [ right random 360 forward 1 ] update-plots tick end
3 模型验证和调试在完成模型开发后验证和调试模型是非常重要的步骤以确保模型的正确性和可靠性。
8.
1 模型验证可以通过比较模型的输出结果与已知的理论模型或实际数据来验证模型的正确性。
例如可以将模型的疾病传播结果与经典的 SIR 模型进行比较。
to output-data output-print (word Number of susceptible turtles: num-susceptible) output-print (word Number of infected turtles: num-infected) output-print (word Number of recovered turtles: num-recovered) end to go ask-concurrent turtles with [status infected] [ spread-disease recover ] isolate-infected vaccinate ask-concurrent turtles with [status susceptible] [ right random 360 forward 1 ] output-data update-plots tick end
8.
2 模型调试可以使用 NetLogo 的调试工具来逐步执行模型观察每个代理的行为和状态变化。
此外还可以使用print命令在模型运行过程中输出中间结果帮助定位问题。
to go ask-concurrent turtles with [status infected] [ print (word Infected turtle who is spreading disease.) spread-disease print (word Infected turtle who is recovering.) recover ] isolate-infected vaccinate ask-concurrent turtles with [status susceptible] [ print (word Susceptible turtle who is moving.) right random 360 forward 1 ] output-data update-plots tick end
9.
总结通过以上内容我们介绍了如何使用 NetLogo 创建一个简单的社会网络仿真模型并逐步扩展和优化模型。
NetLogo 提供了丰富的功能和工具使得用户可以轻松地进行复杂系统的仿真。
希望这些内容能帮助你更好地理解和使用 NetLogo。