{"id":193,"date":"2025-12-25T11:50:15","date_gmt":"2025-12-25T03:50:15","guid":{"rendered":"https:\/\/connectword.dpdns.org\/?p=193"},"modified":"2025-12-25T11:50:15","modified_gmt":"2025-12-25T03:50:15","slug":"a-coding-guide-to-build-an-autonomous-multi-agent-logistics-system-with-route-planning-dynamic-auctions-and-real-time-visualization-using-graph-based-simulation","status":"publish","type":"post","link":"https:\/\/connectword.dpdns.org\/?p=193","title":{"rendered":"A Coding Guide to Build an Autonomous Multi-Agent Logistics System with Route Planning, Dynamic Auctions, and Real-Time Visualization Using Graph-Based Simulation"},"content":{"rendered":"<p>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. We design the system so that each truck behaves 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. 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. Check out the\u00a0<strong><a href=\"https:\/\/github.com\/Marktechpost\/AI-Tutorial-Codes-Included\/blob\/main\/Agentic%20AI%20Codes\/agentic_logistics_swarm_simulation_Marktechpost.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">FULL CODES here<\/a><\/strong>.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">import networkx as nx\nimport matplotlib.pyplot as plt\nimport random\nimport time\nfrom IPython.display import clear_output\nfrom dataclasses import dataclass, field\nfrom typing import List, Dict, Optional\n\n\nNUM_NODES = 30\nCONNECTION_RADIUS = 0.25\nNUM_AGENTS = 5\nSTARTING_BALANCE = 1000\nFUEL_PRICE = 2.0\nPAYOUT_MULTIPLIER = 5.0\nBATTERY_CAPACITY = 100\nCRITICAL_BATTERY = 25\n\n\n@dataclass\nclass Order:\n   id: str\n   target_node: int\n   weight_kg: int\n   payout: float\n   status: str = \"pending\"\n\n\nclass AgenticTruck:\n   def __init__(self, agent_id, start_node, graph, capacity=100):\n       self.id = agent_id\n       self.current_node = start_node\n       self.graph = graph\n       self.battery = BATTERY_CAPACITY\n       self.balance = STARTING_BALANCE\n       self.capacity = capacity\n       self.state = \"IDLE\"\n       self.path: List[int] = []\n       self.current_order: Optional[Order] = None\n       self.target_node: int = start_node<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We set up all the core building blocks of the simulation, including imports, global parameters, and the basic data structures. We also define the AgenticTruck class and initialize key attributes, including position, battery, balance, and operating state. We lay the foundation for all agent behaviors to evolve. Check out the\u00a0<strong><a href=\"https:\/\/github.com\/Marktechpost\/AI-Tutorial-Codes-Included\/blob\/main\/Agentic%20AI%20Codes\/agentic_logistics_swarm_simulation_Marktechpost.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">FULL CODES here<\/a><\/strong>.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\"> def get_path_cost(self, start, end):\n       try:\n           length = nx.shortest_path_length(self.graph, start, end, weight='weight')\n           path = nx.shortest_path(self.graph, start, end, weight='weight')\n           return length, path\n       except nx.NetworkXNoPath:\n           return float('inf'), []\n\n\n   def find_nearest_charger(self):\n       chargers = [n for n, attr in self.graph.nodes(data=True) if attr.get('type') == 'charger']\n       best_charger = None\n       min_dist = float('inf')\n       best_path = []\n       for charger in chargers:\n           dist, path = self.get_path_cost(self.current_node, charger)\n           if dist &lt; min_dist:\n               min_dist = dist\n               best_charger = charger\n               best_path = path\n       return best_charger, best_path\n\n\n   def calculate_bid(self, order):\n       if order.weight_kg &gt; self.capacity:\n           return float('inf')\n       if self.state != \"IDLE\" or self.battery &lt; CRITICAL_BATTERY:\n           return float('inf')\n       dist_to_target, _ = self.get_path_cost(self.current_node, order.target_node)\n       fuel_cost = dist_to_target * FUEL_PRICE\n       expected_profit = order.payout - fuel_cost\n       if expected_profit &lt; 10:\n           return float('inf')\n       return dist_to_target\n\n\n   def assign_order(self, order):\n       self.current_order = order\n       self.state = \"MOVING\"\n       self.target_node = order.target_node\n       _, self.path = self.get_path_cost(self.current_node, self.target_node)\n       if self.path: self.path.pop(0)\n\n\n   def go_charge(self):\n       charger_node, path = self.find_nearest_charger()\n       if charger_node is not None:\n           self.state = \"TO_CHARGER\"\n           self.target_node = charger_node\n           self.path = path\n           if self.path: self.path.pop(0)<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We implement advanced decision-making logic for the trucks. We calculate shortest paths, identify nearby charging stations, and evaluate whether an order is profitable and feasible. We also prepare the truck to accept assignments or proactively seek charging when needed. Check out the\u00a0<strong><a href=\"https:\/\/github.com\/Marktechpost\/AI-Tutorial-Codes-Included\/blob\/main\/Agentic%20AI%20Codes\/agentic_logistics_swarm_simulation_Marktechpost.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">FULL CODES here<\/a><\/strong>.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\"> def step(self):\n       if self.state == \"IDLE\" and self.battery &lt; CRITICAL_BATTERY:\n           self.go_charge()\n\n\n       if self.state == \"CHARGING\":\n           self.battery += 10\n           self.balance -= 5\n           if self.battery &gt;= 100:\n               self.battery = 100\n               self.state = \"IDLE\"\n           return\n\n\n       if self.path:\n           next_node = self.path[0]\n           edge_data = self.graph.get_edge_data(self.current_node, next_node)\n           distance = edge_data['weight']\n           self.current_node = next_node\n           self.path.pop(0)\n           self.battery -= (distance * 2)\n           self.balance -= (distance * FUEL_PRICE)\n\n\n           if not self.path:\n               if self.state == \"MOVING\":\n                   self.balance += self.current_order.payout\n                   self.current_order.status = \"completed\"\n                   self.current_order = None\n                   self.state = \"IDLE\"\n               elif self.state == \"TO_CHARGER\":\n                   self.state = \"CHARGING\"<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We manage the step-by-step actions of each truck as the simulation runs. We handle battery recharging, financial impacts of movement, fuel consumption, and order completion. We ensure that agents transition smoothly between states, such as moving, charging, and idling. Check out the\u00a0<strong><a href=\"https:\/\/github.com\/Marktechpost\/AI-Tutorial-Codes-Included\/blob\/main\/Agentic%20AI%20Codes\/agentic_logistics_swarm_simulation_Marktechpost.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">FULL CODES here<\/a><\/strong>.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">class Simulation:\n   def __init__(self):\n       self.setup_graph()\n       self.setup_agents()\n       self.orders = []\n       self.order_count = 0\n\n\n   def setup_graph(self):\n       self.G = nx.random_geometric_graph(NUM_NODES, CONNECTION_RADIUS)\n       for (u, v) in self.G.edges():\n           self.G.edges[u, v]['weight'] = random.uniform(1.0, 3.0)\n       for i in self.G.nodes():\n           r = random.random()\n           if r &lt; 0.15:\n               self.G.nodes[i]['type'] = 'charger'\n               self.G.nodes[i]['color'] = 'red'\n           else:\n               self.G.nodes[i]['type'] = 'house'\n               self.G.nodes[i]['color'] = '#A0CBE2'\n\n\n   def setup_agents(self):\n       self.agents = []\n       for i in range(NUM_AGENTS):\n           start_node = random.randint(0, NUM_NODES-1)\n           cap = random.choice([50, 100, 200])\n           self.agents.append(AgenticTruck(i, start_node, self.G, capacity=cap))\n\n\n   def generate_order(self):\n       target = random.randint(0, NUM_NODES-1)\n       weight = random.randint(10, 120)\n       payout = random.randint(50, 200)\n       order = Order(id=f\"ORD-{self.order_count}\", target_node=target, weight_kg=weight, payout=payout)\n       self.orders.append(order)\n       self.order_count += 1\n       return order\n\n\n   def run_market(self):\n       for order in self.orders:\n           if order.status == \"pending\":\n               bids = {agent: agent.calculate_bid(order) for agent in self.agents}\n               valid_bids = {k: v for k, v in bids.items() if v != float('inf')}\n               if valid_bids:\n                   winner = min(valid_bids, key=valid_bids.get)\n                   winner.assign_order(order)\n                   order.status = \"assigned\"<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We create the simulated world and orchestrate agent interactions. We generate the graph-based city, spawn trucks with varying capacities, and produce new delivery orders. We also implement a simple market where agents bid for tasks based on profitability and distance. Check out the\u00a0<strong><a href=\"https:\/\/github.com\/Marktechpost\/AI-Tutorial-Codes-Included\/blob\/main\/Agentic%20AI%20Codes\/agentic_logistics_swarm_simulation_Marktechpost.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">FULL CODES here<\/a><\/strong>.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">  def step(self):\n       if random.random() &lt; 0.3:\n           self.generate_order()\n       self.run_market()\n       for agent in self.agents:\n           agent.step()\n\n\n   def visualize(self, step_num):\n       clear_output(wait=True)\n       plt.figure(figsize=(10, 8))\n       pos = nx.get_node_attributes(self.G, 'pos')\n       node_colors = [self.G.nodes[n]['color'] for n in self.G.nodes()]\n       nx.draw(self.G, pos, node_color=node_colors, with_labels=True, node_size=300, edge_color='gray', alpha=0.6)\n\n\n       for agent in self.agents:\n           x, y = pos[agent.current_node]\n           jitter_x = x + random.uniform(-0.02, 0.02)\n           jitter_y = y + random.uniform(-0.02, 0.02)\n           color = 'green' if agent.state == \"IDLE\" else ('orange' if agent.state == \"MOVING\" else 'red')\n           plt.plot(jitter_x, jitter_y, marker='s', markersize=12, color=color, markeredgecolor='black')\n           plt.text(jitter_x, jitter_y+0.03, f\"A{agent.id}n${int(agent.balance)}n{int(agent.battery)}%\",\n                    fontsize=8, ha='center', fontweight='bold', bbox=dict(facecolor='white', alpha=0.7, pad=1))\n\n\n       for order in self.orders:\n           if order.status in [\"assigned\", \"pending\"]:\n               ox, oy = pos[order.target_node]\n               plt.plot(ox, oy, marker='*', markersize=15, color='gold', markeredgecolor='black')\n\n\n       plt.title(f\"Graph-Based Logistics Swarm | Step: {step_num}nRed Nodes = Chargers | Gold Stars = Orders\", fontsize=14)\n       plt.show()\n\n\n\n\nprint(\"Initializing Advanced Simulation...\")\nsim = Simulation()\n\n\nfor t in range(60):\n   sim.step()\n   sim.visualize(t)\n   time.sleep(0.5)\n\n\nprint(\"Simulation Finished.\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We step through the full simulation loop and visualize the logistics swarm in real time. We update agent states, draw the network, display active orders, and animate each truck\u2019s movement. By running this loop, we observe the emergent coordination and competition that define our multi-agent logistics ecosystem.<\/p>\n<p>In conclusion, we saw how the individual components, graph generation, autonomous routing, battery management, auctions, and visualization, come together to form a living, evolving system of agentic trucks. We watch as agents negotiate workloads, compete for profitable opportunities, and respond to environmental pressures such as distance, fuel costs, and charging needs. By running the simulation, we observe emergent dynamics that mirror real-world fleet behavior, providing a powerful sandbox for experimenting with logistics intelligence.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p>Check out the\u00a0<strong><a href=\"https:\/\/github.com\/Marktechpost\/AI-Tutorial-Codes-Included\/blob\/main\/Agentic%20AI%20Codes\/agentic_logistics_swarm_simulation_Marktechpost.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">FULL CODES here<\/a><\/strong>.\u00a0Also,\u00a0feel free to follow us on\u00a0<strong><a href=\"https:\/\/x.com\/intent\/follow?screen_name=marktechpost\" target=\"_blank\" rel=\"noreferrer noopener\"><mark>Twitter<\/mark><\/a><\/strong>\u00a0and don\u2019t forget to join our\u00a0<strong><a href=\"https:\/\/www.reddit.com\/r\/machinelearningnews\/\" target=\"_blank\" rel=\"noreferrer noopener\">100k+ ML SubReddit<\/a><\/strong>\u00a0and Subscribe to\u00a0<strong><a href=\"https:\/\/www.aidevsignals.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">our Newsletter<\/a><\/strong>. Wait! are you on telegram?\u00a0<strong><a href=\"https:\/\/t.me\/machinelearningresearchnews\" target=\"_blank\" rel=\"noreferrer noopener\">now you can join us on telegram as well.<\/a><\/strong><\/p>\n<p>The post <a href=\"https:\/\/www.marktechpost.com\/2025\/12\/24\/a-coding-guide-to-build-an-autonomous-multi-agent-logistics-system-with-route-planning-dynamic-auctions-and-real-time-visualization-using-graph-based-simulation\/\">A Coding Guide to Build an Autonomous Multi-Agent Logistics System with Route Planning, Dynamic Auctions, and Real-Time Visualization Using Graph-Based Simulation<\/a> appeared first on <a href=\"https:\/\/www.marktechpost.com\/\">MarkTechPost<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we build an &hellip;<\/p>\n","protected":false},"author":1,"featured_media":29,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=193"}],"version-history":[{"count":0,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/193\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/media\/29"}],"wp:attachment":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}