← All posts

Modernizing my master's dissertation with AI

Evolution of a 149-cell lattice under the GKL rule, which ends up all white or all black

Introduction

Continuing the series started here, I’m going to tell you about one of my biggest wins using Claude, after the time I spent customizing it.

In 2008 I started a master’s degree in Evolutionary Computation at Universidade Presbiteriana Mackenzie, where I worked on using a ternary representation in genetic algorithms to find rules for binary cellular automata on the Density Classification problem. Didn’t understand a word? Relax, I’ll explain it further down.

What I want to show here are the results in code improvement, structure, and analysis of what was done together with Claude. I started out with the idea of modernizing my code from more than 15 years ago, and ended up with:

  • an absurd improvement in execution time
  • standardized output between the host-only run and the GPU run
  • some hardcoded parameters (please don’t judge me, I had little time to write that code) turned into command-line inputs to configure the experiments
  • easy replication of the original algorithms we used as a baseline
  • tools to analyze all the outputs statistically, including grouping by families of automata rules.

All of this in 1 week, and not coding 24/7. Saying I was impressed is not enough, I was blown away.

What is the problem

Let’s start from the beginning: the density classification problem, for a binary cellular automaton (CA). A cellular automaton (put very simply) is an execution rule placed on a lattice, where all the cells always follow the same rule, all at the same time. Take the simplest case possible, a binary CA of radius 1 (each cell looks only at its left and right neighbours), following the rule shown below:

Rule 30

What this rule says, always from the point of view of the central cell (black = 1, white = 0), is that if the left neighbour is black, the cell only stay black when herself and the right neighbour are both white; if the left one is white, the cell will become black when at least one of the other two is black. And what does that do? Each row below is one step of running this rule, top to bottom, on a lattice that starts with the central cell black and all the others white:

Rule 30: evolution

Cool, and what’s the point? This rule, which by the way is called rule 30 because its binary (00011110) is 30 in decimal, can be used to generate random numbers, besides patterns for Persian rugs…just kidding (I think).

Okay, with that said, what is the density classification problem? The question is: does there exist a single binary CA rule of radius 3 that, given a finite amount of time, settles into an all-white lattice if the initial number of white cells is larger than the number of black ones, or all black otherwise?

If we were just counting, the problem would be trivial, but for a radius 3 CA it is hard, since we have to find one rule among a myriad of possible rules, one that can only see its local configuration and still solve this by the end of a finite run time. Just to give an idea of the size of the search, we are talking about finding one rule among the 2128 2^{128} different CA rules. More precisely, approximately 3.4028×1038 3.4028 \times 10^{38} different rules.

Among those rules there is a famous one, called GKL, which was designed by hand and has an accuracy of approximately 81.5% when each cell has a 50% chance of being black or white. And it was proved that no binary CA can classify 100% of the cases in general (Land and Belew, 1995). Just out of curiosity (this is not a post about CA), see how the GKL starts to “send” information across the time steps:

Density classification

