下面是打开文件、读取文件并将其写入列表的代码(来自另一次讨论):
to setup
reset-timer
; first, we load the database file
; We check to make sure the file exists first
ifelse ( file-exists? "AT_data.txt" )
[
; We are saving the data into a list, so it only needs to be loaded once.
set AT-data []
file-open "AT_data.txt"
while [ not file-at-end? ]
[
; file-read gives variables stored in a double list
; Each iteration we append the next three-tuple to the current list: ID AT1 AT2
set AT-data sentence AT-data (list (list file-read file-read file-read))
]
user-message "File loading complete!"
file-close
;; when adding this, the procedure is running endlessly, to be checked
;; ask patches [ assign-data ]
]
[ user-message "There is no AT_data.txt file in current directory!" ]
file-close-all
print timer
end正如我作为注释所写的,当我调用下一个过程[assign-data]时,过程[assign-data]无休止地运行。我在[assign-data]过程中设置了一个计时器,我看到它一次又一次地运行。当我单独运行[assign-data]时,它运行得很恰当,只有一次。
我在[assign-data]之后尝试用一个[assign-data],但它不起作用。
我肯定还没有得到一些关于使用网络标志的东西,你知道它是什么吗?
下面是assign-data过程的代码(有两个选项,第二个选项运行得更快)
to assign-farmers1
reset-timer
ask patches with [seed = 1] [
set death last (first (filter [current-inner-list -> (item 0 current-inner-list = ID_farm)] AT-data))
set age item 1 (first (filter [current-inner-list -> (item 0 current-inner-list = ID_farm)] AT-data))
]
print timer
end
to assign-data2
reset-timer
ask patches with [seed = 1] [
let i 1
while [i < length AT-data] [
let current-inner-list item i AT-data
ifelse (ID_farm = item 0 current-inner-list)
[ set age item 1 current-inner-list set death item 2 current-inner-list
stop]
[ set i i + 1 ]
]
]
print timer
end->给我带来了另一个问题:如何阻止模拟无休止地运行?我尝试过在命令中心使用stop,但它没有工作。
谢谢你抽出时间。
这里是一个可重复的例子,(不确定我是否应该离开问题的开头) AT_data.txt是一个由3组成的文件,其中第一个从1到100,第二个和第三个只是随机数。
globals [
AT-data
]
patches-own [
ID
AT1
AT2
seed
]
to setup
;; here I just create patches with different values that also appear in the list
ca
ask patches [ set seed random 10 set ID random 100
ifelse (seed = 4)
[ set pcolor orange] [set pcolor white]
]
end
to load
reset-timer
; first, we load the database file
; We check to make sure the file exists first
ifelse ( file-exists? "AT_data.txt" )
[
; We are saving the data into a list, so it only needs to be loaded once.
set AT-data []
file-open "AT_data.txt"
while [ not file-at-end? ]
[
; file-read gives variables stored in a double list
; Each iteration we append the next three-tuple to the current list: ID AT1 AT2
set AT-data sentence AT-data (list (list file-read file-read file-read))
]
user-message "File loading complete!"
file-close
;; when adding this, the procedure is running endlessly, to be checked
ask patches [ assign-data ]
stop
]
[ user-message "There is no AT_data.txt file in current directory!" ]
file-close-all
print timer
end
to assign-data
reset-timer
ask patches with [seed = 4] [
let i 1
while [i < length AT-data] [
let current-inner-list item i AT-data
ifelse (ID = item 0 current-inner-list)
[ set AT1 item 1 current-inner-list set AT2 item 2 current-inner-list
stop]
[ set i i + 1 ]
]
]
print timer
end发布于 2022-05-11 15:12:36
你确定跑步是无止境的,而不是指数吗?您从ask patches到assign-data,在assign-data中,您再次使用ask patches。这意味着每个补丁都在检查每个补丁,并让合格的补丁通过循环,这可能需要一段时间。
https://stackoverflow.com/questions/72199102
复制相似问题