如何在 NetLogo 中循环?
How to loop in NetLogo?
我有以下问题。我需要遍历代码。但是,它不起作用。
将问题具体化:我有 3 个文件(在 .asc 中)代表涉及 3 个年龄段的海龟(2 岁、4 岁和 8 岁)的数据。我想如果用户将值 1 放在 num-user 中,它只会用一个代表 [ "2" ] 的文件 (L_2) 进行模拟;如果用户将值 2 放在 num-user 中,他将使用代表 ["2" "4" ] 的文件(L_2 和 L_4)进行模拟,最后,如果用户将值 3 放入 num-use 中,它将模拟代表 ["2" "4" "8" ] 的文件(L_2、L_4 和 L_8)。问题是循环不工作并给出各种错误。像:
扩展异常:ascii 文件 ./L_8.asc 未找到或无法找到列表 [2 4 8] 的元素 3,它的长度仅为 3 或 go 运行超过 3 次模拟。
我无法在此处附加问题中的 .ascii 文件。但是,如果有人可以查看代码并找出错误,我将不胜感激。我无法使用 BehaviouSpace 来解决这种情况,我需要在代码中使用这个循环。
提前致谢!
extensions [ gis ]
globals [ num turtle-ages num-ages files random-seeds num-user repetitions ]
to setup
ca
set random-seeds 1
random-seed random-seeds
set num 0
set turtle-ages [ "2" "4" "8" ]
set num-ages item num turtle-ages
setup-asc
setup-turtles
reset-ticks
end
to setup-2
clear
random-seed random-seeds
set num-ages item num turtle-ages
setup-asc
setup-turtles
reset-ticks
end
to setup-turtles
ask n-of 5 patches [ sprout 1 ]
end
to clear
set files 0
clear-ticks
clear-turtles
end
to setup-asc
let number1 num-ages
set files gis:load-dataset ( word "./L_" number1 ".asc" ) ;; this loads a one raster file. There are 3 files in the folder with the names: (L_2.asc ; L_4.asc and L_8.asc
end
to go
move
tick
let n count turtles
if n = 0 or ticks = 10
[
set random-seeds random-seeds + 1
set repetitions 1
if random-seeds = repetitions + 1
[
set random-seeds 1
set num num + 1
;; if the user puts the value 1 in num-user, it would only do the simulation with only one file (L_2) that would represent [ "2" ]
;;; if the user puts the value 2 in num-user, he would do the simulation with the files (L_2 and L_4) that would represent [ "2" "4" ]
;;;; and finally, if the user puts the value 3 in num-use, it would simulate the files (L_2 , L_4 and L_8) that would represent [ "2" "4" "8" ]
set num-user 1 ;;
if num = num-user [ stop ]
]
setup-2
]
end
to move
ask turtles [
right random 360
fd 1
if ticks = 5 [ die ]
]
end
只要有可能,我建议将您的代码缩减为 MRE 到 a) 确保这里的用户可以 运行 您的代码(例如,没有您的文件,这不是真的可行)和 b)看看用更简单的术语重新定义你的问题/目标是否有助于让事情顺利进行——至少这对我有用!
我 认为 您可能会发现 foreach
在这里很有用,可以作为一种循环遍历所需模拟的方法,而不是手动跟踪迭代次数。对于此示例,假设 num-user
是界面上的数字输入小部件,下面的设置将确定要处理的年龄:
globals [ ages-to-run ]
to base-setup
ca
; Determine how many simulations to run, based on user input into
; 'num-user' numerical input widget on the interface
ifelse num-user < 1 or num-user > 3 [
print "Incorrect number of simulations indicated"
] [
let possible-sims [ "2" "4" "8" ]
set ages-to-run sublist possible-sims 0 num-user
]
reset-ticks
end
在 运行 执行上述操作后,ages-to-run
变量将包含 ["2"]
、["2" "4"]
或 ["2" "4" "8"]
。接下来,您可以迭代这些所需的年龄以 运行 您的模拟(在评论中更详细一些):
to run-simulations
if ages-to-run = 0 [
print "Simulation setup not complete"
]
foreach ages-to-run [
; This is the loop proper where each "age" is iterated.
; All of your simulation calls (manual variable resetting,
; etc) should all go within this loop.
current-age ->
print word "Running simulations for age: " current-age
let file-to-load ( word "./L_" current-age ".asc" )
print file-to-load
clear-turtles
ask n-of 5 patches [
sprout 1 [
pd
set color runresult current-age + 55
]
]
repeat 20 [
ask turtles [
rt random 60 - 30
fd 1
]
tick
]
print ( word "Simulations complete for age: " current-age "\n" )
]
end
运行 上面 3
输入 num-user
的代码将 运行 每个年龄段的模拟(不同的颜色表示不同的 运行s):
因此,对于 运行 正确的模拟,您所有的 per-age 代码都应该在上面指出的 foreach
循环中 - 并且要小心,就像您在问题中一样,不要重置全局变量。
我有以下问题。我需要遍历代码。但是,它不起作用。
将问题具体化:我有 3 个文件(在 .asc 中)代表涉及 3 个年龄段的海龟(2 岁、4 岁和 8 岁)的数据。我想如果用户将值 1 放在 num-user 中,它只会用一个代表 [ "2" ] 的文件 (L_2) 进行模拟;如果用户将值 2 放在 num-user 中,他将使用代表 ["2" "4" ] 的文件(L_2 和 L_4)进行模拟,最后,如果用户将值 3 放入 num-use 中,它将模拟代表 ["2" "4" "8" ] 的文件(L_2、L_4 和 L_8)。问题是循环不工作并给出各种错误。像: 扩展异常:ascii 文件 ./L_8.asc 未找到或无法找到列表 [2 4 8] 的元素 3,它的长度仅为 3 或 go 运行超过 3 次模拟。
我无法在此处附加问题中的 .ascii 文件。但是,如果有人可以查看代码并找出错误,我将不胜感激。我无法使用 BehaviouSpace 来解决这种情况,我需要在代码中使用这个循环。
提前致谢!
extensions [ gis ]
globals [ num turtle-ages num-ages files random-seeds num-user repetitions ]
to setup
ca
set random-seeds 1
random-seed random-seeds
set num 0
set turtle-ages [ "2" "4" "8" ]
set num-ages item num turtle-ages
setup-asc
setup-turtles
reset-ticks
end
to setup-2
clear
random-seed random-seeds
set num-ages item num turtle-ages
setup-asc
setup-turtles
reset-ticks
end
to setup-turtles
ask n-of 5 patches [ sprout 1 ]
end
to clear
set files 0
clear-ticks
clear-turtles
end
to setup-asc
let number1 num-ages
set files gis:load-dataset ( word "./L_" number1 ".asc" ) ;; this loads a one raster file. There are 3 files in the folder with the names: (L_2.asc ; L_4.asc and L_8.asc
end
to go
move
tick
let n count turtles
if n = 0 or ticks = 10
[
set random-seeds random-seeds + 1
set repetitions 1
if random-seeds = repetitions + 1
[
set random-seeds 1
set num num + 1
;; if the user puts the value 1 in num-user, it would only do the simulation with only one file (L_2) that would represent [ "2" ]
;;; if the user puts the value 2 in num-user, he would do the simulation with the files (L_2 and L_4) that would represent [ "2" "4" ]
;;;; and finally, if the user puts the value 3 in num-use, it would simulate the files (L_2 , L_4 and L_8) that would represent [ "2" "4" "8" ]
set num-user 1 ;;
if num = num-user [ stop ]
]
setup-2
]
end
to move
ask turtles [
right random 360
fd 1
if ticks = 5 [ die ]
]
end
只要有可能,我建议将您的代码缩减为 MRE 到 a) 确保这里的用户可以 运行 您的代码(例如,没有您的文件,这不是真的可行)和 b)看看用更简单的术语重新定义你的问题/目标是否有助于让事情顺利进行——至少这对我有用!
我 认为 您可能会发现 foreach
在这里很有用,可以作为一种循环遍历所需模拟的方法,而不是手动跟踪迭代次数。对于此示例,假设 num-user
是界面上的数字输入小部件,下面的设置将确定要处理的年龄:
globals [ ages-to-run ]
to base-setup
ca
; Determine how many simulations to run, based on user input into
; 'num-user' numerical input widget on the interface
ifelse num-user < 1 or num-user > 3 [
print "Incorrect number of simulations indicated"
] [
let possible-sims [ "2" "4" "8" ]
set ages-to-run sublist possible-sims 0 num-user
]
reset-ticks
end
在 运行 执行上述操作后,ages-to-run
变量将包含 ["2"]
、["2" "4"]
或 ["2" "4" "8"]
。接下来,您可以迭代这些所需的年龄以 运行 您的模拟(在评论中更详细一些):
to run-simulations
if ages-to-run = 0 [
print "Simulation setup not complete"
]
foreach ages-to-run [
; This is the loop proper where each "age" is iterated.
; All of your simulation calls (manual variable resetting,
; etc) should all go within this loop.
current-age ->
print word "Running simulations for age: " current-age
let file-to-load ( word "./L_" current-age ".asc" )
print file-to-load
clear-turtles
ask n-of 5 patches [
sprout 1 [
pd
set color runresult current-age + 55
]
]
repeat 20 [
ask turtles [
rt random 60 - 30
fd 1
]
tick
]
print ( word "Simulations complete for age: " current-age "\n" )
]
end
运行 上面 3
输入 num-user
的代码将 运行 每个年龄段的模拟(不同的颜色表示不同的 运行s):
因此,对于 运行 正确的模拟,您所有的 per-age 代码都应该在上面指出的 foreach
循环中 - 并且要小心,就像您在问题中一样,不要重置全局变量。