foliveira2
foliveira2

Reputation: 41

Netlogo - Divide world equally between n agents

I'm creating an inspection model in Netlogo, and each inspector is responsible for n patches. That is, I need to divide the world in areas that are exclusive for each inspector.

I tried it with set range (world-width * world-height / inspectors) ^ 0.5

But their range is higher than expected, and it allows one inspector to "invade" the area of other inspectors, which is not desirable.

Upvotes: 1

Views: 308

Answers (1)

Luke C
Luke C

Reputation: 10301

This approach is probably way over-complicated but may do what you need- it splits the world into even vertical bands of "territory." Note that it only works properly if the world's minimum xcor is 0, and if the world-width is divisible by the number of inspectors- otherwise one inspector will have an inspection area that is different from all the others. For example, using this setup:

breed [ inspectors inspector ]
inspectors-own [ my-territory ]

to setup
  ca
  resize-world  0 29 0 29
  create-inspectors ninspectors [
    set my-territory nobody
  ]
  split
  reset-ticks
end

Make lists to store the minimum and maximum xcor values, then use those to section off patches to assign as territories to different inspectors. More explanation in comments:

to split 
  ; get a count of the inspectors
  let n count inspectors

  ; get section widths
  let n-xcor ( max-pxcor - min-pxcor ) / n

  ; build lists to define min-maxes of sections
  let n-min ( range min-pxcor max-pxcor n-xcor )
  let n-max lput ( max-pxcor + 1 ) n-min
  set n-max but-first n-max

  ; Foreach of the min-max pairs, set patches within 
  ; the min-max boundary be set to the territory of one
  ; of the inspectors that currently has no territory
  ( foreach n-min n-max [
    [ _min _max ] -> 
    set _min ceiling _min
    set _max ceiling _max
    let cur_inspector one-of inspectors with [ my-territory = nobody ]
    ask patches with [ pxcor >= _min and pxcor < _max ] [
      ask cur_inspector [
        ifelse my-territory = nobody [
          set my-territory myself
        ] [
          set my-territory ( patch-set my-territory myself )
        ]
      ]
      set pcolor ( [color] of cur_inspector ) - 2
    ]
  ]) 

  ; Move inspectors to their territory
  ask inspectors [
    setxy ( mean [pxcor] of my-territory ) ( mean [pycor] of my-territory )
    show my-territory
    pd
  ] 
end

To check that it's working, you can have your inspectors wander around:

to go 
  ask inspectors [
    face one-of neighbors with [ member? self [my-territory] of myself ]
    fd 1
  ]
  tick
end

enter image description here

Upvotes: 5

Related Questions