Skip to content

Commit

Permalink
added participant_condition_sampling attribute to GameSetup for repea…
Browse files Browse the repository at this point in the history
…ted games
  • Loading branch information
phelps-sg committed Nov 10, 2023
1 parent b5c4871 commit f0f1b0e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
8 changes: 4 additions & 4 deletions llm_cooperation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,18 @@ def settings_from_combinations(


def randomized(n: int, grid: Grid) -> Iterable[Settings]:
keys = list(grid.keys())
variables = list(grid.keys())
combinations = list(all_combinations(grid))
num_combinations = len(combinations)
for __i__ in range(n):
random_index: int = int(np.random.randint(num_combinations))
yield settings_from_combinations(keys, combinations[random_index])
yield settings_from_combinations(variables, combinations[random_index])


def exhaustive(grid: Grid) -> Iterable[Settings]:
keys = list(grid.keys())
variables = list(grid.keys())
for values in all_combinations(grid):
yield settings_from_combinations(keys, values)
yield settings_from_combinations(variables, values)


def amount_as_str(amount: float) -> str:
Expand Down
3 changes: 2 additions & 1 deletion llm_cooperation/experiments/dilemma.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np
from openai_pygenerator import Completion

from llm_cooperation import ModelSetup, Payoffs, Settings
from llm_cooperation import ModelSetup, Payoffs, Settings, exhaustive
from llm_cooperation.experiments import AI_PARTICIPANTS, run_and_record_experiment
from llm_cooperation.gametypes import simultaneous
from llm_cooperation.gametypes.repeated import (
Expand Down Expand Up @@ -196,6 +196,7 @@ def run(model_setup: ModelSetup, sample_size: int) -> RepeatedGameResults:
extract_choice=extract_choice_pd,
next_round=simultaneous.next_round,
analyse_rounds=simultaneous.analyse_rounds,
participant_condition_sampling=exhaustive,
model_setup=model_setup,
)
measurement_setup: MeasurementSetup[DilemmaChoice] = MeasurementSetup(
Expand Down
3 changes: 2 additions & 1 deletion llm_cooperation/experiments/ultimatum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
from openai_pygenerator import Completion, content, user_message

from llm_cooperation import ModelSetup, Payoffs, Settings, amount_as_str
from llm_cooperation import ModelSetup, Payoffs, Settings, amount_as_str, exhaustive
from llm_cooperation.experiments import AI_PARTICIPANTS, run_and_record_experiment
from llm_cooperation.gametypes import alternating
from llm_cooperation.gametypes.repeated import (
Expand Down Expand Up @@ -199,6 +199,7 @@ def run(model_setup: ModelSetup, sample_size: int) -> RepeatedGameResults:
next_round=next_round_ultimatum,
analyse_rounds=alternating.analyse_rounds,
model_setup=model_setup,
participant_condition_sampling=exhaustive,
)
measurement_setup: MeasurementSetup[UltimatumChoice] = MeasurementSetup(
num_samples=sample_size,
Expand Down
6 changes: 4 additions & 2 deletions llm_cooperation/gametypes/repeated.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
Payoffs,
Results,
Settings,
exhaustive,
)
from llm_cooperation.gametypes import PromptGenerator, start_game

Expand Down Expand Up @@ -53,6 +52,7 @@ class GameSetup(Generic[CT, RT]):
payoffs: PayoffFunction[CT]
extract_choice: ChoiceExtractor[CT]
model_setup: ModelSetup
participant_condition_sampling: Callable[[Grid], Iterable[Settings]]


@dataclass(frozen=True)
Expand Down Expand Up @@ -259,7 +259,9 @@ def run_experiment(
)
for group, participants in ai_participants.items()
for participant in participants
for participant_condition in exhaustive(participant_conditions)
for participant_condition in game_setup.participant_condition_sampling(
participant_conditions
)
for strategy_name, strategy_fn in partner_conditions.items()
for score, freq, choices, history in generate_samples(
participant=participant,
Expand Down
3 changes: 2 additions & 1 deletion tests/test_dilemma.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from openai_pygenerator import Completion, logger

from llm_cooperation import DEFAULT_MODEL_SETUP, Payoffs
from llm_cooperation import DEFAULT_MODEL_SETUP, Payoffs, exhaustive
from llm_cooperation.experiments.dilemma import (
COLOR_COOPERATE,
COLOR_DEFECT,
Expand Down Expand Up @@ -141,6 +141,7 @@ def test_run_repeated_game(mocker):
payoffs=payoffs_pd,
extract_choice=extract_choice_pd,
model_setup=DEFAULT_MODEL_SETUP,
participant_condition_sampling=exhaustive,
),
)
)
Expand Down
5 changes: 4 additions & 1 deletion tests/test_repeated.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from openai_pygenerator import content, user_message

from llm_cooperation import DEFAULT_MODEL_SETUP, Choice, Grid, Group
from llm_cooperation import DEFAULT_MODEL_SETUP, Choice, Grid, Group, exhaustive
from llm_cooperation.experiments.dilemma import (
Cooperate,
Defect,
Expand Down Expand Up @@ -63,6 +63,7 @@ def test_play_game(mocker):
extract_choice=extract_choice_mock,
generate_instruction_prompt=prompt_generator_mock,
model_setup=DEFAULT_MODEL_SETUP,
participant_condition_sampling=exhaustive,
),
)

Expand All @@ -87,6 +88,7 @@ def test_play_game(mocker):
generate_instruction_prompt=prompt_generator_mock,
num_rounds=n,
model_setup=DEFAULT_MODEL_SETUP,
participant_condition_sampling=exhaustive,
),
),
"test-prompt",
Expand Down Expand Up @@ -168,6 +170,7 @@ def test_run_experiment(mocker):
next_round=simultaneous.next_round,
analyse_rounds=simultaneous.analyse_rounds,
model_setup=DEFAULT_MODEL_SETUP,
participant_condition_sampling=exhaustive,
),
).to_df()
num_participant_conditions = len(participant_conditions["chain_of_thought"])
Expand Down

0 comments on commit f0f1b0e

Please sign in to comment.