Solving Political Boundaries Through Simulation

In this writeup we’ll discuss two algorithms, simulated annealing and genetic algorithms, and show how they can be applied to the problem of drawing political boundaries while avoiding gerrymandering.

This writeup is available on GitHub, or my personal website.

Slides for this post are also available here

Table of Contents

  1. Table of Contents
  2. [An Introduction to Simulated Annealing and Genetic Algorithms](#an-introduction-to-simulated-annealing-and-gene tic-algorithms)
    1. What is a Fitness Function?
    2. What is Simulated Annealing?
    3. What are Genetic Algorithms?
  3. Drawing Political District Boundaries
    1. Procedure
      1. Overall Structure
        1. Some Quick Notes
      2. Helpful Code
        1. Finding Neighbors of a Point
        2. Determining if a District is Valid
        3. Finding District Neighbors
        4. Fitness Function
      3. Generating Random Solutions
      4. Simulated Annealing
        1. Mutations
        2. A Better Starting Point
      5. Genetic Algorithm
        1. Combining Solutions
  4. Using Provided Code
  5. Next Steps

An Introduction to Simulated Annealing and Genetic Algorithms

First let’s talk about our algorithms.

Simulated Annealing and Genetic Algorithms are both methods of finding solutions to problems through simulations. In a nutshell, basically testing a large amount of semi-random solutions, looking at random combinations of our solutions, and then keeping the best that we encounter.

The benefit of these algorithms is that we can relatively quickly approximate a good solution without much computation time. Our solution won’t be “the best” unless we get extraordinarily lucky, however it will be a good approximation.

What is a Fitness Function?

Let’s look at what the wikipedia page says on the matter.

A fitness function is a particular type of objective function that is used to summarise, as a single figure of merit, how close a given design solution is to achieving the set aims.

In other words, it’s a single number that basically tells us how “good” of a solution we have. For our genetic algorithm and simulated annealing approach we’ll want to maximize this number, thereby maximizing how “good” our solutions are.

What is Simulated Annealing?

Simulated annealing can be defined as follows.

  1. Generate a random solution
  2. Generate a “neighboring solution” to our generated solution
  3. Keep whichever is better, or (with decaying probability) take the new one regardless
  4. Go back to 2

Incomplete python code for this is below.

What are Genetic Algorithms?

Genetic algorithms are very similar, and the algorithm can be defined as
follows.

  1. Randomly generate an initial population of solutions
  2. Use our solution population to generate some large number of children (note,
    these children should inherit properties from their parents)
  3. Keep the best of our total population
  4. Go back to 2

Again, incomplete code is below.

Drawing Political District Boundaries

Now that we know what these monsters are, we can dig into how they can be
applied to solving a system.

Let’s say we’re interested in determining how to section off a two-party system
of voters into “equal” districts, for some definition of equal. Our system is
defined in a provided file that simply denotes, for every index, the type of
voter in that location. It looks like this

Which can be plotted for readability.

And our other (larger) state looks like the following.

This pattern will continue for the rest of the writeup, I’ll talk about (and
show) the smaller version first, and then follow up with the larger version.

Procedure

So in the context of our problem, we can examine how the code actually works.

Overall Structure

The structure is as follows:

  • main is responsible for running and calling everything
  • genetic_algorithm uses a genetic approach to solve the problem
  • simulated_annealing likewise uses simulated annealing
  • Each solution uses a System instance which keeps track of the system state
  • Each solution is an instance of a Solution which also keeps track of its
    particular state
  • Each district is a Mask which provides an ease of access through
    abstraction.

So basically how this works is we read in the file, creating a System
instance, and then create an empty Solution instance to keep track of our
changes.

Some Quick Notes

  • There’s a lot of code here, and to be honest, it’s more than is really
    necessary. The reason for it however, is so that I can abstract away the
    more complicated parts and just say something like solution.mutate() instead
    of digging into how or why the mutation algorithm works. This abstraction is
    why the code to do simulated annealing is so terse, abstraction makes
    algorithms terse.
  • All indexing starts from the upper left point, which is at (0, 0), and all
    indices are (y, x) pairs.
  • There’s a hierarchy of the code, our system is represented by a System
    object, and each solution is represented by a Solution object, which
    consists of several Mask objects that provide a direct interface to each
    district.
  • I like properties, so if something looks like it might be doing something
    complicated under the hood (like the value function), it probably is.

Helpful Code

While solving this problem, there are a couple sub-problems that we need to
consider and solve.

Finding Neighbors of a Point

Our first big problem is how we find neighbors of a single point. For any (y,
x)
pair we can express its neighbors using the following algorithm.

  1. Iterate over range(-1, 2) for both x and y
  2. For each loop, accept (y + yi, x + xi) if the following conditions hold:
    • y + yi is within the range of the field
    • x + xi is within our domain of the field
    • xi and yi are not both equal to zero

In python, this is expressed as

Determining if a District is Valid

One of the problems we need to solve is to know if any given district we have is
valid. In our case, valid simply means having a single connected
component
,
which we can determine using connected component
labelling
. The
easiest version of this (which is in the implementation) is just examining a
single component at a time, and the algorithm is as follows (from wikipedia):

  1. Start from the first pixel in the image. Set “curlab” (short for “current
    label”) to 1. Go to (2).
  2. If this pixel is a foreground pixel and it is not already labelled, then
    give it the label “curlab” and add it as the first element in a queue, then
    go to (3). If it is a background pixel or it was already labelled, then
    repeat (2) for the next pixel in the image.
  3. Pop out an element from the queue, and look at its neighbours (based on any
    type of connectivity). If a neighbour is a foreground pixel and is not
    already labelled, give it the “curlab” label and add it to the queue.
    Repeat (3) until there are no more elements in the queue.
  4. Go to (2) for the next pixel in the image and increment “curlab” by 1.

Which is implemented in our code as the following.

Finding District Neighbors

Another huge problem is we have to find all neighbors of a given district. This
is incredible similar to the Connected Component Labelling process above.

The basic algorithm is as follows.

  1. Get a random spot inside the given district
  2. Add this spot to a Queue
  3. Initialize an empty labelling array (as with connected component labelling)
  4. While the queue is not empty, get an new (y, x) pair.
  5. If the point falls within the district, get all of the point’s neighbors, add
    them to the queue, and go back to (4)
  6. If the point does not fall into the district, add it to the list of district
    neighbors.

Fitness Function

Taking a step back from the code and considering the real world, let’s think
about what we’d ideally like to emphasize in a political districting system.

  • We’d want districts to be homogeneous, i.e. each district is comprised of
    either all Republican or all Democrat voters.
  • We want our district ratios to approximately match our population ratios. By
    this I mean, if we have 52% Republican voters in the general population, 52%
    of the districts should have a Republican majority, and vice-versa for the
    Democrat population.
  • We’d want to avoid
    gerrymandering, which is the
    practice of shaping voting districts such that a certain political party has
    an unfair advantage. A real world example of the sort of district we’d like to
    avoid looks like this (which is in Texas)
  • We want all districts to be around the same population size, i.e. there are an
    equal number (within reason) of voters in each district.

We can design our fitness function to meet these criteria. The final fitness
function that we use emphasizes the following qualities in its assessment.

  1. Validity of solution
  2. Make sure the ratio of R to D majority districts matches the ratio of R
    to D in the general population.
  3. Make sure each district is as homogeneous as possible
  4. Reduce the value of the district if its size isn’t close to the “ideal size”,
    which is total_size / num_districts. This is our attempt to reduce the
    “squiggliness” of a district, however it’s not perfect and squiggly districts
    still pop up.
  5. We also take into account that in non-homogeneous districts voters that
    aren’t affiliated with the majority party might be swayed by targeted
    campaigns. To this effect we account each non-affiliated “zone” with a weight
    of -0.9 instead of -1.
  6. Finally, we can also minimize edge length as well as trying to keep each
    district the same size. This will result in hopefully ideal districts

Generating Random Solutions

This algorithm is very straightforward.

  1. Generate a number of “spawn points” equal to the number of districts.
  2. Fill.

The fill algorithm is also straightforward.

  1. Set a list of available districts.
  2. While there are any non-set points, pick a random district, i, from the
    list of available districts.
  3. Get a list of all neighbors of the district, but filter to only 0-valued
    entries.
  4. If no such neighbors exist, remove this district from the list of available
    districts.
  5. Otherwise pick a neighbor at random and set it to i.
  6. Loop back to (2).

We can see how this looks for our two different state sizes.

Simulated Annealing

Now that we can do all of that, let’s talk about simulated annealing. The basic
algorithm is as follows.

  1. Generate a random solution
  2. Generate a solution neighbor
  3. If the new solution is better than the old, set the current solution to the
    new one.
  4. If the solution is worse, but random.random() < math.exp(dv / (k * T)),
    where dv is the difference between solution values, k is a set constant,
    and T is the current iteration value, accept it.

To show how choice of k effects the algorithm, we can plot this.

The entire process looks like this:

Which has the following final solution.

And for the large system,

Which has the following final solution.

Mutations

Much of simulated annealing rests on being able to find a valid neighboring
solution to our current solution. We do this through a process I call
“mutation”, which simply flips a zone from one district to another, based on
some randomness and criteria to make the new solution valid.

The general algorithm can be thought of as follows.

  1. Find all district neighbors
  2. Pick a neighboring point at random.
  3. If the neighboring point’s district has at least size 2, set this neighboring
    point to our district.
  4. Otherwise, pick a different neighboring point.

Which can be visualized as follows.

A Better Starting Point

Instead of naively choosing just one random solution to start with simulated
annealing, we can instead generate many initial random solutions, and then pick
the best for our algorithm.

This has the advantage of starting off from a better place, and theoretically
have less “distance to climb”.

Genetic Algorithm

This algorithm is also straightforward, and is generally as follows.

  1. Generate a set of random “parent” solutions.
  2. From our parents, generate a large set of “children” solutions.
  3. Sort the entire population by their value.
  4. Set our parents to be the “best” of the current population, discard the rest.
  5. Go back to (2).

The entire process looks like this:

Which has the following final solution.

And for the large system,

Which has the following final solution.

Combining Solutions

As simulated annealing relies on mutate() to narrow down on a good solution,
the genetic algorithm relies on combine() to take two solutions and generate a
“child” solution.

We can think of the code as follows.

  1. Shuffle our two parents in an array.
  2. Shuffle a list of districts.
  3. Set a cursor that points to the first parent in the array.
  4. Iterate through our districts with variable i
  5. For the current district, find all points of the parent that our cursor is
    pointing to.
  6. Get all “open” (i.e. set to 0) points for our child solution
  7. For every point that matches between these two sets, make a new bitmask.
  8. If this bitmask is valid (i.e. one connected component), set all point in
    this child solution to our current district
  9. Otherwise, make the district valid and set the bits in the child solution
  10. Flip the cursor

The algorithm behind making a district valid is easy, if we have more than one
connected component in a given district, pick one at random and discard the
other connected components.

Which can be visualized as follows.

Final Thoughts

Both of these approaches can be applied to solve incredibly complex problems
with varying degrees of success, and much of their success hinges on how
effective your evaluation of a given solution fitness’ is.

We also realize that any given “final solution” is somewhat unique, or at the
very least hard to obtain again. If we visualize our solution space as a
two-dimensional plane, with each solution having some value, then a final solution that we find is merely one of these peaks, so a re-run
will not necessarily yield the same solution again.

To get around this, we could theoretically run the code thousands of times, with
different seeds each time, and then pick the best out of all runs.

Using Provided Code

If you’re a TA, this is straightforward! After installed the required libraries
(again check the repository) just run

If you want to dig a little deeper, use the -h flag to see what it can do, but
here’s a short list as well.

  • Use Simulated Annealing on the file
  • Use the Genetic Algorithm on the file
  • Set the number of districts for either solution type
  • Set the precision (number of runs) for either algorithm
  • Animate the solution process
  • Create gifs of the solution process (otherwise just .mp4)
  • Generate report (README.md) assets.
  • Do all of the above in one go.

Next Steps

I want to do more for this project but I’m limited in the time I have. I do have
a couple of ideas for next steps however.

  • Parallelizing – Instead of just running simulations on a single thread, we
    could theoretically spin up a bunch of different threads and run simulations
    on them simultaneously, only keeping the best of all trials.
  • Real Data – It would be amazing to take the approaches used in this writeup
    and apply it to real-world political data. A topic for a different blog post!

Leave a Reply

Your email address will not be published. Required fields are marked *