我正在尝试建立一个无人机模型,其中无人机具有电池电量并移动到充当覆盖点的不同白色斑块并移动到中心/房屋进行充电。目前,无人机会移动到一个随机的区域并停留在那里,直到需要充电为止,但是无人机彼此堆叠,我希望它们移动到一个没有其他无人机的白色区域。
我在我的 go 程序中尝试了以下检查。
set target one-of patches with [pcolor = white and not any? people-here] face target
但是无人机仍然堆积在每个无人机上,即使在看过类似的帖子后我也不知道该怎么做。
这是我的完整代码:
breed [people person]
people-own [target energy]
breed [houses house]
to setup
clear-all
set-default-shape houses "house"
setup-patch
;; Create a house in the middle
create-houses 1 [
setxy 0 0
set size 2
]
create-people number-of-people [
setxy random-xcor random-ycor
set target one-of houses
set energy 100
face target
set color blue
]
reset-ticks
end
to setup-patch
let white-patch-count 0
ask patches [
set pcolor green
if random-float 1 < 0.2 and white-patch-count < number-of-patches [
if not any? other patches in-radius radius-size with [pcolor = white] [
set pcolor white
set white-patch-count white-patch-count + 1
]
]
]
end
to go
ask people [
;; Decrease energy level for each tick
set energy energy - 1
if energy <= 0 [
die
]
;; If at target, choose a new random target
if distance target = 0 [
ifelse pcolor = white [
ifelse energy > energy-level [
stay-at-patch
] [
set target one-of houses
face target
]
] [
set target one-of patches with [pcolor = white and not any? people-here]
face target
]
]
;; Move towards target. Once the distance is less than 1,
;; use move-to to land exactly on the target.
ifelse distance target < 1 [
move-to target
if any? houses-here [set energy 100]
] [
fd 1
]
]
tick
end
to stay-at-patch
;; Implement the behavior for staying at the current patch
;; Keep staying until the energy level is back at 30
if energy = 30 [
set target one-of houses
face target
]
end
请忽略海龟/特工被称为人。一旦我让它工作,我就会重命名它。预先感谢
我一直在尝试使用您的代码解决问题,但我无法实现。但是,我很确定问题在于您定义
target
的方式。
set target one-of patches with [pcolor = white and not any? people-here]
需要某种限制,以避免两台或更多无人机位于同一目标区域中。
因此,我考虑了两种选择:
只要确保您的无人机一直在更新目标,这样如果他们选择的无人机刚刚被占用,他们就会选择另一架。
定义目标时,确保无人机无法选择已被其他无人机设置为目标的补丁。
希望这可以帮助你!