我正在 Netlogo 中创建一个检查模型,每个检查员负责 n 个补丁。也就是说,我需要将世界划分为每个检查员专属的区域。
我尝试使用设置范围(世界宽度*世界高度/检查员)^ 0.5
但它们的范围比预期要高,并且允许一名检查员“侵入”其他检查员的区域,这是不可取的。
这种方法可能过于复杂,但可能会满足您的需要 - 它将世界分成甚至垂直的“领土”带。请注意,只有当世界的最小 xcor 为 0,并且世界宽度可被 的数量整除时,它才能正常工作inspectors- 否则一个检查员将拥有与其他检查员不同的检查区域。例如,使用此设置:
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
Run Code Online (Sandbox Code Playgroud)
制作列表来存储最小值和最大值xcor,然后使用它们来划分补丁以将其作为区域分配给不同的检查员。更多解释见评论:
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
Run Code Online (Sandbox Code Playgroud)
要检查它是否正常工作,您可以让检查员四处走动:
to go
ask inspectors [
face one-of neighbors with [ member? self [my-territory] of myself ]
fd 1
]
tick
end
Run Code Online (Sandbox Code Playgroud)