Read data from file in every ticks

心已入冬 提交于 2019-12-11 09:08:10

问题


I am using NetLogo and I want to read two types of data (for example "x" and "y") from a file for each agent in each time step (tick). Does anyone know how I can do this?

Here is the code:

breed [agents agent]
agents-own [  need  tax]

to setup
  clear-all
  define-xy
  reset-ticks
end

to define-xy
  file-open "D:\\data\\xy.txt"
   while [not file-at-end?]
   [
  let items read-from-string (word "[" file-read-line "]")
   crt 1 [
     set xcor item 0 items  
     set ycor item 1 items
     set label ycor
   ]
  ]
  file-close
end

to go
  tick
  if (ticks = 5)
  [
    stop
    ]
end

Now if I want to define two parameters that are tax and need that change each tick what should I do. I am a little bit confused.In the code above I can do a one time read from a file that has two columns.


回答1:


Well. You give too little information about how the data is going to be structured in the text file, so I will suppose that you have only two columns. So the text file looks like this:

1 3
4 5
9 11
-1 33

First, I think you'll need a way to control the agents, I'm again supposing you create a determinated number of agents and that row 1 of text file is used for agent 1.

That said, next thing you want to do is to use repeat number-of-agents [tasks]

And tasks could look something like:

let auxiliar 0
repeat number-of-agents [
let data-read read-file-line
ask agent auxiliar [ set var1 first data-read 
                     set var2 last data-read ]
tick ;Reads, Assigns, Ticks and all over again.
set auxiliar auxiliar + 1
]


来源:https://stackoverflow.com/questions/28482889/read-data-from-file-in-every-ticks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!