File size: 11,475 Bytes
4b7dc90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
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")