将乌龟向前移动到一个目标
moving turtles forward to a target in one tick
我正在研究医院模型,我希望护士选择其中一名患者作为他们的目标并继续前进,直到他们在一个刻度内到达他们的患者。
我在我的代码中使用 while ,但它只会在一次滴答中向前移动一个补丁。
这是我的代码:
to nurse-visit
ask nurses
[ let target one-of patients
if distance target = 0
[face target]
;;move towards target. once the distance is less than 1
ifelse distance target < 1
[move-to target]
[while [any? patients in-radius 1]
[ fd 1 ]]
这里有人可以帮助我吗?
将行分解成伪代码可能有助于找出它不起作用。目前,您的代码是这样的:
if distance target = 0
[face target]
如果到目标的距离为0,面向目标。在NetLogo中,一旦与目标的距离为0,就没有意义,因为它们共享一个位置。
ifelse distance target < 1
[move-to target]
如果目标距离不到 1 个补丁,则移动到它。太棒了-我们可以稍后重用这一点,我们只需要稍微改变一下顺序。
[while [any? patients in-radius 1]
[ fd 1 ]]
趁附近有患者,向前走。在这里,我们可能要指的是target
而不是一般的patients
。
总的来说,操作的逻辑和顺序似乎不太对。我们真正想做的是让每位护士:
- 选择目标患者
- 确定与该患者的距离
- 直到到目标的距离恰好为 0,执行以下两项操作之一:
- 如果到目标的距离>1,面向目标向前移动1
- 如果到目标的距离<1,向右移动到目标
将其转化为代码,您可以这样做:
breed [ nurses nurse ]
breed [ patients patient ]
to setup
ca
ask n-of 5 patches [ sprout-patients 1 ]
create-nurses 1
reset-ticks
end
to nurse-visit
ask nurses [
; Pick a patient
let target one-of patients
; Until my distance to my target is 0, do the following:
while [ distance target > 0 ] [
ifelse distance target > 1 [
; If the distance to my target is greater than 1, face
; the target and move forward 1
face target
fd 1
wait 0.1
] [
; Otherwise, move *exactly* to the patient
move-to target
]
]
]
请注意,我在其中放置了一个 wait
计时器,这样无论您的显示速度如何,您都可以看到护士在移动。
我正在研究医院模型,我希望护士选择其中一名患者作为他们的目标并继续前进,直到他们在一个刻度内到达他们的患者。 我在我的代码中使用 while ,但它只会在一次滴答中向前移动一个补丁。 这是我的代码:
to nurse-visit
ask nurses
[ let target one-of patients
if distance target = 0
[face target]
;;move towards target. once the distance is less than 1
ifelse distance target < 1
[move-to target]
[while [any? patients in-radius 1]
[ fd 1 ]]
这里有人可以帮助我吗?
将行分解成伪代码可能有助于找出它不起作用。目前,您的代码是这样的:
if distance target = 0
[face target]
如果到目标的距离为0,面向目标。在NetLogo中,一旦与目标的距离为0,就没有意义,因为它们共享一个位置。
ifelse distance target < 1
[move-to target]
如果目标距离不到 1 个补丁,则移动到它。太棒了-我们可以稍后重用这一点,我们只需要稍微改变一下顺序。
[while [any? patients in-radius 1]
[ fd 1 ]]
趁附近有患者,向前走。在这里,我们可能要指的是target
而不是一般的patients
。
总的来说,操作的逻辑和顺序似乎不太对。我们真正想做的是让每位护士:
- 选择目标患者
- 确定与该患者的距离
- 直到到目标的距离恰好为 0,执行以下两项操作之一:
- 如果到目标的距离>1,面向目标向前移动1
- 如果到目标的距离<1,向右移动到目标
将其转化为代码,您可以这样做:
breed [ nurses nurse ]
breed [ patients patient ]
to setup
ca
ask n-of 5 patches [ sprout-patients 1 ]
create-nurses 1
reset-ticks
end
to nurse-visit
ask nurses [
; Pick a patient
let target one-of patients
; Until my distance to my target is 0, do the following:
while [ distance target > 0 ] [
ifelse distance target > 1 [
; If the distance to my target is greater than 1, face
; the target and move forward 1
face target
fd 1
wait 0.1
] [
; Otherwise, move *exactly* to the patient
move-to target
]
]
]
请注意,我在其中放置了一个 wait
计时器,这样无论您的显示速度如何,您都可以看到护士在移动。