Skip to content

Commit 0c22baa

Browse files
committed
reuse same participants across all experiments
1 parent c49d930 commit 0c22baa

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

llm_cooperation/experiments/dilemma.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import re
2626
from dataclasses import dataclass
2727
from enum import Enum, auto
28-
from functools import partial
28+
from functools import lru_cache, partial
2929
from typing import List, Optional, Tuple
3030

3131
import numpy as np
@@ -72,6 +72,8 @@
7272
CONDITION_ROLE = "role"
7373
CONDITION_GROUP = "group"
7474

75+
SEED_VALUE = 101 # Ensure same participants are used across all experiments
76+
7577
logger = 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+
298324
def 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

Comments
 (0)