Frog Crossing
A frog learns to dodge four lanes of traffic, then ride drifting logs across a river into one of three home bays. The genetic algorithm evolves a tiny neural network from hopping into cars to crossing again and again.
Go to the live page Open the Frog Crossing demoThe Frog Crossing demo is a take on the classic frog-across-the-road arcade game. A frog starts at the bottom of the screen and has to get through four lanes of traffic, then across a river on floating logs, and hop into one of three home bays at the top. When it gets home it starts again from the bottom. The genetic algorithm evolves a small neural network that decides each hop. Early frogs hop straight into traffic or into the water. After a few generations they wait for their gaps, ride the logs and step along them to line up with a bay.
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. This demo used to be called Road Crossing; old links still work.
The Problem¶
Each step the frog can hop up, left or right, or wait. There is no hopping back down.
- The road. Four lanes in two back-to-back pairs, with a safe strip between the pairs. Fast lanes (their cars move every step) sit next to slower ones (every other step), and neighbouring lanes travel in opposite directions, so a gap that is safe on one lane is not on the next. Waiting in the middle of a pair usually gets the frog hit, so it has to wait on the strip until both lanes will be clear.
- The river. Four lanes of drifting logs, with a small island in the middle. The frog can only stand on a log: open water drowns it. A log carries the frog along as it drifts, and a frog that rides too far is carried off the edge.
- The home bays. The top row is a hedge with three gaps. Hopping in anywhere else ends the run, so the frog has to use left and right, or let a log carry it, to line up with a bay before the last hop.
The network reads 15 inputs: whether the frog is on the road or a log and how many steps it can safely stay put; for the next row up, whether it is road, river or the goal row, and how many steps until a hop up, up-left or up-right would be safe; the same straight up for the row after that; how far it is from the nearest home bay; and where it is across the screen and how far up the crossing it has got. One hidden layer of eight neurons feeds four outputs, up, wait, left or right, and the largest one wins.
The score counts every row the frog reaches for the first time in a crossing, so a full crossing is worth 12, partial progress shows before the first complete crossing, and hopping sideways or riding a log earns nothing on its own. Fitness rewards each new row, takes a little away for every other step so the frog only waits when it must, and gives a bigger bonus for each crossing. Getting hit, falling in, being carried off the edge or hitting the hedge ends the run with a penalty. A run also ends after 25 steps without reaching a new row.
How a Run Looks¶
Click Start. The current best network plays a run live, with the score for each move shown each step. The rest of the generation plays the same crossing as faint frogs, and Still in play in the Training panel counts how many are left. Use the Show all frogs switch next to the Start button to watch only the best frog. 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 frogs get hit or fall in almost at once. A frog usually gets a crossing or two within the first few cycles, then stalls on the river for a while, often riding a log away from the bays. In our tests, five runs out of six learned to cross over and over, anywhere from the 7th to the 18th cycle; the sixth was still stuck after 20 cycles. If a run doesn't crack it, press Reset and Start to try a new population.
Playing It Yourself¶
Switch the mode toggle from Agent to You, or press M, to take over. Press the up, left or right arrow, or push the joystick over the game, to hop. The joystick appears wherever you touch or click the game, so you can play with your thumb in whichever spot is comfortable, and it stays there for next time. Holding a direction keeps hopping. To wait, do nothing. In You mode there is no idle time-out, so you can wait for a gap or a log as long as you need. When the game ends, tap the game (or press Space, or Play again) to start a new round straight away. 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. The river's logs stay the same.
- Playback speed. How fast the replay plays, from 3 to 12 frames per second, default 5. It slows the frog, the traffic and the logs 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 Frog Crossing achievement the first time an evolved frog safely reaches 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. If you earned it as Road Crossing, you keep it.
Frog 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.