initial project setup with README and ignore
This commit is contained in:
1
app/config/__init__.py
Normal file
1
app/config/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Configuration package for mobile delivery optimization."""
|
||||
204
app/config/dynamic_config.py
Normal file
204
app/config/dynamic_config.py
Normal file
@@ -0,0 +1,204 @@
|
||||
"""
|
||||
Dynamic Configuration - rider-api
|
||||
|
||||
Replaces all hardcoded hyperparameters with DB-backed values.
|
||||
The ML hypertuner writes optimal values here; services read from here.
|
||||
|
||||
Fallback: If DB is unavailable or no tuned values exist, defaults are used.
|
||||
This means zero risk - the system works day 1 with no data.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# --- DB Path ------------------------------------------------------------------
|
||||
_DB_PATH = os.getenv("ML_DB_PATH", "ml_data/ml_store.db")
|
||||
|
||||
|
||||
# --- Hard Defaults (What the system used before ML) ---------------------------
|
||||
DEFAULTS: Dict[str, Any] = {
|
||||
# System Strategy / Prompt
|
||||
"ml_strategy": "balanced",
|
||||
|
||||
# AssignmentService
|
||||
"max_pickup_distance_km": 10.0,
|
||||
"max_kitchen_distance_km": 3.0,
|
||||
"max_orders_per_rider": 12,
|
||||
"ideal_load": 6,
|
||||
"workload_balance_threshold": 0.7,
|
||||
"workload_penalty_weight": 100.0,
|
||||
"distance_penalty_weight": 2.0,
|
||||
"preference_bonus": -15.0,
|
||||
"home_zone_bonus_4km": -3.0,
|
||||
"home_zone_bonus_2km": -5.0,
|
||||
"emergency_load_penalty": 3.0, # km penalty per order in emergency assign
|
||||
|
||||
# RouteOptimizer
|
||||
"search_time_limit_seconds": 5,
|
||||
"avg_speed_kmh": 18.0,
|
||||
"road_factor": 1.3,
|
||||
|
||||
# ClusteringService
|
||||
"cluster_radius_km": 3.0,
|
||||
|
||||
# KalmanFilter
|
||||
"kalman_process_noise": 1e-4,
|
||||
"kalman_measurement_noise": 0.01,
|
||||
|
||||
# RealisticETACalculator
|
||||
"eta_pickup_time_min": 3.0,
|
||||
"eta_delivery_time_min": 4.0,
|
||||
"eta_navigation_buffer_min": 1.5,
|
||||
"eta_short_trip_factor": 0.8, # speed multiplier for dist < 2km
|
||||
"eta_long_trip_factor": 1.1, # speed multiplier for dist > 8km
|
||||
}
|
||||
|
||||
|
||||
class DynamicConfig:
|
||||
"""
|
||||
Thread-safe, DB-backed configuration store.
|
||||
|
||||
Usage:
|
||||
cfg = DynamicConfig()
|
||||
max_dist = cfg.get("max_pickup_distance_km")
|
||||
all_params = cfg.get_all()
|
||||
"""
|
||||
|
||||
_instance: Optional["DynamicConfig"] = None
|
||||
|
||||
def __new__(cls) -> "DynamicConfig":
|
||||
"""Singleton - one config per process."""
|
||||
if cls._instance is None:
|
||||
cls._instance = super().__new__(cls)
|
||||
cls._instance._initialized = False
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
if self._initialized:
|
||||
return
|
||||
self._initialized = True
|
||||
self._cache: Dict[str, Any] = {}
|
||||
self._last_loaded: Optional[datetime] = None
|
||||
self._ensure_db()
|
||||
self._load()
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Public API
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
def get(self, key: str, default: Any = None) -> Any:
|
||||
"""Get a config value. Returns ML-tuned value if available, else default."""
|
||||
self._maybe_reload()
|
||||
val = self._cache.get(key)
|
||||
if val is not None:
|
||||
return val
|
||||
fallback = default if default is not None else DEFAULTS.get(key)
|
||||
return fallback
|
||||
|
||||
def get_all(self) -> Dict[str, Any]:
|
||||
"""Return all current config values (ML-tuned + defaults for missing keys)."""
|
||||
self._maybe_reload()
|
||||
result = dict(DEFAULTS)
|
||||
result.update(self._cache)
|
||||
return result
|
||||
|
||||
def set(self, key: str, value: Any, source: str = "manual") -> None:
|
||||
"""Write a config value to DB (used by hypertuner)."""
|
||||
try:
|
||||
os.makedirs(os.path.dirname(_DB_PATH) or ".", exist_ok=True)
|
||||
conn = sqlite3.connect(_DB_PATH)
|
||||
conn.execute("""
|
||||
INSERT INTO dynamic_config (key, value, source, updated_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(key) DO UPDATE SET
|
||||
value=excluded.value,
|
||||
source=excluded.source,
|
||||
updated_at=excluded.updated_at
|
||||
""", (key, json.dumps(value), source, datetime.utcnow().isoformat()))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
self._cache[key] = value
|
||||
logger.info(f"[DynamicConfig] Set {key}={value} (source={source})")
|
||||
except Exception as e:
|
||||
logger.error(f"[DynamicConfig] Failed to set {key}: {e}")
|
||||
|
||||
def set_bulk(self, params: Dict[str, Any], source: str = "ml_hypertuner") -> None:
|
||||
"""Write multiple config values at once (called after each Optuna study)."""
|
||||
for key, value in params.items():
|
||||
self.set(key, value, source=source)
|
||||
logger.info(f"[DynamicConfig] Bulk update: {len(params)} params from {source}")
|
||||
|
||||
def reset_to_defaults(self) -> None:
|
||||
"""Wipe all ML-tuned values, revert to hardcoded defaults."""
|
||||
try:
|
||||
conn = sqlite3.connect(_DB_PATH)
|
||||
conn.execute("DELETE FROM dynamic_config")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
self._cache.clear()
|
||||
logger.warning("[DynamicConfig] Reset to factory defaults.")
|
||||
except Exception as e:
|
||||
logger.error(f"[DynamicConfig] Reset failed: {e}")
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Internal
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
def _ensure_db(self) -> None:
|
||||
try:
|
||||
os.makedirs(os.path.dirname(_DB_PATH) or ".", exist_ok=True)
|
||||
conn = sqlite3.connect(_DB_PATH)
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS dynamic_config (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL,
|
||||
source TEXT DEFAULT 'manual',
|
||||
updated_at TEXT
|
||||
)
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
logger.error(f"[DynamicConfig] DB init failed: {e}")
|
||||
|
||||
def _load(self) -> None:
|
||||
try:
|
||||
conn = sqlite3.connect(_DB_PATH)
|
||||
rows = conn.execute("SELECT key, value FROM dynamic_config").fetchall()
|
||||
conn.close()
|
||||
self._cache = {}
|
||||
for key, raw in rows:
|
||||
try:
|
||||
self._cache[key] = json.loads(raw)
|
||||
except Exception:
|
||||
self._cache[key] = raw
|
||||
self._last_loaded = datetime.utcnow()
|
||||
if self._cache:
|
||||
logger.info(f"[DynamicConfig] Loaded {len(self._cache)} ML-tuned params from DB")
|
||||
except Exception as e:
|
||||
logger.warning(f"[DynamicConfig] Could not load from DB (using defaults): {e}")
|
||||
self._cache = {}
|
||||
|
||||
def _maybe_reload(self, interval_seconds: int = 300) -> None:
|
||||
"""Reload from DB every 5 minutes - picks up new tuned params without restart."""
|
||||
if self._last_loaded is None:
|
||||
self._load()
|
||||
return
|
||||
delta = (datetime.utcnow() - self._last_loaded).total_seconds()
|
||||
if delta > interval_seconds:
|
||||
self._load()
|
||||
|
||||
|
||||
# --- Module-level convenience singleton ---------------------------------------
|
||||
_cfg = DynamicConfig()
|
||||
|
||||
|
||||
def get_config() -> DynamicConfig:
|
||||
"""Get the global DynamicConfig singleton."""
|
||||
return _cfg
|
||||
33
app/config/mobile_config.py
Normal file
33
app/config/mobile_config.py
Normal file
@@ -0,0 +1,33 @@
|
||||
"""Mobile-specific configuration for delivery route optimization."""
|
||||
|
||||
# Mobile optimization settings
|
||||
MOBILE_CONFIG = {
|
||||
"default_algorithm": "greedy",
|
||||
"max_deliveries": 100,
|
||||
"timeout_seconds": 5,
|
||||
"response_compression": True,
|
||||
"performance_monitoring": True,
|
||||
"mobile_headers": True
|
||||
}
|
||||
|
||||
# Performance targets for mobile
|
||||
PERFORMANCE_TARGETS = {
|
||||
"greedy_algorithm": {
|
||||
"max_response_time": 0.1, # 100ms
|
||||
"max_deliveries": 50,
|
||||
"description": "Ultra-fast for real-time mobile apps"
|
||||
},
|
||||
"tsp_algorithm": {
|
||||
"max_response_time": 3.0, # 3 seconds
|
||||
"max_deliveries": 30,
|
||||
"description": "Optimal but slower, good for planning"
|
||||
}
|
||||
}
|
||||
|
||||
# Mobile app recommendations
|
||||
MOBILE_RECOMMENDATIONS = {
|
||||
"real_time_delivery": "greedy",
|
||||
"route_planning": "tsp",
|
||||
"large_batches": "greedy",
|
||||
"cost_optimization": "tsp"
|
||||
}
|
||||
50
app/config/rider_preferences.py
Normal file
50
app/config/rider_preferences.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""
|
||||
Rider Preferred Kitchens Configuration
|
||||
Mapping of Rider ID (int) to list of preferred Kitchen names (str).
|
||||
Updated based on Deployment Plan.
|
||||
"""
|
||||
|
||||
RIDER_PREFERRED_KITCHENS = {
|
||||
# 1. VIVEK ANANDHAN - LOCAL, RS PURAM TO SELVAPURAM
|
||||
1116: ["Daily grubs(jayanthi kitchen)", "Bhuvaneshwari kitchen", "Hilda kitchen", "Kalpana kitchen"],
|
||||
|
||||
# 2. NARAYANASAMY - VENGATAPURAM, VADAVALI, TADAGAM ROAD
|
||||
1096: ["Daily grubs(jayanthi kitchen)", "Bhuvaneshwari kitchen", "Hilda kitchen", "Kalpana kitchen"],
|
||||
|
||||
# 3. VARUN EDWARD - GN MILLS, KAVUNDAMPALAYAM, THUDIYALUR
|
||||
897: ["Daily grubs(jayanthi kitchen)", "Bhuvaneshwari kitchen", "Hilda kitchen", "Kalpana kitchen"],
|
||||
|
||||
# 4. JAYASABAESH - GANAPTHY
|
||||
950: ["Daily grubs nandhini", "Vidhya kitchen"],
|
||||
|
||||
# 5. TAMILALAHZAN - GANDHIMA NAGAR
|
||||
1114: ["Daily grubs nandhini", "Vidhya kitchen"],
|
||||
|
||||
# 6. RAJAN - PEELAMDU
|
||||
883: ["Daily grubs nandhini", "Vidhya kitchen"],
|
||||
|
||||
# 7. MUTHURAJ - RAMANATHAPURAM TO SAIBABACOLONY
|
||||
1272: ["Daily grubs(jayanthi kitchen)", "Bhuvaneshwari kitchen", "Hilda kitchen", "Kalpana kitchen", "Daily grubs nandhini", "Vidhya kitchen"],
|
||||
|
||||
# 8. MANIKANDAN - SINGNALLUR
|
||||
753: ["Daily grubs nandhini", "Vidhya kitchen"],
|
||||
|
||||
# 9. TACHANAMOORTHI - KOVAI PUTHUR TO KAVUNDAMPALAYAM
|
||||
1271: ["Daily grubs(jayanthi kitchen)", "Bhuvaneshwari kitchen", "Hilda kitchen", "Kalpana kitchen"],
|
||||
1133: ["Daily grubs(jayanthi kitchen)", "Bhuvaneshwari kitchen", "Hilda kitchen", "Kalpana kitchen"], # Active ID
|
||||
}
|
||||
|
||||
# Anchor Coordinates for Riders (Based on Area Name)
|
||||
# Used as fallback if GPS is missing, or to bias assignment to their Home Zone.
|
||||
RIDER_HOME_LOCATIONS = {
|
||||
1116: (11.0067, 76.9558), # VIVEK ANANDAN: RS PURAM
|
||||
1096: (11.0450, 76.9000), # NARAYANASAMY: VADAVALI
|
||||
897: (11.0430, 76.9380), # VARUN EDWARD: KAVUNDAMPALAYAM
|
||||
950: (11.0330, 76.9800), # JAYASABESH: GANAPATHY
|
||||
1114: (11.0450, 77.0000), # TAMILAZHAGAN: GANDHIMA NAGAR
|
||||
883: (11.0200, 77.0000), # RAJAN: PEELAMEDU
|
||||
1272: (10.9950, 77.0000), # MUTHURAJA: RAMANATHAPURAM
|
||||
753: (11.0000, 77.0300), # MANIKANDAN: SINGANALLUR
|
||||
1271: (10.9500, 76.9600), # THATCHINAMOORTHI: KOVAI PUDUR
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user