gpt4 book ai didi

NetLogo 老化/预热问题

转载 作者:行者123 更新时间:2023-12-02 18:00:06 25 4
gpt4 key购买 nike

使用 NetLogo 5.3.1,我尝试设置BehaviorSpace,以便其所有模型运​​行在完全相同的 500 个刻度预热期之后开始。然而,结果对我来说并不直观。

出于说明目的,我将使用模型库中的“Flocking.nlogo”模型。下面是模型代码,在设置末尾添加了 2 行代码,用于在 500 个周期后保存模型的状态。

turtles-own [
flockmates ;; agentset of nearby turtles
nearest-neighbor ;; closest one of our flockmates
]

to setup
clear-all
create-turtles population
[ set color yellow - 2 + random 7 ;; random shades look nice
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set flockmates no-turtles ]
reset-ticks

; Now execute a 500-tick warm-up period and save the model's state
repeat 500 [ go ]
export-world "Flocking-after-500ticks.csv"

end

to go
ask turtles [ flock ]
;; the following line is used to make the turtles
;; animate more smoothly.
repeat 5 [ ask turtles [ fd 0.2 ] display ]
;; for greater efficiency, at the expense of smooth
;; animation, substitute the following line instead:
;; ask turtles [ fd 1 ]
tick
end

to flock ;; turtle procedure
find-flockmates
if any? flockmates
[ find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[ separate ]
[ align
cohere ] ]
end

to find-flockmates ;; turtle procedure
set flockmates other turtles in-radius vision
end

to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end

;;; SEPARATE

to separate ;; turtle procedure
turn-away ([heading] of nearest-neighbor) max-separate-turn
end

;;; ALIGN

to align ;; turtle procedure
turn-towards average-flockmate-heading max-align-turn
end

to-report average-flockmate-heading ;; turtle procedure
;; We can't just average the heading variables here.
;; For example, the average of 1 and 359 should be 0,
;; not 180. So we have to use trigonometry.
let x-component sum [dx] of flockmates
let y-component sum [dy] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end

;;; COHERE

to cohere ;; turtle procedure
turn-towards average-heading-towards-flockmates max-cohere-turn
end

to-report average-heading-towards-flockmates ;; turtle procedure
;; "towards myself" gives us the heading from the other turtle
;; to me, but we want the heading from me to the other turtle,
;; so we add 180
let x-component mean [sin (towards myself + 180)] of flockmates
let y-component mean [cos (towards myself + 180)] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end

;;; HELPER PROCEDURES

to turn-towards [new-heading max-turn] ;; turtle procedure
turn-at-most (subtract-headings new-heading heading) max-turn
end

to turn-away [new-heading max-turn] ;; turtle procedure
turn-at-most (subtract-headings heading new-heading) max-turn
end

;; turn right by "turn" degrees (or left if "turn" is negative),
;; but never turn more than "max-turn" degrees
to turn-at-most [turn max-turn] ;; turtle procedure
ifelse abs turn > max-turn
[ ifelse turn > 0
[ rt max-turn ]
[ lt max-turn ] ]
[ rt turn ]
end


; Copyright 1998 Uri Wilensky.
; See Info tab for full copyright and license.

BehaviorSpace 窗口如下所示:

enter image description here

添加的 2 行代码在 500 个刻度后保存模型的状态,来自 Railsback & Grimm 2012:基于代理和基于个体的建模(第一版)第 9 章中问题 6 的答案。答案继续说明下一步:“然后,在行为空间中,更改“设置命令”以仅导入已保存的世界并再运行 1000 个刻度”。

我这样做了,然后将文件导入到 R 中,通过计算刻度 100、200、300、400 和 500 处的同伴数量的平均值和 SD 来汇总数据。R 代码下方:

df <- read.csv("ibm_table_output-test.csv", skip = 6)

df1 <- df %>%
rename(run_number = X.run.number.,
time_step = X.step.,
mean_flockmates = mean..count.flockmates..of.turtles
) %>%
select(run_number,
time_step,
mean_flockmates,
vision) %>%
arrange(run_number,
time_step) %>%
filter(time_step == 100 |
time_step == 200 |
time_step == 300 |
time_step == 400 |
time_step == 500)

