Mercurial
Mercurial

Reputation: 79

How to change light breed colour in Netlogo

I am currently trying to remodel the town and traffic crowd simulation found here to support self driving AI cars, which can be found here.

What I am currently stuck is is when the traffic lights where by the lights go from red straight to green, I want to add the yellow color into the simulation so the lights go from red, then to yellow then to green this is what i have but it just seems to freeze on yellow.

Please any help would be helpful, thanks.

Upvotes: 1

Views: 103

Answers (1)

JenB
JenB

Reputation: 17678

I have below reformatted your control-traffic-lights code

to control-traffic-lights
  if ticks mod (100) = 0
  [ change-color lightsR  ; red to yellow; others to red
    change-color lightsL  ; red to yellow; others to red
    change-color lightsU  ; red to yellow; others to red
    change-color lightsD  ; red to yellow; others to red
    yellow-color lightsR  ; yellow to green; others to yellow
    yellow-color lightsL  ; yellow to green; others to yellow
    yellow-color lightsU  ; yellow to green; others to yellow
    yellow-color lightsD  ; yellow to green; others to yellow
 ]
end

Imagine that you hit a relevant tick (every 100), start with the lightsR - any that are red get turned yellow on the first line, the get turned green on the fifth line. If they are any colour other than red on entry, they get turned red on the first line and yellow on the fifth line.

All four breeds of lights are going through the same logic. That is, the yellow-color procedure is being immediately applied to the lights based on the colour that they exit the change-color procedure.

UPDATE: I didn't make any changes to your code, I just formatted it so that it was readable to try to explain your logic issue.

If they enter as red, they get changed to green (via yellow) If they enter as yellow, they exit as yellow (via red) If they enter as green, they exit as yellow (via red)

What you need to do is change when the yellow-color procedure runs, so it's a few ticks later than the other colour change rather than in the same tick.

Try changing the second if ticks mod (100) = 0 [yellow-color l ...] to if ticks > 10 and ticks mod (105) = 0 [yellow-color l ...]

Upvotes: 2

Related Questions