Netlogo - 在 n 个代理之间平分世界
Netlogo - Divide world equally between n agents
我正在Netlogo中创建一个检查模型,每个检查员负责n个补丁。也就是说,我需要把世界划分成每个检查员专属的区域。
我试过了
设置范围 (world-width * world-height / inspectors) ^ 0.5
但他们的范围比预期的要高,它允许一个检查员 "invade" 其他检查员的区域,这是不可取的。
这种方法可能过于复杂,但可以满足您的需要 - 它将世界分割成均匀的垂直带 "territory." 请注意,只有当世界的最小 xcor 为 0 并且如果world-width 可以被 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
制作列表来存储最小值和最大值 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
要检查它是否正常工作,您可以让检查员四处走动:
to go
ask inspectors [
face one-of neighbors with [ member? self [my-territory] of myself ]
fd 1
]
tick
end
我正在Netlogo中创建一个检查模型,每个检查员负责n个补丁。也就是说,我需要把世界划分成每个检查员专属的区域。
我试过了 设置范围 (world-width * world-height / inspectors) ^ 0.5
但他们的范围比预期的要高,它允许一个检查员 "invade" 其他检查员的区域,这是不可取的。
这种方法可能过于复杂,但可以满足您的需要 - 它将世界分割成均匀的垂直带 "territory." 请注意,只有当世界的最小 xcor 为 0 并且如果world-width 可以被 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
制作列表来存储最小值和最大值 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
要检查它是否正常工作,您可以让检查员四处走动:
to go
ask inspectors [
face one-of neighbors with [ member? self [my-territory] of myself ]
fd 1
]
tick
end