首页 > 解决方案 > 为两个不同的场景运行从同一个补丁初始化海龟

问题描述

我想运行我的 NetLogo 模型,看看景观场景“基线”和“未来”的变化如何影响代理的年度旅行距离。我已经在其住宅邮政编码边界内的随机补丁上初始化了代理。

为了能够比较旅行的变化,我应该让它们从两个场景的同一个补丁中初始化。我一直在尝试使用随机种子,但无法让它工作。我想为 100 个不同的初始补丁运行模型,但在单次运行中为每个基线和未来场景维护相同的补丁(理想情况下,这是两次运行,一个带有基线场景,一个是未来场景。当我在设置中使用随机种子时, 它为每个场景从不同的补丁初始化代理。我尝试设置一个全局变量 random-seed-turtles 并尝试在行为空间中使用两个不同的种子运行 2 次。

[ "random-seed-turtles"  1 2 ]   
[  "landscape-scenario"   "baseline" "future"] 

它为基线的每次运行从相同的补丁创建海龟,但对于未来的场景有所不同。

在此处输入图像描述

有没有一种方法可以编码,以便我可以为 100 次运行中的每一次运行的海龟提供不同的初始补丁,但单个运行的来源相同。例如

run1 
baseline my_home  = patch 113 224
future  my_home  = patch 113 224

另外,插入随机种子命令的位置是否重要?补丁值(景观可用性)每个刻度都会更改,从为该时间步准备的栅格中读取。Landscape_scenario 是界面上的选择器,“基线”和“未来”的值分别读取一组不同的栅格。这会干扰随机种子吗?

标签: gisnetlogo

解决方案


注意:下面的答案假设您的情况是您希望基线未来场景作为单个模型迭代的一部分运行。

另外,我提出了两种(非常相似的)方法。您更喜欢哪一种取决于您的具体情况和需求,我们不知道,因为您没有共享模型的结构。


您必须创建一个海龟自己的变量,每个海龟将直接存储其起始补丁。

然后,当您关闭基线场景并准备未来场景时,您将不得不手动删除所有要删除的变量,除了海龟存储其起始补丁的变量(因此,例如,您不应该使用clear-allclear-turtles那个段落,因为这样的命令也会清除你想要保留的海龟自己的变量)。

要通过示例显示该方法,请参见下面的代码:

globals [
 ; Put here your gobal variables.
]

patches-own [
 ; Put here your patches' variables.
]

turtles-own [
 my-start
 my-value  ; I only included this to show that you have to manually clear the turtles' variables EXCEPT from 'my-start'.
]


to setup
  clear-all
  reset-ticks
  
  ; Do here what you need to do to prepare the baseline scenario.

  create-turtles 20 [
   setxy random-xcor random-ycor
   set my-value random 10
   set my-start patch-here
  ]
end


to go
  run-model
  
  clear-baseline-scenario
  prepare-future-scenario
  
  run-model
end


to run-model
  ; Put here all the things you need to have to run your model,
  ; including a stop condition (and ticks, if you want them).
end


to clear-baseline-scenario
  clear-globals
  clear-patches
  reset-ticks
  
  ; Now, you also have to manually clear all your turtles' variables EXCEPT
  ; from 'my-start':
  ask turtles [
   set my-value 0 
  ]
end


to prepare-future-scenario
  ; Do here what you need to do to prepare the future scenario,
  ; and also tell your agents to go to their starting patch:
  
  ask turtles [
   move-to my-start 
  ]
end

还有另一种方法,基本上是相同的解决方案,但适用于补丁而不是海龟。

在这种情况下,您将拥有一个 patch-own 变量,该变量表明一个补丁是否是一个起始补丁(例如,通过取 1/0 值,或者TRUE/FALSE如果您愿意)。

然后,当从基线场景转到未来场景时,您将清除该 patch-own 变量之外的所有需要​​清除的内容(即在该段落中不使用clear-allor clear-patches,因为此类命令也会清除 patch-own 变量你想保留的)。

如果您不想让完全相同的海龟从同一个补丁开始,这种方法是可行的,但是您很高兴让任何海龟从任何起始补丁开始。

所以它会是这样的:

globals [
 ; Put here your gobal variables.
]

patches-own [
 slope  ; I only included this to show that you have to manually clear the patches' variables EXCEPT from 'starting-patch?'.
 starting-patch?
]

turtles-own [
  ; Put here your turtles' variables.
]


to setup
  clear-all
  reset-ticks
  
  ; Do here what you need to do to prepare the baseline scenario.
  
  create-turtles 20 [
   setxy random-xcor random-ycor
   set starting-patch? TRUE
  ]
  
  ask patches [
   set slope random 5
   if (starting-patch? = 0) [
     set starting-patch? FALSE
    ] 
  ]
end


to go
  run-model
  
  clear-baseline-scenario
  prepare-future-scenario
  
  run-model
end


to run-model
  ; Put here all the things you need to have to run your model,
  ; including a stop condition (and ticks, if you want them).
end


to clear-baseline-scenario
  clear-globals
  clear-turtles
  reset-ticks
  
  ; Now, you also have to manually clear all your patches' variables that you
  ; want to clear EXCEPT from 'my-start':
  ask patches [
   set slope 0 
  ]
end


to prepare-future-scenario
  ; Do here what you need to do to prepare the future scenario,
  ; and also tell your agents to go to an empty starting patch:
  
  create-turtles 20 [
   move-to one-of patches with [(starting-patch?) AND (not any? turtles-here)]
  ]
end

推荐阅读