Spaces:
Runtime error
Runtime error
| """ | |
| Floor Plan Module - Represents building layout with rooms, corridors, and connections | |
| """ | |
| import numpy as np | |
| from typing import Dict, List, Tuple, Optional | |
| class Room: | |
| """Represents a room or area in the building""" | |
| def __init__(self, room_id: str, name: str, position: Tuple[float, float], | |
| room_type: str = "room", size: Tuple[float, float] = (5, 5)): | |
| self.room_id = room_id | |
| self.name = name | |
| self.position = position # (x, y) coordinates | |
| self.room_type = room_type # room, corridor, exit, stairwell | |
| self.size = size # (width, height) | |
| self.connected_to = [] # List of connected room IDs | |
| self.has_oxygen_cylinder = False | |
| self.has_fire_extinguisher = False | |
| def add_connection(self, room_id: str, distance: float = None): | |
| """Add connection to another room""" | |
| if distance is None: | |
| distance = 1.0 | |
| self.connected_to.append((room_id, distance)) | |
| def __repr__(self): | |
| return f"Room({self.room_id}, {self.name})" | |
| class FloorPlan: | |
| """Represents the complete building floor plan""" | |
| def __init__(self, floor_name: str = "Ground Floor"): | |
| self.floor_name = floor_name | |
| self.rooms: Dict[str, Room] = {} | |
| self.exits: List[str] = [] | |
| def add_room(self, room: Room): | |
| """Add a room to the floor plan""" | |
| self.rooms[room.room_id] = room | |
| if room.room_type == "exit": | |
| self.exits.append(room.room_id) | |
| def add_connection(self, room_id1: str, room_id2: str, distance: float = 1.0): | |
| """Create bidirectional connection between two rooms""" | |
| if room_id1 in self.rooms and room_id2 in self.rooms: | |
| self.rooms[room_id1].add_connection(room_id2, distance) | |
| self.rooms[room_id2].add_connection(room_id1, distance) | |
| def get_neighbors(self, room_id: str) -> List[Tuple[str, float]]: | |
| """Get all neighboring rooms and their distances""" | |
| if room_id in self.rooms: | |
| return self.rooms[room_id].connected_to | |
| return [] | |
| def get_all_exits(self) -> List[str]: | |
| """Get all exit points""" | |
| return self.exits | |
| def get_room(self, room_id: str) -> Optional[Room]: | |
| """Get room by ID""" | |
| return self.rooms.get(room_id) | |
| def create_sample_floor_plan() -> FloorPlan: | |
| """ | |
| Create an expanded floor plan with multiple rooms and 5 exit routes | |
| Layout: | |
| [R1] - [C1] - [R2] - [C4] - [R5] - [EXIT1] (Route 1: has oxygen cylinder) | |
| | | | | |
| [C2] [C3] [C8] | |
| | | | | |
| [R3] - [C5] - [R4] - [C6] - [R6] - [EXIT2] (Route 2) | |
| | | | |
| [C7] [C9] | |
| | | | |
| [EXIT3] (Route 3) [EXIT4] (Route 4) | |
| [R7] - [C10] - [EXIT5] (Route 5) | |
| """ | |
| plan = FloorPlan("Ground Floor") | |
| # Create rooms with better spacing | |
| rooms = [ | |
| # Main floor rooms | |
| Room("R1", "Room 101", (10, 10), "room", (12, 12)), | |
| Room("R2", "Room 102", (60, 10), "room", (12, 12)), | |
| Room("R3", "Room 103", (10, 50), "room", (12, 12)), | |
| Room("R4", "Room 104", (60, 50), "room", (12, 12)), | |
| Room("R5", "Room 105", (110, 10), "room", (12, 12)), | |
| Room("R6", "Room 106", (110, 50), "room", (12, 12)), | |
| Room("R7", "Room 107", (10, 100), "room", (12, 12)), | |
| # Corridors | |
| Room("C1", "Corridor 1", (35, 10), "corridor", (15, 6)), | |
| Room("C2", "Corridor 2", (10, 30), "corridor", (6, 12)), | |
| Room("C3", "Corridor 3", (60, 30), "corridor", (6, 12)), | |
| Room("C4", "Corridor 4", (85, 10), "corridor", (15, 6)), | |
| Room("C5", "Corridor 5", (35, 50), "corridor", (15, 6)), | |
| Room("C6", "Corridor 6", (85, 50), "corridor", (15, 6)), | |
| Room("C7", "Corridor 7", (10, 75), "corridor", (6, 15)), | |
| Room("C8", "Corridor 8", (110, 30), "corridor", (6, 12)), | |
| Room("C9", "Corridor 9", (110, 75), "corridor", (6, 15)), | |
| Room("C10", "Corridor 10", (35, 100), "corridor", (15, 6)), | |
| # Exits (5 exits now!) | |
| Room("EXIT1", "North Exit", (135, 10), "exit", (8, 8)), | |
| Room("EXIT2", "East Exit", (135, 50), "exit", (8, 8)), | |
| Room("EXIT3", "South Exit", (10, 125), "exit", (8, 8)), | |
| Room("EXIT4", "Southeast Exit", (110, 125), "exit", (8, 8)), | |
| Room("EXIT5", "West Exit", (60, 100), "exit", (8, 8)), | |
| ] | |
| # Add oxygen cylinder to corridor 1 (Route 1) | |
| for room in rooms: | |
| if room.room_id == "C1": | |
| room.has_oxygen_cylinder = True | |
| if room.room_id in ["R2", "R3", "R5"]: | |
| room.has_fire_extinguisher = True | |
| # Add all rooms to floor plan | |
| for room in rooms: | |
| plan.add_room(room) | |
| # Create connections (building the graph) | |
| connections = [ | |
| # Route 1 path (North - via oxygen cylinder area) | |
| ("R1", "C1", 1.0), | |
| ("C1", "R2", 1.0), | |
| ("R2", "C4", 1.0), | |
| ("C4", "R5", 1.0), | |
| ("R5", "EXIT1", 1.0), | |
| # Route 2 path (East) | |
| ("R1", "C2", 1.0), | |
| ("C2", "R3", 1.0), | |
| ("R3", "C5", 1.0), | |
| ("C5", "R4", 1.0), | |
| ("R4", "C6", 1.0), | |
| ("C6", "R6", 1.0), | |
| ("R6", "EXIT2", 1.0), | |
| # Route 3 path (South) | |
| ("R3", "C7", 1.0), | |
| ("C7", "EXIT3", 1.0), | |
| # Route 4 path (Southeast) | |
| ("R6", "C9", 1.0), | |
| ("C9", "EXIT4", 1.0), | |
| # Route 5 path (West) | |
| ("R7", "C10", 1.0), | |
| ("C10", "EXIT5", 1.0), | |
| # Cross connections | |
| ("R2", "C3", 1.0), | |
| ("C3", "R4", 1.0), | |
| ("R5", "C8", 1.0), | |
| ("C8", "R6", 1.0), | |
| ("R3", "R7", 1.0), # Direct connection | |
| ] | |
| for room1, room2, distance in connections: | |
| plan.add_connection(room1, room2, distance) | |
| return plan | |