我创造了3辆汽车,但我只有ID为0和1的汽车路线:
车0 - >路线[4 3]
Car 1 - >路线[7 6 5]
Car 2 - > route []因为我希望这辆车停下来,这是多余的
我想为汽车创建崩溃?变量,即 IF 达到25个刻度,变量崩溃?它是 TRUE ,其中一辆车停在路上......
在我的go程序中,我试着把它,但只有一个:
to go
ask carr with [ not route-complete? and not crash?] [
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
if route = [] [
set target spawn-target
]
if target = nobody [
set target item route-counter route
]
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
ifelse target != spawn-target [
set route-counter route-counter + 1
] [
set route-complete? true
]
set target nobody
]
if route-counter > length route - 1 [
set route-counter 0
set target spawn-target
]
]
tick
end
我应该只停止一个拥有路线的车辆而不计算已经停止的ID为2的车辆。 因为我认为这个代码占了所有的汽车,并选择一个停止,但我想停止或汽车0或1。 怎么修?
然后,我怎样才能将汽车定义为备用汽车(ID为2的汽车)以停靠汽车路线访问失踪的房屋? 因为如果其中一辆车0或1停在25蜱,肯定会有房子没有被访问过......
如果有人能帮助我完成两项无法发展的任务,那将会非常有帮助。谢谢
答案 0 :(得分:2)
我认为最简单的可能就是让那辆车崩溃了#39;使用sublist
将其路线的剩余部分传递到备用车辆以索引route
变量 - 请查看dictionary definition以获取更多详情。为了尽可能少地更改代码,请尝试在go
过程的开头插入以下代码(代码注释提供更多详细信息,下面是完整的go
过程):
to go
; only run this chunk if ticks = 25
if ticks = 25 [
ask one-of carr with [ not crash? and not route-complete? ] [
; get one of the still moving cars to:
; set crash? to true, change color to yellow
; for easy visibility
set crash? true
set color yellow
; make a temporary variable to hold the remaining house
; targets using sublist
let remaining-route sublist route route-counter ( length route )
ask one-of carr with [ route = [] ] [
; have the 'spare' care take over the remaining-route,
; set route-complete to false, and set the current
; target to 'nobody' so that the other conditions for
; movement are satisfied
set route remaining-route
set route-complete? false
set target nobody
]
]
]
;; ask carr with route-complete set to false
ask carr with [ not route-complete? and not crash? ] [
;for the spare car not to move
if route = [] [
set target spawn-target
]
if target = nobody [
set target item route-counter route
]
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
ifelse target != spawn-target [
set route-counter route-counter + 1
] [
set route-complete? true
]
set target nobody
]
;; If the route counter would index outside of the
; list boundaries, reset it to 0
if route-counter > length route - 1 [
set route-counter 0
set target spawn-target
]
]
tick
end
请注意,活动carr
在技术上(很少)可以在ticks
= 25之前完成路由。为避免这种情况,请在路由完成时停止模型或解决某些问题。其他方式。