To do a netlogo model with several patch layers (think floors of a building) do I need to go to Netlogo 3D?

Viewed 52

I have a netlogo application in mind which involves multiple non interacting layers. Think floors of a building. Would I need to go to netlogo 3D or is there a suggested way to handle in regular netlogo?

1 Answers

This was an interesting enough question that I decided to make a simple sample.

The method I use is to have a global variable to track the number-of-floors we'll have in our building, as well as the active-floor that we are currently working with. Then all our agents, walls and workers, have a floor-num that tracks which they are on. We have a set-active-floor procedure that handles switching our currently active floor that we want to see and work with, making the patches a certain color (active-color) if they have a wall agent present and swapping which workers are hidden?. In this way, most of the magic happens in the setup-floor and set-active-floor procedures, and the real work of our model in go can be pretty typical NetLogo code asking the active-workers to do whatever we want.

While the model is running you can call set-active-floor n to any value 0 to 4 to change the current workers and walls. I also included a show-all procedure that'll un-hide all the walls and workers and let you see where they are at; you'll need to run that with the model stopped.

globals [colors active-color number-of-floors active-floor]

turtles-own [floor-num]

breed [walls wall]
breed [workers worker]

to setup
  clear-all
  set colors (list red blue green orange violet)
  set number-of-floors 5
  foreach (range 0 number-of-floors) setup-floor
  reset-ticks
  set-active-floor 0
end

to setup-floor [num]
  set active-floor num
  set active-color (item num colors)
  ask patches [ set pcolor black ]
  ; make some random walls
  create-walls (count patches * 0.2) [
    set floor-num num
    set hidden? true
    set size 0.5
    setxy random-pxcor random-pycor
    set pcolor active-color
  ]
  ; only one wall per patch
  ask patches [
    let patch-walls floor-walls-here
    let wall-count count patch-walls
    if (wall-count > 1) [
      ask n-of (wall-count - 1) patch-walls [ die ] 
    ]
  ]
  ; make some workes
  create-workers 10 [
    set floor-num num
    set hidden? true
    set shape "person"
    set color active-color - 2
    move-to one-of patches with [pcolor != active-color]
  ]
end

to go
  ; this only "runs" the active floor, but you could run all of them
  ; using the same `foreach (range 0 number-of-floors) ...` code as 
  ; in the `setup` procedure.
  set-active-floor active-floor
  ask active-workers [
    ; this code can be about the same as you'd write in a normal model...
    move-to one-of patches with [pcolor != active-color]
  ]
  tick
end

to set-active-floor [num]
  set active-floor num
  set active-color (item num colors)
  ; after the `pcolor` is set, we can use that to determine if a wall
  ; exists or not for an `active-worker`, we don't have to check the 
  ; floor number at all while we do our work.
  ask walls [ set hidden? true ]
  ask patches [
    set pcolor ifelse-value no-walls-here [ black ] [ active-color ]
  ]
  ask workers [
    set hidden? floor-num != active-floor
  ]
end

to-report active-workers 
  report workers with [floor-num = active-floor]
end

to-report floor-walls-here
  report walls-here with [floor-num = active-floor]
end

to-report no-walls-here
  report (count floor-walls-here = 0)
end

to show-all
  foreach (range 0 number-of-floors) [ num ->
    ask patches [ 
      set pcolor ifelse-value any? walls-here [ red - 3 ] [ black ]
    ]
    ask turtles with [floor-num = num] [ 
      set color item num colors 
      set hidden? false
    ]
  ]
end

Finally, if things got much more complicated than this, I would probably choose to move to NetLogo 3D instead.

Related