memory - 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”
任何建议都会有所帮助,我将不胜感激。提前致谢。
解决方案
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
推荐阅读
- google-cloud-platform - “您的 Google Cloud Platform/API 项目现在可以让您解决任何合规性问题。” 但是VM状态仍然被阻止,我现在想做什么?
- r - 为getsymbols创建一个函数
- c# - 重命名 XDocument 元素名称
- javascript - URL 片段是否有任何官方格式?
- java - 如何在不使用 indexOf 方法的情况下找到字符串中第一个 char 's' 的位置
- gulp - 尝试运行 gulp 时出现错误找不到模块“gulp-util”?
- java - Hibernate 5.3,坚持 java.time.* 截断到微秒
- python - 我正在制作一个 Discord 机器人,而“坏话”的自动版主不起作用
- powershell - Powershell Send-MailMessage foreach csv中的不同值
- android - 傻瓜式安卓开发