This is a type of CA where you can observe particles, in which the boundaries between domains carry the earlier information and pass it on to the next iterations. But the interesting part is not that question, it is another one: can I use a simple Genetic Algorithm to find rules with similar performance? And this is where my dissertation starts. Normally, the rules in Genetic Algorithms, especially for this problem, are always represented in binary (1 or 0), like the representation of rule 30, or of the GKL (which, because of its size, is normally written as a hexadecimal number). What if we used the idea of a schema for this, where we put some “templates” as a ternary representation (0, 1 or #) in the genotype? Could we find good rules that way too? And what kind of rules would we find? The short version is that yes, it is possible to find rules in the same domain as the GKL with this technique. If anyone is interested, the paper was published by IEEE and can be found officially here.

The starting point and what changed

I then decided to throw my master’s code at the AI and see whether it could improve it. If anyone wants to see the resulting code, feel free: it is public and available here. I chose this particular code for two reasons:

  1. I know the problem. I spent a good amount of time studying this specific problem, so I have an unusual depth of knowledge about it.
  2. It was built with autotools, which today is outdated, and I wanted to migrate it to cmake, so why not use Claude for that.

I made the change in 3 parts:

  1. I asked it to analyze the sequential code and find points of improvement, bugs, etc.
  2. I handed it the paper, asked it to understand the paper and compare it with the code, still the sequential one.
  3. I asked it to make the sequential code and the CUDA code match.

In 2 calendar days, during my free time, I got results that impressed me:

  1. The sequential code now built with CMake, was replicable, had no fixed parameters, and was ready to recreate the paper’s data.
  2. I migrated the same code, same algorithm and same seeds to the CUDA code. That means that if I want to replicate in CUDA exactly the same evolution done by the sequential code, bit by bit, I can now. And the same search parameters keep working.
  3. The CUDA code went from 8 minutes per run to approximately 13 seconds. An improvement of 35x on average!!! And this while using the GPU less, just by improving the parallel code. I considered that an absurd gain (and it even taught me a few CUDA tricks).

This was a 2011 code (more than 15 years old). Talk about legacy.

And the analysis of the results?

Well, just modernizing the code and improving performance in 2 days (which I estimate would take me at least 4 weeks) was not enough for me. I decided to go a bit further and, since the outputs were now completely standardized, why not build tools to analyze the outputs, the performance of the algorithm, classify the CAs found, etc.?

When I say analysis, I mean that I am using plots to try to find answers, over a statistically valid number of runs, to provide answers to questions for helping me understand how the algorithm behaves.

One of them: among the runs, how far is the performance from the GKL? That is, across all results, what is the performance distance to the GKL, considered one of the best rules for this problem? Distance to the GKL rule

Another, even more important to understand: what fraction reaches a given level of success as the computational budget (number of generations, I know that name is bad…) grows? That is, for each budget g, what proportion of the n runs (300 in this case) already has an answer at that level (solid line) and what proportion reached it at some point up to g (dashed line)? The difference between the two is the runs that found a rule at that level and then lost it, and the curve shows whether running more generations is still worth it or whether it is better to restart everything. CA types over time

And the time to build these tools, run the experiment and do the analysis? Around another 3-4 days, still during my free time.

Where the AI needed help

Not everything is roses. I had to talk through and explain quite a lot to Claude, for example:

  • What the ternary representation is and how it is represented in my code.
  • How to replicate what the paper shows.
  • What kind of statistical analysis I wanted.
  • How I wanted to set up the experiments.

Even with all of that explained, the AI started heading somewhere that looked right at first but was not. Since I have command of the subject, I was able to notice this on two fronts:

  1. In the AI chat. This is where I noticed these mistakes the most. Since I already had the custom instructions in place, the AI was more direct about what it was doing and, because of that, it was relatively easy to find where it was wrong and explain how to fix it.
  2. In the code itself. Not that the AI got the code wrong, but some of the things it said in the chat did not correctly represent what was in the code.

A pleasant surprise

And at the end of it all, I got a pleasant surprise: the GA I used was so simple that it exposed one of its weaknesses, how hard it is for it to escape solutions that are locally optimal but not globally optimal. That is a classic GA problem.

A genetic algorithm stuck in a local optimum

That made me wonder what the solutions to this kind of problem are and what the literature says about it. And so a possible next piece of research begins…(to be confirmed, I’m not committing to anything here).

How I use AI in research today

And so I decided to build a development and research workflow together with Claude, which seems quite logical to me:

  1. The research is done by me, the old way: reading papers, studying, looking for references, etc.
  2. I build a library of those papers that both Claude and I can access (if you want a tip for a paper organizer, take a look at Zotero).
  3. I build a quick prototype of what I want to prove, check how well it fits my hypotheses, and repeat until I’m happy.
  4. I use Claude to build tools that help me analyze the outputs and make the code more “polished”. Basically assisted development.
  5. If I’m happy (and it is relevant), before writing the paper I ask Claude to port the code to CUDA, which makes it easier to replicate the data with a larger population. Also assisted, and since I already built a correct oracle in steps #3 and #4, it is simpler to know whether the CUDA code is correct or not.
  6. I write the paper showing what was done and, together with Claude, I look for flaws in it, giving it the library of papers I already researched and the results I found. This part impressed me: it did very well on the test I ran with the original paper, not only pointing out questions that were asked during my presentation at IEEE 2011, but also pointing out possible replicability gaps between the paper and the original code.

Final thoughts

I can say this was one of the most complex uses I have had of Claude so far. I used it as if it were my research colleague, I managed to port legacy code and modernize it, I managed to create a research workflow and, in the end, I proved that yes, Generative AI is here to stay, if you know what you are doing.

And you, have you used Generative AI for things more complex than just generating text or code?