globals [ ;; for farm color assignments new-color ;; the last color provided by the next-color utility new-color-inc ;; the direction (plus or minus) to shift the shade new-color-base ;; the shade of gray that is the base color for the current color series ] patches-own [ farm-id ] breed [ farms farm ] farms-own [ farm-root farm-patches farm-size ] to setup ca display build-farms histogram-farm-size end to build-farms ;; create the initial farm ask patch min-pxcor min-pycor [ sprout-farms 1 [ set farm-size world-width initialize-farm ] ] ;; first, enforce MAX-FARM-SIZE, split any farms over the max size. while [ any? large-farms ] [ ask large-farms [ split-farm ] ] ifelse seek-mean-farm-size? [ ;; sanity check on mean-farm-size if desired-mean-farm-size < min-farm-size [ user-message (word "The desired mean farm size can't be less than the minimum farm size,\n" "or the desired mean can never be reached!\n\n" "Please change either the minimum farm size or the desired mean farm size." ) stop ] ;; enforce mean farm size while [ mean [ farm-size ] of farms > desired-mean-farm-size and any? splittable-farms ] [ ask one-of splittable-farms [ split-farm ] if show-steps? [ display ] ] ][ ;; enforce MIN-FARM-COUNT ;; split randomly, until at least enough farms while [ count farms < desired-min-farm-count and any? splittable-farms ] [ ask one-of splittable-farms [ split-farm ] if show-steps? [ display ] ] ] ;; tag all patches with current farm id ask farms [ ask farm-patches [ set farm-id myself ] ] ;; for visual effect, move FARM turtle to center of farm, size for visual effect ask farms [ position-farm ] ;; highlight the borders ask farms [ highlight-borders ] end to position-farm let scale farm-size / 2 set size scale setxy ( [ pxcor ] of farm-root + scale ) ( [ pycor ] of farm-root + scale ) end to highlight-borders ask farm-patches with [ any? neighbors4 with [ farm-id != [ farm-id ] of myself ] ] [ set pcolor [ color + 3 ] of farm-id ] end to-report large-farms report farms with [ farm-size > max-farm-size ] end to-report splittable-farms report (farms with [ farm-size > min-farm-size ] ) end to split-farm ;; do not even try to split if this farm is only 1 unit wide. if farm-size <= 1 [ stop ] ;; OK, split! let xx 0 let yy 0 let new-size farm-size / 2 ;; create new farm-roots relative to current farm root ;; (do this relative to farm-root so that farm turtle's location is coupled to ;; determinig patches for splitting, etc) let new-farm-roots [ patches at-points map [ (list (first ? * new-size) (last ? * new-size) ) ] [ [ 0 0 ] [ 0 1 ] [ 1 1 ] [ 1 0 ] ] ] of farm-root ask new-farm-roots [ sprout-farms 1 [ set farm-size new-size initialize-farm ] ] die end to initialize-farm ;; store ID of this patch, ;; so we can use farm-root to id the lower-left corner of farm ;; letting us put farm turtle anywhere, if we =hoose. set farm-root patch-here set farm-patches patches with [ pxcor >= [ xcor ] of myself and pxcor < [ xcor + farm-size] of myself and pycor >= [ ycor ] of myself and pycor < [ ycor + farm-size] of myself ] set shape "farm" set color next-color ask farm-patches [ set pcolor [ color ] of myself ] ;; move/size farm decorously, only if "show steps" is on. if show-steps? [ position-farm ] end to-report next-color ifelse new-color = 0 [ set new-color gray ] [ set new-color wrap-color (new-color + 10 ) ] report new-color end to-report between [ value minimum maximum ] report (minimum <= value and value <= maximum) end to histogram-farm-size set-current-plot "by farm size" histogram [ log farm-size 2] of farms end @#$#@#$#@ GRAPHICS-WINDOW 350 10 744 425 -1 -1 3.0 1 10 1 1 1 0 1 1 1 0 127 0 127 1 1 1 ticks CC-WINDOW 5 509 753 604 Command Center 0 BUTTON 10 10 73 43 NIL setup\n NIL 1 T OBSERVER NIL NIL NIL NIL MONITOR 130 10 225 55 NIL count farms 0 1 11 INPUTBOX 195 275 340 335 desired-min-farm-count 100 1 0 Number INPUTBOX 10 115 105 175 min-farm-size 4 1 0 Number INPUTBOX 10 50 105 110 max-farm-size 32 1 0 Number PLOT 10 345 170 495 by farm size NIL NIL 0.0 10.0 0.0 10.0 true false PENS "default" 1.0 1 -16777216 true MONITOR 235 10 327 55 mean farm size mean [ farm-size ] of farms 2 1 11 INPUTBOX 10 275 155 335 desired-mean-farm-size 8 1 0 Number SWITCH 10 235 187 268 seek-mean-farm-size? seek-mean-farm-size? 0 1 -1000 SWITCH 130 125 252 158 show-steps? show-steps? 1 1 -1000 TEXTBOX 10 190 210 235 Turn on to seek desired mean farm size. Otherwise, divide until desired farm count is met or exceeded. 11 0.0 1 TEXTBOX 130 65 330 135 Turn on \"show-steps?\" to see the subdivison process as it proceeds (you may have to use the speed slider to slow things down).\n 11 0.0 1 @#$#@#$#@ WHAT IS IT? ----------- This model demonstrates a method of generating square clusters of patches of varying sizes using progressive subdivision. HOW IT WORKS ------------ A farm is a turtle that manages a square cluster of patches. By making 4 copies of the farm, and dividing the patches in the cluster among the copies, then removing the original farm, a cluster of patches is divided into four clusters. By repeating the subdivision process, we can create clusters with the desired properties. HOW TO USE IT ------------- Set the min-farm-size, max-farm-size, and min-farms to the desired values. Note: this model does not do any sanity checking on these inputs. min-farm-size: desired minimum number of patches that a cluster may contain. such farms can not be further subdivided. max-in a cluster: desired maximum number of patches that a cluster may contain. such farms are always subdivided. min-farms: the desired minimum number of farms. If the number of farms is less than this number, and there are any farms larger than the minimum size, one of them will be subdivided. THINGS TO NOTICE ---------------- The ratio of the count of farms of various sizes are rather consistant. EXTENDING THE MODEL ------------------- You can use the concepts and techniques in this model to create sub-divided patch clusters for your own models. RELATED MODELS -------------- See the voronoi models in the Models Library or on TurtleZero.com to see another way to subdivide patches based on a "root" patch or turtle. @#$#@#$#@ default true 0 Polygon -7500403 true true 150 5 40 250 150 205 260 250 bug false 0 Circle -7500403 true true 96 182 108 Circle -7500403 true true 110 127 80 Circle -7500403 true true 110 75 80 Line -7500403 true 150 100 80 30 Line -7500403 true 150 100 220 30 circle false 0 Circle -7500403 true true 0 0 300 cow false 0 Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167 Polygon -7500403 true true 73 210 86 251 62 249 48 208 Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123 face happy false 0 Circle -7500403 true true 8 8 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240 face neutral false 0 Circle -7500403 true true 8 7 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Rectangle -16777216 true false 60 195 240 225 face sad false 0 Circle -7500403 true true 8 8 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183 farm false 0 Polygon -1 true false 0 300 0 135 45 60 120 60 180 60 210 30 270 30 300 60 300 300 Rectangle -7500403 true true 30 165 180 270 Rectangle -7500403 true true 210 135 270 270 Polygon -7500403 true true 30 135 60 90 150 90 180 135 Polygon -7500403 true true 210 105 210 75 225 60 255 60 270 75 270 105 plant false 0 Polygon -1 true false 105 300 45 255 15 195 45 165 15 120 45 90 15 45 45 15 90 30 120 0 180 0 210 30 255 15 285 45 255 90 285 120 255 165 285 195 255 255 195 300 Rectangle -7500403 true true 135 90 165 285 Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285 Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285 Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210 Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135 Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135 Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60 Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90 square false 0 Rectangle -7500403 true true 0 0 300 300 @#$#@#$#@ NetLogo 4.0.4 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@ @#$#@#$#@ @#$#@#$#@ default 0.0 -0.2 0 0.0 1.0 0.0 1 1.0 0.0 0.2 0 0.0 1.0 link direction true 0 Line -7500403 true 150 150 90 180 Line -7500403 true 150 150 210 180 @#$#@#$#@