df1_long <- melt(df1, # Apply melt function
id.vars = c("run_number", "time_step","vision"))

# Calculate a summary table
df1.summ <- df1_long %>%
group_by(time_step, vision) %>%
summarise(avg = mean(value),
sd = sd(value))

输出如下:

 # A tibble: 15 × 4
# Groups: time_step [5]
time_step vision avg sd
<int> <int> <dbl> <dbl>
1 100 1 8.34 0
2 100 2 8.34 0
3 100 3 8.34 0
4 200 1 7.83 0
5 200 2 7.83 0
6 200 3 7.83 0
7 300 1 7.95 0
8 300 2 7.95 0
9 300 3 7.95 0
10 400 1 7.45 0
11 400 2 7.45 0
12 400 3 7.45 0
13 500 1 7.92 0
14 500 2 7.92 0
15 500 3 7.92 0

对我来说这个输出没有意义。

我的问题是,为什么同一 time_step 组内不同视力水平的平均同伴数量相同?为什么SD全是0呢?换句话说,为什么模型运行会产生相同的输出?我认为启动老化期将为所有模拟启动相同的起始位置,但由于使用不同的随机数,为每次运行创建不同的平均值和 SD 值?还是我理解错了?


编辑:SD 为 0 的原因是平均值没有变化,但我不明白为什么没有变化。以下是 df1_long 数据框:

   run_number time_step vision        variable    value
1 1 100 1 mean_flockmates 8.340000
2 1 200 1 mean_flockmates 7.833333
3 1 300 1 mean_flockmates 7.953333
4 1 400 1 mean_flockmates 7.446667
5 1 500 1 mean_flockmates 7.920000
6 2 100 1 mean_flockmates 8.340000
7 2 200 1 mean_flockmates 7.833333
8 2 300 1 mean_flockmates 7.953333
9 2 400 1 mean_flockmates 7.446667
10 2 500 1 mean_flockmates 7.920000
11 3 100 2 mean_flockmates 8.340000
12 3 200 2 mean_flockmates 7.833333
13 3 300 2 mean_flockmates 7.953333
14 3 400 2 mean_flockmates 7.446667
15 3 500 2 mean_flockmates 7.920000
16 4 100 2 mean_flockmates 8.340000
17 4 200 2 mean_flockmates 7.833333
18 4 300 2 mean_flockmates 7.953333
19 4 400 2 mean_flockmates 7.446667
20 4 500 2 mean_flockmates 7.920000
21 5 100 3 mean_flockmates 8.340000
22 5 200 3 mean_flockmates 7.833333
23 5 300 3 mean_flockmates 7.953333
24 5 400 3 mean_flockmates 7.446667
25 5 500 3 mean_flockmates 7.920000
26 6 100 3 mean_flockmates 8.340000
27 6 200 3 mean_flockmates 7.833333
28 6 300 3 mean_flockmates 7.953333
29 6 400 3 mean_flockmates 7.446667
30 6 500 3 mean_flockmates 7.920000

最佳答案

我的理解是,您手动运行一次setup,然后运行您的BehaviorSpace 实验。您将遇到的问题是,随机数生成器种子包含在您通过运行setup 过程生成的export-world 数据中。然后,当您在每次实验运行的设置命令:中调用import-world时,您也将获得导入的RNG种子。导出实际上包括 RNG 的完整状态,但将其视为相同的种子就足够接近了。

LeirsW 是正确的,Flocking(与大多数 NetLogo 模型一样,也可能是您遇到问题的原始模型)是完全确定性的。因此,每次使用相同的 RNG 种子,结果都会相同。

解决方法很简单,只需在运行 random-seed new-seedimport-world 之后向您的BehaviorSpace 实验中添加第二行设置命令: 。这将确保每个模型运行都有一个新的、独特的 RNG 种子可用于其运行的其余部分。

关于NetLogo 老化/预热问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74633755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com