Shr*_*hya 5 memory history global-variables 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]
Run Code Online (Sandbox Code Playgroud)
计算 time-car 的逻辑已经就位并正在工作,其中所有三个都是全局变量“time-car”、“time-car_t-1”和“time-car_t-2”
任何建议都会有所帮助,我将不胜感激。提前致谢。
NetLogo 不记得过去的值,因此它无法为您提供过去某个时间点的变量值(或报告器的结果)。您需要在生成这些值时将其保存在模型中,这通常是通过使用列表来完成的。在下面的代码中,每只海龟都设置了time-car-history长度为 5 的 a(由 指定history-length),最初用 -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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
686 次 |
| 最近记录: |