问题
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 ]
]]
回答1:
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.
Instead, write turtles-on on-left = no-turtles
or better yet not any? turtles-on on-left
.
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.
来源:https://stackoverflow.com/questions/49709206/how-to-overtake-in-a-two-lane-scenario