Inspiration

I was inspired by seeing diagrams of gerrymandering like Gerrymandering diagram

and I thought, could I make a program to do that to any grid of people?

What it does

It simulates a grid of people, each with a red or blue political party. It then draws district lines around groups of people, and each district is "won" by which party has more voters in the district.

The program can then re-draw the district lines in order to make each party win more/less districts.

It also allows the user to change the number of people in each party, the number of people in each district, and the number of total districts in the simulation.

How I built it

I built this over many months in late 2020. Here are a couple major steps.

  1. First I had to make a system to store information about the people and districts in the simulation and display them on the screen. Each person has a position and a party and each district has a list of people contained.

  2. Then I had to make a swapping system, where two adjacent districts can "trade" people between them.

  3. Next I made the computer try to swap with one party's interests. This required a lot of thinking about how to make the computer pick the right people to swap between districts, and using a lot of complicated conditions in the swap logic.

  4. I then had to optimize this algorithm, from taking a couple seconds to gerrymander a grid, to taking a couple of milliseconds. One part of this was profiling the code to see which lines took the longest, but a more influential (and time consuming) step was re-writing all the code in JavaScript, instead of python.

  5. Finally, I had to design the interactive parts of the website, with pie charts to adjust population and districts, and slider to adjust simulation parameters.

Challenges we ran into

  • Making the program more intuitive to use and understand
  • Making the algorithm run faster

Built With

Share this project:

Updates