From f79150625c1ea66990fc942bb0f8d14e2866bf17 Mon Sep 17 00:00:00 2001 From: stephanebonnevay <stephane.bonnevay@lizeo-group.com> Date: Wed, 4 Jun 2025 15:26:34 +0200 Subject: [PATCH] Match Pennies --- data/mp/{mp.csv => mp_.csv} | 0 src/mp/mp.py | 70 ++++++++++++++++++++++++++++++------- src/mp/mp_draw_constant.py | 2 +- src/mp/mp_experiments.py | 2 +- 4 files changed, 60 insertions(+), 14 deletions(-) rename data/mp/{mp.csv => mp_.csv} (100%) diff --git a/data/mp/mp.csv b/data/mp/mp_.csv similarity index 100% rename from data/mp/mp.csv rename to data/mp/mp_.csv diff --git a/src/mp/mp.py b/src/mp/mp.py index 1f994de..abc4153 100644 --- a/src/mp/mp.py +++ b/src/mp/mp.py @@ -15,7 +15,7 @@ from autogen_ext.models.openai import OpenAIChatCompletionClient # Load API keys from environment variables ################### OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") -OPENAI_API_KEY = "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" +OPENAI_API_KEY = "cle" ################### PAGODA_API_KEY = os.getenv("PAGODA_API_KEY") PAGODA_API_KEY = "cle" @@ -24,7 +24,7 @@ if not OPENAI_API_KEY: if not PAGODA_API_KEY: raise ValueError("Missing PAGODA_API_KEY. Set it as an environment variable.") -CSV_FILE_PATH = "../../data/mp/mp.csv" +CSV_FILE_PATH = "../../data/mp/mp_.csv" # Define the expected response format as a Pydantic model class AgentResponse(BaseModel): @@ -201,14 +201,60 @@ class MP: def apply_strategy(self): """Play the next move using a heuristic.""" - opponent_move = self.opponent_strategy_fn(self.history) - # Default: at random - move = random.choice(["Head", "Tail"]) - reasoning = "Choosing randomly." + prediction = "" + print(self.model) + + if self.model == "gpt-4.5-preview-2025-02-27": + """ + Play the next move using a heuristic. + - Simple strategy: Alternate between "Head" and "Tail". + - Prediction: Assume opponent is also alternating. + """ + moves = ["Head", "Tail"] + + # If no rounds played, start with "Head" + if not self.history: + move = "Head" + prediction = "Tail" if self.prediction else "None" + reasoning = ( + "First round, no history. Starting with 'Head'. " + + (f"Predicting opponent starts with 'Tail' (alternating)." if self.prediction else "") + ) + return move, prediction, reasoning + + # Get last move you played and the opponent's last move + last_agent_move = self.history[-1]["Agent Move"] + last_opponent_move = self.history[-1]["Opponent Move"] + + # Alternate your move from last time + move = "Tail" if last_agent_move == "Head" else "Head" + + # Prediction: Suppose opponent is also alternating + if self.prediction: + prediction = "Tail" if last_opponent_move == "Head" else "Head" + else: + prediction = "None" + + reasoning = ( + f"Last round I played {last_agent_move}, opponent played {last_opponent_move}. " + f"I'm alternating my move to '{move}'. " + + ( + f"Predicting opponent will alternate to '{prediction}'." + if self.prediction else "No prediction mode." + ) + ) + return move, prediction, reasoning - outcome = self.determine_winner(move, opponent_move) - self.update_score(outcome) # Use the correct outcome here - return move, reasoning + else: + opponent_move = self.opponent_strategy_fn(self.history) + # Default: at random + move = random.choice(["Head", "Tail"]) + reasoning = "Choosing randomly." + + outcome = self.determine_winner(move, opponent_move) + self.update_score(outcome) # Use the correct outcome here + + return move, prediction, reasoning @staticmethod def determine_winner(player_move: str, opponent_move: str) -> int: @@ -243,12 +289,12 @@ class MP: # Runner async def main(): game = MP( - model="qwen3", # "gpt-4.5-preview-2025-02-27", "qwen3", "llama3", "llama3.3", "mixtral", "mistral-small", "deepseek-r1" + model="gpt-4.5-preview-2025-02-27", # "gpt-4.5-preview-2025-02-27", "qwen3", "llama3", "llama3.3", "mixtral", "mistral-small", "deepseek-r1" temperature=0.7, game_id=1, prediction=True, - opponent_strategy_fn=lambda history: "Tail", - strategy=False # or True for rule-based + opponent_strategy_fn=lambda history: "Tail" if len(history) % 2 == 0 else "Head", + strategy=True # or True for rule-based ) num_rounds = 10 for round_id in range(1, num_rounds + 1): diff --git a/src/mp/mp_draw_constant.py b/src/mp/mp_draw_constant.py index 181122c..ec7a7f1 100644 --- a/src/mp/mp_draw_constant.py +++ b/src/mp/mp_draw_constant.py @@ -4,7 +4,7 @@ import matplotlib.pyplot as plt import numpy as np # Path to the CSV file -CSV_FILE_PATH = "../../data/mp/mp.csv" +CSV_FILE_PATH = "../../data/mp/mp_.csv" FIGURE_DIR = "../../figures/mp" os.makedirs(FIGURE_DIR, exist_ok=True) diff --git a/src/mp/mp_experiments.py b/src/mp/mp_experiments.py index c1ea57e..87a22b2 100644 --- a/src/mp/mp_experiments.py +++ b/src/mp/mp_experiments.py @@ -7,7 +7,7 @@ from http.cookiejar import debug from mp import MP from typing import Callable -CSV_FILE_PATH = "../../data/mp/mp.csv" +CSV_FILE_PATH = "../../data/mp/mp_.csv" class MPExperiment: def __init__(self): -- GitLab