Road Crossing
A frog learns to time its dash across six lanes of traffic. The genetic algorithm evolves a tiny neural network from stepping into cars to crossing again and again.
Go to the live page Open the Road Crossing demoThe Road Crossing demo is a take on the classic frog-across-the-road game. A frog starts at the bottom of the screen and has to reach the top through six lanes of moving cars. When it gets there it starts again from the bottom. The genetic algorithm evolves a small neural network to decide when to move. Early frogs hop straight into traffic. After a few generations they wait on the safe strips and dash through the gaps.
It lives alongside the other live-agent games (Flappy Bird, Snake, the Self-Driving Car, Dino Runner, Breakout, Pong and Gem Collector) under the same Live Agent demo. Use the tabs at the top of the page to switch between them.
The Problem¶
The frog stays in one column and has only two choices each step: move up one row, or stay where it is. There is no way back down. Between every pair of car lanes there is a safe median, so the frog never spends more than one step on a lane. The one decision that matters is whether it is safe to step onto the next lane right now. Cars in neighbouring lanes travel in opposite directions and move one cell every two steps.
The network reads six inputs: whether the frog is on a lane, whether the row above is a lane, how far away the nearest car in that lane is and which way it is moving, how far the nearest car in the frog's current lane is, and how far up the crossing the frog has got. One hidden layer of eight neurons feeds two outputs, up or stay, and the larger one wins.
The score counts every row the frog safely advances, so a full crossing is worth 12 and partial progress shows even before the first complete crossing. Fitness rewards each step up, takes a little away for each step spent waiting so the frog only waits when it must, and gives a bigger bonus for each full crossing. Getting hit ends the run with a penalty. A run also ends after 20 steps without moving up.
How a Run Looks¶
Click Start. The current best network plays a run live, with the up and stay scores shown each step. The chart on the right tracks the best score for each cycle.
The default traffic is dense, so this game uses a bigger population (80) and runs 15 generations between each replay you watch. Early generations get hit almost at once. Within a few cycles the frog starts waiting on the medians and timing its moves, and a strong policy crosses again and again without a scratch.
Playing It Yourself¶
Switch the mode toggle from Agent to You, or press M, to take over. Press the up arrow, or the up button on the on-screen pad, to step forward. To wait, do nothing. In You mode there is no idle time-out, so you can wait for a gap as long as you need. Press P to pause or resume and Esc to reset. Switch back to Agent at any time and the evolved network picks up from where you left off.
Settings Worth Trying¶
Open Settings to tune the next run:
- Traffic density. From 1 (light) to 5 (heavy), default 4. Higher means more cars and tighter gaps to time.
- Playback speed. How fast the replay plays, from 3 to 12 frames per second, default 5. It slows the frog and the traffic together, so each decision is easier to follow.
- Population size. More frogs per generation means more variety to select from, at the cost of a slower cycle.
- Generations per cycle. How much evolving happens between each live run you watch, from 1 to 15.
- Mutation rate. How much the networks are randomly perturbed each generation. A little keeps the search exploring; too much stops it from settling on a good policy.
What the Achievement Rewards¶
Signed-in users unlock the Road Crossing achievement the first time an evolved frog safely advances at least 60 rows (five full crossings) in one of the runs you watch in Agent mode. Your own runs in You mode don't count toward it.
Road Crossing records and shares like the other demos. See Sharing and recording for how to capture a clip of a clean crossing.
For more on how the demos catalog works, see What the Demos Do.