在两车道情况下如何超车? [英] how to overtake in a two lane scenario?
问题描述
我一直在为两车道场景编写代码方面的基础知识,我尝试了下面的编码,我认为这样可以正常工作,但是汽车仍然不会接管代码,我不确定是否存在矛盾陈述.到目前为止,这部分编码将使汽车加速并在汽车在前面时停止它们,但由于某种原因不会移动到另一个车道.有什么想法吗?
i have been working a basic over taking a code for a two lane scenario, i have attempted the coding below and i thought that it would work as it is, however cars still wont over take, im not sure if there are contradicting statements. so far this section of coding will accelerate the cars and stop them when a car is in front but for some reason will not move to the other lane. any ideas?
let in-lane (patch-set patch-ahead 2 patch-ahead 1.5 patch-ahead 1 ); patch-
left-and-ahead 26 1 patch-right-and-ahead 26 1)
let car01-ahead one-of cars01-on in-lane
let on-left (patch-set patch-left-and-ahead 90 1 );patch-left-and-ahead 45 1.5 patch-left-and-ahead 135 1.5)
let on-right (patch-set patch-right-and-ahead 90 1 );patch-right-and-ahead 45 1.5 patch-right-and-ahead 135 1.5)
; set meaning "road-all" = "road-right" "road-left"
ifelse car01-ahead = nobody [
ifelse speed < maxSpeed [set speed speed + acceleration] [set speed speed - deceleration]
]
[
ifelse [meaning] of patch-left-and-ahead 90 1 != "road-left"
[ifelse turtles-on on-left = nobody
[move-to patch-left-and-ahead 90 1]
[ifelse [speed] of car01-ahead = 0
[set speed 0] [
ifelse [speed] of car01-ahead >= maxSpeed
[ set speed maxSpeed
set speed speed - deceleration]
[ set speed maxSpeed
set speed speed - deceleration ]]]][
ifelse [meaning] of patch-right-and-ahead 90 1 != "road-left"
[ifelse turtles-on on-left = nobody
[move-to patch-right-and-ahead 90 1]
[ifelse [speed] of car01-ahead = 0
[set speed 0] [
ifelse [speed] of car01-ahead >= maxSpeed
[ set speed maxSpeed
set speed speed - deceleration]
[ set speed maxSpeed
set speed speed - deceleration ]]]][ set speed maxSpeed
set speed speed - deceleration ]
]]
推荐答案
turtles-on on-left = nobody
始终为false,因为turtles-on
始终返回代理集,而nobody
不是代理集. nobody
发生在预期只有一个代理但没有可用代理的情况下;与代理集分开.
turtles-on on-left = nobody
is always false, because turtles-on
always returns an agentset, and nobody
is not an agentset. nobody
occurs in contexts where a single agent was expected but none was available; that is separate from agentsets.
相反,写turtles-on on-left = no-turtles
或更好的not any? turtles-on on-left
.
在这种情况下,我只需要看一下代码就能发现问题(或至少一个问题).如果我无法做到这一点,我建议在代码中添加print
语句,以检查变量的值是否符合您的预期,以及是否实际采用了预期的分支.该技术将发现turtles-on on-left = nobody
始终为假.
In this case, I was able to spot the problem (or at least, one problem) just by eyeballing the code. If I hadn't been able to do that, I would have suggested adding print
statements to the code to check whether the values of variables are what you expected, and whether branches you expected to be taken were actually taken. That technique would have uncovered that turtles-on on-left = nobody
is always false.
这篇关于在两车道情况下如何超车?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!