DEV Community

Shekhar Chandra
Shekhar Chandra

Posted on

2 2

Visualizing Artificial Ant Problem using pygame

DEAP

I recently started learning about genetic algorithms and programming. I came across several easy to start tools, libraries and tutorials.There are many cool visualizations available online to showcase how the algorithm improves the population generation over generation. Seems like a nice way to grasp the concept and build intuition.

Since I mainly code in Python, I ended up using DEAP for prototyping and learning. Documentations are very clear and with lots of examples to study from.

Artificial Ant Problem

One particular example was, Artificial Ant Problem. The goal is to evolve an individual that can control an ant and eat as many food items from the environment as possible given a limited amount and types of moves.

The code runs seamlessly and you could see the output in the console that generation over generation the population is actually able to increase the amount of food items eaten.

Code Changes

That’s all well and good, but I wanted to visualise the solution to see for myself and get a better understanding. First step was to actually read the best individual attributes hall of fame.

Original Base Code for Reference
I captured the best individual after running the program and recompiled the attribute for that individual.

from deap import gp
hof_promitive_tree = gp.PrimitiveTree(hof[0])
routine = gp.compile(hof_promitive_tree , pset)
Enter fullscreen mode Exit fullscreen mode

Modified the AntSimulator class a little bit to record every move for later use.

class AntSimulator(object):
def __init__(self, max_moves):
...
self.recorded_moves = []
        ...
    def record_move(self):
        self.recorded_moves.append(self.position)
Enter fullscreen mode Exit fullscreen mode

Next step was to run the routine again on modified AntSimulator and get the recorder moves for simulation

ant.run(routine)
# contains the initial food item and ant location
grid = ant.matrix
# recorder moves of best individual
moves = ant.recorded_moves
Enter fullscreen mode Exit fullscreen mode

Pygame

Given this much information, initialisation involved placing ant and food items on the pygame screen. The main loop consists of playing each move one by one and removing food items once eaten. Complete modified code can be found here

And here is the end result. Shows the individual controlling the first 600 moves of our artificial ant.

one generation

Ten Generations

Forty Generations

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay