首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Netlogo :打开文件后调用过程

Netlogo :打开文件后调用过程
EN

Stack Overflow用户
提问于 2022-05-11 10:03:21
回答 1查看 42关注 0票数 0

下面是打开文件、读取文件并将其写入列表的代码(来自另一次讨论):

代码语言:javascript
复制
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过程的代码(有两个选项,第二个选项运行得更快)

代码语言:javascript
复制
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,第二个和第三个只是随机数。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-11 15:12:36

你确定跑步是无止境的,而不是指数吗?您从ask patchesassign-data,在assign-data中,您再次使用ask patches。这意味着每个补丁都在检查每个补丁,并让合格的补丁通过循环,这可能需要一段时间。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72199102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档