2525import re
2626from dataclasses import dataclass
2727from enum import Enum , auto
28- from functools import partial
28+ from functools import lru_cache , partial
2929from typing import List , Optional , Tuple
3030
3131import numpy as np
7272CONDITION_ROLE = "role"
7373CONDITION_GROUP = "group"
7474
75+ SEED_VALUE = 101 # Ensure same participants are used across all experiments
76+
7577logger = logging .getLogger (__name__ )
7678
7779
@@ -295,6 +297,30 @@ def compute_freq_pd(choices: List[Choices[DilemmaChoice]]) -> float:
295297 return len ([c for c in choices if c .ai == Cooperate ]) / len (choices )
296298
297299
300+ @lru_cache ()
301+ def create_participants (num_participant_samples : int ) -> List [Participant ]:
302+ participant_conditions = GROUP_PROMPT_CONDITIONS
303+ random_attributes : Grid = {
304+ CONDITION_CHAIN_OF_THOUGHT : [True , False ],
305+ CONDITION_LABEL : all_values (Label ),
306+ CONDITION_CASE : all_values (Case ),
307+ CONDITION_PRONOUN : all_values (Pronoun ),
308+ CONDITION_DEFECT_FIRST : [True , False ],
309+ CONDITION_LABELS_REVERSED : [True , False ],
310+ }
311+ result = list (
312+ participants (
313+ participant_conditions ,
314+ random_attributes ,
315+ num_participant_samples ,
316+ seed = SEED_VALUE ,
317+ )
318+ )
319+ for i , participant in enumerate (result ):
320+ participant ["id" ] = i
321+ return result
322+
323+
298324def run (
299325 model_setup : ModelSetup , num_replications : int , num_participant_samples : int
300326) -> RepeatedGameResults :
@@ -311,19 +337,8 @@ def run(
311337 num_replications = num_replications ,
312338 compute_freq = compute_freq_pd ,
313339 )
314- participant_conditions = GROUP_PROMPT_CONDITIONS
315- random_attributes : Grid = {
316- CONDITION_CHAIN_OF_THOUGHT : [True , False ],
317- CONDITION_LABEL : all_values (Label ),
318- CONDITION_CASE : all_values (Case ),
319- CONDITION_PRONOUN : all_values (Pronoun ),
320- CONDITION_DEFECT_FIRST : [True , False ],
321- CONDITION_LABELS_REVERSED : [True , False ],
322- }
323340 return run_experiment (
324- participants = participants (
325- participant_conditions , random_attributes , num_participant_samples
326- ),
341+ participants = iter (create_participants (num_participant_samples )),
327342 partner_conditions = {
328343 "unconditional cooperate" : strategy_cooperate ,
329344 "unconditional defect" : strategy_defect ,
0 commit comments