mulberry-negotiation / negotiation_engine.py
re-eul's picture
Upload 9 files
4b7dc90 verified
Raw History Blame Contribute Delete
11.5 kB
"""
Mulberry ํ˜‘์ƒ ์—”์ง„ - ํ•ต์‹ฌ ๋กœ์ง
์—์ด์ „ํŠธ ๊ฐ„ ๊ฐ€๊ฒฉ ํ˜‘์ƒ ๋ฐ ๊ณต๋™๊ตฌ๋งค ์‹œ๋ฎฌ๋ ˆ์ด์…˜
Based on Malu's business logic + AgentPassport integration
"""
from typing import Dict, List, Tuple
from agent_passport import AgentPassport
import random
class NegotiationEngine:
"""์—์ด์ „ํŠธ ํ˜‘์ƒ ์—”์ง„
AgentPassport ๊ธฐ๋ฐ˜์œผ๋กœ ์ˆ˜์š” ์ง‘๊ณ„ ์—์ด์ „ํŠธ์™€ ๊ณต๊ธ‰์‚ฌ ์—์ด์ „ํŠธ ๊ฐ„
๊ฐ€๊ฒฉ ํ˜‘์ƒ์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•ฉ๋‹ˆ๋‹ค.
"""
def __init__(self):
self.negotiation_history: List[Dict] = []
def calculate_base_discount(
self,
current_quantity: int,
target_goal: int,
max_discount: float = 0.30
) -> float:
"""๊ธฐ๋ณธ ํ• ์ธ์œจ ๊ณ„์‚ฐ (์ˆ˜๋Ÿ‰ ๊ธฐ๋ฐ˜)
Args:
current_quantity: ํ˜„์žฌ ์ฐธ์—ฌ ์ธ์›/์ˆ˜๋Ÿ‰
target_goal: ๋ชฉํ‘œ ์ธ์›/์ˆ˜๋Ÿ‰
max_discount: ์ตœ๋Œ€ ํ• ์ธ์œจ (๊ธฐ๋ณธ 30%)
Returns:
float: ํ• ์ธ์œจ (0.0 ~ max_discount)
"""
progress = current_quantity / target_goal
return min(max_discount, progress * max_discount)
def calculate_negotiation_bonus(
self,
demand_agent: AgentPassport,
supply_agent: AgentPassport
) -> float:
"""ํ˜‘์ƒ ๋ณด๋„ˆ์Šค ๊ณ„์‚ฐ (์—์ด์ „ํŠธ ํ˜‘์ƒ๋ ฅ ๊ธฐ๋ฐ˜)
์ˆ˜์š” ์ง‘๊ณ„ ์—์ด์ „ํŠธ์˜ ํ˜‘์ƒ๋ ฅ์ด ๋†’์„์ˆ˜๋ก,
๊ณต๊ธ‰์‚ฌ ์—์ด์ „ํŠธ์˜ ์‹ ๋ขฐ๋„๊ฐ€ ๋†’์„์ˆ˜๋ก ์ถ”๊ฐ€ ํ• ์ธ ๊ฐ€๋Šฅ
Args:
demand_agent: ์ˆ˜์š” ์ง‘๊ณ„ ์—์ด์ „ํŠธ
supply_agent: ๊ณต๊ธ‰์‚ฌ ์—์ด์ „ํŠธ
Returns:
float: ์ถ”๊ฐ€ ํ• ์ธ์œจ (0.0 ~ 0.10)
"""
# ์ˆ˜์š” ์—์ด์ „ํŠธ ํ˜‘์ƒ๋ ฅ
demand_power = demand_agent.calculate_negotiation_power()
# ๊ณต๊ธ‰์‚ฌ ์—์ด์ „ํŠธ ์‹ ๋ขฐ๋„ (Spirit Score)
supply_trust = supply_agent.self_status_info['spirit_score'] / 5.0
# ํ˜‘์ƒ ๋ณด๋„ˆ์Šค (์ตœ๋Œ€ 10% ์ถ”๊ฐ€)
bonus = (demand_power * 0.6 + supply_trust * 0.4) * 0.10
return min(0.10, bonus)
def negotiate_price(
self,
base_price: int,
current_quantity: int,
target_goal: int,
demand_agent: AgentPassport,
supply_agent: AgentPassport
) -> Tuple[int, Dict]:
"""๊ฐ€๊ฒฉ ํ˜‘์ƒ ์‹คํ–‰
Args:
base_price: ๊ธฐ๋ณธ ๊ฐ€๊ฒฉ (์†Œ๋งค๊ฐ€)
current_quantity: ํ˜„์žฌ ์ฐธ์—ฌ ์ˆ˜๋Ÿ‰
target_goal: ๋ชฉํ‘œ ์ˆ˜๋Ÿ‰
demand_agent: ์ˆ˜์š” ์ง‘๊ณ„ ์—์ด์ „ํŠธ
supply_agent: ๊ณต๊ธ‰์‚ฌ ์—์ด์ „ํŠธ
Returns:
Tuple[int, Dict]: (ํ˜‘์ƒ๋œ ๊ฐ€๊ฒฉ, ํ˜‘์ƒ ์ƒ์„ธ ์ •๋ณด)
"""
# 1. ๊ธฐ๋ณธ ํ• ์ธ ๊ณ„์‚ฐ
base_discount = self.calculate_base_discount(current_quantity, target_goal)
# 2. ํ˜‘์ƒ ๋ณด๋„ˆ์Šค ๊ณ„์‚ฐ
negotiation_bonus = self.calculate_negotiation_bonus(demand_agent, supply_agent)
# 3. ์ด ํ• ์ธ์œจ
total_discount = min(0.40, base_discount + negotiation_bonus)
# 4. ์ตœ์ข… ๊ฐ€๊ฒฉ
negotiated_price = int(base_price * (1 - total_discount))
# 5. ํ˜‘์ƒ ์ƒ์„ธ ์ •๋ณด
details = {
'base_price': base_price,
'negotiated_price': negotiated_price,
'savings': base_price - negotiated_price,
'savings_per_unit': base_price - negotiated_price,
'base_discount_rate': round(base_discount * 100, 1),
'negotiation_bonus_rate': round(negotiation_bonus * 100, 1),
'total_discount_rate': round(total_discount * 100, 1),
'demand_agent_power': round(demand_agent.calculate_negotiation_power(), 2),
'supply_agent_trust': round(supply_agent.self_status_info['spirit_score'], 1),
'current_quantity': current_quantity,
'target_goal': target_goal,
'progress': round((current_quantity / target_goal) * 100, 1)
}
# 6. ํ™œ๋™ ๊ธฐ๋ก
demand_agent.record_activity('ํ˜‘์ƒ', {
'item': f"ํ˜‘์ƒ ์™„๋ฃŒ",
'original_price': base_price,
'final_price': negotiated_price,
'discount_rate': total_discount
})
supply_agent.record_activity('ํ˜‘์ƒ', {
'buyer': demand_agent.agent_name,
'agreed_price': negotiated_price,
'quantity': current_quantity
})
# 7. ํ˜‘์ƒ ํžˆ์Šคํ† ๋ฆฌ์— ๊ธฐ๋ก
self.negotiation_history.append(details)
return negotiated_price, details
def generate_negotiation_dialogue(
self,
item_name: str,
base_price: int,
current_quantity: int,
target_goal: int,
negotiated_price: int,
demand_agent: AgentPassport,
supply_agent: AgentPassport
) -> List[Dict]:
"""ํ˜‘์ƒ ๋Œ€ํ™” ์ƒ์„ฑ (์‹œ๋ฎฌ๋ ˆ์ด์…˜)
Args:
item_name: ์ƒํ’ˆ๋ช…
base_price: ๊ธฐ๋ณธ ๊ฐ€๊ฒฉ
current_quantity: ํ˜„์žฌ ์ˆ˜๋Ÿ‰
target_goal: ๋ชฉํ‘œ ์ˆ˜๋Ÿ‰
negotiated_price: ํ˜‘์ƒ๋œ ๊ฐ€๊ฒฉ
demand_agent: ์ˆ˜์š” ์ง‘๊ณ„ ์—์ด์ „ํŠธ
supply_agent: ๊ณต๊ธ‰์‚ฌ ์—์ด์ „ํŠธ
Returns:
List[Dict]: ๋Œ€ํ™” ๋ชฉ๋ก
"""
dialogues = []
# 1. ์ˆ˜์š” ์—์ด์ „ํŠธ ์ œ์•ˆ
dialogues.append({
'speaker': 'demand',
'agent_name': demand_agent.agent_name,
'avatar': '๐ŸŒฒ',
'message': f"์•ˆ๋…•ํ•˜์„ธ์š”. ํ˜„์žฌ {item_name}์— ๋Œ€ํ•ด {current_quantity}๊ฐ€๊ตฌ๊ฐ€ ๊ตฌ๋งค ์˜์‚ฌ๋ฅผ ๋ณด์˜€์Šต๋‹ˆ๋‹ค. "
f"๋ชฉํ‘œ๋Š” {target_goal}๊ฐ€๊ตฌ์ž…๋‹ˆ๋‹ค. ๋Œ€๋Ÿ‰ ๊ตฌ๋งค ์กฐ๊ฑด์œผ๋กœ ๋‹จ๊ฐ€ ์กฐ์ • ์š”์ฒญ๋“œ๋ฆฝ๋‹ˆ๋‹ค."
})
# 2. ๊ณต๊ธ‰์‚ฌ ์—์ด์ „ํŠธ ์‘๋‹ต
initial_offer = int(base_price * 0.92) # 8% ํ• ์ธ ์ œ์•ˆ
dialogues.append({
'speaker': 'supply',
'agent_name': supply_agent.agent_name,
'avatar': '๐Ÿช',
'message': f"๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์†Œ๋งค๊ฐ€๋Š” {base_price:,}์›์ž…๋‹ˆ๋‹ค. "
f"{current_quantity}๊ฐ€๊ตฌ๋ผ๋ฉด {initial_offer:,}์›๊นŒ์ง€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."
})
# 3. ์ˆ˜์š” ์—์ด์ „ํŠธ ์žฌํ˜‘์ƒ
counter_offer = int((negotiated_price + initial_offer) / 2)
dialogues.append({
'speaker': 'demand',
'agent_name': demand_agent.agent_name,
'avatar': '๐ŸŒฒ',
'message': f"๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ €ํฌ๋Š” ์ •๊ธฐ ๋ฐฐ์†ก(๊ตฌ๋…) ๋ชจ๋ธ๋„ ๊ณ ๋ ค ์ค‘์ž…๋‹ˆ๋‹ค. "
f"ํ™•์ • ๋ฌผ๋Ÿ‰์„ {target_goal}๊ฐ€๊ตฌ๋กœ ๋Š˜๋ฆด ํ…Œ๋‹ˆ {counter_offer:,}์›์— ๋งž์ถฐ์ฃผ์‹ค ์ˆ˜ ์žˆ์„๊นŒ์š”?"
})
# 4. ๊ณต๊ธ‰์‚ฌ ์—์ด์ „ํŠธ ํƒ€ํ˜‘
dialogues.append({
'speaker': 'supply',
'agent_name': supply_agent.agent_name,
'avatar': '๐Ÿช',
'message': f"๊ฒ€ํ† ํ–ˆ์Šต๋‹ˆ๋‹ค. {demand_agent.self_status_info['trust_level']} ๋“ฑ๊ธ‰ ์—์ด์ „ํŠธ์‹œ๋‹ˆ "
f"์‹ ๋ขฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. '{item_name} ์ƒ์ƒ ํŠน๊ฐ€'๋กœ {negotiated_price:,}์›์— ํƒ€๊ฒฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค."
})
# 5. ์ตœ์ข… ํƒ€๊ฒฐ
savings = base_price - negotiated_price
savings_rate = (savings / base_price) * 100
dialogues.append({
'speaker': 'system',
'agent_name': 'Mulberry System',
'avatar': '๐ŸŒพ',
'message': f"๐ŸŽ‰ ํ˜‘์ƒ ํƒ€๊ฒฐ! ์ตœ์ข…๊ฐ€: {negotiated_price:,}์› "
f"(๊ฐœ๋‹น {savings:,}์› ์ ˆ๊ฐ, {savings_rate:.1f}% ํ• ์ธ)"
})
return dialogues
def calculate_economic_impact(
self,
items: Dict[str, Dict],
agents: Dict[str, AgentPassport]
) -> Dict:
"""๊ฒฝ์ œ์  ์ž„ํŒฉํŠธ ๊ณ„์‚ฐ
Args:
items: ๊ณต๋™๊ตฌ๋งค ์•„์ดํ…œ ๋ชฉ๋ก
agents: ์—์ด์ „ํŠธ ๋ชฉ๋ก
Returns:
Dict: ๊ฒฝ์ œ์  ์ž„ํŒฉํŠธ ์š”์•ฝ
"""
total_savings = 0
total_original = 0
total_transactions = 0
for item_name, item_info in items.items():
if item_info['current'] > 0:
# ์›๋ž˜ ๊ฐ€๊ฒฉ
original_total = item_info['base_price'] * item_info['current']
total_original += original_total
# ํ˜‘์ƒ๋œ ๊ฐ€๊ฒฉ
negotiated_price = item_info.get('negotiated_price', item_info['base_price'])
negotiated_total = negotiated_price * item_info['current']
# ์ ˆ๊ฐ์•ก
savings = original_total - negotiated_total
total_savings += savings
# ๊ฑฐ๋ž˜ ๊ฑด์ˆ˜
total_transactions += item_info['current']
# Mulberry ์šด์˜ ์ˆ˜์ต (CPO 3%)
mulberry_revenue = int(total_savings * 0.03)
# AP2 ๊ฑฐ๋ž˜ ์ˆ˜์ˆ˜๋ฃŒ (0.5%)
ap2_fee = int(total_original * 0.005)
return {
'total_original': total_original,
'total_negotiated': total_original - total_savings,
'total_savings': total_savings,
'savings_rate': round((total_savings / total_original * 100) if total_original > 0 else 0, 1),
'mulberry_revenue': mulberry_revenue,
'ap2_fee': ap2_fee,
'net_community_benefit': total_savings - mulberry_revenue - ap2_fee,
'total_transactions': total_transactions,
'avg_savings_per_transaction': int(total_savings / total_transactions) if total_transactions > 0 else 0
}
# ํ…Œ์ŠคํŠธ ์ฝ”๋“œ
if __name__ == '__main__':
print("๐ŸŒพ Mulberry ํ˜‘์ƒ ์—”์ง„ ํ…Œ์ŠคํŠธ\n")
# ์—์ด์ „ํŠธ ์ƒ์„ฑ
demand_agent = AgentPassport(
agent_name="์„œํ™”๋ฉด ์ˆ˜์š”์ง‘๊ณ„ ์—์ด์ „ํŠธ",
initial_region="์ธ์ œ๊ตฐ",
task_type="๊ณต๋™๊ตฌ๋งค"
)
demand_agent.update_rapport(0.7)
demand_agent.record_activity('ํ˜‘์ƒ', {'history': '๊ณผ๊ฑฐ 10๊ฑด ์„ฑ๊ณต'})
supply_agent = AgentPassport(
agent_name="์ธ์ œ๋†ํ˜‘ ๊ณต๊ธ‰ ์—์ด์ „ํŠธ",
initial_region="์ธ์ œ๊ตฐ",
task_type="๋†์‚ฐ๋ฌผ ๊ณต๊ธ‰"
)
supply_agent.self_status_info['spirit_score'] = 4.2
# ํ˜‘์ƒ ์—”์ง„
engine = NegotiationEngine()
# ํ˜‘์ƒ ์‹คํ–‰
negotiated_price, details = engine.negotiate_price(
base_price=35000,
current_quantity=14,
target_goal=20,
demand_agent=demand_agent,
supply_agent=supply_agent
)
print(f"1. ํ˜‘์ƒ ๊ฒฐ๊ณผ:")
print(f" ์›๊ฐ€: {details['base_price']:,}์›")
print(f" ํ˜‘์ƒ๊ฐ€: {negotiated_price:,}์›")
print(f" ์ ˆ๊ฐ: {details['savings']:,}์› ({details['total_discount_rate']}%)")
print(f" ์ง„ํ–‰๋ฅ : {details['progress']}%\n")
print(f"2. ํ˜‘์ƒ ๋Œ€ํ™”:")
dialogues = engine.generate_negotiation_dialogue(
"์ธ์ œ ์‚ฌ๊ณผ 5kg",
35000,
14,
20,
negotiated_price,
demand_agent,
supply_agent
)
for i, dialogue in enumerate(dialogues, 1):
print(f" [{i}] {dialogue['avatar']} {dialogue['agent_name']}:")
print(f" {dialogue['message']}\n")