NetLogo:能否将最后两个刻度的全局变量的值存储在列表中并在过程中调用?

问题描述 投票:4回答:1

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

    `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]

用于计算时间车的逻辑已经就绪并且可以正常工作,其中所有三个都是全局变量“时间车”,“时间车t-1”和“时间车t-2”]

任何建议都会有所帮助,我将不胜感激。预先感谢。

memory global-variables netlogo history
1个回答
2
投票
NetLogo不会记住过去的值,因此它无法在过去的变动中为您提供变量的值(或报告者的结果)。您需要在生成这些值时将其保存在模型中,通常通过使用列表来完成。在下面的代码中,每只海龟都设置有长度为5(由time-car-history指定)的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

我希望这可以帮助您入门,并欢迎您进入NetLogo社区。

查尔斯

© www.soinside.com 2019 - 2024. All rights reserved.