File size: 24,315 Bytes
66f749a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
"""

Simulation orchestrator - manages large-scale agent simulations.



Uses async patterns inspired by AgentSociety:

- QwenLLM actor pool for distributed LLM calls (via HuggingFace Space Ollama API)

- asyncio.gather() for concurrent agent processing

- Agents are plain objects, not Ray actors

"""
import logging
import asyncio
import json
import os
from typing import List, Dict, Any, Optional

from simulation.ray_cluster import init_ray_cluster, shutdown_ray
from simulation.agents.social_agent import SocialAgent
from simulation.utils.profile_generator import ProfileGenerator
from simulation.llm_client import QwenLLM, shutdown_llm_pool

logger = logging.getLogger(__name__)


def _clean_env(name: str, default: str = "") -> str:
    """Read env var and trim surrounding whitespace/newlines."""
    return (os.getenv(name, default) or default).strip()


class SimulationOrchestrator:
    """

    Orchestrate large-scale agent simulations.



    Manages:

    - LLM actor pool (Ray-backed, Qwen via Ollama API)

    - Agent spawning (plain objects)

    - Ad content distribution

    - Result collection and analysis

    - Risk detection

    """

    def __init__(

        self,

        experiment_id: str,

        num_agents: int = 1000,

        mqtt_host: str = "localhost",

        mqtt_port: int = 1883,

        mqtt_transport: str = "tcp",

        mqtt_path: Optional[str] = None,

        chroma_host: str = "localhost",

        chroma_port: int = 8000,

        chroma_ssl: bool = False,

    ):
        self.experiment_id = experiment_id
        self.num_agents = num_agents
        self.chroma_host = chroma_host
        self.chroma_port = chroma_port
        self.chroma_ssl = chroma_ssl

        self.agents: List[SocialAgent] = []
        self.profiles = []
        self.social_network = {}
        self.event_logs = []
        self.llm_pool: Optional[QwenLLM] = None

    async def run(

        self,

        ad_content: str,

        demographic_filter: Optional[Dict[str, Any]] = None,

        simulation_days: int = 5,

        redis_client=None,

        custom_agent_profiles: Optional[List[Dict]] = None,

        use_custom_agents_only: bool = False,

    ) -> Dict[str, Any]:
        """

        Run full simulation (async).



        Args:

            ad_content: VLM-generated description of ad

            demographic_filter: Target demographics

            simulation_days: Days to simulate

            redis_client: Redis client for progress updates



        Returns:

            Simulation results with engagement score, sentiment, risk flags

        """
        logger.info(
            f"Starting simulation {self.experiment_id} "
            f"with {self.num_agents} agents"
        )

        try:
            # Initialize Ray cluster
            init_ray_cluster(num_cpus=None)

            # Create LLM actor pool (Qwen via Ollama API)
            num_actors = min(4, max(1, self.num_agents // 3))
            self.llm_pool = QwenLLM(num_actors=num_actors)
            logger.info(f"LLM actor pool created with {num_actors} actors")

            # Profiles and spawning agents
            self._update_progress(redis_client, 5, 0, 0)
            self._spawn_agents(custom_agent_profiles, use_custom_agents_only, demographic_filter)
            logger.info(f"Spawned {len(self.agents)} agents")

            # Have all agents perceive the ad
            self._update_progress(redis_client, 20, 1, len(self.agents))
            final_states = await self._run_simulation(
                ad_content, simulation_days, redis_client
            )

            # Analyze results
            results = self._analyze_results(final_states)

            # Cleanup
            self._cleanup()

            logger.info(
                f"Simulation complete. "
                f"Engagement score: {results['engagement_score']:.1f}"
            )

            return results

        except Exception as e:
            logger.error(f"Simulation failed: {e}")
            self._cleanup()
            raise

    def _spawn_agents(

        self, 

        custom_agent_profiles: Optional[List[Dict]] = None, 

        use_custom_agents_only: bool = False,

        demographic_filter: Optional[Dict[str, Any]] = None

    ):
        """Spawn all agents as plain Python objects and configure network"""
        self.agents = []

        if use_custom_agents_only:
            # Only use the explicitly provided custom profiles — no AI padding
            if custom_agent_profiles:
                self.profiles = list(custom_agent_profiles)
            else:
                logger.warning(
                    "use_custom_agents_only=True but no custom profiles were provided. "
                    "Simulation will run with 0 agents."
                )
                self.profiles = []
        elif custom_agent_profiles:
            self.profiles = list(custom_agent_profiles)
            remaining = self.num_agents - len(self.profiles)
            if remaining > 0:
                self.profiles.extend(ProfileGenerator.generate_profiles(remaining, demographic_filter))
            self.profiles = self.profiles[:self.num_agents]
        else:
            self.profiles = ProfileGenerator.generate_profiles(self.num_agents, demographic_filter)
            
        self.social_network = ProfileGenerator.generate_social_network(
            self.profiles, avg_friends=8
        )

        # Try to create a shared memory store (optional)
        memory_store = self._create_memory_store()

        for profile in self.profiles:
            agent_id = profile["agent_id"]
            friends = self.social_network.get(agent_id, [])

            agent = SocialAgent(
                agent_id=agent_id,
                profile=profile,
                experiment_id=self.experiment_id,
                llm_pool=self.llm_pool,
                friends=friends,
                memory_store=memory_store,
            )

            self.agents.append(agent)

    def _create_memory_store(self):
        """Try to create a shared ChromaDB memory store (optional)"""
        try:
            from simulation.agents.agent_memory import AgentMemoryStore

            return AgentMemoryStore(
                chroma_host=self.chroma_host,
                chroma_port=self.chroma_port,
                collection_name=f"exp_{self.experiment_id}",
                ssl=self.chroma_ssl,
            )
        except Exception as e:
            logger.info(
                f"ChromaDB not available, running without memory: {e}"
            )
            return None

    async def _run_simulation(

        self,

        ad_content: str,

        simulation_days: int,

        redis_client=None,

    ) -> List[Dict[str, Any]]:
        """

        Run the actual simulation using asyncio.gather().



        Each agent perceives the ad via async calls to the LLM actor pool.

        Rate limiting is applied between batches.

        """
        logger.info("Broadcasting ad content to all agents...")

        # Qwen HF Space has no per-minute rate limits, but responses are slow
        # (~30-60s per request on free CPU). Using batch_size=3 with 2s delay.
        batch_size = 3
        batch_delay = 2
        all_states = []

        total_batches = (len(self.agents) + batch_size - 1) // batch_size
        logger.info(
            f"Processing {len(self.agents)} agents in {total_batches} "
            f"batches (Qwen via Ollama API)"
        )

        for i in range(0, len(self.agents), batch_size):
            batch = self.agents[i : i + batch_size]
            batch_num = (i // batch_size) + 1

            # Check for cancellation
            if redis_client:
                try:
                    if redis_client.get(f"sim:{self.experiment_id}:cancel"):
                        logger.warning(f"Simulation {self.experiment_id} was cancelled by user.")
                        raise Exception("Cancelled by user")
                except Exception as e:
                    if str(e) == "Cancelled by user":
                        raise e
                    logger.debug(f"Redis check failed: {e}")

            logger.info(
                f"Batch {batch_num}/{total_batches}: "
                f"Processing {len(batch)} agents..."
            )

            # Use asyncio.gather for concurrent processing within batch
            tasks = [agent.perceive_ad(ad_content) for agent in batch]
            batch_states = await asyncio.gather(*tasks, return_exceptions=True)

            # Collect results, handle any exceptions
            for j, state in enumerate(batch_states):
                if isinstance(state, Exception):
                    logger.error(
                        f"Agent {batch[j].agent_id} failed: {state}"
                    )
                    all_states.append(batch[j].get_state())
                else:
                    all_states.append(state)
                    logger.info(
                        f"  ✓ {state.get('agent_id','?')}: "
                        f"{state.get('emotion','?')} / {state.get('opinion_on_ad','?')} "
                        f"— {state.get('reasoning','')[:80]}"
                    )

            # Update progress
            progress = 20 + int(
                (i + len(batch)) / len(self.agents) * 60
            )
            agents_per_day = max(
                1, len(self.agents) // max(1, simulation_days)
            )
            current_day = min(
                (i // agents_per_day) + 1, simulation_days
            )
            self._update_progress(
                redis_client, progress, current_day, len(all_states)
            )

            logger.info(
                f"Processed {len(all_states)}/{len(self.agents)} agents"
            )

            # Rate limiting delay between batches
            if i + batch_size < len(self.agents):
                logger.info(f"Rate limit pause: {batch_delay}s...")
                await asyncio.sleep(batch_delay)

        agents_by_id = {a.agent_id: a for a in self.agents}

        # Simulate social influence over days
        for day in range(2, simulation_days + 1):
            if redis_client:
                try:
                    if redis_client.get(f"sim:{self.experiment_id}:cancel"):
                        logger.warning(f"Simulation {self.experiment_id} was cancelled by user.")
                        raise Exception("Cancelled by user")
                except Exception as e:
                    if str(e) == "Cancelled by user":
                        raise e
                    logger.debug(f"Redis check failed: {e}")
                    
            logger.info(f"Day {day} Social Loop - Messaging Phase")
            for agent in self.agents:
                if agent.has_seen_ad and agent.opinion_on_ad is not None:
                    message = await agent.generate_social_message()
                    for friend_id in agent.friends:
                        friend = agents_by_id.get(friend_id)
                        if friend:
                            friend.receive_peer_message(agent.agent_id, agent.opinion_on_ad, message)

            logger.info(f"Day {day} Social Loop - Deliberation Phase")
            deliberation_tasks = [agent.social_deliberation(ad_content) for agent in self.agents]
            await asyncio.gather(*deliberation_tasks, return_exceptions=True)

            if redis_client:
                try:
                    redis_client.setex(
                        f"sim:{self.experiment_id}:status",
                        300,
                        json.dumps({
                            "progress": int((day / simulation_days) * 100),
                            "current_day": day,
                            "active_agents": len(self.agents)
                        })
                    )
                except Exception as e:
                    logger.debug(f"Failed to update progress: {e}")

        # Get final states (direct calls — agents are local objects)
        self._update_progress(
            redis_client, 95, simulation_days, len(all_states)
        )
        final_states = [agent.get_state() for agent in self.agents]

        # Get event logs
        self.event_logs = []
        for agent in self.agents:
            self.event_logs.extend(agent.get_event_log())

        return final_states

    def _analyze_results(

        self, final_states: List[Dict[str, Any]]

    ) -> Dict[str, Any]:
        """Calculate engagement score, sentiment breakdown, and risk flags"""

        opinions = [
            s.get("opinion") for s in final_states if s.get("opinion")
        ]

        sentiment_counts = {
            "positive": sum(1 for o in opinions if o == "POSITIVE"),
            "neutral": sum(1 for o in opinions if o == "NEUTRAL"),
            "negative": sum(1 for o in opinions if o == "NEGATIVE"),
        }

        total = len(opinions) or 1

        # Engagement score: high if strong reactions (positive OR negative)
        strong_reactions = (
            sentiment_counts["positive"] + sentiment_counts["negative"]
        )
        engagement_score = (strong_reactions / total) * 100

        # Detect controversies
        risk_flags = self._detect_controversies(final_states)

        # Prepare agent logs for storage
        agent_logs = self.event_logs[:1000]

        # Extract opinion trajectory for top 50 agents
        opinion_trajectory = {}
        for state in final_states[:50]:
            agent_id = state.get("agent_id")
            opinion_history = state.get("opinion_history")
            if agent_id and opinion_history is not None:
                opinion_trajectory[agent_id] = opinion_history

        # Build map data (lightweight for map rendering)
        map_data = []
        for state in final_states:
            profile = state.get("profile", {})
            agent_id = state.get("agent_id", "")
            map_data.append({
                "agent_id": agent_id,
                "coordinates": profile.get("coordinates", [0, 0]),
                "opinion": state.get("opinion", "NEUTRAL"),
                "friends": self.social_network.get(agent_id, []),
            })

        # Build enriched agent states (for detail popups)
        agent_states = []
        for state in final_states:
            profile = state.get("profile", {})
            agent_states.append({
                "agent_id": state.get("agent_id", ""),
                "coordinates": profile.get("coordinates", [0, 0]),
                "opinion": state.get("opinion", "NEUTRAL"),
                "emotion": state.get("emotion", "neutral"),
                "emotion_intensity": state.get("emotion_intensity", 0),
                "reasoning": state.get("reasoning", ""),
                "friends": self.social_network.get(state.get("agent_id", ""), []),
                "profile": {
                    "name": profile.get("name"),
                    "age": profile.get("age"),
                    "gender": profile.get("gender"),
                    "location": profile.get("location"),
                    "occupation": profile.get("occupation"),
                    "education": profile.get("education"),
                    "income_level": profile.get("income_level"),
                    "religion": profile.get("religion"),
                    "ethnicity": profile.get("ethnicity"),
                    "social_media_usage": profile.get("social_media_usage"),
                    "political_leaning": profile.get("political_leaning"),
                    "personality_traits": profile.get("personality_traits", []),
                    "values": profile.get("values", []),
                },
            })

        return {
            "engagement_score": round(engagement_score, 2),
            "sentiment_breakdown": sentiment_counts,
            "total_agents": len(final_states),
            "responding_agents": len(opinions),
            "risk_flags": risk_flags,
            "agent_logs": agent_logs,
            "map_data": map_data,
            "agent_states": agent_states,
            "opinion_trajectory": opinion_trajectory,
        }

    def _detect_controversies(

        self, final_states: List[Dict[str, Any]]

    ) -> List[Dict[str, Any]]:
        """Identify controversial reactions by demographic segment"""
        flags = []

        # Add an 'overall' bucket to catch widespread backlash regardless of demographic
        groups = {"overall": {"All Agents": []}, "age": {}, "gender": {}, "location": {}, "values": {}}

        for state in final_states:
            profile = state.get("profile", {})
            opinion = state.get("opinion")
            if not opinion:
                continue

            # Overall
            groups["overall"]["All Agents"].append(state)

            # Age groups
            age = profile.get("age", 0)
            age_bracket = f"{(age // 10) * 10}-{(age // 10) * 10 + 9}"
            groups["age"].setdefault(age_bracket, []).append(state)

            # Gender
            gender = profile.get("gender", "Unknown")
            groups["gender"].setdefault(gender, []).append(state)

            # Location
            location = profile.get("location", "Unknown")
            groups["location"].setdefault(location, []).append(state)

            # Values
            for value in profile.get("values", []):
                groups["values"].setdefault(value, []).append(state)

        total_responding = len(groups["overall"]["All Agents"])
        min_group_size = 5 if total_responding >= 20 else 1

        overall_states = groups["overall"]["All Agents"]
        overall_negative_rate = sum(1 for s in overall_states if s.get("opinion") == "NEGATIVE") / max(1, total_responding)

        seen_reasonings = set()

        for group_type, group_data in groups.items():
            for group_name, states in group_data.items():
                if len(states) < min_group_size and group_type != "overall":
                    continue

                negative_count = sum(1 for s in states if s.get("opinion") == "NEGATIVE")
                total = len(states)
                negative_rate = negative_count / max(1, total)

                # Skip if not generally negative
                if negative_rate < 0.5:
                    continue

                # If this is a subset demographic, only flag it distinctively if it is notably worse than the general population.
                # If the general population already hates it (e.g. overall = 90%), we don't need to report every single trait individually.
                if group_type != "overall" and negative_rate < (overall_negative_rate + 0.15):
                    continue

                if negative_rate > 0.8:
                    severity = "CRITICAL"
                elif negative_rate > 0.7:
                    severity = "HIGH"
                elif negative_rate > 0.6:
                    severity = "MEDIUM"
                else:
                    severity = "LOW"

                sample_reactions = []
                for s in states:
                    if s.get("opinion") == "NEGATIVE":
                        reasoning = s.get("reasoning", "")[:120]
                        if reasoning and reasoning not in seen_reasonings:
                            sample_reactions.append({
                                "agent_id": s.get("agent_id"),
                                "reasoning": reasoning
                            })
                            seen_reasonings.add(reasoning)
                    if len(sample_reactions) >= 3:
                        break

                # Fallback if no uniquely new reasonings exist
                if not sample_reactions:
                    sample_reactions = [
                        {"agent_id": s.get("agent_id"), "reasoning": s.get("reasoning", "")[:120]}
                        for s in states if s.get("opinion") == "NEGATIVE"
                    ][:3]

                flags.append({
                    "flag_type": f"{group_type.upper()}_BACKLASH",
                    "severity": severity,
                    "description": f"{int(negative_rate * 100)}% of {group_type}={group_name} reacted negatively",
                    "affected_demographics": {group_type: group_name},
                    "sample_agent_reactions": sample_reactions,
                })

        severity_order = {"CRITICAL": 0, "HIGH": 1, "MEDIUM": 2, "LOW": 3}
        flags.sort(key=lambda x: severity_order.get(x["severity"], 4))

        return flags[:10]

    def _update_progress(

        self, redis_client, progress: int, current_day: int,

        active_agents: int

    ):
        """Update progress in Redis for frontend polling"""
        if redis_client:
            try:
                status = {
                    "progress": progress,
                    "current_day": current_day,
                    "active_agents": active_agents,
                }
                redis_client.setex(
                    f"sim:{self.experiment_id}:status",
                    60,
                    json.dumps(status),
                )
            except Exception as e:
                logger.debug(f"Failed to update progress: {e}")

    def _cleanup(self):
        """Clean up resources"""
        self.agents = []
        shutdown_llm_pool()
        shutdown_ray()


async def run_simulation_async(

    experiment_id: str,

    ad_content: str,

    demographic_filter: Optional[Dict[str, Any]] = None,

    num_agents: int = 10,

    simulation_days: int = 5,

    redis_client=None,

    custom_agent_profiles: Optional[List[Dict]] = None,

    use_custom_agents_only: bool = False,

) -> Dict[str, Any]:
    """

    Async convenience function to run a simulation.

    """
    mqtt_host = _clean_env("MQTT_BROKER_HOST", "localhost")
    mqtt_port = int(_clean_env("MQTT_BROKER_PORT", "1883"))
    mqtt_transport = _clean_env("MQTT_TRANSPORT", "tcp")
    mqtt_path = _clean_env("MQTT_PATH", "") or None
    chroma_host = _clean_env("CHROMA_HOST", "localhost")
    chroma_port = int(_clean_env("CHROMA_PORT", "8000"))
    chroma_ssl = _clean_env("CHROMA_SSL", "False").lower() in ("true", "1", "yes")

    orchestrator = SimulationOrchestrator(
        experiment_id=experiment_id,
        num_agents=num_agents,
        mqtt_host=mqtt_host,
        mqtt_port=mqtt_port,
        mqtt_transport=mqtt_transport,
        mqtt_path=mqtt_path,
        chroma_host=chroma_host,
        chroma_port=chroma_port,
        chroma_ssl=chroma_ssl,
    )

    return await orchestrator.run(
        ad_content=ad_content,
        demographic_filter=demographic_filter,
        simulation_days=simulation_days,
        redis_client=redis_client,
        custom_agent_profiles=custom_agent_profiles,
        use_custom_agents_only=use_custom_agents_only,
    )


def run_simulation(

    experiment_id: str,

    ad_content: str,

    demographic_filter: Optional[Dict[str, Any]] = None,

    num_agents: int = 10,

    simulation_days: int = 5,

    redis_client=None,

    custom_agent_profiles: Optional[List[Dict]] = None,

    use_custom_agents_only: bool = False,

) -> Dict[str, Any]:
    """

    Synchronous convenience function to run a simulation.

    Called from Celery task or test scripts.

    """
    return asyncio.run(
        run_simulation_async(
            experiment_id=experiment_id,
            ad_content=ad_content,
            demographic_filter=demographic_filter,
            num_agents=num_agents,
            simulation_days=simulation_days,
            redis_client=redis_client,
            custom_agent_profiles=custom_agent_profiles,
            use_custom_agents_only=use_custom_agents_only,
        )
    )