"""이번 턴 제시가 계산 보조 — 구매자 자신의 비공개 기준과 히스토리로 이번 턴에 낼 가격을 정한다. 규칙은 학습 데이터(train) 구매자 제시 턴에서 확인한 것이다 (홀드아웃은 확인에만 씀). - 와일드카드(1% 추가네고·재원부족·양보 전략): 카드 안내의 금액 그대로 - 구매자의 마지막 회차(남은 턴 2 이하)에서 수락하지 않으면: 최대 수용가 (77/77) - 아직 가격을 낸 적이 없으면: 첫 제안가 (261/261) - 주장: 직전 가격 유지 (88/88) - 양보·타협: 직전 가격에서 판매자 직전 제시가 쪽으로 간격의 일정 비율만큼 (비율은 grounding_policy.json 의 pricing) 제시 단위는 시뮬레이터와 같다: 현 단가 1천원 미만 1원, 1만원 미만 10원, 100만원 미만 100원, 그 이상 1,000원. """ import math import re from dataclasses import dataclass from typing import Dict, Optional from services.grounding.parser import NegotiationContext PRICE_UNITS = ((1_000, 1), (10_000, 10), (1_000_000, 100), (math.inf, 1_000)) GUIDE_PRICE_RE = re.compile(r"(\d[\d,]*)원") WILDCARDS = ("1% 추가네고", "재원부족", "양보 전략") @dataclass class OfferAdvice: price: int rule: str # 로그용 (wildcard / last_round / first_offer / hold / step) def price_unit(list_price: Optional[int]) -> int: if not list_price: return 1 return next(unit for bound, unit in PRICE_UNITS if list_price < bound) def round_price(value: float, unit: int) -> int: """단위 가격으로 반올림 (.5 는 올림) — 시뮬레이터 round_price 와 같다.""" return int(math.floor(round(value / unit, 6) + 0.5) * unit) def suggest_offer(ctx: NegotiationContext, step_ratio: Dict[str, float]) -> Optional[OfferAdvice]: """이번 턴 제시가. 탐색 턴·첫 턴·수락 턴은 호출하지 않는다 (None 이면 제시가 안내 없음).""" prices = ctx.private_prices if not prices: return None if ctx.card_name in WILDCARDS: found = GUIDE_PRICE_RE.search(ctx.card_guide or "") return OfferAdvice(int(found.group(1).replace(",", "")), "wildcard") if found else None own, seller = ctx.prev_buyer_price, ctx.prev_seller_price if ctx.remaining is not None and ctx.remaining <= 2 and seller is not None: return OfferAdvice(max(prices["max"], own or 0), "last_round") if own is None: return OfferAdvice(prices["open"], "first_offer") ratio = step_ratio.get(ctx.strategy or "") if ratio is None or seller is None or seller <= own: return OfferAdvice(own, "hold") unit = price_unit(ctx.list_price) upper = min(seller, prices["max"]) while unit > 1 and round_price(own + ratio * (seller - own), unit) <= own: unit //= 10 # 간격이 단위보다 좁으면 더 작은 단위로 (시뮬레이터 규칙) offer = round_price(own + ratio * (seller - own), unit) if offer <= own or offer > upper: offer = max(own, min(offer, upper)) return OfferAdvice(offer, "step")