-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpricing_engine.py
More file actions
45 lines (37 loc) · 1.37 KB
/
pricing_engine.py
File metadata and controls
45 lines (37 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from src.common.models import PricingEvent
from src.common.utils import get_logger, utc_now
class PricingEngine:
"""
Dynamic surge pricing engine.
Computes surge multipliers based on supply/demand
in specific geographic zones.
"""
def __init__(self, base_surge: float = 1.0, sensitivity: float = 0.15):
"""
base_surge: minimum multiplier
sensitivity: how strongly demand affects pricing
"""
self.base_surge = base_surge
self.sensitivity = sensitivity
self.logger = get_logger("PricingEngine")
def compute_surge(self, demand: int, supply: int, zone_id: str) -> PricingEvent:
"""
demand: number of ride requests in zone
supply: number of available drivers in zone
"""
if supply == 0:
surge_multiplier = self.base_surge * 3.0 # high surge cap
else:
ratio = demand / supply
surge_multiplier = self.base_surge + (ratio * self.sensitivity)
surge_multiplier = round(max(surge_multiplier, 1.0), 2)
self.logger.info(
f"[Zone {zone_id}] Demand={demand}, Supply={supply}, Surge={surge_multiplier}"
)
return PricingEvent(
zone_id=zone_id,
demand=demand,
supply=supply,
surge_multiplier=surge_multiplier,
timestamp=utc_now(),
)