What is Genetic Brick Cars?
A genetic algorithm builds cars out of LEGO parts, lets them roll down a ramp, and breeds the ones that get furthest. It is a version of HTML5 Genetic Cars and its 3D sequel built from LEGO bricks, itself loosely based on BoxCar2D. Part models from the LDraw parts library (CC BY 4.0), physics by Rapier, rendering by three.js, seedrandom.js written by David Bau. (thanks!)
How the cars are built
- Every car is assembled from LEGO bricks, plates and slopes that fill a grid of sizes (1 or 2 studs wide, 1 to 8 long, plate or brick high), plus axle plates and wheels. Similar parts sit next to each other in the genome, so a small mutation turns a 2 x 2 brick into a 2 x 3, a 1 x 2 or a 2 x 2 plate.
- Axle plates and wheels are separate parts, and evolution works out where the wheels go: each wheel goes on a free pin of an axle plate it fits. Only real pairings are used: three wheel sizes (14 to 20 mm) on three axle plates with thin wheel pins (2 x 2, 2 x 2 extended and the wide 1 x 4), and a big 30 mm wheel on the plate 2 x 4 with rounded pins. Axle plates can be turned any way, so wheels only roll when their pins point across the ramp.
- Parts snap together like the real thing: each new part presses onto at least one stud of a part already in the car, either on top of it or hanging underneath. Studs only fit into the hollow underside of a part, and nothing may overlap (wheels included), so a part that can't find room is left out.
- The pile of bricks moves as one rigid body; wheels spin freely on their pins.
- Cars can break. Every stud that presses into another part grips it with a clutch force (about 1.5 N for a real brick). When the car takes a knock, each piece needs a force of its mass times the jolt to stay on, and that force pulls on all the studs holding the piece (and whatever hangs off it) to the rest of the car. The harder the pull compared to what those studs can hold, the likelier it is to snap off, so a slope held by one stud goes long before a plate clamped by eight. Broken pieces tumble away on their own, and the rest of the car carries on.
The ramp
- There is no motor: cars start at rest near the top and gravity does the rest.
- Everything is at real LEGO scale: the ramp is about 10 m long and 36 cm wide, a 2 x 4 brick weighs 2.3 g, and distances, speeds and weights are what a real toy car would do. (The physics engine works on a model 25 times bigger, slowed down 5 times, which moves exactly the same way under gravity.)
- The ramp starts smooth, with a gentle 7° slope: a car that drags much of its body along stalls, so it needs wheels that carry most of its weight. Further down it levels off but gets more and more irregular: bumps, dips and sideways tilts, shown by the baseplate turning from green to orange.
- A car that stops making progress for a second and a half, or goes over the side, is out. The red and white curbs are solid and one plate high, so they turn back a car drifting sideways, but a fast or tall one can trip over them.
- Cars can't steer: physics alone decides where they go, so lopsided cars veer off, slide, tip over and fall off.
- Each car runs in its own physics world, so the same car on the same ramp always rolls exactly the same way. Elite clones repeat their score.
Controls
The translucent car is the best run so far, replayed alongside the current generation.
| Save Population | Saves current population locally. |
|---|---|
| Restore Saved Population | Restore a previously saved population. |
| Surprise! | Toggles drawing, makes the simulation much faster. |
| New Population | Keeps the generated ramp and restarts the whole car population. |
| Fast Forward | Instantly finishes the current generation. |
| Create new world with seed | The same seed always creates the same ramp, so you can agree on a seed with your friends and compete. :) |
| Mutation rate | The chance that each gene in each individual will mutate to a random value when a new generation is born. |
| Mutation size | The range each gene can mutate into. Lower numbers mean the gene will have values closer to the original. |
| Elite clones | The top n cars that will be copied over to the next generation. |
| View top replay | Pause the current simulation and show the top performing car. Click a second time to resume simulation. |
| Camera | Drag to orbit, scroll to zoom, and use the view buttons for preset angles. Click a car (or its bar in the car list, or its marker on the ramp profile) to follow it. The parts list shows the car you follow. |
The genome
20 part slots, each with 10 genes:
- Used: whether the slot adds a part at all (the first slot always does)
- Variant: plain, slope, axle plate or wheel
- Length: none, 1, 2, 3, 4, 6 or 8 studs (for a wheel, its size)
- Width: 1 or 2 studs
- Height: plate or brick
- Anchor: which earlier part to connect to (for a wheel, which free pin)
- Side: on top of the anchor or underneath it
- Offset: which of the anchor's studs to use
- Rotation: quarter turns (wheels ignore it)
- Colour
LEGO® is a trademark of the LEGO Group, which does not sponsor, authorize or endorse this project.