首页 > 解决方案 > NetLogo:最后两个刻度的全局变量的值可以存储在列表中并在过程中调用吗?

问题描述

我希望代理只记住最后一个刻度的全局变量的值,我想将它们存储在一个列表中并稍后使用它,其中代理应该比较列表项并为当前刻度做出决定。我已经尝试实现以下代码,但这些努力都是徒劳的。

    `set time-car_t-2 time-car of tick [n - 2]
     set time-car_t-1 time-car of last tick
     set history-time-car [list time-car_t-1 time-car_t-2 time-car_t]

计算时间汽车的逻辑已经到位并且正在工作,其中三个都是全局变量“time-car”、“time-car_t-1”和“time-car_t-2”

任何建议都会有所帮助,我将不胜感激。提前致谢。

标签: memoryglobal-variablesnetlogohistory

解决方案


NetLogo 不记得过去的值,因此它不能为您提供过去滴答时变量(或报告者的结果)的值。您需要在生成这些值时将它们保存在模型中,这通常通过使用列表来完成。在下面的代码中,每个海龟都设置了time-car-history长度为 5(由 指定history-length)的 a,最初用 -1 填充。然后,在 中test,每只海龟的值time-car(这里只是一个随机数)并将其添加到其历史的开头。因此,当前值item 0在其time-car-history中,前一个刻度的值是item 1 time-car-history,等等,返回四个刻度。请注意,在将当前值添加到time-car-history我使用删除最后一个值时but-last,因此只保存最近的五个值。如果将此代码粘贴到空白模型中,在命令行中键入“setup”,然后重复键入“test”,您应该会看到它是如何工作的。

turtles-own [time-car-history]

to setup
  clear-all
  let history-length 5 ; the number of periods you want to save
  create-turtles 10 [
    ; creates a history list of the right length, filled with -1's.
    set time-car-history n-values history-length [-1]
  ]
  reset-ticks
end

to test
  ask turtles [
    set time-car-history fput time-car but-last time-car-history
  ]

  ask turtle 3 [
    show time-car-history
    show item 0 time-car-history
    show item 1 time-car-history
    show item 2 time-car-history
  ]
  tick
end

to-report time-car
  report random 10
end

推荐阅读