Netlogo:打开文件后调用过程
Netlogo : calling a procedure after opening a file
以下是打开文件、读取文件并将其写入列表的代码(灵感来自另一个讨论):
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]
过程中设置了一个计时器,我看到它一次又一次地 运行ning。当我 运行 [assign-data]
独自工作时,它正常工作,只有一次。
我尝试在 [assign-data]
之后使用 stop
,但它不起作用。
关于Netlogo的使用,一定有什么我没弄明白的地方,你知道是什么吗?
这里是assign-data
程序的代码(有2个选择,第二个运行ning更快)
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
-> 这让我想到了另一个问题:如何从 运行ning 无休止地停止模拟?我在命令中心尝试使用 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
您确定 运行 是无限的而不是指数的吗?您 ask patches
到 assign-data
并且在 assign-data
中您再次使用 ask patches
。这意味着每个补丁都在检查每个补丁并让合格的补丁通过循环,这可能需要一段时间。
以下是打开文件、读取文件并将其写入列表的代码(灵感来自另一个讨论):
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]
过程中设置了一个计时器,我看到它一次又一次地 运行ning。当我 运行 [assign-data]
独自工作时,它正常工作,只有一次。
我尝试在 [assign-data]
之后使用 stop
,但它不起作用。
关于Netlogo的使用,一定有什么我没弄明白的地方,你知道是什么吗?
这里是assign-data
程序的代码(有2个选择,第二个运行ning更快)
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
-> 这让我想到了另一个问题:如何从 运行ning 无休止地停止模拟?我在命令中心尝试使用 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
您确定 运行 是无限的而不是指数的吗?您 ask patches
到 assign-data
并且在 assign-data
中您再次使用 ask patches
。这意味着每个补丁都在检查每个补丁并让合格的补丁通过循环,这可能需要一段时间。