MINISTRY OF DIGITAL TECHNOLOGIES OF THE REPUBLIC
OF UZBEKISTAN
Tashkent University of Information Technologies
named after Muhammad Al-Khwarizmi
PRACTICAL TASK - 6
Group: MAD401-1
Student: Mahmudjonov Sardor
Teacher: Karimberdiyev Jaxongir
Tashkent-2025
PRACTICAL TASK – 6
System Integration and Evaluation
Disaster Response and Rescue Agents
Emergency agents: Finding survivors,
10. Mahmudjonov Sardorjon Xojiakbar o'g'li
clearing roads, and managing resources
during crises.
!pip install matplotlib --quiet
import random
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colors
class SharedKnowledge:
def __init__(self):
self.known_survivors = set()
self.blocked_survivor_paths = set()
self.resources_needed = 0
class Environment:
def __init__(self, size=10, num_survivors=5, num_obstacles=8):
self.size = size
self.grid = [['.' for _ in range(size)] for _ in range(size)]
self.agents = []
self.survivors = set()
self.obstacles = set()
self.resources_used = 0
self.place_survivors(num_survivors)
self.place_obstacles(num_obstacles)
def place_survivors(self, num):
while len(self.survivors) < num:
pos = (random.randint(0, self.size-1), random.randint(0,
self.size-1))
if self.grid[pos[0]][pos[1]] == '.':
self.survivors.add(pos)
self.grid[pos[0]][pos[1]] = 'S'
def place_obstacles(self, num):
while len(self.obstacles) < num:
pos = (random.randint(0, self.size-1), random.randint(0,
self.size-1))
if self.grid[pos[0]][pos[1]] == '.':
self.obstacles.add(pos)
self.grid[pos[0]][pos[1]] = 'X'
def add_agent(self, agent):
self.agents.append(agent)
self.grid[agent.pos[0]][agent.pos[1]] = agent.symbol
def update(self):
for agent in self.agents:
agent.act(self)
DIRECTIONS = [(-1,0), (1,0), (0,-1), (0,1)]
class Agent:
def __init__(self, name, pos, symbol):
self.name = name
self.pos = pos
self.symbol = symbol
def move(self, env, direction):
dx, dy = direction
new_pos = (self.pos[0] + dx, self.pos[1] + dy)
if 0 <= new_pos[0] < env.size and 0 <= new_pos[1] < env.size:
if env.grid[new_pos[0]][new_pos[1]] == '.':
env.grid[self.pos[0]][self.pos[1]] = '.'
self.pos = new_pos
env.grid[self.pos[0]][self.pos[1]] = self.symbol
class SurvivorDetectionAgent(Agent):
def __init__(self, name, pos, shared_knowledge):
super().__init__(name, pos, 'D')
self.shared_knowledge = shared_knowledge
self.found = 0
def act(self, env):
for d in DIRECTIONS:
nx, ny = self.pos[0] + d[0], self.pos[1] + d[1]
if (nx, ny) in env.survivors:
env.survivors.remove((nx, ny))
self.shared_knowledge.known_survivors.add((nx, ny))
self.found += 1
print(f"{self.name} found a survivor at {(nx, ny)}!")
elif (nx, ny) in env.obstacles:
bx, by = nx + d[0], ny + d[1]
if 0 <= bx < env.size and 0 <= by < env.size and
env.grid[bx][by] == 'S':
self.shared_knowledge.blocked_survivor_paths.add((nx, ny))
self.move(env, random.choice(DIRECTIONS))
class ClearingAgent(Agent):
def __init__(self, name, pos, shared_knowledge):
super().__init__(name, pos, 'C')
self.shared_knowledge = shared_knowledge
self.cleared = 0
def act(self, env):
# Prioritize clearing known blocked survivor paths
targets = list(self.shared_knowledge.blocked_survivor_paths)
random.shuffle(targets)
for target in targets:
if self._move_toward(env, target):
if target in env.obstacles:
env.obstacles.remove(target)
env.grid[target[0]][target[1]] = '.'
self.shared_knowledge.blocked_survivor_paths.remove(target
)
self.cleared += 1
print(f"{self.name} cleared a path to survivor at
{target}")
return
self.move(env, random.choice(DIRECTIONS))
def _move_toward(self, env, target):
dx = np.sign(target[0] - self.pos[0])
dy = np.sign(target[1] - self.pos[1])
move = (dx, dy) if random.random() > 0.5 else (dx, 0) if dx != 0 else
(0, dy)
self.move(env, move)
return self.pos == target
class ResourceManagerAgent(Agent):
def __init__(self, name, pos, shared_knowledge):
super().__init__(name, pos, 'R')
self.shared_knowledge = shared_knowledge
self.resources_used = 0
def act(self, env):
if self.shared_knowledge.blocked_survivor_paths:
self.shared_knowledge.resources_needed += 1
self.resources_used += 1
env.resources_used += 1
print(f"{self.name} dispatched resource to support clearing
effort.")
self.move(env, random.choice(DIRECTIONS))
def display_grid(env):
cmap = colors.ListedColormap(['white', 'green', 'red', 'blue', 'black',
'gray'])
grid_colors = []
for row in env.grid:
grid_row = []
for cell in row:
if cell == '.':
grid_row.append(0)
elif cell == 'S':
grid_row.append(1)
elif cell == 'X':
grid_row.append(2)
elif cell == 'D':
grid_row.append(3)
elif cell == 'C':
grid_row.append(4)
elif cell == 'R':
grid_row.append(5)
grid_colors.append(grid_row)
plt.figure(figsize=(5, 5))
plt.imshow(grid_colors, cmap=cmap, interpolation='none')
plt.grid(True)
plt.xticks(np.arange(env.size))
plt.yticks(np.arange(env.size))
plt.show()
def run_collaborative_simulation(steps=20):
env = Environment()
knowledge = SharedKnowledge()
d_agent = SurvivorDetectionAgent("Detector", (0, 0), knowledge)
c_agent = ClearingAgent("Clearer", (9, 0), knowledge)
r_agent = ResourceManagerAgent("ResManager", (9, 9), knowledge)
env.add_agent(d_agent)
env.add_agent(c_agent)
env.add_agent(r_agent)
for step in range(steps):
print(f"\n--- Step {step + 1} ---")
env.update()
display_grid(env)
print("\n✅ Simulation Complete")
print("Survivors Found:", d_agent.found)
print("Obstacles Cleared:", c_agent.cleared)
print("Resources Used:", r_agent.resources_used)
run_collaborative_simulation(steps=15)
System Components
Environment: A 10x10 grid representing the disaster zone.
Agents
Survivor Detection Agent (D): Locates and reports survivors.
Clearing Agent (C): Removes obstacles blocking access to survivors.
Resource Manager Agent (R): Provides support (e.g., tools, supplies) when urgent tasks
are detected.
Shared Knowledge: A central communication hub that agents use to share information
and coordinate tasks.
Key Features
Agents operate autonomously but collaborate by sharing known survivor locations and
blocked paths.
The system tracks survivors found, obstacles cleared, and resources used.
The simulation runs over multiple time steps to evaluate agent performance and system
behavior.
Conclusion
This assignment focuses on building and evaluating a collaborative multi-agent
system designed for disaster response and rescue operations. The system integrates
multiple autonomous agents within a shared grid-based environment to simulate realworld crisis management tasks.