"""Q-Learning State 표현 (162 states = 3×3×3×3×2).""" from dataclasses import dataclass from enum import IntEnum from typing import List _REVENUE_DESCRIPTIONS = ("Low (≤1,000만원)", "Mid (1,000~3,000만원)", "High (>3,000만원)") _DISTRIBUTION_DESCRIPTIONS = ("제조", "총판", "유통") _PARTNER_DESCRIPTIONS = ("Single (단독)", "Multiple (다수)", "None (없음)") _ACCEPTANCE_DESCRIPTIONS = ("Low (<3%)", "Mid (3~9%)", "High (>9%)") _PRICE_ZONE_DESCRIPTIONS = ("PZ1 (A≤P≤T)", "PZ2 (P>T)") class RevenueRange(IntEnum): """매출액 가격구간""" LOW = 0 # ≤ 1,000만원 MID = 1 # 1,000만원 ~ 3,000만원 HIGH = 2 # > 3,000만원 @property def description(self) -> str: return _REVENUE_DESCRIPTIONS[self.value] @classmethod def from_amount(cls, amount: float) -> "RevenueRange": if amount <= 10_000_000: return cls.LOW elif amount <= 30_000_000: return cls.MID return cls.HIGH class DistributionStructure(IntEnum): """유통 구조""" MANUFACTURER = 0 # 제조 (M) WHOLESALER = 1 # 총판 (W) RETAILER = 2 # 유통 (R) @property def description(self) -> str: return _DISTRIBUTION_DESCRIPTIONS[self.value] @classmethod def from_code(cls, code: str) -> "DistributionStructure": normalized = (code or "").strip().upper() mapping = {"M": cls.MANUFACTURER, "W": cls.WHOLESALER, "R": cls.RETAILER} if normalized not in mapping: raise ValueError(f"unknown distribution code: {code}") return mapping[normalized] class PartnerType(IntEnum): """파트너사 종류""" SINGLE = 0 # 단독 MULTIPLE = 1 # 다수 NONE = 2 # 없음 @property def description(self) -> str: return _PARTNER_DESCRIPTIONS[self.value] @classmethod def from_count(cls, count: int) -> "PartnerType": if count == 0: return cls.NONE elif count == 1: return cls.SINGLE return cls.MULTIPLE class AcceptanceRate(IntEnum): """가격 수용률 구간""" LOW = 0 # < 3% MID = 1 # 3% ~ 9% HIGH = 2 # > 9% @property def description(self) -> str: return _ACCEPTANCE_DESCRIPTIONS[self.value] @classmethod def from_ratio(cls, ratio: float) -> "AcceptanceRate": if ratio < 0.03: return cls.LOW elif ratio <= 0.09: return cls.MID return cls.HIGH class InputPriceZone(IntEnum): """입력 금액 구간""" PZ1 = 0 # A ≤ P ≤ T PZ2 = 1 # P > T @property def description(self) -> str: return _PRICE_ZONE_DESCRIPTIONS[self.value] @classmethod def from_prices(cls, input_price: float, anchor_price: float, target_price: float) -> "InputPriceZone": if anchor_price <= 0 or target_price <= 0: raise ValueError("anchor_price and target_price must be positive") if anchor_price > target_price: raise ValueError("anchor_price must not exceed target_price") if anchor_price <= input_price <= target_price: return cls.PZ1 return cls.PZ2 @dataclass class State: """State = (매출구간, 유통구조, 파트너, 수용률, 가격구간) — 3×3×3×3×2 = 162""" revenue_range: RevenueRange distribution: DistributionStructure partner_type: PartnerType acceptance_rate: AcceptanceRate input_price_zone: InputPriceZone def to_array(self) -> List[int]: return [ self.revenue_range.value, self.distribution.value, self.partner_type.value, self.acceptance_rate.value, self.input_price_zone.value, ] def to_index(self) -> int: """State를 1차원 인덱스(0~161)로 변환""" return ( self.revenue_range.value * 54 + self.distribution.value * 18 + self.partner_type.value * 6 + self.acceptance_rate.value * 2 + self.input_price_zone.value ) @classmethod def from_index(cls, index: int) -> "State": if index < 0 or index >= 162: raise ValueError(f"index {index} out of range [0, 162)") revenue_value, remainder = divmod(index, 54) dist_value, remainder = divmod(remainder, 18) partner_value, remainder = divmod(remainder, 6) accept_value, pricezone_value = divmod(remainder, 2) return cls( revenue_range=RevenueRange(revenue_value), distribution=DistributionStructure(dist_value), partner_type=PartnerType(partner_value), acceptance_rate=AcceptanceRate(accept_value), input_price_zone=InputPriceZone(pricezone_value), ) def __str__(self) -> str: return ( f"State(" f"revenue={self.revenue_range.description}, " f"distribution={self.distribution.description}, " f"partner={self.partner_type.description}, " f"acceptance={self.acceptance_rate.description}, " f"price_zone={self.input_price_zone.description})" )