campus-buildings-path-graph


Information

Created with NetLogo version NetLogo 4.0.4
Running with NetLogoLite.jar version 404.


WHAT IS IT?


A model that builds a campus of buildings, then creates links between building features that are "important" in creating a graph of the paths around the buildings.
The intent is that an agent using a* or something could use the reduced graph (rather than using the "all-adjacent-patches" graph) to find its way through the campus.

HOW DOES IT WORK?


THe building part is easy and pretty obvious. A purpose-built random building generator makes a campus of random buildings.
THe fun part is finding the corners
The ReALLY fun part is linking the corners

FIND THE CORNERS

TO find the corners we first find OUTSIDE corners
These are black patches with exactly ONE red neighbor
Next we find the inside corners.
These are black patches with GREATER THAN THREE red neighbors.
This also fills up "alleys"--spaces one patch wide--with nodes. These are filtered out later.

LINK THE CORNERS

The corners sprout turtles.
TO link the corners, each turtle examines every "younger" turtle.
Using N-values with PATCH-AT-HEADING-AND-DISTANCE, a set of the patches that lie on the line between the two turtles is created. If NONE of these patches are RED, then a link is created, because the path does not cross through a building.
Works pretty well, except that a link sometimes goes through the corner of a building, where the path steps don't enter the patch.
Could probably fix that by also testing the neighbors of the path patches--if one is red and another contains a turtle, and the two are adjacent, then the path is no good. Maybe.

Procedures

NetLogo Version: NetLogo 4.0.4

globals
[ space-color
  building-color
]

;; BREEDS USED TO CREATE CAMPUS GRAPH
breed [ builders builder ]
breed [ waypoints waypoint ]
undirected-link-breed [ paths path ]

to startup setup end

to generate
   setup
   display
end
   
to setup
   ca
   setup-world
   setup-graph
end
   
to setup-world

   set space-color black
   set building-color red
   ask patches [ set pcolor space-color ]
   
   let index 0
   
   repeat 10
   [ create-builders 1
     [ set index ++ index
       show index
       ;; magic numbers:
       ;; 3 x 3 = minimum dimensions of a building chunk
       ;; 18 x 18 = maximum dimensions of a building chunk
       let w 3 + random 15
       let h 3 + random 15
       
       
       ;; place the builder randomly, but so that the edges clear the world
       ;; edge by at least one unit
       
       setxy (1 + min-pxcor + random ((world-width  - 1 - w)) )
             (1 + min-pycor + random ((world-height - 1 - h)) )
       
       ;; orient the builder correctly to start building.
       ;; builders start in lower left corner of chunk
       ;; then zigzag left to right, top to bottom
       
       set heading 90
       let turn 90
       
       repeat h
       [ repeat w
         [ set pcolor building-color
           ; set plabel index
           jump 1
         ]
         lt turn
         jump 1
         lt turn
         jump 1
         set turn (- turn)
       ]
       show index
       ;; when done, it die
       die
     ]
   ]
end

to setup-graph
   let out-corners patches with [ is-open-space? and is-outside-corner? ]
   ask out-corners [ set pcolor sky ]
   
   let in-corners patches with [ is-open-space? and between-ex 2 (count neighbors with [ pcolor = red ] ) 8 and not has-colinear-red-neighbors? and not in-alley? ]
   ask in-corners [ set pcolor lime ]
   
   let corners (patch-set in-corners out-corners)
   ask corners
   [ sprout 1
     [ set breed waypoints
       set shape "cross"
       set color white
       set size 1.0
       set heading 0
       
     ]
   ]
   
   ask waypoints
   [ let me self
     ask waypoints with [ who > [ who ] of myself ]
     [ let you self
       create-path-with me
       [ set color pink
         set thickness .5 
       ]
       
       let head towards me
       let resolution 2 ;; resolution of corner crossing
       let tiles (patch-set n-values (resolution * floor distance me)
              [ patch-at-heading-and-distance head ((? + 1) / resolution ) ] )
       ifelse any? tiles with  
          [ ;; NO PART GOES THROUGH A BUILDING
             is-building?
             or 
             ;; NO PART PASSES THOUGH ANOTHER waypoint
             ;; (that is, no overlapping paths!)
             any? waypoints-here with [ self != me and self != you ]
          ]
       [ ask path-with me [ die ] ]
       [ ask tiles [ ] ; [ set pcolor magenta ]
         ask path-with me 
         [ set color green
           set thickness .5
         ]
       ]
     ]
   ]
   
   ;let mags patches with [ pcolor > 0 and shade-of? pcolor gray ]
   ;let magsp [ pcolor ] of mags 
   ;let minm min magsp
   ;let maxm max magsp
   ;let spanm maxm - minm
   ;ask mags [ set pcolor scale-color magenta pcolor minm maxm ]
end
   
to-report between-ex [ a b c ]
   report (a < b and b < c)
end

to-report is-open-space?
   report ( pcolor != building-color )
end

to-report is-building?
   report ( pcolor = building-color )
end

to-report neighbors-that-are-building-parts
   report ( neighbors with [ is-building? ] )
end   

to-report is-outside-corner?
  report ( 1 = count neighbors-that-are-building-parts) 
end
   
to-report has-colinear-red-neighbors?
   let rn neighbors-that-are-building-parts
   if not any? rn [ report false ]
   if count rn != 3 [ report false ]
   let spx [pxcor] of rn
   let spy [pycor] of rn
   ifelse mean spx = first spx or mean spy = first spy
   [ report true ]
   [ report false]   
end   
   
to-report in-alley?
   let rn neighbors-that-are-building-parts
   if not any? rn [ report false ]
   if count rn != 6 [ report false ]
   if all? patches at-points [ [-1 0][1 0] ] [ is-open-space? ] [ report true ]
   if all? patches at-points [ [0 -1][0 1] ] [ is-open-space? ] [ report true ]
   report false
end

   
   
   
to-report ++ [ value ] report value + 1 end
to-report -- [ value ] report value - 1 end
   

                    


Download Link

View or download the complete model file (to download: right-click, save-link-as):
-- Download campus-buildings-path-graph --