如何使用补丁来计算 Netlogo 购物模型中的周数?

发布于 2025-01-12 20:53:54 字数 95 浏览 5 评论 0原文

我必须创建一个 Netlogo 世界,其中 N 个绿色块用于家庭,M 个蓝色块用于超市。在世界上,海龟从家(绿色区域)到超市(蓝色区域),然后再回到家。我在下面发布了我的代码。

I have to create a Netlogo-world with N green patches for households and M blue patches for supermarkets. In the world turtles go from home (green patch) to the supermarket (blue patch) and back home again. I posted my code of this below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2025-01-19 20:53:54

为什么要让 week 变量成为补丁变量?换句话说,为什么需要补丁来测量时间?

我认为您应该考虑其他两个选项:

  1. 使用全局变量测量时间:已经有 ticks 全局变量,但您可以创建自己的全局变量,每个代理都会能够阅读它。
  2. 让你的乌龟测量它们想去商店的频率:每只乌龟都可以有一个乌龟自己的变量,用作倒计时。当倒计时到0时,乌龟就会离开去逛商店。

例如,如果据我了解,您希望每只乌龟每 70 个刻度访问一次他们选择的商店,您可以这样做。

turtles-own [
  housePatch
  target
  storeVisited
  phase
  countdown
]


to setup
  clear-all
  reset-ticks
  
  ask n-of 10 patches [set pcolor blue]
  
  ask n-of 15 patches with [ pcolor != blue ][
    set pcolor green
    sprout 1 [
      set target one-of patches with [pcolor = blue]
      set housePatch patch-here
      set phase 1

      set color red
      set size 1
    ]
  ]
end 


to go
  ask turtles [
    ifelse (phase = 1)
      [shop]
      [return]
  ]
  
  ask turtles with [countdown > 0] [
    set countdown countdown - 1
  ]
  
  tick
end


to shop
  if (countdown = 0) [
    face target
    forward 1
  ]
  
  if (patch-here = target) [
    set storeVisited storeVisited + 1
    set phase 2
  ]
end


to return
  face housePatch
  forward 1
  
  if (patch-here = housePatch) [
    set countdown 70
    set phase 1
  ]
end

让我们探讨一下我在这里实现的主要内容:

  1. 阶段:设置模型的方式意味着,在 go 的大多数迭代中,每只海龟要么执行 shop 或执行 return。然而,你的go是:
to go
 ask turtles [
   shop
   return
 ]
end

当我去实现倒计时时,这造成了一些混乱,所以我想用一个phase海龟自己的变量来清楚地分割海龟的活动:值为 1 意味着海龟必须执行 shop;值为 2 意味着海龟必须执行 returnphase的值在setup时和每次乌龟回家时设置为1,每次乌龟到达商店时设置为2。

正如您所看到的,我从模型中删除了 stay 因为它使海龟什么也不做。

  1. 倒计时:每只海龟都有一个倒计时变量,在shop开始时检查。如果countdown达到0,那么乌龟实际上会向商店移动。 countdown 的值在设置时保留默认值 0,这意味着一开始每只海龟都会朝商店移动。每次访问商店后返回家时,countdown 的值设置为 70。在 go 的每次迭代中,所有倒计时大于零的海龟都会带上它下降了一位。

请注意,我对代码进行了一些其他细微更改,其中一些是:

  • 我从 setup 过程中删除了 set storeVisited 0。 NetLogo中变量的默认值为0,所以不需要一开始就把它们设置为0。
  • go 结束时,您使用的是 reset-ticks 而不是 tick
  • shop 中,我将 if (pcolor = blue) 替换为 if (patch-here = target)。这是因为,在前往目标的途中,海龟可能会发现自己位于一块不是其目标的蓝色斑块上。

Why do you want the week variable to be a patch variable? In other words, why do you want patches to measure time?

I think you should consider two other options:

  1. Measure time with a global variable: there already is the ticks global variable, but you can create your own global variable and every agent will be able to read it.
  2. Let your turtles measure how often they want to go to the shop: each turtle can have a turtle-own variable that is used as a countdown. When the countdown reaches 0, the turtle will leave to visit the shop.

For example, if as I understood you want each turtle to visit their chosen shop every 70 ticks, you can do something like.

turtles-own [
  housePatch
  target
  storeVisited
  phase
  countdown
]


to setup
  clear-all
  reset-ticks
  
  ask n-of 10 patches [set pcolor blue]
  
  ask n-of 15 patches with [ pcolor != blue ][
    set pcolor green
    sprout 1 [
      set target one-of patches with [pcolor = blue]
      set housePatch patch-here
      set phase 1

      set color red
      set size 1
    ]
  ]
end 


to go
  ask turtles [
    ifelse (phase = 1)
      [shop]
      [return]
  ]
  
  ask turtles with [countdown > 0] [
    set countdown countdown - 1
  ]
  
  tick
end


to shop
  if (countdown = 0) [
    face target
    forward 1
  ]
  
  if (patch-here = target) [
    set storeVisited storeVisited + 1
    set phase 2
  ]
end


to return
  face housePatch
  forward 1
  
  if (patch-here = housePatch) [
    set countdown 70
    set phase 1
  ]
end

Let's explore the main things I implemented here:

  1. Phases: The way you setup your model implies that, on most iterations of go, each turtle either executes shop or executes return. However, your go was:
to go
 ask turtles [
   shop
   return
 ]
end

This was creating some mess as I went to implement a countdown, so I thought to clearly split turtles' activities with a phase turtles-own variable: a value of 1 means that turtles will have to execute shop; a value of 2 means that turtles will have to execute return. The value of phase is set as 1 upon setup and every time a turtle returns home, and it is set as 2 every time a turtle reaches the shop.

As you can see, I removed stay from the model because it made turtles do nothing.

  1. Countdown: Each turtle has a countdown variable, that is checked at the beginning of shop. If countdown reached the value of 0, then the turtle actually moves towards the shop. The value of countdown is left at its default of 0 upon setup, which means that at the beginning every turtle will start by moving towards the shop. Upon returning home after each visit to the shop, the value of countdown is set to 70. At every iteration of go, all turtles whose countdown is greater than zero will bring it down by one.

Note that I implemented some other minor changes to the code, some of them being:

  • I removed set storeVisited 0 from within the setup procedure. The default value of variables is 0 in NetLogo, so there is no need to set them to 0 at the beginning.
  • At the end of go, you were using reset-ticks instead of tick.
  • In shop, I substituted if (pcolor = blue) with if (patch-here = target). This is because, on the way to target, a turtle might find itself on a blue patch which is not its target.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文