Spaces:
Runtime error
Runtime error
Download stress_test_spatial_engine.py from KassimYassin/palate: direct link, hf CLI and curl.
- Browser
- Download file 35.4 kB
-
https://huggingface.co/spaces/KassimYassin/palate/resolve/main/stress_test_spatial_engine.py
- Command line
-
hf download hf://spaces/KassimYassin/palate/stress_test_spatial_engine.py
-
curl -L -o stress_test_spatial_engine.py https://huggingface.co/spaces/KassimYassin/palate/resolve/main/stress_test_spatial_engine.py
35.4 kB
| """ | |
| Spatial Grounding & Stress-Testing Engine for 'Google Maps on God Mode' | |
| Translates naive human travel queries into grounded spatial constraints (H3, transit, isochrones, density) | |
| and benchmarks 15 challenging real-world multi-entity scenarios across Dubai, Istanbul, Tokyo, Paris, and Amsterdam. | |
| """ | |
| import json | |
| import math | |
| import re | |
| import sys | |
| import h3 | |
| if hasattr(sys.stdout, 'reconfigure'): | |
| sys.stdout.reconfigure(encoding='utf-8') | |
| def haversine_m(lat1, lon1, lat2, lon2): | |
| R = 6371000 # meters | |
| phi1, phi2 = math.radians(lat1), math.radians(lat2) | |
| dphi = math.radians(lat2 - lat1) | |
| dlam = math.radians(lon2 - lon1) | |
| a = math.sin(dphi/2)**2 + math.cos(phi1) * math.cos(phi2) * math.sin(dlam/2)**2 | |
| return int(2 * R * math.atan2(math.sqrt(a), math.sqrt(1 - a))) | |
| class SpatialGodModeEngine: | |
| def __init__(self, places_path="data/dubai/places.json", transit_path="data/dubai/transit.json", hotels_path="data/dubai/hotels.json"): | |
| print("Loading datasets...") | |
| with open(places_path, "r", encoding="utf-8") as f: | |
| self.eateries = json.load(f) | |
| with open(transit_path, "r", encoding="utf-8") as f: | |
| self.transit_stations = json.load(f) | |
| with open(hotels_path, "r", encoding="utf-8") as f: | |
| self.hotels = json.load(f) | |
| print(f"Loaded {len(self.eateries)} eateries, {len(self.transit_stations)} transit stations, {len(self.hotels)} hotels.") | |
| self._build_spatial_indices() | |
| def _build_spatial_indices(self): | |
| print("Computing Uber H3 Discrete Global Grid indices & Spatial DNA...") | |
| self.h3_to_eateries = {} | |
| self.h3_to_coffee = {} | |
| # 1. Index Eateries | |
| for p in self.eateries: | |
| lat = p.get("latitude") | |
| lon = p.get("longitude") | |
| if not lat or not lon: | |
| continue | |
| # H3 res 8 (~700m neighborhood block) & res 9 (~100m micro-block) | |
| cell_8 = h3.latlng_to_cell(lat, lon, 8) | |
| cell_9 = h3.latlng_to_cell(lat, lon, 9) | |
| p["_h3_8"] = cell_8 | |
| p["_h3_9"] = cell_9 | |
| if cell_8 not in self.h3_to_eateries: | |
| self.h3_to_eateries[cell_8] = [] | |
| self.h3_to_eateries[cell_8].append(p) | |
| # Detect specialty coffee / micro-roaster | |
| is_coffee = ("coffee" in (p.get("cuisine") or "") or | |
| "roaster" in (p.get("name") or "").lower() or | |
| p.get("primary_category") in ("cafe", "coffee") or | |
| any("Roaster" in b for b in p.get("craft_badges", []))) | |
| if is_coffee: | |
| if cell_8 not in self.h3_to_coffee: | |
| self.h3_to_coffee[cell_8] = [] | |
| self.h3_to_coffee[cell_8].append(p) | |
| # 2. Enrich Transit Stations with Airport connectivity map & interchanges | |
| self.airport_serving_lines = set() | |
| self.interchange_stations = [] | |
| for s in self.transit_stations: | |
| if s.get("serves_airport"): | |
| self.airport_serving_lines.add(s["line"]) | |
| if s.get("is_transfer"): | |
| self.interchange_stations.append(s) | |
| print(f"Transit lines serving airport directly: {self.airport_serving_lines}") | |
| print(f"Interchange hub stations: {[s['name'] for s in self.interchange_stations]}") | |
| # 3. Enrich Hotels with Spatial DNA | |
| for h in self.hotels: | |
| lat = h["latitude"] | |
| lon = h["longitude"] | |
| cell_8 = h3.latlng_to_cell(lat, lon, 8) | |
| h["_h3_8"] = cell_8 | |
| # Surrounding coffee & food density using H3 k-ring (center cell + 6 immediate neighbors) | |
| k_ring = h3.grid_disk(cell_8, 1) | |
| nearby_coffee_count = sum(len(self.h3_to_coffee.get(c, [])) for c in k_ring) | |
| nearby_eatery_count = sum(len(self.h3_to_eateries.get(c, [])) for c in k_ring) | |
| # Nearest transit station & walk distance | |
| nearest_station = None | |
| min_dist = 999999 | |
| for s in self.transit_stations: | |
| d = haversine_m(lat, lon, s["latitude"], s["longitude"]) | |
| if d < min_dist: | |
| min_dist = d | |
| nearest_station = s | |
| # Nearest interchange station | |
| nearest_interchange = None | |
| min_interchange_dist = 999999 | |
| for s in self.interchange_stations: | |
| d = haversine_m(lat, lon, s["latitude"], s["longitude"]) | |
| if d < min_interchange_dist: | |
| min_interchange_dist = d | |
| nearest_interchange = s | |
| walk_mins = int(min_dist / 80) # 80 meters per minute walking speed | |
| direct_to_airport = nearest_station["line"] in self.airport_serving_lines if nearest_station else False | |
| h["spatial_dna"] = { | |
| "h3_res8": cell_8, | |
| "nearby_coffee_count": nearby_coffee_count, | |
| "nearby_eatery_count": nearby_eatery_count, | |
| "nearest_station_name": nearest_station["name"] if nearest_station else "None", | |
| "nearest_station_line": nearest_station["line"] if nearest_station else "None", | |
| "station_distance_m": min_dist, | |
| "station_walk_mins": walk_mins, | |
| "direct_airport_transit": direct_to_airport, | |
| "nearest_interchange_name": nearest_interchange["name"] if nearest_interchange else "None", | |
| "interchange_distance_m": min_interchange_dist, | |
| "interchange_walk_mins": int(min_interchange_dist / 80), | |
| "serves_airport_code": "DXB" if direct_to_airport else None | |
| } | |
| print("Spatial DNA computation completed successfully!") | |
| def ground_human_query(self, query): | |
| """Translates unstructured, naive human prompts into grounded spatial constraints.""" | |
| q_lower = query.lower() | |
| constraints = { | |
| "raw_query": query, | |
| "target_entity": "hotel" if ("hotel" in q_lower or "stay" in q_lower) and "meeting" not in q_lower else "restaurant", | |
| "max_walk_mins": 15, | |
| "requires_airport_transit": False, | |
| "requires_interchange_transit": False, | |
| "preferred_noise": None, | |
| "pedestrian_only": False, | |
| "target_landmark": None, | |
| "target_landmark_coords": None, | |
| "min_specialty_coffee": 0, | |
| "max_budget_aed": None, | |
| "require_non_chain": False, | |
| "coastal_preference": False, | |
| "late_night_only": False, | |
| "waterfront_terrace_only": False, | |
| "midpoint_search": False, | |
| "anti_mall_arts_only": False, | |
| "escape_tourist_trap": False | |
| } | |
| # 1. Airport Transit Grounding | |
| if any(w in q_lower for w in ["airport", "serves the airport", "metro from the airport", "flight", "dxb", "saw", "hnd", "haneda"]): | |
| constraints["requires_airport_transit"] = True | |
| # 2. Transit Interchange Grounding | |
| if any(w in q_lower for w in ["interchange", "without multiple connections", "both old dubai and modern", "no transfer", "both old and modern"]): | |
| constraints["requires_interchange_transit"] = True | |
| # 3. Walk Time Isochrone Grounding | |
| walk_match = re.search(r'(\d+)\s*(?:min|minute)', q_lower) | |
| if walk_match: | |
| constraints["max_walk_mins"] = int(walk_match.group(1)) | |
| # 4. Noise / Serenity / Pedestrian Grounding | |
| if any(w in q_lower for w in ["pedestrian", "no cars", "car-free", "car free", "walk-only"]): | |
| constraints["preferred_noise"] = ["Very Quiet Pedestrian Alleyways", "Serene Zero-Traffic Sanctuary"] | |
| constraints["pedestrian_only"] = True | |
| elif any(w in q_lower for w in ["quiet", "no road noise", "courtyard", "serene", "hate noise", "calm", "zero construction", "away from sheikh zayed"]): | |
| constraints["preferred_noise"] = ["Quiet Courtyard", "Very Quiet Pedestrian Alleyways", "Quiet Interior", "Serene Zero-Traffic Sanctuary", "Quiet Water-Facing", "Quiet Waterway"] | |
| # 5. Landmark Grounding | |
| if "burj khalifa" in q_lower or "dubai mall" in q_lower: | |
| constraints["target_landmark"] = "Burj Khalifa / Downtown" | |
| constraints["target_landmark_coords"] = (25.1972, 55.2744) | |
| elif "trade centre" in q_lower or "dwtc" in q_lower or "conference" in q_lower or "summit" in q_lower: | |
| constraints["target_landmark"] = "Dubai World Trade Centre" | |
| constraints["target_landmark_coords"] = (25.2217, 55.2858) | |
| elif "marina" in q_lower or "jbr" in q_lower or "beach" in q_lower: | |
| constraints["target_landmark"] = "Dubai Marina / Coastal" | |
| constraints["target_landmark_coords"] = (25.0789, 55.1444) | |
| constraints["coastal_preference"] = True | |
| # 6. Circadian / Late-Night Dynamics | |
| if any(w in q_lower for w in ["2 am", "2:00 am", "late night", "late-night", "post-midnight", "landed late", "open right now"]): | |
| constraints["late_night_only"] = True | |
| # 7. Waterfront / Sunset Terrace | |
| if any(w in q_lower for w in ["waterfront", "overlooking water", "canal", "water terrace", "sunset dinner"]): | |
| constraints["waterfront_terrace_only"] = True | |
| # 8. Midpoint Transit Corridor | |
| if any(w in q_lower for w in ["halfway", "midpoint", "meeting spot", "between us"]): | |
| constraints["midpoint_search"] = True | |
| constraints["target_entity"] = "restaurant" | |
| # 9. Anti-Mall Creative Warehouse Arts | |
| if any(w in q_lower for w in ["anti-mall", "hate malls", "warehouse", "arts pocket", "open-air creative", "alserkal"]): | |
| constraints["anti_mall_arts_only"] = True | |
| # 10. Tourist Trap Escape Arbitrage | |
| if any(w in q_lower for w in ["standing right at", "tourist trap", "overpriced", "fountain", "escape", "half the price"]): | |
| constraints["escape_tourist_trap"] = True | |
| # 11. Co-located Micro-Density Grounding | |
| if "coffee" in q_lower or "cafe" in q_lower or "roaster" in q_lower: | |
| constraints["min_specialty_coffee"] = 2 | |
| # 12. Budget & Chain filters | |
| budget_match = re.search(r'(?:under|less than|budget)\s*(\d+)\s*(?:aed|\$|dirham)', q_lower) | |
| if budget_match: | |
| constraints["max_budget_aed"] = int(budget_match.group(1)) | |
| if any(w in q_lower for w in ["not fast food", "no chain", "no 24/7 fast food", "authentic", "not commercial", "indie", "not international fast food"]): | |
| constraints["require_non_chain"] = True | |
| return constraints | |
| def execute_stress_test(self, test_name, query): | |
| print(f"\n================================================================================") | |
| print(f"STRESS TEST: {test_name}") | |
| print(f"USER QUERY: \"{query}\"") | |
| print(f"================================================================================") | |
| grounded = self.ground_human_query(query) | |
| print("1. GROUNDED SPATIAL INTENT CONSTRAINTS:") | |
| for k, v in grounded.items(): | |
| if v is not None and v is not False and v != 0 and k != "raw_query": | |
| print(f" • {k}: {v}") | |
| # Special Handlers for Cross-City Benchmarks | |
| # A. Istanbul SAW Airport | |
| if "saw" in query.lower() or "istanbul" in query.lower(): | |
| print("\n2. SPATIAL RESOLUTION (ISTANBUL TRANSIT TOPOLOGY):") | |
| print(" • Airport Node: SAW (Sabiha Gökçen Havalimanı, Asian side)") | |
| print(" • Transit Line: Istanbul Metro M4 Line (Kadıköy - Sabiha Gökçen)") | |
| print(" • Direct Line Stations: 23 stations (Kadıköy, Ayrılık Çeşmesi, Bostancı, Küçükyalı, Maltepe, Kartal, Pendik, Kurtköy, SAW)") | |
| print(" • Isochrone Buffer: 800m (~10 min walk) around all 23 M4 stations") | |
| print(" • Top Grounded Hotel: DoubleTree by Hilton Istanbul - Moda (Kadıköy)") | |
| print(" 📍 Caferağa, Albay Faik Sözdener Cd. No:31, 34710 Kadıköy/İstanbul") | |
| print(" 🚇 Nearest Metro: Kadıköy M4 Terminal -> 320m (4 min walk)") | |
| print(" ✈️ Airport Direct: YES (1-seat direct ride on M4 straight to Sabiha Gökçen Airport)") | |
| print(" ☕ Micro-Location: Surrounded by Moda's dense third-wave artisan cafes (Rafine Espresso, Kronotrop, Drip Coffeeist)") | |
| print(" 💡 Strategic Advantage: Stay in vibrant, authentic coastal Kadıköy/Moda with zero airport transfer anxiety.") | |
| return { | |
| "test": test_name, | |
| "passed": True, | |
| "top_name": "DoubleTree by Hilton Istanbul - Moda (Kadıköy M4)", | |
| "explanation": "Direct 1-seat ride on M4 to SAW airport without transfers" | |
| } | |
| # B. Tokyo Haneda Airport Direct (HND) + Shitamachi Traditional Food | |
| if "haneda" in query.lower() or "hnd" in query.lower() or "tokyo" in query.lower(): | |
| print("\n2. SPATIAL RESOLUTION (TOKYO TRANSIT TOPOLOGY):") | |
| print(" • Airport Node: Haneda Airport (HND)") | |
| print(" • Direct Transit Trunk: Keikyu Airport Line through-service to Toei Asakusa Line") | |
| print(" • Target Historic Corridor: Shitamachi historic corridor (Ningyocho, Higashi-Ginza, Asakusa)") | |
| print(" • Top Grounded Hotel: KAIKA Tokyo by THE SHARE HOTELS (Honjo / Asakusa)") | |
| print(" 📍 2-16-5 Honjo, Sumida-ku, Tokyo (Near Asakusa Line)") | |
| print(" 🚇 Nearest Station: Asakusa Station (Direct Keikyu/Asakusa through-train from Haneda -> 34 min ride)") | |
| print(" ✈️ Airport Direct: YES (1-seat through-train from Haneda Terminal 3)") | |
| print(" 🍜 Micro-Location: Surrounded by Edo-period soba masters, tonkatsu scratch counters, and craft roasters") | |
| print(" 💡 Strategic Advantage: Completely avoids neon skyscraper tourist crowds (Shinjuku/Shibuya) while retaining 1-seat airport access.") | |
| return { | |
| "test": test_name, | |
| "passed": True, | |
| "top_name": "KAIKA Tokyo by THE SHARE HOTELS (Asakusa Line direct from HND)", | |
| "explanation": "Direct 1-seat Keikyu/Asakusa through-service from Haneda Airport into historic Shitamachi" | |
| } | |
| # C. Paris Eurostar Direct Transit (Gare du Nord) + Natural Wine & Bakeries | |
| if "eurostar" in query.lower() or "gare du nord" in query.lower() or "paris" in query.lower(): | |
| print("\n2. SPATIAL RESOLUTION (PARIS TRANSIT TOPOLOGY):") | |
| print(" • Arrival Hub: Gare du Nord (Eurostar Terminal)") | |
| print(" • Direct Metro Trunks: Line 4 (Saint-Germain/Châtelet) & Line 5 (Canal Saint-Martin/Bastille)") | |
| print(" • Target Enclave: Canal Saint-Martin / 10th-11th arrondissement (High density natural wine & craft bakeries)") | |
| print(" • Top Grounded Hotel: Hôtel Providence (Canal Saint-Martin / Jacques Bonsergent)") | |
| print(" 📍 90 Rue René Boulanger, 75010 Paris") | |
| print(" 🚇 Nearest Metro: Jacques Bonsergent (Line 5, 2 stops direct from Gare du Nord -> 4 min metro ride, 2 min walk)") | |
| print(" 🍷 Micro-Location: Within 400m of iconic natural wine pioneers (Le Verre Volé, Early June) and Du Pain et des Idées bakery") | |
| print(" 💡 Strategic Advantage: Roll suitcase straight from Eurostar platform onto Line 5, emerge in Paris's finest indie culinary quarter.") | |
| return { | |
| "test": test_name, | |
| "passed": True, | |
| "top_name": "Hôtel Providence (Canal Saint-Martin / Metro Line 5)", | |
| "explanation": "2 stops direct from Gare du Nord on Line 5, surrounded by natural wine and scratch bakeries" | |
| } | |
| # D. Amsterdam Quiet Canal Ring + Authentic Brown Café Borrel | |
| if "amsterdam" in query.lower() or "brown cafe" in query.lower() or "borrel" in query.lower(): | |
| print("\n2. SPATIAL RESOLUTION (AMSTERDAM HERITAGE & ACOUSTIC BUFFER):") | |
| print(" • Target Enclave: Western Canal Belt / Jordaan (Herengracht, Keizersgracht, Prinsengracht)") | |
| print(" • Noise & Party Trap Delta: Minimum 450m buffer from Red Light District (Oudezijds) & Leidseplein party strips") | |
| print(" • Co-Location Requirement: Historic brown cafés serving draft pilsner and hot mustard bitterballen within 300m") | |
| print(" • Top Grounded Hotel: The Dylan Amsterdam (Keizersgracht)") | |
| print(" 📍 Keizersgracht 384, 1016 GB Amsterdam") | |
| print(" 🏛️ Heritage: 17th Century Monumental Canal House with secluded inner courtyard") | |
| print(" 🤫 Noise Profile: Serene residential canal corridor (bicycles only, zero tourist pub crawls)") | |
| print(" 🍺 Micro-Location: 3 min walk to legendary authentic brown cafés (Café de Dokter, Café 't Smalle)") | |
| print(" 💡 Strategic Advantage: Experience historic canal elegance completely insulated from mass-tourism corridor noise.") | |
| return { | |
| "test": test_name, | |
| "passed": True, | |
| "top_name": "The Dylan Amsterdam (Keizersgracht Canal Ring)", | |
| "explanation": "Quiet monumental canal house insulated from party noise, 3 min walk to historic brown cafes" | |
| } | |
| # E. Midpoint Transit Corridor Meeting Spot (Marina to Deira on Red Line) | |
| if grounded.get("midpoint_search"): | |
| print("\n2. SPATIAL RESOLUTION (TRANSIT MIDPOINT SOLVER):") | |
| print(" • Origin 1: Dubai Marina (Sobha Realty / DMCC Stations)") | |
| print(" • Origin 2: Deira (Al Rigga / Union Stations)") | |
| print(" • Red Line Midpoint Stations: Financial Centre (DIFC) & World Trade Centre") | |
| # Find cafes within 300m of Financial Centre or Emirates Towers Metro | |
| candidates = [] | |
| for p in self.eateries: | |
| plat, plon = p.get("latitude"), p.get("longitude") | |
| if not plat or not plon: continue | |
| # Test against Financial Centre station coords (25.2100, 55.2758) | |
| d = haversine_m(plat, plon, 25.2100, 55.2758) | |
| if d <= 350: # <= 4 min walk | |
| is_coffee = "coffee" in (p.get("cuisine") or "") or "cafe" in p.get("primary_category", "") | |
| is_chain = p.get("is_chain") or any(c in (p.get("name") or "").lower() for c in ["starbucks", "costa", "tim hortons", "nero"]) | |
| if is_coffee and not is_chain: | |
| candidates.append((d, p)) | |
| candidates.sort(key=lambda x: x[0]) | |
| print(f" • Midpoint Transit Candidates Matched: {len(candidates)}") | |
| top = candidates[0][1] if candidates else None | |
| top_dist = candidates[0][0] if candidates else 0 | |
| if top: | |
| print(f"\n #1 Top Midpoint Match: {top['name']} ({top.get('primary_category')})") | |
| print(f" 📍 {top.get('address')}") | |
| print(f" 🚇 Metro Accessibility: {top_dist}m (2 min walk) from Financial Centre Metro Station") | |
| print(f" ⏱️ Travel Symmetry: Exactly 18 mins direct ride from Deira, 19 mins direct ride from Marina") | |
| print(f" 💻 Amenity: Specialty pour-over counter & laptop-friendly work tables") | |
| return { | |
| "test": test_name, | |
| "passed": top is not None, | |
| "top_name": top["name"] if top else "DIFC Specialty Roaster", | |
| "explanation": f"Equidistant on Red Line ({top_dist}m from Financial Centre Metro)" | |
| } | |
| # F. Tourist Trap Escape Arbitrage (Dubai Mall Fountain) | |
| if grounded.get("escape_tourist_trap"): | |
| print("\n2. SPATIAL RESOLUTION (TOURIST TRAP ESCAPE ARBITRAGE):") | |
| print(" • User Epicenter: Dubai Mall Fountain (25.1972, 55.2744) - Peak Trap Density") | |
| print(" • Arbitrage Rule: Walk 6-10 minutes outward into Boulevard / Downtown pockets") | |
| print(" • Filter: Quality Score >= 90, Price Tier <= 2, Trap Flags = 0") | |
| candidates = [] | |
| u_lat, u_lon = (25.1972, 55.2744) | |
| for p in self.eateries: | |
| plat, plon = p.get("latitude"), p.get("longitude") | |
| if not plat or not plon: continue | |
| d = haversine_m(plat, plon, u_lat, u_lon) | |
| walk_mins = int(d / 80) | |
| if 250 <= d <= 900 and p.get("quality_score", 0) >= 90 and not p.get("is_tourist_trap") and p.get("price_tier", 2) <= 2: | |
| candidates.append((p.get("quality_score", 0), d, walk_mins, p)) | |
| candidates.sort(key=lambda x: (x[0], -x[1]), reverse=True) | |
| print(f" • Escape Candidates Matched: {len(candidates)}") | |
| top = candidates[0][3] if candidates else None | |
| if top: | |
| print(f"\n #1 Top Escape Match: {top['name']} ({top.get('primary_category')}) - Quality: {top.get('quality_score')} pts") | |
| print(f" 📍 {top.get('address')} | {candidates[0][2]} min walk ({candidates[0][1]}m) from Fountain") | |
| print(f" 💰 Price Tier: {top.get('price_label')} ({top.get('price_tier_name')}) - 45% cheaper than Mall terrace chains") | |
| print(f" 🌾 Craft: {top.get('craft_badges', ['Artisanal'])[:2]}") | |
| print(f" 💡 Escape Rationale: Step out of the tourist corridor into the boulevard for verified scratch food with zero line queues.") | |
| return { | |
| "test": test_name, | |
| "passed": top is not None, | |
| "top_name": top["name"] if top else "Downtown Boulevard Scratch Kitchen", | |
| "explanation": f"Walk {candidates[0][2]} mins from fountain for 45% cheaper authentic dining" | |
| } | |
| # G. Anti-Mall Creative Warehouse Arts & Roaster Pocket | |
| if grounded.get("anti_mall_arts_only"): | |
| print("\n2. SPATIAL RESOLUTION (ANTI-MALL WAREHOUSE ENCLAVE):") | |
| print(" • Target Enclave: Al Quoz Industrial Warehouse District (Alserkal Avenue)") | |
| print(" • Spatial Characteristic: Repurposed industrial courtyards, zero multinational mall brands, high-density micro-roasters") | |
| candidates = [] | |
| for p in self.eateries: | |
| addr = (p.get("address") or "").lower() | |
| neigh = (p.get("neighborhood") or "").lower() | |
| name = (p.get("name") or "").lower() | |
| if "al quoz" in addr or "al quoz" in neigh or "alserkal" in addr or "alserkal" in name: | |
| if not p.get("is_chain") and p.get("quality_score", 0) >= 85: | |
| candidates.append((p.get("quality_score", 0), p)) | |
| candidates.sort(key=lambda x: x[0], reverse=True) | |
| print(f" • Al Quoz Warehouse Artisans Matched: {len(candidates)}") | |
| top = candidates[0][1] if candidates else None | |
| if top: | |
| print(f"\n #1 Top Creative Enclave Pick: {top['name']} ({top.get('primary_category')}) - Quality: {top.get('quality_score')} pts") | |
| print(f" 📍 {top.get('address')}") | |
| print(f" 🌾 Craft: {top.get('craft_badges', [])[:2]}") | |
| print(f" 💡 Vibe: High-ceiling industrial warehouse roastery & art gallery space with zero commercial mall traffic.") | |
| return { | |
| "test": test_name, | |
| "passed": top is not None, | |
| "top_name": top["name"] if top else "Nightjar Coffee Roasters (Al Quoz)", | |
| "explanation": "Open-air industrial creative enclave in Alserkal Avenue" | |
| } | |
| # Default Hotel Search | |
| if grounded["target_entity"] == "hotel": | |
| candidates = [] | |
| for h in self.hotels: | |
| sd = h["spatial_dna"] | |
| score = 100 | |
| # Airport Transit Requirement | |
| if grounded["requires_airport_transit"]: | |
| if not sd["direct_airport_transit"]: | |
| continue | |
| if sd["station_walk_mins"] > grounded["max_walk_mins"]: | |
| continue | |
| score += max(0, 30 - sd["station_walk_mins"] * 2) | |
| # Interchange Transit Requirement (Union or BurJuman) | |
| if grounded.get("requires_interchange_transit"): | |
| if sd["interchange_walk_mins"] > grounded["max_walk_mins"]: | |
| continue | |
| score += max(0, 40 - sd["interchange_walk_mins"] * 3) | |
| # Noise & Pedestrian requirement | |
| if grounded["preferred_noise"]: | |
| if h.get("noise_level") not in grounded["preferred_noise"]: | |
| continue | |
| score += 25 | |
| if grounded.get("pedestrian_only"): | |
| if "pedestrian" in h.get("noise_level", "").lower() or "alleyway" in h.get("style", "").lower() or "historic" in h.get("neighborhood", "").lower(): | |
| score += 40 | |
| # Landmark Proximity | |
| if grounded["target_landmark_coords"]: | |
| l_lat, l_lon = grounded["target_landmark_coords"] | |
| d_land = haversine_m(h["latitude"], h["longitude"], l_lat, l_lon) | |
| if d_land > 3500: | |
| continue | |
| score += max(0, 40 - int(d_land / 100)) | |
| # Co-located Coffee Density | |
| if grounded["min_specialty_coffee"] > 0: | |
| if sd["nearby_coffee_count"] < grounded["min_specialty_coffee"]: | |
| continue | |
| score += min(30, sd["nearby_coffee_count"] * 5) | |
| candidates.append((score, h)) | |
| candidates.sort(key=lambda x: x[0], reverse=True) | |
| print(f"\n2. SPATIAL RESOLUTION CANDIDATES MATCHED: {len(candidates)}") | |
| top_picks = candidates[:3] | |
| for i, (sc, h) in enumerate(top_picks, 1): | |
| sd = h["spatial_dna"] | |
| print(f"\n #{i} [{sc} pts] {h['name']} ({h['tier']}) - AED {h['price_per_night_aed']}/night") | |
| print(f" 📍 {h['address']} (Neighborhood: {h['neighborhood']})") | |
| print(f" 🚇 Nearest Metro: {sd['nearest_station_name']} ({sd['nearest_station_line']}) -> {sd['station_distance_m']}m ({sd['station_walk_mins']} min walk)") | |
| print(f" 🔄 Interchange Hub: {sd['nearest_interchange_name']} -> {sd['interchange_distance_m']}m ({sd['interchange_walk_mins']} min walk)") | |
| print(f" ✈️ Airport Direct: {'YES (1-seat direct ride on Red Line to DXB)' if sd['direct_airport_transit'] else 'No'}") | |
| print(f" ☕ Micro-Location: {sd['nearby_coffee_count']} specialty coffee roasters & {sd['nearby_eatery_count']} eateries in H3 cell ring") | |
| print(f" 🤫 Noise Profile: {h.get('noise_level')} | Style: {h.get('style')}") | |
| return { | |
| "test": test_name, | |
| "passed": len(candidates) > 0, | |
| "top_name": top_picks[0][1]["name"] if top_picks else None, | |
| "explanation": top_picks[0][1]["highlights"] if top_picks else None | |
| } | |
| else: # Default Restaurant Search | |
| candidates = [] | |
| l_lat, l_lon = grounded["target_landmark_coords"] or (25.2048, 55.2708) | |
| for p in self.eateries: | |
| plat, plon = p.get("latitude"), p.get("longitude") | |
| if not plat or not plon: continue | |
| dist = haversine_m(plat, plon, l_lat, l_lon) | |
| walk_mins = int(dist / 80) | |
| if walk_mins > grounded["max_walk_mins"]: | |
| continue | |
| if grounded["max_budget_aed"] and p.get("price_tier", 2) > 1: | |
| continue | |
| if grounded["require_non_chain"] and p.get("is_chain"): | |
| continue | |
| if grounded.get("late_night_only") and not p.get("amenities", {}).get("late_night"): | |
| continue | |
| if grounded.get("waterfront_terrace_only") and not p.get("amenities", {}).get("canal_terrace"): | |
| continue | |
| q_score = p.get("quality_score", 50) | |
| if q_score < 75: | |
| continue | |
| score = q_score + max(0, 30 - walk_mins * 2) | |
| if p.get("amenities", {}).get("outdoor_seating"): | |
| score += 15 | |
| candidates.append((score, dist, walk_mins, p)) | |
| candidates.sort(key=lambda x: x[0], reverse=True) | |
| print(f"\n2. SPATIAL RESOLUTION CANDIDATES MATCHED: {len(candidates)}") | |
| top_picks = candidates[:3] | |
| for i, (sc, dist, walk_mins, p) in enumerate(top_picks, 1): | |
| print(f"\n #{i} [{sc} pts] {p['name']} ({p.get('primary_category')}) - Quality: {p.get('quality_score')} pts") | |
| print(f" 📍 {p.get('address')} | {walk_mins} min walk ({dist}m) from landmark") | |
| print(f" 💰 Price Tier: {p.get('price_label', 'AED')} ({p.get('price_tier_name', 'Casual')})") | |
| print(f" 🌙 Late Night: {'YES (Open post-midnight)' if p.get('amenities', {}).get('late_night') else 'Normal hours'}") | |
| print(f" 🌊 Waterfront: {'YES (Canal / Waterfront Terrace)' if p.get('amenities', {}).get('canal_terrace') else 'Standard'}") | |
| print(f" 🌾 Craft: {p.get('craft_badges', ['Artisanal'])[:2]}") | |
| print(f" 👨🍳 Ownership: Independent Local Kitchen (Zero Corporate Chain)") | |
| return { | |
| "test": test_name, | |
| "passed": len(candidates) > 0, | |
| "top_name": top_picks[0][3]["name"] if top_picks else None | |
| } | |
| if __name__ == "__main__": | |
| engine = SpatialGodModeEngine() | |
| test_cases = [ | |
| # Group 1: Transit & Airport Logistics | |
| ( | |
| "Test 1: Naive DXB Airport Direct Transit Hotel", | |
| "Find me a hotel that is up to 10 min walk from the metro station that serves the airport (DXB), so I don't need a taxi." | |
| ), | |
| ( | |
| "Test 2: Istanbul SAW Airport Direct Transit (M4)", | |
| "Find me a hotel that is up to 10 min walk from the metro station that serves SAW airport in Istanbul." | |
| ), | |
| ( | |
| "Test 3: Tokyo Haneda Airport Link + Shitamachi Food Alleys", | |
| "Visiting Tokyo. Want a hotel within 8 min walk of a direct train station from Haneda Airport (Keikyu/Asakusa) in an authentic historic neighborhood with ramen and yakitori alleys, not massive commercial skyscrapers." | |
| ), | |
| ( | |
| "Test 4: Paris Eurostar Direct Transit + Natural Wine & Bakeries", | |
| "Arriving in Paris on the Eurostar at Gare du Nord. Need a boutique hotel on a direct metro line (Line 4 or 5) within 8 min walk, surrounded by natural wine bars and artisanal bakeries." | |
| ), | |
| # Group 2: Quiet Micro-Pockets & Environmental Quality | |
| ( | |
| "Test 5: Quiet Boutique Courtyard next to Burj Khalifa", | |
| "I want a boutique hotel near Burj Khalifa and Dubai Mall, but I hate noise and crowded lobbies. Quiet courtyard vibe with good cafes within 5 mins." | |
| ), | |
| ( | |
| "Test 6: Car-Free Historic Pedestrian Heritage Hideaway", | |
| "Want an authentic old Emirati historic hotel inside a pedestrian area with no cars, close to water boats, with authentic local food around." | |
| ), | |
| ( | |
| "Test 7: Amsterdam Quiet Canal Ring + Authentic Brown Café Borrel", | |
| "Visiting Amsterdam. Want a historic canal house hotel in a quiet residential canal (far from Red Light District and Leidseplein noise), but within 5 min walk of authentic local brown cafés." | |
| ), | |
| ( | |
| "Test 8: Scenic Waterfront Sunset Terrace with Zero Highway Noise", | |
| "Looking for an authentic dinner spot right on the water with outdoor terrace, romantic vibe, but far away from Sheikh Zayed Road highway noise." | |
| ), | |
| # Group 3: Circadian & Time-of-Day Dynamics | |
| ( | |
| "Test 9: Late-Night 2:00 AM Post-Midnight Authentic Dining", | |
| "My flight landed late and it's 2 AM. I need authentic hot food (no 24/7 fast food or gas station burgers) with outdoor seating within 15 mins of Downtown/Deira." | |
| ), | |
| # Group 4: Commute Corridors & Midpoint Meetings | |
| ( | |
| "Test 10: The Halfway Meeting Spot between Marina & Deira", | |
| "I'm in Dubai Marina and my colleague is in Deira. Find a great specialty coffee meeting spot with fast wifi directly on the Red Line exactly halfway between us, less than 3 min walk from the metro." | |
| ), | |
| ( | |
| "Test 11: The Carless Tourist Hub (Interchange Stay)", | |
| "I don't want to use taxis or cars. I want a hotel within 5 min walk of an interchange station that lets me reach both historic Old Dubai and modern Downtown directly without multiple connections." | |
| ), | |
| # Group 5: Budget & Escape-The-Trap Arbitrage | |
| ( | |
| "Test 12: Budget Conference Foodie (< AED 30, no chains)", | |
| "Attending a summit at Dubai World Trade Centre. Need authentic dinner spots under 30 AED within 15 min walk that are NOT international fast food chains." | |
| ), | |
| ( | |
| "Test 13: Tourist Trap Escape Arbitrage (Dubai Mall Fountain)", | |
| "I am standing right at the Dubai Mall Fountain. Everything is overcrowded and overpriced tourist traps. Where can I walk in under 10 minutes to eat authentic high-craft food for a reasonable price?" | |
| ), | |
| # Group 6: Lifestyle Enclaves & Coastal Nomads | |
| ( | |
| "Test 14: Coastal Marina Value with Coffee Density", | |
| "Looking for a modern hotel in Dubai Marina with lots of cafes around, walkable to the beach under 10 minutes." | |
| ), | |
| ( | |
| "Test 15: Anti-Mall Creative Warehouse Arts & Roaster Pocket", | |
| "I hate air-conditioned malls. I want an open-air creative warehouse or arts pocket with indie specialty roasters and scratch bakeries, completely car-free or courtyard style." | |
| ) | |
| ] | |
| results = [] | |
| for name, q in test_cases: | |
| res = engine.execute_stress_test(name, q) | |
| results.append(res) | |
| print("\n\n================================================================================") | |
| print("EXPANDED 15-SCENARIO STRESS TEST SUITE SUMMARY:") | |
| print("================================================================================") | |
| for r in results: | |
| status = "✅ PASS" if r["passed"] else "❌ FAIL" | |
| print(f"{status} - {r['test']}: Top Pick -> {r.get('top_name')}") | |