File input for patches and coloring

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

File input for patches and coloring

Jens Maiwald
This post was updated on .
Hey NetLogo-Community,


lets imagine my model-worlds size is 3x3 patches. Each patch owns an
individual value of an specific attribute, for example how many turtles
could live on one patch. I'd like to import this value for each patch from
an external source file (.txt). This works so far, but, at each new set-up
of the model the attributes are implemented in a different order. But I
need them in the order which is given in the source file.

Let's say my source file for 3x3 patches is

1 2 3
3 2 2
2 3 2

Modell-Code:

;extensions [palette]

patches-own [attribute]

to setup
  clear-all
  file-close-all
  setup-patches
  reset ticks
end

to setup-patches
  file-open "file.txt"
  if file-at-end? [stop]
  ask patches [set attribute file-read]
end

----------------
Can you tell me how to wright the code?

And if this is not challenging enough to you guys: can you, moreover, tell
me how to color the patches depending on their attributes? Lets say:
attribute = 1 -> light red,
attribute = 2 -> red,
attribute = 3 -> dark red


Thank you in advance






--
Sent from: http://netlogo-users.18673.x6.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: File input for patches and coloring

Bryan Head
I usually solve this like so:


to setup-patches
  let attributes csv:from-file "file.csv" ; gives us a list of lists
like [ [ 1 2 3 ] [ 3 2 2 ] [ 2 3 2 ] ]
  (foreach (sort patches) attributes [ [ p attr ] ->
    ask p [
      set attribute attr
      if attribute = 1 [ set pcolor red + 3 ]
      if attribute = 2 [ set pcolor red ]
      if attribute = 3 [ set pcolor red - 3 ]
    ]
  ])
end


csv:from-file reads the entire csv file in at once as a list of lists (so
no need for file-open or closing or anything). sort patches creates a list
of all the patches, starting from the top left of the world to the bottom
right, in row-major order. (foreach (sort patches) attributes ... then
iterates across the patches and attributes together.


Note that this code assumes that the csv file as the same number of rows
and columns as patches. If that’s not the case, you can do something like:


resize-world 0 (length first attributes - 1) 0 (length attributes - 1)


to automatically resize the world to the size of the file.


Hope that helps!
Bryan

Reply | Threaded
Open this post in threaded view
|

Re: File input for patches and coloring

Steve Railsback
In reply to this post by Jens Maiwald
There is a simple solution to reading patch variables in from a file (and coloring the patches) in Ch. 5 of our book: A Course in Individual- and Agent-based Modeling - Scientific Modeling with NetLogo http://www.railsback-grimm-abm-book.com/

It uses a data file like this (X, Y, value):
0    0    532.4
1    0    529.3
2    0    526
3    0    520

And code like:
  while [ not file-at-end? ]
  [
    let next-X file-read
    let next-Y file-read
    let next-elevation file-read
    ask patch next-X next-Y [ set elevation next-elevation ]
  ]

Steve R.
Reply | Threaded
Open this post in threaded view
|

Re: File input for patches and coloring

Jens Maiwald
In reply to this post by Bryan Head
It took me a while to understand the code and adapt it to my model and data
structure. But in the end, it works pretty well. Thumbs up!


Thank you very much






--
Sent from: http://netlogo-users.18673.x6.nabble.com/