<RETURN_TO_BASE

Build an Autonomous Multi-Agent Logistics System

A deep dive into creating a logistics simulation with autonomous delivery trucks.

The Concept of Autonomous Logistics

In this tutorial, we build an advanced, fully autonomous logistics simulation in which multiple smart delivery trucks operate within a dynamic city-wide road network. Each truck acts as an agent capable of bidding on delivery orders, planning optimal routes, managing battery levels, seeking charging stations, and maximizing profit through self-interested decision-making.

Building the Simulation

Through each code snippet, we explore how agentic behaviors emerge from simple rules, how competition shapes order allocation, and how a graph-based world enables realistic movement, routing, and resource constraints.

Setting Up the Environment

To kickstart the simulation:

import networkx as nx  
import matplotlib.pyplot as plt  
import random  
import time  
from IPython.display import clear_output  
from dataclasses import dataclass, field  
from typing import List, Dict, Optional  
 
NUM_NODES = 30  
CONNECTION_RADIUS = 0.25  
NUM_AGENTS = 5  
STARTING_BALANCE = 1000  
FUEL_PRICE = 2.0  
PAYOUT_MULTIPLIER = 5.0  
BATTERY_CAPACITY = 100  
CRITICAL_BATTERY = 25  
 
@dataclass  
class Order:  
    id: str  
    target_node: int  
    weight_kg: int  
    payout: float  
    status: str = "pending"  

We set up all the core building blocks of the simulation, including global parameters and basic data structures.

Defining the AgenticTruck Class

Next, we define the AgenticTruck class to initialize key agent attributes.

class AgenticTruck:
    def __init__(self, agent_id, start_node, graph, capacity=100):
        self.id = agent_id
        self.current_node = start_node
        self.graph = graph
        self.battery = BATTERY_CAPACITY
        self.balance = STARTING_BALANCE
        self.capacity = capacity
        self.state = "IDLE"
        self.path: List[int] = []
        self.current_order: Optional[Order] = None
        self.target_node: int = start_node

This setup lays the foundation for agent behaviors to evolve.

Advanced Decision-Making Logic

To make our agents smarter, we implement decision-making logic to evaluate orders, calculate paths, and manage charging. This makes sure agents make economically viable choices.

def calculate_bid(self, order):
    if order.weight_kg > self.capacity:
        return float('inf')
    if self.state != "IDLE" or self.battery < CRITICAL_BATTERY:
        return float('inf')
    dist_to_target, _ = self.get_path_cost(self.current_node, order.target_node)
    fuel_cost = dist_to_target * FUEL_PRICE
    expected_profit = order.payout - fuel_cost
    if expected_profit < 10:
        return float('inf')
    return dist_to_target

Each truck calculates whether it can afford a job based on its current conditions.

Simulation Execution Loop

class Simulation:
    def __init__(self):
        self.setup_graph()
        self.setup_agents()
        self.orders = []
        self.order_count = 0

We create the simulated world, generate the graph-based city, and enable agents to compete for assignments. This is where emergent behavior occurs as agents interact dynamically.

Visualizing the Results

def visualize(self, step_num):
    clear_output(wait=True)
    plt.figure(figsize=(10, 8))
    pos = nx.get_node_attributes(self.G, 'pos')
    node_colors = [self.G.nodes[n]['color'] for n in self.G.nodes()]
    nx.draw(self.G, pos, node_color=node_colors, with_labels=True)

We visualize agent movements, orders, and the network in real time.

Observations

By running the simulation, we see how agents negotiate workloads, compete for orders, and manage resources like battery and fuel efficiently. Each component—from graph generation to routing—contributes to a functioning logistics system.

Explore Further

Check out the FULL CODES here for a complete implementation, and join our community for more insights into logistics and AI solutions.

🇷🇺

Сменить язык

Читать эту статью на русском

Переключить на Русский