I built a 17-agent AI swarm on my phone — here's how
Okay, buckle up. This is going to be a bit of a deep dive, and probably a little unconventional. I've been obsessed with the idea of decentralized, multi-agent systems for a while now. The concept of a swarm of AI, each with a specific task, coordinating to achieve a larger goal... it's just cool. And I wanted to see if I could run one, not on a server farm, or even a desktop, but on my phone.
The result? A 17-agent AI swarm built using Python, Termux, and a whole lot of tinkering. It's not going to solve world hunger, but it does demonstrate a surprising amount of processing power can be harnessed in your pocket. Here's how I did it.
The Problem: Mobile AI is Hard (But Getting Easier)
Running complex AI tasks on a phone presents some hurdles. Memory is limited, processing power is constrained (compared to a server), and background execution can be… unreliable. Traditional machine learning frameworks like TensorFlow and PyTorch can be deployed to Android, but often require significant optimization and a willingness to wrestle with native development tools.
I wanted something simpler. I also wanted to avoid constantly being tethered to a Wi-Fi connection. My goal was a fully functional, local swarm.
The Solution: Python, Termux, and a Dash of Cleverness
My tech stack ended up being surprisingly straightforward:
- Python: The backbone. It's relatively lightweight, readable, and has a wealth of libraries for AI and networking.
- Termux: A terminal emulator for Android that gives you a Linux environment on your phone. Crucially, it allows you to install Python and other tools.
- ZeroMQ: A high-performance asynchronous messaging library. This is the key to coordinating the agents. It’s much lighter and simpler than something like RabbitMQ for this use case.
- Simple AI Algorithms: We're not talking GPT-4 here. I opted for simpler algorithms like basic rule-based systems, random walks with biases, and a little bit of basic PID control. The goal was to demonstrate the swarm concept, not build AGI.
The Architecture: Swarm Dynamics 101
The swarm consists of 17 agents. Each agent has a specific 'role' and communicates with the others via ZeroMQ. The roles are broadly categorized as:
- Sensors (5 agents): These agents simulate environmental sensing. They generate random data representing temperature, pressure, light levels, etc.
- Processors (7 agents): These agents receive data from the sensors and apply some simple processing logic. For example, one processor might calculate an average temperature, another might detect anomalies in the data.
- Actuators (5 agents): These agents receive instructions from the processors and ‘act’ accordingly – simulating adjusting settings, triggering alerts, or reporting results.
The communication flow is essentially: Sensors -> Processors -> Actuators.
Think of it like a simplified factory control system. Sensors monitor conditions, processors analyze them, and actuators make adjustments.
Code Snippets: Bringing the Swarm to Life
Let's look at some simplified code. This is just a taste – the full project is more complex, but these snippets illustrate the core concepts.
Sensor Agent (sensor.py):
import zmq
import random
import time
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5555") # Port for sensor data
while True:
temperature = random.uniform(20, 30) # Simulate temperature
pressure = random.uniform(990, 1010) # Simulate pressure
data = f"TEMP:{temperature:.2f},PRES:{pressure:.2f}"
print(f"Sensor: Publishing: {data}")
socket.send_string(data)
time.sleep(1)
Processor Agent (processor.py):
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://localhost:5555")
socket.setsockopt_string(zmq.SUBSCRIBE, "") # Subscribe to all messages
while True:
message = socket.recv_string()
print(f"Processor: Received: {message}")
try:
data = message.split(",")
temp = float(data[0].split(":")[1])
pres = float(data[1].split(":")[1])
if temp > 28:
action = "ALERT: High Temperature"
else:
action = "Normal"
print(f"Processor: Action: {action}")
# Simulate sending to actuator (in real life, different port)
#For simplicity, just print
except:
print("Failed to process")
continue
time.sleep(0.5)
Actuator Agent (actuator.py):
import zmq
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://localhost:5556")
socket.setsockopt_string(zmq.SUBSCRIBE, "")
while True:
message = socket.recv_string()
print(f"Actuator: Received: {message}")
# In a real system, this would trigger an action
print(f"Actuator: Executing: {message}")
Key Points:
-
ZeroMQ Pub/Sub: The
PUBsocket (sensor) publishes messages. TheSUBsockets (processors, actuators) subscribe to receive them. - Port Numbers: Each component listens on different ports. I used 5555 for sensor data and 5556 for actuator commands.
- Simplified Logic: The processors perform basic filtering and decision-making based on the sensor data.
Running the Swarm on Termux
- Install Termux: Get it from F-Droid (https://f-droid.org/en/packages/com.termux/).
-
Install Python:
pkg install python -
Install ZeroMQ:
pkg install zeromq -
Save the Scripts: Copy the Python code into separate files (
sensor.py,processor.py,actuator.py) on your phone within the Termux environment (e.g., in the~/storage/downloadsdirectory). - Run the Agents: Open multiple Termux sessions. In each session, run the
Top comments (0)