Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- src_code_for_reproducibility/__pycache__/__init__.cpython-311.pyc +0 -0
- src_code_for_reproducibility/__pycache__/__init__.cpython-312.pyc +0 -0
- src_code_for_reproducibility/chat_utils/apply_template.py +84 -0
- src_code_for_reproducibility/chat_utils/chat_turn.py +27 -0
- src_code_for_reproducibility/chat_utils/template_specific.py +109 -0
- src_code_for_reproducibility/docs/Makefile +19 -0
- src_code_for_reproducibility/docs/generate_docs.py +249 -0
- src_code_for_reproducibility/docs/make.bat +35 -0
- src_code_for_reproducibility/markov_games/__init__.py +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/__init__.cpython-311.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/__init__.cpython-312.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/agent.cpython-312.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/alternative_actions_runner.cpython-312.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/gather_and_export_utils.cpython-312.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/group_timesteps.cpython-312.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/linear_runner.cpython-312.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/markov_game.cpython-312.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/mg_utils.cpython-312.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/rollout_tree.cpython-311.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/rollout_tree.cpython-312.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/run_markov_games.cpython-312.pyc +0 -0
- src_code_for_reproducibility/markov_games/__pycache__/simulation.cpython-312.pyc +0 -0
- src_code_for_reproducibility/markov_games/linear_runner.py +30 -0
- src_code_for_reproducibility/markov_games/negotiation/dond_agent.py +61 -0
- src_code_for_reproducibility/markov_games/negotiation/dond_simulation.py +153 -0
- src_code_for_reproducibility/markov_games/negotiation/nego_agent.py +242 -0
- src_code_for_reproducibility/markov_games/negotiation/nego_simulation.py +241 -0
- src_code_for_reproducibility/markov_games/negotiation/negotiation_statistics.py +244 -0
- src_code_for_reproducibility/markov_games/negotiation/no_press_nego_agent.py +94 -0
- src_code_for_reproducibility/markov_games/negotiation/no_press_nego_simulation.py +168 -0
- src_code_for_reproducibility/markov_games/negotiation/tas_agent.py +108 -0
- src_code_for_reproducibility/markov_games/negotiation/tas_rps_agent.py +118 -0
- src_code_for_reproducibility/markov_games/negotiation/tas_rps_simulation.py +248 -0
- src_code_for_reproducibility/markov_games/negotiation/tas_simple_agent.py +90 -0
- src_code_for_reproducibility/markov_games/negotiation/tas_simple_simulation.py +169 -0
- src_code_for_reproducibility/markov_games/negotiation/tas_simulation.py +172 -0
- src_code_for_reproducibility/markov_games/run_markov_games.py +24 -0
- src_code_for_reproducibility/markov_games/vine_ppo.py +10 -0
- src_code_for_reproducibility/models/__pycache__/__init__.cpython-312.pyc +0 -0
- src_code_for_reproducibility/models/__pycache__/adapter_training_wrapper.cpython-312.pyc +0 -0
- src_code_for_reproducibility/models/__pycache__/human_policy.cpython-312.pyc +0 -0
- src_code_for_reproducibility/models/__pycache__/inference_backend.cpython-312.pyc +0 -0
- src_code_for_reproducibility/models/__pycache__/inference_backend_dummy.cpython-312.pyc +0 -0
- src_code_for_reproducibility/models/__pycache__/inference_backend_sglang.cpython-312.pyc +0 -0
- src_code_for_reproducibility/models/__pycache__/inference_backend_vllm.cpython-312.pyc +0 -0
- src_code_for_reproducibility/models/__pycache__/large_language_model_api.cpython-312.pyc +0 -0
- src_code_for_reproducibility/models/__pycache__/large_language_model_local.cpython-312.pyc +0 -0
- src_code_for_reproducibility/models/__pycache__/scalar_critic.cpython-312.pyc +0 -0
- src_code_for_reproducibility/training/__pycache__/__init__.cpython-312.pyc +0 -0
- src_code_for_reproducibility/training/__pycache__/annealing_methods.cpython-312.pyc +0 -0
src_code_for_reproducibility/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (160 Bytes). View file
|
|
|
src_code_for_reproducibility/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (148 Bytes). View file
|
|
|
src_code_for_reproducibility/chat_utils/apply_template.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
from mllm.chat_utils.chat_turn import ChatTurn
|
| 4 |
+
from mllm.chat_utils.template_specific import (
|
| 5 |
+
custom_gemma3_template,
|
| 6 |
+
custom_llama3_template,
|
| 7 |
+
custom_qwen2_template,
|
| 8 |
+
custom_qwen3_template,
|
| 9 |
+
gemma3_assistant_postfix,
|
| 10 |
+
qwen2_assistant_postfix,
|
| 11 |
+
qwen3_assistant_postfix,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def get_custom_chat_template(tokenizer) -> str:
|
| 16 |
+
"""
|
| 17 |
+
Get the chat template for the tokenizer.
|
| 18 |
+
"""
|
| 19 |
+
if "qwen2" in tokenizer.name_or_path.lower():
|
| 20 |
+
return custom_qwen2_template
|
| 21 |
+
elif "llama" in tokenizer.name_or_path.lower():
|
| 22 |
+
return custom_llama3_template
|
| 23 |
+
elif "qwen3" in tokenizer.name_or_path.lower():
|
| 24 |
+
return custom_qwen3_template
|
| 25 |
+
elif "gemma" in tokenizer.name_or_path.lower():
|
| 26 |
+
return custom_gemma3_template
|
| 27 |
+
else:
|
| 28 |
+
raise ValueError(f"Tokenizer {tokenizer.name_or_path} not supported")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def get_custom_assistant_postfix(tokenizer) -> torch.Tensor:
|
| 32 |
+
"""
|
| 33 |
+
Get the custom assistant postfix for the tokenizer.
|
| 34 |
+
"""
|
| 35 |
+
if "qwen2" in tokenizer.name_or_path.lower():
|
| 36 |
+
return qwen2_assistant_postfix
|
| 37 |
+
elif "qwen3" in tokenizer.name_or_path.lower():
|
| 38 |
+
return qwen3_assistant_postfix
|
| 39 |
+
elif "gemma" in tokenizer.name_or_path.lower():
|
| 40 |
+
return gemma3_assistant_postfix
|
| 41 |
+
return torch.tensor([], dtype=torch.long)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def tokenize_chats(chats: list[ChatTurn], tokenizer, enable_thinking) -> None:
|
| 45 |
+
"""
|
| 46 |
+
Set the chat_template_token_ids for each chat turn.
|
| 47 |
+
# TODO: use engine tokens if available
|
| 48 |
+
"""
|
| 49 |
+
custom_template = get_custom_chat_template(tokenizer)
|
| 50 |
+
custom_assistant_postfix: torch.Tensor = get_custom_assistant_postfix(tokenizer)
|
| 51 |
+
for i, chat in enumerate(chats):
|
| 52 |
+
if chat.chat_template_token_ids is None:
|
| 53 |
+
if chat.role == "user":
|
| 54 |
+
next_chat = chats[i + 1] if i + 1 < len(chats) else None
|
| 55 |
+
add_generation_prompt = True
|
| 56 |
+
if next_chat and next_chat.role == "user":
|
| 57 |
+
add_generation_prompt = False
|
| 58 |
+
encoded_chat = tokenizer.apply_chat_template(
|
| 59 |
+
[chat],
|
| 60 |
+
return_tensors="pt",
|
| 61 |
+
chat_template=custom_template,
|
| 62 |
+
add_generation_prompt=add_generation_prompt,
|
| 63 |
+
add_system_prompt=True if i == 0 else False,
|
| 64 |
+
enable_thinking=enable_thinking,
|
| 65 |
+
).flatten()
|
| 66 |
+
previous_chat = chats[i - 1] if i > 0 else None
|
| 67 |
+
if previous_chat and previous_chat.role == "assistant":
|
| 68 |
+
encoded_chat = torch.cat([custom_assistant_postfix, encoded_chat])
|
| 69 |
+
elif chat.role == "assistant":
|
| 70 |
+
encoded_chat = chat.out_token_ids
|
| 71 |
+
chat.chat_template_token_ids = encoded_chat
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def chat_turns_to_token_ids(
|
| 75 |
+
chats: list[ChatTurn], tokenizer, enable_thinking
|
| 76 |
+
) -> list[int]:
|
| 77 |
+
"""
|
| 78 |
+
Tokenize the chat turns and set the chat_template_token_ids for each chat turn.
|
| 79 |
+
"""
|
| 80 |
+
tokenize_chats(chats=chats, tokenizer=tokenizer, enable_thinking=enable_thinking)
|
| 81 |
+
token_ids = []
|
| 82 |
+
for chat in chats:
|
| 83 |
+
token_ids.append(chat.chat_template_token_ids)
|
| 84 |
+
return torch.cat(token_ids)
|
src_code_for_reproducibility/chat_utils/chat_turn.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from typing import Any, List, Literal, Optional, Tuple
|
| 7 |
+
|
| 8 |
+
import jsonschema
|
| 9 |
+
import torch
|
| 10 |
+
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
| 11 |
+
|
| 12 |
+
AgentId = str
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class ChatTurn(BaseModel):
|
| 16 |
+
model_config = ConfigDict(arbitrary_types_allowed=True) # needed for torch tensors
|
| 17 |
+
|
| 18 |
+
role: str = Field(pattern="^(user|assistant)$")
|
| 19 |
+
agent_id: AgentId # ID of the agent with which the chat occured
|
| 20 |
+
content: str
|
| 21 |
+
reasoning_content: str | None = None
|
| 22 |
+
chat_template_token_ids: torch.LongTensor | None = None # Token ids of chat template format. For example, token ids of "<assistant>{content}</assistant>""
|
| 23 |
+
out_token_ids: torch.LongTensor | None = (
|
| 24 |
+
None # tokens generated from inference engine
|
| 25 |
+
)
|
| 26 |
+
log_probs: torch.FloatTensor | None = None
|
| 27 |
+
is_state_end: bool = False # indicates whether this chat turn marks the end of a state in the trajectory
|
src_code_for_reproducibility/chat_utils/template_specific.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import huggingface_hub
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
|
| 5 |
+
custom_llama3_template = """
|
| 6 |
+
{%- if add_system_prompt %}
|
| 7 |
+
{{- '<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nCutting Knowledge Date: December 2023\nToday Date: 26 Jul 2024\n\n<|eot_id|>' }}
|
| 8 |
+
{%- endif %}
|
| 9 |
+
{%- for message in messages %}
|
| 10 |
+
{{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] | trim + '<|eot_id|>' }}
|
| 11 |
+
{%- endfor %}
|
| 12 |
+
|
| 13 |
+
{%- if add_generation_prompt %}
|
| 14 |
+
{{- '<|start_header_id|>' + 'assistant' + '<|end_header_id|>\n\n' }}
|
| 15 |
+
{%- endif %}
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
qwen2_assistant_postfix = (
|
| 19 |
+
AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct")
|
| 20 |
+
.encode("\n", return_tensors="pt")
|
| 21 |
+
.flatten()
|
| 22 |
+
)
|
| 23 |
+
qwen3_assistant_postfix = (
|
| 24 |
+
AutoTokenizer.from_pretrained("Qwen/Qwen3-8B")
|
| 25 |
+
.encode("\n", return_tensors="pt")
|
| 26 |
+
.flatten()
|
| 27 |
+
)
|
| 28 |
+
gemma3_assistant_postfix = (
|
| 29 |
+
AutoTokenizer.from_pretrained("google/gemma-3-4b-it")
|
| 30 |
+
.encode("\n", return_tensors="pt")
|
| 31 |
+
.flatten()
|
| 32 |
+
)
|
| 33 |
+
custom_qwen2_template = """
|
| 34 |
+
{%- if add_system_prompt %}
|
| 35 |
+
{{- '<|im_start|>system\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n' }}
|
| 36 |
+
{%- endif %}
|
| 37 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
| 38 |
+
{%- for message in messages %}
|
| 39 |
+
{%- if message.content is string %}
|
| 40 |
+
{%- set content = message.content %}
|
| 41 |
+
{%- else %}
|
| 42 |
+
{%- set content = '' %}
|
| 43 |
+
{%- endif %}
|
| 44 |
+
{%- if (message.role == "user") %}
|
| 45 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 46 |
+
{%- elif message.role == "assistant" %}
|
| 47 |
+
{%- set reasoning_content = '' %}
|
| 48 |
+
{%- if message.reasoning_content is string %}
|
| 49 |
+
{%- set reasoning_content = message.reasoning_content %}
|
| 50 |
+
{%- else %}
|
| 51 |
+
{%- if '</think>' in content %}
|
| 52 |
+
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
|
| 53 |
+
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
|
| 54 |
+
{%- endif %}
|
| 55 |
+
{%- endif %}
|
| 56 |
+
{%- if loop.index0 > ns.last_query_index %}
|
| 57 |
+
{%- if reasoning_content %}
|
| 58 |
+
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
|
| 59 |
+
{%- else %}
|
| 60 |
+
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 61 |
+
{%- endif %}
|
| 62 |
+
{%- else %}
|
| 63 |
+
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 64 |
+
{%- endif %}
|
| 65 |
+
{{- '<|im_end|>\n' }}
|
| 66 |
+
{%- endif %}
|
| 67 |
+
{%- endfor %}
|
| 68 |
+
{%- if add_generation_prompt %}
|
| 69 |
+
{{- '<|im_start|>assistant\n' }}
|
| 70 |
+
{%- endif %}
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
custom_qwen3_template = """
|
| 74 |
+
{%- for message in messages %}
|
| 75 |
+
{%- if message.content is string %}
|
| 76 |
+
{%- set content = message.content %}
|
| 77 |
+
{%- else %}
|
| 78 |
+
{%- set content = '' %}
|
| 79 |
+
{%- endif %}
|
| 80 |
+
{%- if (message.role == "user") %}
|
| 81 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 82 |
+
{%- elif message.role == "assistant" %}
|
| 83 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 84 |
+
{%- endif %}
|
| 85 |
+
{%- endfor %}
|
| 86 |
+
{%- if add_generation_prompt %}
|
| 87 |
+
{{- '<|im_start|>assistant\n' }}
|
| 88 |
+
{%- if enable_thinking is defined and enable_thinking is false %}
|
| 89 |
+
{{- '<think>\n\n</think>\n\n' }}
|
| 90 |
+
{%- endif %}
|
| 91 |
+
{%- endif %}
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
custom_gemma3_template = """
|
| 95 |
+
{%- if add_system_prompt %}
|
| 96 |
+
{{- bos_token -}}
|
| 97 |
+
{%- endif %}
|
| 98 |
+
{%- for message in messages -%}
|
| 99 |
+
{%- if message['role'] == 'assistant' -%}
|
| 100 |
+
{%- set role = 'model' -%}
|
| 101 |
+
{%- else -%}
|
| 102 |
+
{%- set role = message['role'] -%}
|
| 103 |
+
{%- endif -%}
|
| 104 |
+
{{ '<start_of_turn>' + role + '\n' + message['content'] | trim + '<end_of_turn>\n' }}
|
| 105 |
+
{%- endfor -%}
|
| 106 |
+
{%- if add_generation_prompt -%}
|
| 107 |
+
{{ '<start_of_turn>model\n' }}
|
| 108 |
+
{%- endif -%}
|
| 109 |
+
"""
|
src_code_for_reproducibility/docs/Makefile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Minimal makefile for Sphinx documentation
|
| 2 |
+
|
| 3 |
+
# You can set these variables from the command line, and also
|
| 4 |
+
# from the environment for the first two.
|
| 5 |
+
SPHINXOPTS ?=
|
| 6 |
+
SPHINXBUILD ?= sphinx-build
|
| 7 |
+
SOURCEDIR = source
|
| 8 |
+
BUILDDIR = build
|
| 9 |
+
|
| 10 |
+
# Put it first so that "make" without argument is like "make help".
|
| 11 |
+
help:
|
| 12 |
+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(SPHINXFLAGS)
|
| 13 |
+
|
| 14 |
+
.PHONY: help Makefile
|
| 15 |
+
|
| 16 |
+
# Catch-all target: route all unknown targets to Sphinx using the new
|
| 17 |
+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
| 18 |
+
%: Makefile
|
| 19 |
+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(SPHINXFLAGS)
|
src_code_for_reproducibility/docs/generate_docs.py
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Script to automatically generate Sphinx documentation for all modules and build the HTML website.
|
| 4 |
+
"""
|
| 5 |
+
import importlib.util
|
| 6 |
+
import os
|
| 7 |
+
import subprocess
|
| 8 |
+
import sys
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def check_and_install_dependencies():
|
| 12 |
+
"""Check for required dependencies and install them if missing."""
|
| 13 |
+
required_packages = [
|
| 14 |
+
"sphinx",
|
| 15 |
+
"sphinx-rtd-theme",
|
| 16 |
+
"sphinxcontrib-napoleon",
|
| 17 |
+
"sphinxcontrib-mermaid",
|
| 18 |
+
"sphinx-autodoc-typehints",
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
missing_packages = []
|
| 22 |
+
|
| 23 |
+
for package in required_packages:
|
| 24 |
+
# Convert package name to module name (replace - with _)
|
| 25 |
+
module_name = package.replace("-", "_")
|
| 26 |
+
|
| 27 |
+
# Check if the package is installed
|
| 28 |
+
if importlib.util.find_spec(module_name) is None:
|
| 29 |
+
missing_packages.append(package)
|
| 30 |
+
|
| 31 |
+
# Install missing packages
|
| 32 |
+
if missing_packages:
|
| 33 |
+
print(f"Installing missing dependencies: {', '.join(missing_packages)}")
|
| 34 |
+
subprocess.check_call(
|
| 35 |
+
[sys.executable, "-m", "pip", "install"] + missing_packages
|
| 36 |
+
)
|
| 37 |
+
print("Dependencies installed successfully")
|
| 38 |
+
else:
|
| 39 |
+
print("All required dependencies are already installed")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def create_makefile(docs_dir):
|
| 43 |
+
"""Create a Makefile for Sphinx documentation if it doesn't exist."""
|
| 44 |
+
makefile_path = os.path.join(docs_dir, "Makefile")
|
| 45 |
+
|
| 46 |
+
if os.path.exists(makefile_path):
|
| 47 |
+
print(f"Makefile already exists at {makefile_path}")
|
| 48 |
+
return
|
| 49 |
+
|
| 50 |
+
print(f"Creating Makefile at {makefile_path}")
|
| 51 |
+
|
| 52 |
+
makefile_content = """# Minimal makefile for Sphinx documentation
|
| 53 |
+
|
| 54 |
+
# You can set these variables from the command line, and also
|
| 55 |
+
# from the environment for the first two.
|
| 56 |
+
SPHINXOPTS ?=
|
| 57 |
+
SPHINXBUILD ?= sphinx-build
|
| 58 |
+
SOURCEDIR = source
|
| 59 |
+
BUILDDIR = build
|
| 60 |
+
|
| 61 |
+
# Put it first so that "make" without argument is like "make help".
|
| 62 |
+
help:
|
| 63 |
+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(SPHINXFLAGS)
|
| 64 |
+
|
| 65 |
+
.PHONY: help Makefile
|
| 66 |
+
|
| 67 |
+
# Catch-all target: route all unknown targets to Sphinx using the new
|
| 68 |
+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
| 69 |
+
%: Makefile
|
| 70 |
+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(SPHINXFLAGS)
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
with open(makefile_path, "w") as f:
|
| 74 |
+
f.write(makefile_content)
|
| 75 |
+
|
| 76 |
+
print("Makefile created successfully")
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def create_make_bat(docs_dir):
|
| 80 |
+
"""Create a make.bat file for Windows if it doesn't exist."""
|
| 81 |
+
make_bat_path = os.path.join(docs_dir, "make.bat")
|
| 82 |
+
|
| 83 |
+
if os.path.exists(make_bat_path):
|
| 84 |
+
print(f"make.bat already exists at {make_bat_path}")
|
| 85 |
+
return
|
| 86 |
+
|
| 87 |
+
print(f"Creating make.bat at {make_bat_path}")
|
| 88 |
+
|
| 89 |
+
make_bat_content = """@ECHO OFF
|
| 90 |
+
|
| 91 |
+
pushd %~dp0
|
| 92 |
+
|
| 93 |
+
REM Command file for Sphinx documentation
|
| 94 |
+
|
| 95 |
+
if "%SPHINXBUILD%" == "" (
|
| 96 |
+
set SPHINXBUILD=sphinx-build
|
| 97 |
+
)
|
| 98 |
+
set SOURCEDIR=source
|
| 99 |
+
set BUILDDIR=build
|
| 100 |
+
|
| 101 |
+
%SPHINXBUILD% >NUL 2>NUL
|
| 102 |
+
if errorlevel 9009 (
|
| 103 |
+
echo.
|
| 104 |
+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
|
| 105 |
+
echo.installed, then set the SPHINXBUILD environment variable to point
|
| 106 |
+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
|
| 107 |
+
echo.may add the Sphinx directory to PATH.
|
| 108 |
+
echo.
|
| 109 |
+
echo.If you don't have Sphinx installed, grab it from
|
| 110 |
+
echo.https://www.sphinx-doc.org/
|
| 111 |
+
exit /b 1
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
if "%1" == "" goto help
|
| 115 |
+
|
| 116 |
+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
| 117 |
+
goto end
|
| 118 |
+
|
| 119 |
+
:help
|
| 120 |
+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
| 121 |
+
|
| 122 |
+
:end
|
| 123 |
+
popd
|
| 124 |
+
"""
|
| 125 |
+
|
| 126 |
+
with open(make_bat_path, "w") as f:
|
| 127 |
+
f.write(make_bat_content)
|
| 128 |
+
|
| 129 |
+
print("make.bat created successfully")
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def main():
|
| 133 |
+
# Check and install required dependencies
|
| 134 |
+
print("=== Checking dependencies ===")
|
| 135 |
+
check_and_install_dependencies()
|
| 136 |
+
|
| 137 |
+
# Get the directory of this script
|
| 138 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 139 |
+
|
| 140 |
+
# Path to the project root
|
| 141 |
+
project_root = os.path.dirname(script_dir)
|
| 142 |
+
|
| 143 |
+
# Path to the source directory
|
| 144 |
+
source_dir = os.path.join(project_root, "src")
|
| 145 |
+
|
| 146 |
+
# Path to the docs source directory
|
| 147 |
+
docs_source_dir = os.path.join(script_dir, "source")
|
| 148 |
+
|
| 149 |
+
# Print paths for debugging
|
| 150 |
+
print(f"Script directory: {script_dir}")
|
| 151 |
+
print(f"Project root: {project_root}")
|
| 152 |
+
print(f"Source directory: {source_dir}")
|
| 153 |
+
print(f"Docs source directory: {docs_source_dir}")
|
| 154 |
+
|
| 155 |
+
# Make sure the source directory exists
|
| 156 |
+
if not os.path.exists(source_dir):
|
| 157 |
+
print(f"Error: Source directory {source_dir} does not exist!")
|
| 158 |
+
sys.exit(1)
|
| 159 |
+
|
| 160 |
+
# Make sure the docs source directory exists
|
| 161 |
+
if not os.path.exists(docs_source_dir):
|
| 162 |
+
print(f"Creating docs source directory: {docs_source_dir}")
|
| 163 |
+
os.makedirs(docs_source_dir)
|
| 164 |
+
|
| 165 |
+
# Step 1: Run sphinx-apidoc to generate .rst files for all modules
|
| 166 |
+
print("\n=== Generating API documentation ===")
|
| 167 |
+
cmd = [
|
| 168 |
+
"sphinx-apidoc",
|
| 169 |
+
"-f", # Force overwriting of existing files
|
| 170 |
+
"-e", # Put module documentation before submodule documentation
|
| 171 |
+
"-M", # Put module documentation before subpackage documentation
|
| 172 |
+
"-o",
|
| 173 |
+
docs_source_dir, # Output directory
|
| 174 |
+
source_dir, # Source code directory
|
| 175 |
+
]
|
| 176 |
+
|
| 177 |
+
print(f"Running command: {' '.join(cmd)}")
|
| 178 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 179 |
+
|
| 180 |
+
# Print the output of the command
|
| 181 |
+
print("STDOUT:")
|
| 182 |
+
print(result.stdout)
|
| 183 |
+
|
| 184 |
+
print("STDERR:")
|
| 185 |
+
print(result.stderr)
|
| 186 |
+
|
| 187 |
+
if result.returncode != 0:
|
| 188 |
+
print(f"Error: sphinx-apidoc failed with return code {result.returncode}")
|
| 189 |
+
sys.exit(1)
|
| 190 |
+
|
| 191 |
+
# List the files in the docs source directory
|
| 192 |
+
print("\nFiles in docs/source directory:")
|
| 193 |
+
for file in sorted(os.listdir(docs_source_dir)):
|
| 194 |
+
print(f" {file}")
|
| 195 |
+
|
| 196 |
+
print("\nDocumentation source files generated successfully!")
|
| 197 |
+
|
| 198 |
+
# Step 2: Create Makefile and make.bat if they don't exist
|
| 199 |
+
create_makefile(script_dir)
|
| 200 |
+
create_make_bat(script_dir)
|
| 201 |
+
|
| 202 |
+
# Step 3: Build the HTML documentation
|
| 203 |
+
print("\n=== Building HTML documentation ===")
|
| 204 |
+
|
| 205 |
+
# Determine the build command based on the platform
|
| 206 |
+
if os.name == "nt": # Windows
|
| 207 |
+
build_cmd = ["make.bat", "html"]
|
| 208 |
+
else: # Unix/Linux/Mac
|
| 209 |
+
build_cmd = ["make", "html"]
|
| 210 |
+
|
| 211 |
+
# Change to the docs directory to run the build command
|
| 212 |
+
os.chdir(script_dir)
|
| 213 |
+
|
| 214 |
+
print(f"Running command: {' '.join(build_cmd)}")
|
| 215 |
+
build_result = subprocess.run(build_cmd, capture_output=True, text=True)
|
| 216 |
+
|
| 217 |
+
# Print the output of the build command
|
| 218 |
+
print("STDOUT:")
|
| 219 |
+
print(build_result.stdout)
|
| 220 |
+
|
| 221 |
+
print("STDERR:")
|
| 222 |
+
print(build_result.stderr)
|
| 223 |
+
|
| 224 |
+
if build_result.returncode != 0:
|
| 225 |
+
print(f"Error: HTML build failed with return code {build_result.returncode}")
|
| 226 |
+
sys.exit(1)
|
| 227 |
+
|
| 228 |
+
# Get the path to the built HTML documentation
|
| 229 |
+
html_dir = os.path.join(script_dir, "build", "html")
|
| 230 |
+
index_path = os.path.join(html_dir, "index.html")
|
| 231 |
+
|
| 232 |
+
if os.path.exists(index_path):
|
| 233 |
+
print(f"\nHTML documentation built successfully!")
|
| 234 |
+
print(f"You can view it by opening: {index_path}")
|
| 235 |
+
|
| 236 |
+
# Try to open the documentation in a browser
|
| 237 |
+
try:
|
| 238 |
+
import webbrowser
|
| 239 |
+
|
| 240 |
+
print("\nAttempting to open documentation in your default browser...")
|
| 241 |
+
webbrowser.open(f"file://{index_path}")
|
| 242 |
+
except Exception as e:
|
| 243 |
+
print(f"Could not open browser automatically: {e}")
|
| 244 |
+
else:
|
| 245 |
+
print(f"\nWarning: HTML index file not found at {index_path}")
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
if __name__ == "__main__":
|
| 249 |
+
main()
|
src_code_for_reproducibility/docs/make.bat
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@ECHO OFF
|
| 2 |
+
|
| 3 |
+
pushd %~dp0
|
| 4 |
+
|
| 5 |
+
REM Command file for Sphinx documentation
|
| 6 |
+
|
| 7 |
+
if "%SPHINXBUILD%" == "" (
|
| 8 |
+
set SPHINXBUILD=sphinx-build
|
| 9 |
+
)
|
| 10 |
+
set SOURCEDIR=source
|
| 11 |
+
set BUILDDIR=build
|
| 12 |
+
|
| 13 |
+
%SPHINXBUILD% >NUL 2>NUL
|
| 14 |
+
if errorlevel 9009 (
|
| 15 |
+
echo.
|
| 16 |
+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
|
| 17 |
+
echo.installed, then set the SPHINXBUILD environment variable to point
|
| 18 |
+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
|
| 19 |
+
echo.may add the Sphinx directory to PATH.
|
| 20 |
+
echo.
|
| 21 |
+
echo.If you don't have Sphinx installed, grab it from
|
| 22 |
+
echo.https://www.sphinx-doc.org/
|
| 23 |
+
exit /b 1
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
if "%1" == "" goto help
|
| 27 |
+
|
| 28 |
+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
| 29 |
+
goto end
|
| 30 |
+
|
| 31 |
+
:help
|
| 32 |
+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
| 33 |
+
|
| 34 |
+
:end
|
| 35 |
+
popd
|
src_code_for_reproducibility/markov_games/__init__.py
ADDED
|
File without changes
|
src_code_for_reproducibility/markov_games/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (173 Bytes). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (161 Bytes). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/agent.cpython-312.pyc
ADDED
|
Binary file (3.2 kB). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/alternative_actions_runner.cpython-312.pyc
ADDED
|
Binary file (4.95 kB). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/gather_and_export_utils.cpython-312.pyc
ADDED
|
Binary file (46.5 kB). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/group_timesteps.cpython-312.pyc
ADDED
|
Binary file (6.17 kB). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/linear_runner.cpython-312.pyc
ADDED
|
Binary file (1.25 kB). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/markov_game.cpython-312.pyc
ADDED
|
Binary file (9.72 kB). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/mg_utils.cpython-312.pyc
ADDED
|
Binary file (3.98 kB). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/rollout_tree.cpython-311.pyc
ADDED
|
Binary file (4.75 kB). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/rollout_tree.cpython-312.pyc
ADDED
|
Binary file (3.67 kB). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/run_markov_games.cpython-312.pyc
ADDED
|
Binary file (1.14 kB). View file
|
|
|
src_code_for_reproducibility/markov_games/__pycache__/simulation.cpython-312.pyc
ADDED
|
Binary file (3.9 kB). View file
|
|
|
src_code_for_reproducibility/markov_games/linear_runner.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import json
|
| 3 |
+
import os.path
|
| 4 |
+
|
| 5 |
+
from mllm.markov_games.markov_game import MarkovGame
|
| 6 |
+
from mllm.markov_games.rollout_tree import RolloutTreeNode, RolloutTreeRootNode
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
async def LinearRunner(
|
| 10 |
+
markov_game: MarkovGame, output_folder: str
|
| 11 |
+
) -> RolloutTreeRootNode:
|
| 12 |
+
"""
|
| 13 |
+
This method generates a trajectory without branching.
|
| 14 |
+
"""
|
| 15 |
+
time_step = 0
|
| 16 |
+
terminated = False
|
| 17 |
+
root = RolloutTreeRootNode(
|
| 18 |
+
id=markov_game.get_id(),
|
| 19 |
+
crn_id=markov_game.get_crn_id(),
|
| 20 |
+
agent_ids=markov_game.get_agent_ids(),
|
| 21 |
+
)
|
| 22 |
+
previous_node = root
|
| 23 |
+
while not terminated:
|
| 24 |
+
terminated, step_log = await markov_game.step()
|
| 25 |
+
current_node = RolloutTreeNode(step_log=step_log, time_step=time_step)
|
| 26 |
+
previous_node.child = current_node
|
| 27 |
+
previous_node = current_node
|
| 28 |
+
time_step += 1
|
| 29 |
+
|
| 30 |
+
return root
|
src_code_for_reproducibility/markov_games/negotiation/dond_agent.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import copy
|
| 2 |
+
import re
|
| 3 |
+
from collections.abc import Callable
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
from typing import Any, Dict, List, Tuple
|
| 6 |
+
|
| 7 |
+
from mllm.markov_games.agent import Agent
|
| 8 |
+
from mllm.markov_games.rollout_tree import AgentActLog, ChatTurn
|
| 9 |
+
from mllm.markov_games.negotiation.dond_simulation import (
|
| 10 |
+
DealNoDealObs,
|
| 11 |
+
)
|
| 12 |
+
from mllm.markov_games.negotiation.nego_simulation import Split
|
| 13 |
+
from mllm.markov_games.negotiation.nego_agent import NegotiationAgent, NegotiationAgentState
|
| 14 |
+
|
| 15 |
+
class DealNoDealAgent(NegotiationAgent):
|
| 16 |
+
def __init__(
|
| 17 |
+
self,
|
| 18 |
+
*args,
|
| 19 |
+
**kwargs,
|
| 20 |
+
):
|
| 21 |
+
super().__init__(*args, **kwargs)
|
| 22 |
+
self.intro_prompt = (
|
| 23 |
+
"You are {agent_id}. You are playing an iterated game. "
|
| 24 |
+
"At each round, you and other agent will try to distribute among yourselves items of types {item_types}. "
|
| 25 |
+
"You only know how much you value each item type, but not the other agent's values. "
|
| 26 |
+
"You can communicate with the other agent by sending up to {quota_messages_per_agent_per_round} short messages per round. "
|
| 27 |
+
"Each round, after exchanging messages, you and the other agent will submit a private proposal. "
|
| 28 |
+
"A deal is accepted only if both proposals match exactly and are within stock; otherwise no deal (0 points for both at that round). "
|
| 29 |
+
"The values of the items of the other agent at the previous round are revealed to you after each round. "
|
| 30 |
+
"Your goal is: {goal}."
|
| 31 |
+
)
|
| 32 |
+
self.new_round_prompt = ("New round {round_nb}. Items: {stock}. Your values: {values}. ")
|
| 33 |
+
self.last_round_prompt = ("Last round, other agent's values: {previous_values_coagent}. ")
|
| 34 |
+
self.send_split_prompt = ("Respond with <split>...</split> where you propose how many items of each type you want to keep.")
|
| 35 |
+
|
| 36 |
+
def get_message_regex(self, observation: DealNoDealObs) -> str:
|
| 37 |
+
return r"<message>[\s\S]{0,400}</message>"
|
| 38 |
+
|
| 39 |
+
def get_split_regex(self, observation: DealNoDealObs) -> str:
|
| 40 |
+
parts = []
|
| 41 |
+
for t in observation.item_types:
|
| 42 |
+
s = int(observation.quantities.get(t, 0))
|
| 43 |
+
allowed = "|".join(str(k) for k in range(0, s + 1))
|
| 44 |
+
rng = f"({allowed})"
|
| 45 |
+
parts.append(fr"<{t}>{rng}</{t}>")
|
| 46 |
+
items_block = "".join(parts)
|
| 47 |
+
return fr"(<split>{items_block}</split>)"
|
| 48 |
+
|
| 49 |
+
def get_split_action(self, policy_output: str, observation: DealNoDealObs) -> Split:
|
| 50 |
+
import re as _re
|
| 51 |
+
allocations: Dict[str, int] = {}
|
| 52 |
+
for t in observation.item_types:
|
| 53 |
+
m = _re.search(fr"<{t}>([0-9]+)</{t}>", policy_output)
|
| 54 |
+
if m:
|
| 55 |
+
allocations[t] = int(m.group(1))
|
| 56 |
+
else:
|
| 57 |
+
allocations[t] = 0
|
| 58 |
+
return Split(items_given_to_self=allocations)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
src_code_for_reproducibility/markov_games/negotiation/dond_simulation.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import copy
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import Any, Dict, List, Tuple
|
| 4 |
+
|
| 5 |
+
from numpy.random import default_rng
|
| 6 |
+
|
| 7 |
+
from mllm.markov_games.rollout_tree import SimulationStepLog
|
| 8 |
+
from mllm.markov_games.negotiation.nego_simulation import Split, NegotiationState, NegotiationObs, NegotiationSimulation
|
| 9 |
+
from mllm.utils.get_coagent_id import get_coagent_id
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
AgentId = str
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@dataclass
|
| 16 |
+
class DealNoDealState(NegotiationState):
|
| 17 |
+
item_types: List[str]
|
| 18 |
+
values: Dict[AgentId, Dict[str, int]]
|
| 19 |
+
|
| 20 |
+
@dataclass
|
| 21 |
+
class DealNoDealObs(NegotiationObs):
|
| 22 |
+
my_values: Dict[str, int]
|
| 23 |
+
item_types: List[str]
|
| 24 |
+
previous_values_coagent: Dict[str, int] | None
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def random_partition_integer(rng, total: int, parts: int) -> List[int]:
|
| 28 |
+
if parts <= 0:
|
| 29 |
+
return []
|
| 30 |
+
if total <= 0:
|
| 31 |
+
return [0 for _ in range(parts)]
|
| 32 |
+
cuts = sorted(rng.integers(0, total + 1, size=parts - 1).tolist())
|
| 33 |
+
vals = []
|
| 34 |
+
prev = 0
|
| 35 |
+
for c in cuts + [total]:
|
| 36 |
+
vals.append(c - prev)
|
| 37 |
+
prev = c
|
| 38 |
+
return vals
|
| 39 |
+
|
| 40 |
+
class DealNoDealSimulation(NegotiationSimulation):
|
| 41 |
+
|
| 42 |
+
def __init__(
|
| 43 |
+
self,
|
| 44 |
+
item_types: List[str] = ["books", "hats", "balls"],
|
| 45 |
+
*args,
|
| 46 |
+
**kwargs,
|
| 47 |
+
):
|
| 48 |
+
super().__init__(item_types=item_types, *args, **kwargs)
|
| 49 |
+
self.reset()
|
| 50 |
+
|
| 51 |
+
def _other(self, agent_id: AgentId) -> AgentId:
|
| 52 |
+
return get_coagent_id(self.agent_ids, agent_id)
|
| 53 |
+
|
| 54 |
+
def _sample_stock(self) -> Dict[str, int]:
|
| 55 |
+
# total items between 5 and 7
|
| 56 |
+
total_items = int(self.rng.integers(5, 8))
|
| 57 |
+
# nonnegative per-type counts summing to total_items
|
| 58 |
+
parts = random_partition_integer(self.rng, total_items, len(self.item_types))
|
| 59 |
+
# allow zeros per type
|
| 60 |
+
return {t: int(c) for t, c in zip(self.item_types, parts)}
|
| 61 |
+
|
| 62 |
+
def _sample_values_pair(self) -> Dict[AgentId, Dict[str, int]]:
|
| 63 |
+
# Each agent has integer non-negative values that sum to 10
|
| 64 |
+
# Each item type valued by at least one agent
|
| 65 |
+
# Some item type valued by both agents
|
| 66 |
+
while True:
|
| 67 |
+
vals_a = random_partition_integer(self.rng, 10, len(self.item_types))
|
| 68 |
+
vals_b = random_partition_integer(self.rng, 10, len(self.item_types))
|
| 69 |
+
a = {t: int(v) for t, v in zip(self.item_types, vals_a)}
|
| 70 |
+
b = {t: int(v) for t, v in zip(self.item_types, vals_b)}
|
| 71 |
+
# each item valued by at least one
|
| 72 |
+
ok1 = all((a[t] > 0) or (b[t] > 0) for t in self.item_types)
|
| 73 |
+
# some item valued by both
|
| 74 |
+
ok2 = any((a[t] > 0) and (b[t] > 0) for t in self.item_types)
|
| 75 |
+
if ok1 and ok2:
|
| 76 |
+
return {self.agent_ids[0]: a, self.agent_ids[1]: b}
|
| 77 |
+
|
| 78 |
+
def _is_valid_allocation(self, allocation: Dict[str, int], stock: Dict[str, int]) -> bool:
|
| 79 |
+
for t in self.item_types:
|
| 80 |
+
v = allocation.get(t)
|
| 81 |
+
if v is None:
|
| 82 |
+
return False
|
| 83 |
+
if not isinstance(v, int):
|
| 84 |
+
return False
|
| 85 |
+
if v < 0 or v > int(stock.get(t, 0)):
|
| 86 |
+
return False
|
| 87 |
+
return True
|
| 88 |
+
|
| 89 |
+
def set_new_round_of_variant(self):
|
| 90 |
+
# Keep same values, resample stock
|
| 91 |
+
self.state.quantities = self._sample_stock()
|
| 92 |
+
|
| 93 |
+
def get_info_of_variant(self, state: NegotiationState, actions: Dict[AgentId, Any]) -> Dict[str, Any]:
|
| 94 |
+
return {
|
| 95 |
+
"quantities": copy.deepcopy(state.quantities),
|
| 96 |
+
"values": copy.deepcopy(state.values),
|
| 97 |
+
'splits': copy.deepcopy(state.splits),
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
def get_rewards(self, splits: Dict[AgentId, Split]) -> Dict[AgentId, float]:
|
| 101 |
+
"""
|
| 102 |
+
Returns the rewards for each agent.
|
| 103 |
+
"""
|
| 104 |
+
split_a = splits[self.agent_ids[0]].items_given_to_self
|
| 105 |
+
split_b = splits[self.agent_ids[1]].items_given_to_self
|
| 106 |
+
rewards = {self.agent_ids[0]: 0, self.agent_ids[1]: 0}
|
| 107 |
+
for t in self.item_types:
|
| 108 |
+
# If not complementary, return 0!
|
| 109 |
+
if not split_a[t] + split_b[t] == self.state.quantities[t]:
|
| 110 |
+
return {self.agent_ids[0]: 0, self.agent_ids[1]: 0}
|
| 111 |
+
rewards[self.agent_ids[0]] += split_a[t] * self.state.values[self.agent_ids[0]][t]
|
| 112 |
+
rewards[self.agent_ids[1]] += split_b[t] * self.state.values[self.agent_ids[1]][t]
|
| 113 |
+
return rewards
|
| 114 |
+
|
| 115 |
+
def get_obs(self):
|
| 116 |
+
return {agent_id: self.get_obs_agent(agent_id) for agent_id in self.agent_ids}
|
| 117 |
+
|
| 118 |
+
def get_obs_agent(self, agent_id):
|
| 119 |
+
other_id = self._other(agent_id)
|
| 120 |
+
obs = DealNoDealObs(
|
| 121 |
+
round_nb=self.state.round_nb,
|
| 122 |
+
last_message=self.state.last_message,
|
| 123 |
+
current_agent=self.state.current_agent,
|
| 124 |
+
quantities=copy.deepcopy(self.state.quantities),
|
| 125 |
+
value=0.0, # unused in DOND
|
| 126 |
+
other_agent_split=None, # not meaningful until split
|
| 127 |
+
split_phase=self.state.split_phase,
|
| 128 |
+
quota_messages_per_agent_per_round=self.quota_messages_per_agent_per_round,
|
| 129 |
+
my_values=copy.deepcopy(self.state.values[agent_id]),
|
| 130 |
+
item_types=list(self.item_types),
|
| 131 |
+
previous_values_coagent=copy.deepcopy(self.state.values.get(other_id, {})),
|
| 132 |
+
)
|
| 133 |
+
return obs
|
| 134 |
+
|
| 135 |
+
def reset(self):
|
| 136 |
+
start_agent = self.agent_ids[self._starting_agent_index]
|
| 137 |
+
stock = self._sample_stock()
|
| 138 |
+
values = self._sample_values_pair()
|
| 139 |
+
self.state = DealNoDealState(
|
| 140 |
+
round_nb=0,
|
| 141 |
+
last_message="",
|
| 142 |
+
current_agent=start_agent,
|
| 143 |
+
quantities=stock,
|
| 144 |
+
values=values,
|
| 145 |
+
previous_values=None,
|
| 146 |
+
splits={aid: None for aid in self.agent_ids},
|
| 147 |
+
nb_messages_sent={aid: 0 for aid in self.agent_ids},
|
| 148 |
+
split_phase=False,
|
| 149 |
+
item_types=list(self.item_types),
|
| 150 |
+
)
|
| 151 |
+
return self.get_obs()
|
| 152 |
+
|
| 153 |
+
|
src_code_for_reproducibility/markov_games/negotiation/nego_agent.py
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import copy
|
| 2 |
+
from abc import abstractmethod
|
| 3 |
+
from collections.abc import Callable
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
from typing import Any, Dict, List, Tuple
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
from mllm.markov_games.agent import Agent
|
| 10 |
+
from mllm.markov_games.negotiation.nego_simulation import Message, NegotiationObs, Split
|
| 11 |
+
from mllm.markov_games.rollout_tree import AgentActLog, ChatTurn
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@dataclass
|
| 15 |
+
class NegotiationAgentState:
|
| 16 |
+
round_nb: int
|
| 17 |
+
nb_messages_sent_this_round: int
|
| 18 |
+
chat_counter: int
|
| 19 |
+
chat_history: List[ChatTurn]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class NegotiationAgent(Agent):
|
| 23 |
+
def __init__(
|
| 24 |
+
self,
|
| 25 |
+
seed: int,
|
| 26 |
+
agent_id: str,
|
| 27 |
+
agent_name: str,
|
| 28 |
+
policy: Callable[[List[Dict]], str],
|
| 29 |
+
goal: str,
|
| 30 |
+
exploration_prompts: List[str] = [],
|
| 31 |
+
exploration_prompt_probs: List[float] = [],
|
| 32 |
+
):
|
| 33 |
+
self.seed = seed
|
| 34 |
+
self.agent_id = agent_id
|
| 35 |
+
self.agent_name = agent_name
|
| 36 |
+
self.policy = policy
|
| 37 |
+
self.goal = goal
|
| 38 |
+
self.exploration_prompts_toggled = len(exploration_prompts) > 0
|
| 39 |
+
if self.exploration_prompts_toggled:
|
| 40 |
+
exploration_prompts = copy.deepcopy(exploration_prompts)
|
| 41 |
+
exploration_prompts.append(None)
|
| 42 |
+
self.exploration_prompts = exploration_prompts
|
| 43 |
+
self.exploration_prompt_probs = np.array(exploration_prompt_probs)
|
| 44 |
+
assert self.exploration_prompt_probs.sum() <= 1
|
| 45 |
+
assert np.all(self.exploration_prompt_probs >= 0)
|
| 46 |
+
self.exploration_prompt_probs = np.append(
|
| 47 |
+
self.exploration_prompt_probs, 1 - self.exploration_prompt_probs.sum()
|
| 48 |
+
)
|
| 49 |
+
self.state = NegotiationAgentState(
|
| 50 |
+
round_nb=0, nb_messages_sent_this_round=0, chat_counter=0, chat_history=[]
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Implemented in variants
|
| 54 |
+
self.intro_prompt = ""
|
| 55 |
+
self.new_round_prompt = ""
|
| 56 |
+
self.last_round_prompt = ""
|
| 57 |
+
self.send_split_prompt = ""
|
| 58 |
+
self.wait_for_message_prompt = ""
|
| 59 |
+
self.last_message_prompt = ""
|
| 60 |
+
self.send_message_prompt = ""
|
| 61 |
+
|
| 62 |
+
@abstractmethod
|
| 63 |
+
def get_message_regex(self, observation: NegotiationObs) -> str:
|
| 64 |
+
pass
|
| 65 |
+
|
| 66 |
+
@abstractmethod
|
| 67 |
+
def get_split_regex(self, observation: NegotiationObs) -> str:
|
| 68 |
+
pass
|
| 69 |
+
|
| 70 |
+
@abstractmethod
|
| 71 |
+
def get_split_action(
|
| 72 |
+
self, policy_output: str, observation: NegotiationObs
|
| 73 |
+
) -> Split:
|
| 74 |
+
pass
|
| 75 |
+
|
| 76 |
+
async def act(self, observation: NegotiationObs) -> Tuple[Any, AgentActLog]:
|
| 77 |
+
def dict_to_str(d: dict) -> str:
|
| 78 |
+
return ", ".join(f"{v} {k}" for k, v in d.items())
|
| 79 |
+
|
| 80 |
+
def dict_to_eq_str(d: dict) -> str:
|
| 81 |
+
return ", ".join(f"{k}={v}" for k, v in d.items())
|
| 82 |
+
|
| 83 |
+
is_our_turn = observation.current_agent == self.agent_id
|
| 84 |
+
action: Any = None
|
| 85 |
+
round_nb = observation.round_nb
|
| 86 |
+
|
| 87 |
+
prompt_parts: List[str] = []
|
| 88 |
+
obs_ctx = vars(observation)
|
| 89 |
+
obs_ctx_formmated = obs_ctx.copy()
|
| 90 |
+
for key in obs_ctx_formmated:
|
| 91 |
+
if isinstance(obs_ctx_formmated[key], dict) and "value" not in key:
|
| 92 |
+
obs_ctx_formmated[key] = dict_to_str(obs_ctx_formmated[key])
|
| 93 |
+
elif isinstance(obs_ctx_formmated[key], dict) and "value" in key:
|
| 94 |
+
obs_ctx_formmated[key] = dict_to_eq_str(obs_ctx_formmated[key])
|
| 95 |
+
|
| 96 |
+
#######################################
|
| 97 |
+
# build user prompt
|
| 98 |
+
#######################################
|
| 99 |
+
|
| 100 |
+
# First-ever call
|
| 101 |
+
is_intro = round_nb == 0 and self.state.chat_counter == 0
|
| 102 |
+
if is_intro:
|
| 103 |
+
prompt_parts.append(
|
| 104 |
+
self.intro_prompt.format(
|
| 105 |
+
goal=self.goal, agent=self.agent_name, **obs_ctx_formmated
|
| 106 |
+
)
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# New round
|
| 110 |
+
is_new_round = round_nb > self.state.round_nb
|
| 111 |
+
if is_new_round or is_intro:
|
| 112 |
+
self.state.nb_messages_sent_this_round = 0
|
| 113 |
+
if not is_intro:
|
| 114 |
+
prompt_parts.append(self.last_round_prompt.format(**obs_ctx_formmated))
|
| 115 |
+
prompt_parts.append(self.new_round_prompt.format(**obs_ctx_formmated))
|
| 116 |
+
if self.exploration_prompts_toggled:
|
| 117 |
+
exploration_prompt = self.exploration_prompts[
|
| 118 |
+
np.random.choice(
|
| 119 |
+
len(self.exploration_prompts), p=self.exploration_prompt_probs
|
| 120 |
+
)
|
| 121 |
+
]
|
| 122 |
+
if exploration_prompt is not None:
|
| 123 |
+
prompt_parts.append(exploration_prompt)
|
| 124 |
+
self.state.round_nb = round_nb
|
| 125 |
+
|
| 126 |
+
# Wait for message
|
| 127 |
+
if not is_our_turn and not observation.split_phase:
|
| 128 |
+
prompt_parts.append(
|
| 129 |
+
self.wait_for_message_prompt.format(**obs_ctx_formmated)
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# Get last message
|
| 133 |
+
if is_our_turn and not is_new_round and not is_intro:
|
| 134 |
+
prompt_parts.append(self.last_message_prompt.format(**obs_ctx_formmated))
|
| 135 |
+
|
| 136 |
+
# Prompt to send message
|
| 137 |
+
must_send_message = not observation.split_phase and is_our_turn
|
| 138 |
+
if must_send_message:
|
| 139 |
+
prompt_parts.append(self.send_message_prompt.format(**obs_ctx_formmated))
|
| 140 |
+
|
| 141 |
+
# Prompt to give split
|
| 142 |
+
must_send_split = not must_send_message and observation.split_phase
|
| 143 |
+
if must_send_split:
|
| 144 |
+
var_names = ["x", "y", "z", "w"] # Extend as needed
|
| 145 |
+
items_str = ", ".join(
|
| 146 |
+
[
|
| 147 |
+
f"{var_names[i]} {item}"
|
| 148 |
+
for i, item in enumerate(obs_ctx["quantities"].keys())
|
| 149 |
+
]
|
| 150 |
+
)
|
| 151 |
+
ranges_str = ", ".join(
|
| 152 |
+
[
|
| 153 |
+
f"{var_names[i]}: 0-{obs_ctx['quantities'][item]} (integer)"
|
| 154 |
+
for i, item in enumerate(obs_ctx["quantities"].keys())
|
| 155 |
+
]
|
| 156 |
+
)
|
| 157 |
+
proposal_style = f"Proposal: {items_str} where {ranges_str}."
|
| 158 |
+
proposal_style2 = (
|
| 159 |
+
f"<items_to_self> {items_str} </items_to_self> where {ranges_str}."
|
| 160 |
+
)
|
| 161 |
+
prompt_parts.append(
|
| 162 |
+
self.send_split_prompt.format(
|
| 163 |
+
proposal_style=proposal_style,
|
| 164 |
+
proposal_style2=proposal_style2,
|
| 165 |
+
**obs_ctx_formmated,
|
| 166 |
+
)
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# Append one ChatTurn with is_state_end=True
|
| 170 |
+
user_prompt = "\n".join(prompt_parts)
|
| 171 |
+
self.state.chat_history.append(
|
| 172 |
+
ChatTurn(
|
| 173 |
+
agent_id=self.agent_id,
|
| 174 |
+
role="user",
|
| 175 |
+
content=user_prompt,
|
| 176 |
+
is_state_end=True,
|
| 177 |
+
)
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
#######################################
|
| 181 |
+
# Get policy action
|
| 182 |
+
#######################################
|
| 183 |
+
|
| 184 |
+
# Query policy for the appropriate format
|
| 185 |
+
if must_send_message:
|
| 186 |
+
return_regex = self.get_message_regex(observation)
|
| 187 |
+
policy_output = await self.policy(
|
| 188 |
+
state=self.state.chat_history,
|
| 189 |
+
agent_id=self.agent_id,
|
| 190 |
+
regex=return_regex,
|
| 191 |
+
)
|
| 192 |
+
self.state.chat_history.append(
|
| 193 |
+
ChatTurn(
|
| 194 |
+
agent_id=self.agent_id,
|
| 195 |
+
role="assistant",
|
| 196 |
+
content=policy_output.content,
|
| 197 |
+
reasoning_content=policy_output.reasoning_content,
|
| 198 |
+
log_probs=policy_output.log_probs,
|
| 199 |
+
out_token_ids=policy_output.out_token_ids,
|
| 200 |
+
is_state_end=False,
|
| 201 |
+
)
|
| 202 |
+
)
|
| 203 |
+
action = Message(message=policy_output.content)
|
| 204 |
+
self.state.nb_messages_sent_this_round += 1
|
| 205 |
+
|
| 206 |
+
elif must_send_split:
|
| 207 |
+
return_regex = self.get_split_regex(observation)
|
| 208 |
+
policy_output = await self.policy(
|
| 209 |
+
state=self.state.chat_history,
|
| 210 |
+
agent_id=self.agent_id,
|
| 211 |
+
regex=return_regex,
|
| 212 |
+
)
|
| 213 |
+
self.state.chat_history.append(
|
| 214 |
+
ChatTurn(
|
| 215 |
+
agent_id=self.agent_id,
|
| 216 |
+
role="assistant",
|
| 217 |
+
content=policy_output.content,
|
| 218 |
+
reasoning_content=policy_output.reasoning_content,
|
| 219 |
+
log_probs=policy_output.log_probs,
|
| 220 |
+
out_token_ids=policy_output.out_token_ids,
|
| 221 |
+
is_state_end=False,
|
| 222 |
+
)
|
| 223 |
+
)
|
| 224 |
+
action = self.get_split_action(policy_output.content, observation)
|
| 225 |
+
else:
|
| 226 |
+
action = None
|
| 227 |
+
|
| 228 |
+
agent_step_log = AgentActLog(
|
| 229 |
+
chat_turns=self.state.chat_history[self.state.chat_counter :], info=None
|
| 230 |
+
)
|
| 231 |
+
self.state.chat_counter = len(self.state.chat_history)
|
| 232 |
+
return action, agent_step_log
|
| 233 |
+
|
| 234 |
+
def get_safe_copy(self):
|
| 235 |
+
agent_copy = copy.copy(self)
|
| 236 |
+
agent_copy.state = copy.deepcopy(self.state)
|
| 237 |
+
return agent_copy
|
| 238 |
+
|
| 239 |
+
def reset(self):
|
| 240 |
+
self.state = NegotiationAgentState(
|
| 241 |
+
round_nb=0, nb_messages_sent_this_round=0, chat_counter=0, chat_history=[]
|
| 242 |
+
)
|
src_code_for_reproducibility/markov_games/negotiation/nego_simulation.py
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Negotiation simulation environment
|
| 3 |
+
other agent is set at the start of every round. Even though current agent changes over message turns in a round.
|
| 4 |
+
"""
|
| 5 |
+
import copy
|
| 6 |
+
from abc import abstractmethod
|
| 7 |
+
from dataclasses import dataclass
|
| 8 |
+
from typing import Any, Dict, List, Tuple
|
| 9 |
+
|
| 10 |
+
from numpy.random import default_rng
|
| 11 |
+
|
| 12 |
+
from mllm.markov_games.rollout_tree import SimulationStepLog
|
| 13 |
+
from mllm.markov_games.simulation import Simulation
|
| 14 |
+
from mllm.utils.get_coagent_id import get_coagent_id
|
| 15 |
+
|
| 16 |
+
AgentId = str
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class Split:
|
| 21 |
+
items_given_to_self: Dict[str, int]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@dataclass
|
| 25 |
+
class Message:
|
| 26 |
+
message: str
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@dataclass # gets extended by variants
|
| 30 |
+
class NegotiationState:
|
| 31 |
+
round_nb: int
|
| 32 |
+
last_message: str
|
| 33 |
+
current_agent: AgentId
|
| 34 |
+
quantities: Dict[str, int]
|
| 35 |
+
values: Dict[AgentId, Dict[str, float]]
|
| 36 |
+
splits: Dict[AgentId, Split | None]
|
| 37 |
+
nb_messages_sent: Dict[AgentId, int]
|
| 38 |
+
previous_values: Dict[AgentId, Dict[str, float]] | None
|
| 39 |
+
previous_splits: Dict[AgentId, Dict[str, int] | None] | None
|
| 40 |
+
previous_points: Dict[AgentId, float] | None
|
| 41 |
+
previous_quantities: Dict[str, int] | None
|
| 42 |
+
split_phase: bool
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@dataclass # gets extended by variants
|
| 46 |
+
class NegotiationObs:
|
| 47 |
+
round_nb: int
|
| 48 |
+
last_message: str
|
| 49 |
+
quota_messages_per_agent_per_round: int
|
| 50 |
+
current_agent: AgentId
|
| 51 |
+
other_agent: str
|
| 52 |
+
quantities: Dict[str, int]
|
| 53 |
+
item_types: List[str]
|
| 54 |
+
value: Dict[str, int]
|
| 55 |
+
split_phase: bool
|
| 56 |
+
last_split_agent: Dict[str, int] | None
|
| 57 |
+
last_value_agent: Dict[str, int] | None
|
| 58 |
+
last_points_agent: float | None
|
| 59 |
+
last_split_coagent: Dict[str, int] | None
|
| 60 |
+
last_value_coagent: Dict[str, int] | None
|
| 61 |
+
last_points_coagent: float | None
|
| 62 |
+
last_quantities: Dict[str, int] | None
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def compute_tas_style_rewards(
|
| 66 |
+
agent_ids: List[AgentId],
|
| 67 |
+
values: Dict[AgentId, float],
|
| 68 |
+
splits: Dict[AgentId, Split],
|
| 69 |
+
quantities: Dict[str, int],
|
| 70 |
+
) -> Dict[AgentId, float]:
|
| 71 |
+
"""
|
| 72 |
+
TAS-like reward computation: if sum of proposed coins exceeds max_coins,
|
| 73 |
+
allocate proportionally. Otherwise, use proposed amounts directly.
|
| 74 |
+
Rewards are quantity_kept * per-coin value for each agent.
|
| 75 |
+
"""
|
| 76 |
+
a0, a1 = agent_ids[0], agent_ids[1]
|
| 77 |
+
r0, r1 = 0.0, 0.0
|
| 78 |
+
|
| 79 |
+
for item in quantities:
|
| 80 |
+
max_item = quantities[item]
|
| 81 |
+
item_to_self_0 = int(
|
| 82 |
+
(splits[a0].items_given_to_self.get(item, 0))
|
| 83 |
+
if splits[a0] is not None
|
| 84 |
+
else 0
|
| 85 |
+
)
|
| 86 |
+
item_to_self_1 = int(
|
| 87 |
+
(splits[a1].items_given_to_self.get(item, 0))
|
| 88 |
+
if splits[a1] is not None
|
| 89 |
+
else 0
|
| 90 |
+
)
|
| 91 |
+
denom = max(int(max_item), item_to_self_0 + item_to_self_1)
|
| 92 |
+
q0 = float(max_item) * float(item_to_self_0) / float(denom)
|
| 93 |
+
q1 = float(max_item) * float(item_to_self_1) / float(denom)
|
| 94 |
+
if type(values[a0]) is not dict:
|
| 95 |
+
r0 += q0 * float(values[a0])
|
| 96 |
+
r1 += q1 * float(values[a1])
|
| 97 |
+
else:
|
| 98 |
+
r0 += q0 * float(values[a0][item])
|
| 99 |
+
r1 += q1 * float(values[a1][item])
|
| 100 |
+
return {a0: r0, a1: r1}
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
class NegotiationSimulation(Simulation):
|
| 104 |
+
def __init__(
|
| 105 |
+
self,
|
| 106 |
+
agent_ids: List[AgentId],
|
| 107 |
+
agent_names: List[str],
|
| 108 |
+
seed: int,
|
| 109 |
+
nb_of_rounds: int,
|
| 110 |
+
quota_messages_per_agent_per_round: int,
|
| 111 |
+
item_types: List[str] | None = None,
|
| 112 |
+
):
|
| 113 |
+
self.seed = seed
|
| 114 |
+
self.rng = default_rng(self.seed)
|
| 115 |
+
self.agent_ids = list(agent_ids)
|
| 116 |
+
self.agent_names = agent_names
|
| 117 |
+
self.agent_id_to_name = {
|
| 118 |
+
agent_id: agent_name for agent_id, agent_name in zip(agent_ids, agent_names)
|
| 119 |
+
}
|
| 120 |
+
self.nb_of_rounds = int(nb_of_rounds)
|
| 121 |
+
self.quota_messages_per_agent_per_round = int(
|
| 122 |
+
quota_messages_per_agent_per_round
|
| 123 |
+
)
|
| 124 |
+
if item_types is not None:
|
| 125 |
+
self.item_types = [item.lower() for item in item_types]
|
| 126 |
+
else:
|
| 127 |
+
self.item_types = ["coins"]
|
| 128 |
+
self.state: NegotiationState | None = None
|
| 129 |
+
self._starting_agent_index = self.rng.choice([0, 1])
|
| 130 |
+
self.reset()
|
| 131 |
+
|
| 132 |
+
def _other(self, agent_id: AgentId) -> AgentId:
|
| 133 |
+
return get_coagent_id(self.agent_ids, agent_id)
|
| 134 |
+
|
| 135 |
+
@abstractmethod
|
| 136 |
+
def set_new_round_of_variant(self):
|
| 137 |
+
pass
|
| 138 |
+
|
| 139 |
+
@abstractmethod
|
| 140 |
+
def get_info_of_variant(
|
| 141 |
+
self, state: NegotiationState, actions: Dict[AgentId, Any]
|
| 142 |
+
) -> Dict[str, Any]:
|
| 143 |
+
pass
|
| 144 |
+
|
| 145 |
+
def step(self, actions: Any) -> Tuple[bool, SimulationStepLog]:
|
| 146 |
+
"""
|
| 147 |
+
Returns terminated, step_log
|
| 148 |
+
"""
|
| 149 |
+
assert self.state is not None
|
| 150 |
+
current_agent = self.state.current_agent
|
| 151 |
+
a0, a1 = self.agent_ids[0], self.agent_ids[1]
|
| 152 |
+
action = actions.get(current_agent)
|
| 153 |
+
|
| 154 |
+
# Split phase: require both splits in the same timestep
|
| 155 |
+
if self.state.split_phase:
|
| 156 |
+
action_a0 = actions.get(a0)
|
| 157 |
+
action_a1 = actions.get(a1)
|
| 158 |
+
have_both_splits = isinstance(action_a0, Split) and isinstance(
|
| 159 |
+
action_a1, Split
|
| 160 |
+
)
|
| 161 |
+
if not have_both_splits:
|
| 162 |
+
rewards = {agent_id: 0.0 for agent_id in self.agent_ids}
|
| 163 |
+
return False, SimulationStepLog(
|
| 164 |
+
rewards=rewards, info={"type": "waiting_for_splits"}
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
# Record splits
|
| 168 |
+
self.state.splits[a0] = action_a0
|
| 169 |
+
self.state.splits[a1] = action_a1
|
| 170 |
+
|
| 171 |
+
# Compute rewards and end round
|
| 172 |
+
rewards = self.get_rewards(self.state.splits)
|
| 173 |
+
|
| 174 |
+
# Info
|
| 175 |
+
info = self.get_info_of_variant(self.state, actions)
|
| 176 |
+
|
| 177 |
+
# Prepare next round
|
| 178 |
+
# Alternate starting agent
|
| 179 |
+
self.state.round_nb += 1
|
| 180 |
+
self._starting_agent_index = 1 - self._starting_agent_index
|
| 181 |
+
self.state.current_agent = self.agent_ids[self._starting_agent_index]
|
| 182 |
+
self.state.previous_values = copy.deepcopy(self.state.values)
|
| 183 |
+
self.state.previous_splits = copy.deepcopy(self.state.splits)
|
| 184 |
+
self.state.previous_quantities = copy.deepcopy(self.state.quantities)
|
| 185 |
+
self.state.previous_points = copy.deepcopy(rewards)
|
| 186 |
+
self.state.last_message = ""
|
| 187 |
+
self.set_new_round_of_variant() # variant specific
|
| 188 |
+
self.state.splits = {agent_id: None for agent_id in self.agent_ids}
|
| 189 |
+
self.state.nb_messages_sent = {agent_id: 0 for agent_id in self.agent_ids}
|
| 190 |
+
is_last_timestep_in_round = True
|
| 191 |
+
done = self.state.round_nb >= self.nb_of_rounds
|
| 192 |
+
|
| 193 |
+
# Message phase
|
| 194 |
+
elif isinstance(action, Message):
|
| 195 |
+
self.state.last_message = action.message
|
| 196 |
+
self.state.nb_messages_sent[current_agent] += 1
|
| 197 |
+
|
| 198 |
+
# Move turn to other agent
|
| 199 |
+
self.state.current_agent = self._other(current_agent)
|
| 200 |
+
|
| 201 |
+
# If both agents have reached their message quota, enter split phase
|
| 202 |
+
if all(
|
| 203 |
+
self.state.nb_messages_sent[agent_id]
|
| 204 |
+
>= self.quota_messages_per_agent_per_round
|
| 205 |
+
for agent_id in self.agent_ids
|
| 206 |
+
):
|
| 207 |
+
self.state.split_phase = True
|
| 208 |
+
is_last_timestep_in_round = False
|
| 209 |
+
done = False
|
| 210 |
+
rewards = {agent_id: 0.0 for agent_id in self.agent_ids}
|
| 211 |
+
info = {"type": "message"}
|
| 212 |
+
|
| 213 |
+
info[
|
| 214 |
+
"is_last_timestep_in_round"
|
| 215 |
+
] = is_last_timestep_in_round # Used later to group round timesteps if needed
|
| 216 |
+
return done, SimulationStepLog(rewards=rewards, info=info)
|
| 217 |
+
|
| 218 |
+
def get_obs(self):
|
| 219 |
+
"""Returns all agent observations in dict"""
|
| 220 |
+
return {agent_id: self.get_obs_agent(agent_id) for agent_id in self.agent_ids}
|
| 221 |
+
|
| 222 |
+
@abstractmethod
|
| 223 |
+
def get_rewards(self, splits: Dict[AgentId, Split]) -> Dict[AgentId, float]:
|
| 224 |
+
pass
|
| 225 |
+
|
| 226 |
+
@abstractmethod
|
| 227 |
+
def get_obs_agent(self, agent_id):
|
| 228 |
+
pass
|
| 229 |
+
|
| 230 |
+
def get_state(self):
|
| 231 |
+
return self.state
|
| 232 |
+
|
| 233 |
+
def get_safe_copy(self):
|
| 234 |
+
"""Return a safe copy of the simulation."""
|
| 235 |
+
simulation_copy = copy.copy(self)
|
| 236 |
+
simulation_copy.state = copy.deepcopy(self.state)
|
| 237 |
+
return simulation_copy
|
| 238 |
+
|
| 239 |
+
@abstractmethod
|
| 240 |
+
def reset(self) -> dict[AgentId, NegotiationObs]:
|
| 241 |
+
pass
|
src_code_for_reproducibility/markov_games/negotiation/negotiation_statistics.py
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import Callable, Dict, List, Tuple
|
| 4 |
+
|
| 5 |
+
from mllm.markov_games.negotiation.nego_simulation import Split
|
| 6 |
+
from mllm.markov_games.rollout_tree import SimulationStepLog
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def avg_reward(sl: SimulationStepLog) -> List[Tuple[str, float]]:
|
| 10 |
+
"""Average (per-step) reward for each agent and overall.
|
| 11 |
+
|
| 12 |
+
What it computes:
|
| 13 |
+
- Returns the raw reward for every (non-buffer) agent at the current
|
| 14 |
+
simulation step.
|
| 15 |
+
- Adds an aggregate key ``all_agents`` which is the simple arithmetic
|
| 16 |
+
mean across the agents present in ``sl.rewards``.
|
| 17 |
+
|
| 18 |
+
Rationale / motivation:
|
| 19 |
+
Monitoring the reward stream at each step helps:
|
| 20 |
+
* Diagnose reward shaping issues (e.g., unintended negative drift).
|
| 21 |
+
* Provide a fairness snapshot (are rewards systematically skewed?).
|
| 22 |
+
* Supply a ubiquitous baseline metric used by other higher‑level
|
| 23 |
+
summaries (efficiency, surplus allocation, etc.).
|
| 24 |
+
|
| 25 |
+
Return shape:
|
| 26 |
+
{ agent_id: float, ..., "all_agents": float }
|
| 27 |
+
If any agent id contains the substring "buffer" we treat this step as
|
| 28 |
+
an implementation artifact (e.g., rollout buffer) and return ``None``
|
| 29 |
+
to avoid polluting aggregates.
|
| 30 |
+
"""
|
| 31 |
+
for aid in sl.rewards.keys():
|
| 32 |
+
if "buffer" in str(aid) and "live" not in str(aid):
|
| 33 |
+
return None
|
| 34 |
+
# One value per agent at each step
|
| 35 |
+
rewards_dict = {f"reward-{aid}": float(v) for aid, v in (sl.rewards or {}).items()}
|
| 36 |
+
return [(key, value) for key, value in rewards_dict.items() if value is not None]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def split_efficiency(sl: SimulationStepLog) -> List[Tuple[str, float]] | None:
|
| 40 |
+
"""Final‑round allocation efficiency relative to an upper bound.
|
| 41 |
+
|
| 42 |
+
What it computes (only on the last timestep of a negotiation round):
|
| 43 |
+
- Uses ``info['values']`` (per‑agent per‑item valuations) and
|
| 44 |
+
``info['quantities']`` (available item counts) to form a greedy
|
| 45 |
+
*upper bound* on achievable total reward: allocate each unit of an
|
| 46 |
+
item to the single agent who values that item most.
|
| 47 |
+
- Compares the actually realized sum of rewards at that final
|
| 48 |
+
timestep to this constructed maximum.
|
| 49 |
+
- Emits a single scalar under key ``"all_agents"`` equal to
|
| 50 |
+
achieved / theoretical_max.
|
| 51 |
+
|
| 52 |
+
Motivation:
|
| 53 |
+
Efficiency (a core welfare notion) distinguishes between coordination
|
| 54 |
+
failures (low efficiency) versus strategic distributional disputes
|
| 55 |
+
(high efficiency but uneven splits). Tracking this per round helps
|
| 56 |
+
evaluate whether models learn to identify and realize joint surplus.
|
| 57 |
+
|
| 58 |
+
Notes / caveats:
|
| 59 |
+
- Only defined for 2+ non‑buffer agents; if a buffer agent is present
|
| 60 |
+
returns ``None`` to exclude spurious steps.
|
| 61 |
+
- Requires the environment to have populated ``values`` and
|
| 62 |
+
``quantities``; otherwise returns ``None``.
|
| 63 |
+
- This is an optimistic bound (not necessarily reachable under
|
| 64 |
+
protocol constraints) but is simple, fast, and comparable across
|
| 65 |
+
runs.
|
| 66 |
+
"""
|
| 67 |
+
info = sl.info or {}
|
| 68 |
+
if not info or not info.get("is_last_timestep_in_round"):
|
| 69 |
+
return None
|
| 70 |
+
quantities = info.get("quantities") or {}
|
| 71 |
+
values = info.get("values") or {}
|
| 72 |
+
if not values or not quantities:
|
| 73 |
+
return None
|
| 74 |
+
agent_ids = list(sl.rewards.keys())
|
| 75 |
+
if type(values[agent_ids[0]]) is dict:
|
| 76 |
+
item_keys = list(values.values())[0].keys()
|
| 77 |
+
max_vals, max_quantities = [], []
|
| 78 |
+
for item in item_keys:
|
| 79 |
+
max_val = max(float(agent_vals[item]) for agent_vals in values.values())
|
| 80 |
+
max_vals.append(max_val)
|
| 81 |
+
max_quantities.append(quantities[item])
|
| 82 |
+
else:
|
| 83 |
+
max_vals = [max(float(v) for v in values.values())]
|
| 84 |
+
max_quantities = [quantities[item] for item in quantities.keys()]
|
| 85 |
+
for aid in sl.rewards.keys():
|
| 86 |
+
if "buffer" in str(aid) and "live" not in str(aid):
|
| 87 |
+
return None
|
| 88 |
+
achieved = sum(float(v) for v in sl.rewards.values())
|
| 89 |
+
max_reward = sum(d * v for d, v in zip(max_quantities, max_vals))
|
| 90 |
+
# Efficiency is a global metric; emit same value for a special key "all"
|
| 91 |
+
return [("split_efficiency", achieved / max_reward)]
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def _extract_items_from_split(raw_split: Dict) -> Dict[str, float] | None:
|
| 95 |
+
"""Return a mapping item->proposal amount from a split structure.
|
| 96 |
+
|
| 97 |
+
Supports both generic negotiation splits with nested structure
|
| 98 |
+
{ 'items_given_to_self': {item: qty, ...}}
|
| 99 |
+
and TAS coin-only variants which may already be a flat mapping {'coins': qty}.
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
if raw_split is None:
|
| 103 |
+
return {}
|
| 104 |
+
elif isinstance(raw_split, Split):
|
| 105 |
+
return {k: float(v) for k, v in raw_split.items_given_to_self.items()}
|
| 106 |
+
elif isinstance(raw_split, dict):
|
| 107 |
+
if "items_given_to_self" in raw_split and isinstance(
|
| 108 |
+
raw_split["items_given_to_self"], dict
|
| 109 |
+
):
|
| 110 |
+
return {k: float(v) for k, v in raw_split["items_given_to_self"].items()}
|
| 111 |
+
# Fallback: assume already flat mapping of items
|
| 112 |
+
elif hasattr(raw_split, "items_given_to_self"):
|
| 113 |
+
return {k: float(v) for k, v in raw_split["items_given_to_self"].items()}
|
| 114 |
+
return {
|
| 115 |
+
k: float(v) for k, v in raw_split.items() if isinstance(v, (int, float))
|
| 116 |
+
}
|
| 117 |
+
return {}
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def _average_proposal_relative_value(
|
| 121 |
+
sl: SimulationStepLog,
|
| 122 |
+
metric_name: str,
|
| 123 |
+
comparator: Callable[[float, float], bool],
|
| 124 |
+
opposite_comparator: Callable[[float, float], bool],
|
| 125 |
+
) -> Dict[str, float | None] | None:
|
| 126 |
+
"""Shared implementation for proposal size conditioned on relative value.
|
| 127 |
+
|
| 128 |
+
Parameters:
|
| 129 |
+
comparator: returns True when agent_0's value relation (e.g. < or >)
|
| 130 |
+
to agent_1 holds for an item and we should collect agent_0's
|
| 131 |
+
proposed quantity for that item.
|
| 132 |
+
opposite_comparator: inverse relation used to collect agent_1's items.
|
| 133 |
+
|
| 134 |
+
Behavior:
|
| 135 |
+
- Executes only on final timestep of a round (where the definitive
|
| 136 |
+
proposal / allocation is known via ``info['splits']``).
|
| 137 |
+
- For each item, classifies which agent's value satisfies the chosen
|
| 138 |
+
relation and records that agent's proposed quantity from the split.
|
| 139 |
+
- Averages (mean) across all qualifying items per agent; if no items
|
| 140 |
+
qualify for an agent returns ``None`` for that agent id.
|
| 141 |
+
- Adds ``all_agents`` mean across the numeric (non-None) agent values.
|
| 142 |
+
|
| 143 |
+
Why this matters:
|
| 144 |
+
Distinguishing how much an agent *asks for* when it subjectively
|
| 145 |
+
values items more (or less) than its counterpart reveals patterns of
|
| 146 |
+
opportunism vs. concession. This is especially useful when raw reward
|
| 147 |
+
differences are subtle but allocation *intent* differs.
|
| 148 |
+
"""
|
| 149 |
+
info = sl.info or {}
|
| 150 |
+
if not info or not info.get("is_last_timestep_in_round"):
|
| 151 |
+
return None
|
| 152 |
+
quantities = info.get("quantities") or {}
|
| 153 |
+
splits = info.get("splits") or {}
|
| 154 |
+
values = info.get("values") or {}
|
| 155 |
+
agent_ids: List[str] = list(sl.rewards.keys())
|
| 156 |
+
if len(agent_ids) != 2:
|
| 157 |
+
return None # Only defined for 2-agent case.
|
| 158 |
+
for aid in agent_ids:
|
| 159 |
+
if "buffer" in str(aid) and "live" not in str(aid):
|
| 160 |
+
return None
|
| 161 |
+
# Extract per-agent item proposals robustly
|
| 162 |
+
split_items = {aid: _extract_items_from_split(splits.get(aid)) for aid in agent_ids}
|
| 163 |
+
agent_0_vals: List[float] = []
|
| 164 |
+
agent_1_vals: List[float] = []
|
| 165 |
+
for item in quantities.keys():
|
| 166 |
+
# Values may be either a float (same for all items) or dict per item
|
| 167 |
+
v0_raw = values[agent_ids[0]]
|
| 168 |
+
v1_raw = values[agent_ids[1]]
|
| 169 |
+
v0 = float(v0_raw[item]) if isinstance(v0_raw, dict) else float(v0_raw)
|
| 170 |
+
v1 = float(v1_raw[item]) if isinstance(v1_raw, dict) else float(v1_raw)
|
| 171 |
+
if comparator(v0, v1):
|
| 172 |
+
agent_0_vals.append(split_items[agent_ids[0]].get(item, 0.0))
|
| 173 |
+
elif opposite_comparator(v0, v1):
|
| 174 |
+
agent_1_vals.append(split_items[agent_ids[1]].get(item, 0.0))
|
| 175 |
+
out: Dict[str, float | None] = {}
|
| 176 |
+
out[f"{metric_name}-{agent_ids[0]}"] = (
|
| 177 |
+
sum(agent_0_vals) / len(agent_0_vals) if agent_0_vals else None
|
| 178 |
+
)
|
| 179 |
+
out[f"{metric_name}-{agent_ids[1]}"] = (
|
| 180 |
+
sum(agent_1_vals) / len(agent_1_vals) if agent_1_vals else None
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
return [(key, value) for key, value in out.items() if value is not None]
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def average_proposal_when_agent_values_item_lower(
|
| 187 |
+
sl: SimulationStepLog,
|
| 188 |
+
) -> List[Tuple[str, float | None]] | None:
|
| 189 |
+
"""Mean quantity an agent proposes for items it values *less* than opponent.
|
| 190 |
+
|
| 191 |
+
Interpretation:
|
| 192 |
+
A higher value implies the agent still claims (or is allocated) a
|
| 193 |
+
notable share of items where it has a comparative *disadvantage* in
|
| 194 |
+
valuation, signaling either strategic over-claiming or protocol-driven
|
| 195 |
+
egalitarian splits. Conversely, very low numbers can indicate
|
| 196 |
+
efficient specialization or excessive concession.
|
| 197 |
+
|
| 198 |
+
Returns:
|
| 199 |
+
Mapping { agent_id: float | None, "all_agents": float | None } where
|
| 200 |
+
None indicates no qualifying items for that agent in the round.
|
| 201 |
+
"""
|
| 202 |
+
return _average_proposal_relative_value(
|
| 203 |
+
sl,
|
| 204 |
+
"average_proposal_when_agent_values_item_lower",
|
| 205 |
+
lambda a, b: a < b,
|
| 206 |
+
lambda a, b: a > b,
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def average_proposal_when_agent_values_item_higher(
|
| 211 |
+
sl: SimulationStepLog,
|
| 212 |
+
) -> List[Tuple[str, float | None]] | None:
|
| 213 |
+
"""Mean quantity an agent proposes for items it values *more* than opponent.
|
| 214 |
+
|
| 215 |
+
Interpretation:
|
| 216 |
+
Captures how aggressively an agent claims items where it holds a
|
| 217 |
+
comparative *advantage*. Elevated values can reflect rational
|
| 218 |
+
specialization (efficient exploitation of comparative advantage) or
|
| 219 |
+
potentially unfair grabs if paired with low concession in the lower
|
| 220 |
+
valuation metric. Comparing this with the 'lower' counterpart helps
|
| 221 |
+
profile negotiation style (cooperative vs. exploitative).
|
| 222 |
+
|
| 223 |
+
Returns:
|
| 224 |
+
Mapping { agent_id: float | None, "all_agents": float | None } where
|
| 225 |
+
None indicates no qualifying items.
|
| 226 |
+
"""
|
| 227 |
+
return _average_proposal_relative_value(
|
| 228 |
+
sl,
|
| 229 |
+
"average_proposal_when_agent_values_item_higher",
|
| 230 |
+
lambda a, b: a > b,
|
| 231 |
+
lambda a, b: a < b,
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
# Explicit list of metric functions exported for rendering. Helper functions
|
| 236 |
+
# starting with '_' are intentionally excluded. Update this list when adding
|
| 237 |
+
# new public statistics so render.py can rely on it instead of introspecting
|
| 238 |
+
# every callable in the module.
|
| 239 |
+
stat_functs: list[Callable[[SimulationStepLog], List[Tuple[str, float]]]] = [
|
| 240 |
+
avg_reward,
|
| 241 |
+
average_proposal_when_agent_values_item_lower,
|
| 242 |
+
average_proposal_when_agent_values_item_higher,
|
| 243 |
+
split_efficiency,
|
| 244 |
+
]
|
src_code_for_reproducibility/markov_games/negotiation/no_press_nego_agent.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Dict, List, Tuple
|
| 2 |
+
|
| 3 |
+
from mllm.markov_games.negotiation.nego_agent import (
|
| 4 |
+
NegotiationAgent,
|
| 5 |
+
NegotiationAgentState,
|
| 6 |
+
)
|
| 7 |
+
from mllm.markov_games.negotiation.nego_simulation import Split
|
| 8 |
+
from mllm.markov_games.negotiation.no_press_nego_simulation import NoPressObs
|
| 9 |
+
from mllm.markov_games.rollout_tree import AgentActLog, ChatTurn
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class NoPressAgent(NegotiationAgent):
|
| 13 |
+
def __init__(self, *args, **kwargs):
|
| 14 |
+
super().__init__(*args, **kwargs)
|
| 15 |
+
# No communication in this variant
|
| 16 |
+
self.intro_prompt = (
|
| 17 |
+
"Welcome to an iterated game. You are {agent}. The other agent is {other_agent}.\n"
|
| 18 |
+
"Setup:\n"
|
| 19 |
+
"1. The game consists of multiple independent rounds.\n"
|
| 20 |
+
"2. In each round, there are multiple items to split between the two agents.\n"
|
| 21 |
+
"3. Both agents are assigned a per-item value between 1 and 20 (inclusive) in each round.\n"
|
| 22 |
+
"4. You can observe per-item values of both agents.\n"
|
| 23 |
+
"5. Because assignments are random, both agents are equally likely to have same expected per-item value.\n"
|
| 24 |
+
"\n"
|
| 25 |
+
"Protocol:\n"
|
| 26 |
+
"1. Both agents simultaneously propose the amount of each item they will keep.\n"
|
| 27 |
+
"2. If the total sum of proposals is less than or equal to the item quantity, both agents receive their proposed amounts.\n"
|
| 28 |
+
"3. If the total sum of proposals exceeds the item quantity, they are allocated proportionally.\n"
|
| 29 |
+
"4. Your points for the round = (amount you receive per item) x (your per-item value for that round), added across all items.\n"
|
| 30 |
+
"5. Points are accumulated across rounds.\n"
|
| 31 |
+
"Your goal: {goal}\n"
|
| 32 |
+
)
|
| 33 |
+
self.new_round_prompt = (
|
| 34 |
+
"A New Round Begins\n"
|
| 35 |
+
"The items to split are {quantities}.\n"
|
| 36 |
+
"Your per-item values are {value} and {other_agent}'s per-item values are {other_value}."
|
| 37 |
+
)
|
| 38 |
+
self.last_round_prompt = (
|
| 39 |
+
"Last Round Summary:\n"
|
| 40 |
+
" - Items to split: {last_quantities}\n"
|
| 41 |
+
" - Your per-item values: {last_value_agent}\n"
|
| 42 |
+
" - {other_agent}'s per-item values: {last_value_coagent}\n"
|
| 43 |
+
" - You proposed: {last_split_agent}\n"
|
| 44 |
+
" - You earned: {last_points_agent} points\n"
|
| 45 |
+
" - {other_agent} proposed: {last_split_coagent}\n"
|
| 46 |
+
" - {other_agent} earned: {last_points_coagent} points\n"
|
| 47 |
+
" - Round Complete.\n"
|
| 48 |
+
)
|
| 49 |
+
self.send_split_prompt = "Submit Your Proposal\n" "Respond as {proposal_style}"
|
| 50 |
+
|
| 51 |
+
def get_message_regex(self, observation: NoPressObs) -> str:
|
| 52 |
+
return r"^$" # No messages allowed
|
| 53 |
+
|
| 54 |
+
def get_split_regex(self, observation: NoPressObs) -> str:
|
| 55 |
+
items = list(observation.quantities.keys())
|
| 56 |
+
# Accept both singular and plural forms
|
| 57 |
+
item_pattern = "|".join(
|
| 58 |
+
[f"{item[:-1]}s?" if item.endswith("s") else f"{item}s?" for item in items]
|
| 59 |
+
)
|
| 60 |
+
regex = rf"(?i)Proposal:\s*((?:\s*(?P<num>(10|[0-9]))\s*(?P<item>{item_pattern})\s*,?)+)"
|
| 61 |
+
return regex
|
| 62 |
+
|
| 63 |
+
def get_split_action(self, policy_output: str, observation: NoPressObs) -> Split:
|
| 64 |
+
items = list(observation.quantities.keys())
|
| 65 |
+
import re as _re
|
| 66 |
+
|
| 67 |
+
split_regex = self.get_split_regex(observation)
|
| 68 |
+
items_given_to_self = {item: 0 for item in items}
|
| 69 |
+
m = _re.match(split_regex, policy_output.strip())
|
| 70 |
+
if m:
|
| 71 |
+
# Find all (number, item) pairs
|
| 72 |
+
item_pattern = "|".join(
|
| 73 |
+
[
|
| 74 |
+
f"{item[:-1]}s?" if item.endswith("s") else f"{item}s?"
|
| 75 |
+
for item in items
|
| 76 |
+
]
|
| 77 |
+
)
|
| 78 |
+
inner_regex = rf"(?i)(10|[0-9])\s*({item_pattern})"
|
| 79 |
+
|
| 80 |
+
def normalize_item_name(item_str):
|
| 81 |
+
for orig in items:
|
| 82 |
+
if item_str.lower() == orig.lower():
|
| 83 |
+
return orig
|
| 84 |
+
if orig.endswith("s") and item_str.lower() == orig[:-1].lower():
|
| 85 |
+
return orig
|
| 86 |
+
if (
|
| 87 |
+
not orig.endswith("s")
|
| 88 |
+
and item_str.lower() == orig.lower() + "s"
|
| 89 |
+
):
|
| 90 |
+
return orig
|
| 91 |
+
|
| 92 |
+
for num, item in _re.findall(inner_regex, m.group(1)):
|
| 93 |
+
items_given_to_self[normalize_item_name(item)] = int(num)
|
| 94 |
+
return Split(items_given_to_self=items_given_to_self)
|
src_code_for_reproducibility/markov_games/negotiation/no_press_nego_simulation.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import copy
|
| 2 |
+
from collections import defaultdict
|
| 3 |
+
from dataclasses import dataclass
|
| 4 |
+
from typing import Any, Dict, List, Literal, Tuple
|
| 5 |
+
|
| 6 |
+
from mllm.markov_games.negotiation.nego_simulation import (
|
| 7 |
+
NegotiationObs,
|
| 8 |
+
NegotiationSimulation,
|
| 9 |
+
NegotiationState,
|
| 10 |
+
Split,
|
| 11 |
+
compute_tas_style_rewards,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
AgentId = str
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@dataclass
|
| 18 |
+
class NoPressState(NegotiationState):
|
| 19 |
+
pass
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@dataclass
|
| 23 |
+
class NoPressObs(NegotiationObs):
|
| 24 |
+
other_value: Dict[str, float]
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class NoPressSimulation(NegotiationSimulation):
|
| 28 |
+
def __init__(
|
| 29 |
+
self,
|
| 30 |
+
game_type: Literal["10-1-exclusive", "10-1-ties", "1-to-20"] = "1-to-20",
|
| 31 |
+
same_round_value: bool = True,
|
| 32 |
+
atleast_one_conflict: bool = False,
|
| 33 |
+
*args,
|
| 34 |
+
**kwargs,
|
| 35 |
+
):
|
| 36 |
+
self.game_type = game_type
|
| 37 |
+
self.same_round_value = same_round_value
|
| 38 |
+
self.atleast_one_conflict = atleast_one_conflict
|
| 39 |
+
super().__init__(*args, **kwargs)
|
| 40 |
+
|
| 41 |
+
def _sample_values(self) -> Dict[AgentId, dict]:
|
| 42 |
+
values = defaultdict(dict)
|
| 43 |
+
if self.state is None:
|
| 44 |
+
item_types = self.item_types
|
| 45 |
+
else:
|
| 46 |
+
item_types = list(self.state.quantities.keys())
|
| 47 |
+
while True:
|
| 48 |
+
for item in item_types:
|
| 49 |
+
if self.game_type == "10-1-exclusive":
|
| 50 |
+
v = int(self.rng.choice([1, 10]))
|
| 51 |
+
values[self.agent_ids[0]][item] = v
|
| 52 |
+
values[self.agent_ids[1]][item] = 10 if v == 1 else 1
|
| 53 |
+
elif self.game_type == "10-1-ties":
|
| 54 |
+
for aid in self.agent_ids:
|
| 55 |
+
values[aid][item] = int(self.rng.choice([1, 10]))
|
| 56 |
+
elif self.game_type == "1-to-20":
|
| 57 |
+
for aid in self.agent_ids:
|
| 58 |
+
values[aid][item] = int(self.rng.integers(1, 21))
|
| 59 |
+
if self.atleast_one_conflict:
|
| 60 |
+
has_conflict = False
|
| 61 |
+
for item in item_types:
|
| 62 |
+
agent_values_for_item = [
|
| 63 |
+
values[aid][item] for aid in self.agent_ids
|
| 64 |
+
]
|
| 65 |
+
if len(set(agent_values_for_item)) > 1:
|
| 66 |
+
has_conflict = True
|
| 67 |
+
break
|
| 68 |
+
if not has_conflict:
|
| 69 |
+
continue
|
| 70 |
+
agent_values = [sum(v.values()) for v in values.values()]
|
| 71 |
+
if len(set(agent_values)) == 1 or not self.same_round_value:
|
| 72 |
+
break
|
| 73 |
+
return values
|
| 74 |
+
|
| 75 |
+
def _sample_quantities(self) -> Dict[str, int]:
|
| 76 |
+
return {item.lower(): 10 for item in self.item_types}
|
| 77 |
+
|
| 78 |
+
def set_new_round_of_variant(self):
|
| 79 |
+
self.state.quantities = self._sample_quantities()
|
| 80 |
+
self.state.values = self._sample_values()
|
| 81 |
+
self.state.split_phase = True
|
| 82 |
+
|
| 83 |
+
def get_info_of_variant(
|
| 84 |
+
self, state: NegotiationState, actions: Dict[AgentId, Any]
|
| 85 |
+
) -> Dict[str, Any]:
|
| 86 |
+
return {
|
| 87 |
+
"quantities": copy.deepcopy(state.quantities),
|
| 88 |
+
"values": copy.deepcopy(state.values),
|
| 89 |
+
"splits": copy.deepcopy(state.splits),
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
def get_rewards(self, splits: Dict[AgentId, Split]) -> Dict[AgentId, float]:
|
| 93 |
+
return compute_tas_style_rewards(
|
| 94 |
+
self.agent_ids, self.state.values, splits, self.state.quantities
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
def get_obs(self):
|
| 98 |
+
return {agent_id: self.get_obs_agent(agent_id) for agent_id in self.agent_ids}
|
| 99 |
+
|
| 100 |
+
def get_obs_agent(self, agent_id):
|
| 101 |
+
other_id = self._other(agent_id)
|
| 102 |
+
last_value_coagent = (
|
| 103 |
+
None
|
| 104 |
+
if self.state.previous_values is None
|
| 105 |
+
else self.state.previous_values.get(other_id)
|
| 106 |
+
)
|
| 107 |
+
last_points_coagent = (
|
| 108 |
+
None
|
| 109 |
+
if self.state.previous_points is None
|
| 110 |
+
else round(self.state.previous_points.get(other_id), 1)
|
| 111 |
+
)
|
| 112 |
+
last_value_agent = (
|
| 113 |
+
None
|
| 114 |
+
if self.state.previous_values is None
|
| 115 |
+
else self.state.previous_values.get(agent_id)
|
| 116 |
+
)
|
| 117 |
+
last_points_agent = (
|
| 118 |
+
None
|
| 119 |
+
if self.state.previous_points is None
|
| 120 |
+
else round(self.state.previous_points.get(agent_id), 1)
|
| 121 |
+
)
|
| 122 |
+
last_split_coagent = None
|
| 123 |
+
last_split_agent = None
|
| 124 |
+
if self.state.previous_splits is not None:
|
| 125 |
+
last_split_coagent = self.state.previous_splits[
|
| 126 |
+
other_id
|
| 127 |
+
].items_given_to_self
|
| 128 |
+
last_split_agent = self.state.previous_splits[agent_id].items_given_to_self
|
| 129 |
+
obs = NoPressObs(
|
| 130 |
+
round_nb=self.state.round_nb,
|
| 131 |
+
last_message="",
|
| 132 |
+
quota_messages_per_agent_per_round=self.quota_messages_per_agent_per_round,
|
| 133 |
+
current_agent=self.state.current_agent,
|
| 134 |
+
other_agent=self.agent_id_to_name[other_id],
|
| 135 |
+
quantities=self.state.quantities,
|
| 136 |
+
item_types=self.item_types,
|
| 137 |
+
value=self.state.values[agent_id],
|
| 138 |
+
split_phase=self.state.split_phase,
|
| 139 |
+
last_split_agent=last_split_agent,
|
| 140 |
+
last_value_agent=last_value_agent,
|
| 141 |
+
last_points_agent=last_points_agent,
|
| 142 |
+
last_split_coagent=last_split_coagent,
|
| 143 |
+
last_value_coagent=last_value_coagent,
|
| 144 |
+
last_points_coagent=last_points_coagent,
|
| 145 |
+
other_value=self.state.values[other_id],
|
| 146 |
+
last_quantities=self.state.previous_quantities,
|
| 147 |
+
)
|
| 148 |
+
return obs
|
| 149 |
+
|
| 150 |
+
def reset(self):
|
| 151 |
+
start_agent = self.agent_ids[self._starting_agent_index]
|
| 152 |
+
quantities = self._sample_quantities()
|
| 153 |
+
values = self._sample_values()
|
| 154 |
+
self.state = NoPressState(
|
| 155 |
+
round_nb=0,
|
| 156 |
+
last_message="",
|
| 157 |
+
current_agent=start_agent,
|
| 158 |
+
quantities=quantities,
|
| 159 |
+
values=values,
|
| 160 |
+
previous_values=None,
|
| 161 |
+
splits={aid: None for aid in self.agent_ids},
|
| 162 |
+
nb_messages_sent={aid: 0 for aid in self.agent_ids},
|
| 163 |
+
split_phase=True,
|
| 164 |
+
previous_splits=None,
|
| 165 |
+
previous_points=None,
|
| 166 |
+
previous_quantities=None,
|
| 167 |
+
)
|
| 168 |
+
return self.get_obs()
|
src_code_for_reproducibility/markov_games/negotiation/tas_agent.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mllm.markov_games.negotiation.nego_agent import NegotiationAgent
|
| 2 |
+
from mllm.markov_games.negotiation.nego_simulation import Split
|
| 3 |
+
from mllm.markov_games.negotiation.tas_simulation import TrustAndSplitObs
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TrustAndSplitAgent(NegotiationAgent):
|
| 7 |
+
def __init__(self, num_message_chars, *args, **kwargs):
|
| 8 |
+
self.num_message_chars = num_message_chars
|
| 9 |
+
super().__init__(*args, **kwargs)
|
| 10 |
+
self.intro_prompt = (
|
| 11 |
+
"Welcome to an iterated game. You are {agent}. The other agent is {other_agent}.\n"
|
| 12 |
+
"Setup:\n"
|
| 13 |
+
"1. The game has multiple independent rounds.\n"
|
| 14 |
+
"2. In each round, there are multiple items to split between the two agents.\n"
|
| 15 |
+
"3. Both agents are assigned a per-item value between 1 and 20 (inclusive) in each round.\n"
|
| 16 |
+
"4. You can only observe your own per-item values.\n"
|
| 17 |
+
"5. Because assignments are random, both agents are equally likely to have same expected per-item value.\n"
|
| 18 |
+
"\n"
|
| 19 |
+
"Protocol:\n"
|
| 20 |
+
"1. At the start of the round, one agent begins the conversation. The starting role alternates each round.\n"
|
| 21 |
+
"2. Agents exchange a short chat ({quota_messages_per_agent_per_round} messages per round per agent) to negotiate how to split the item.\n"
|
| 22 |
+
" - Use this chat to communicate your private per-item value to make informed proposals.\n"
|
| 23 |
+
"3. After the chat, both agents simultaneously propose the amount of each item they will keep.\n"
|
| 24 |
+
"4. If the total sum of proposals is less than or equal to the item quantity, both agents receive their proposed amounts.\n"
|
| 25 |
+
"5. If the total sum of proposals exceeds the item quantity, they are allocated proportionally.\n"
|
| 26 |
+
"6. Your points for the round = (amount you receive per item) x (your per-item value for that round), added across all items.\n"
|
| 27 |
+
"7. Points are accumulated across rounds.\n"
|
| 28 |
+
"Your goal: {goal}\n"
|
| 29 |
+
)
|
| 30 |
+
self.new_round_prompt = (
|
| 31 |
+
"A New Round Begins\n"
|
| 32 |
+
"The items to split are {quantities}.\n"
|
| 33 |
+
"Your per-item values are {value}."
|
| 34 |
+
)
|
| 35 |
+
self.last_round_prompt = (
|
| 36 |
+
"Last Round Summary:\n"
|
| 37 |
+
" - Items to split: {last_quantities}\n"
|
| 38 |
+
" - Your per-item values: {last_value_agent}\n"
|
| 39 |
+
" - {other_agent}'s per-item values: {last_value_coagent}\n"
|
| 40 |
+
" - You proposed: {last_split_agent}\n"
|
| 41 |
+
" - You earned: {last_points_agent} points\n"
|
| 42 |
+
" - {other_agent} proposed: {last_split_coagent}\n"
|
| 43 |
+
" - {other_agent} earned: {last_points_coagent} points\n"
|
| 44 |
+
" - Round Complete.\n"
|
| 45 |
+
)
|
| 46 |
+
self.send_split_prompt = (
|
| 47 |
+
"Message quota is finished for this round.\n"
|
| 48 |
+
"{other_agent} has finalized their proposal.\n"
|
| 49 |
+
"Submit your finalization now\n"
|
| 50 |
+
"Respond with {proposal_style2}"
|
| 51 |
+
)
|
| 52 |
+
# self.wait_for_message_prompt = "Wait for {other_agent} to send a message..."
|
| 53 |
+
self.wait_for_message_prompt = ""
|
| 54 |
+
self.last_message_prompt = "{other_agent} said: {last_message}"
|
| 55 |
+
# self.send_message_prompt = (
|
| 56 |
+
# f"Send your message now (max {self.num_message_chars} chars)."
|
| 57 |
+
# )
|
| 58 |
+
self.send_message_prompt = f"Send your message now in <message>...</message> (<={self.num_message_chars} chars)."
|
| 59 |
+
|
| 60 |
+
def get_message_regex(self, observation: TrustAndSplitObs) -> str:
|
| 61 |
+
return rf"<message>[\s\S]{{0,{self.num_message_chars}}}</message>"
|
| 62 |
+
|
| 63 |
+
# def get_message_regex(self, observation: TrustAndSplitObs) -> str:
|
| 64 |
+
# return rf"(?s).{{0,{self.num_message_chars}}}"
|
| 65 |
+
|
| 66 |
+
def get_split_regex(self, observation: TrustAndSplitObs) -> str:
|
| 67 |
+
items = list(observation.quantities.keys())
|
| 68 |
+
# Accept both singular and plural forms
|
| 69 |
+
item_pattern = "|".join(
|
| 70 |
+
[f"{item[:-1]}s?" if item.endswith("s") else f"{item}s?" for item in items]
|
| 71 |
+
)
|
| 72 |
+
regex = rf"(?i)<items_to_self> ?((?:\s*(?P<num>(10|[0-9]))\s*(?P<item>{item_pattern})\s*,?)+) ?</items_to_self>"
|
| 73 |
+
return regex
|
| 74 |
+
|
| 75 |
+
def get_split_action(
|
| 76 |
+
self, policy_output: str, observation: TrustAndSplitObs
|
| 77 |
+
) -> Split:
|
| 78 |
+
items = list(observation.quantities.keys())
|
| 79 |
+
import re as _re
|
| 80 |
+
|
| 81 |
+
split_regex = self.get_split_regex(observation)
|
| 82 |
+
items_given_to_self = {item: 0 for item in items}
|
| 83 |
+
m = _re.match(split_regex, policy_output.strip())
|
| 84 |
+
if m:
|
| 85 |
+
# Find all (number, item) pairs
|
| 86 |
+
item_pattern = "|".join(
|
| 87 |
+
[
|
| 88 |
+
f"{item[:-1]}s?" if item.endswith("s") else f"{item}s?"
|
| 89 |
+
for item in items
|
| 90 |
+
]
|
| 91 |
+
)
|
| 92 |
+
inner_regex = rf"(?i)(10|[0-9])\s*({item_pattern})"
|
| 93 |
+
|
| 94 |
+
def normalize_item_name(item_str):
|
| 95 |
+
for orig in items:
|
| 96 |
+
if item_str.lower() == orig.lower():
|
| 97 |
+
return orig
|
| 98 |
+
if orig.endswith("s") and item_str.lower() == orig[:-1].lower():
|
| 99 |
+
return orig
|
| 100 |
+
if (
|
| 101 |
+
not orig.endswith("s")
|
| 102 |
+
and item_str.lower() == orig.lower() + "s"
|
| 103 |
+
):
|
| 104 |
+
return orig
|
| 105 |
+
|
| 106 |
+
for num, item in _re.findall(inner_regex, m.group(1)):
|
| 107 |
+
items_given_to_self[normalize_item_name(item)] = int(num)
|
| 108 |
+
return Split(items_given_to_self=items_given_to_self)
|
src_code_for_reproducibility/markov_games/negotiation/tas_rps_agent.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import copy
|
| 2 |
+
from collections.abc import Callable
|
| 3 |
+
from dataclasses import dataclass
|
| 4 |
+
from typing import Any, Dict, List, Tuple
|
| 5 |
+
|
| 6 |
+
from mllm.markov_games.agent import Agent
|
| 7 |
+
from mllm.markov_games.negotiation.nego_agent import (
|
| 8 |
+
Message,
|
| 9 |
+
NegotiationAgent,
|
| 10 |
+
NegotiationAgentState,
|
| 11 |
+
Split,
|
| 12 |
+
)
|
| 13 |
+
from mllm.markov_games.negotiation.tas_rps_simulation import TrustAndSplitRPSObs
|
| 14 |
+
from mllm.markov_games.rollout_tree import AgentActLog, ChatTurn
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class TrustAndSplitRPSAgent(NegotiationAgent):
|
| 18 |
+
def __init__(
|
| 19 |
+
self,
|
| 20 |
+
num_message_chars: int,
|
| 21 |
+
message_start_end_format: bool = False,
|
| 22 |
+
proposal_start_end_format: bool = False,
|
| 23 |
+
*args,
|
| 24 |
+
**kwargs,
|
| 25 |
+
):
|
| 26 |
+
self.num_message_chars = num_message_chars
|
| 27 |
+
self.message_start_end_format = message_start_end_format
|
| 28 |
+
self.proposal_start_end_format = proposal_start_end_format
|
| 29 |
+
super().__init__(*args, **kwargs)
|
| 30 |
+
self.intro_prompt = (
|
| 31 |
+
"Welcome to an iterated game. You are {agent}. The other agent is {other_agent}.\n"
|
| 32 |
+
"\n"
|
| 33 |
+
"Setup:\n"
|
| 34 |
+
"1. The game has multiple independent rounds.\n"
|
| 35 |
+
"2. In each round, there are 10 coins to split between the two agents.\n"
|
| 36 |
+
"3. Each agent's per-coin value for that round is determined as follows:\n"
|
| 37 |
+
" - Both agents are randomly assigned a rock, paper or scissors hands\n"
|
| 38 |
+
" - Rock has the upper hand over scissors, scissors has the upper hand over paper and paper has the upper hand over rock.\n"
|
| 39 |
+
" - The agent with the upper hand has a per-coin value of 10.\n"
|
| 40 |
+
" - The agent with the lower hand has a per-coin value of 1.\n"
|
| 41 |
+
"4. You only see your own hand, but you may communicate it in messages and infer your value based on the other agent's hand.\n"
|
| 42 |
+
"5. Over many rounds both agents are equally likely to have the upper and lower hand.\n"
|
| 43 |
+
"\n"
|
| 44 |
+
"Protocol:\n"
|
| 45 |
+
"1. At the start of the round, one agent begins the conversation. The starting role alternates each round.\n"
|
| 46 |
+
"2. Agents exchange a short chat ({quota_messages_per_agent_per_round} messages per round per agent) to negotiate how to split the 10 coins.\n"
|
| 47 |
+
" - Use this chat to communicate your hand so that both agents can determine their per-coin values.\n"
|
| 48 |
+
"3. After the chat, both agents simultaneously propose how many coins they keep.\n"
|
| 49 |
+
"4. If the total sum of proposals is less than or equal to 10, both agents receive their proposals.\n"
|
| 50 |
+
"5. If the total sum of proposals exceeds 10, the coins are allocated proportionally.\n"
|
| 51 |
+
"6. Your points for the round = (coins you receive) x (your per-coin value for that round). \n"
|
| 52 |
+
"7. The points are accumulated across rounds.\n"
|
| 53 |
+
"Your goal: {goal}\n"
|
| 54 |
+
)
|
| 55 |
+
self.new_round_prompt = (
|
| 56 |
+
"A New Round Begins\n"
|
| 57 |
+
"Your hand is {hand}. You don't know {other_agent}'s hand yet.\n"
|
| 58 |
+
)
|
| 59 |
+
# self.last_round_prompt = (
|
| 60 |
+
# "Last Round Summary:\n"
|
| 61 |
+
# " - Your hand: {last_hand_agent}\n"
|
| 62 |
+
# " - {other_agent}'s hand: {last_hand_coagent}\n"
|
| 63 |
+
# " - Your value per coin: {last_value_agent}\n"
|
| 64 |
+
# " - {other_agent}'s value per coin: {last_value_coagent}\n"
|
| 65 |
+
# " - You proposed: {last_split_agent} coins\n"
|
| 66 |
+
# " - You earned: {last_points_agent} points\n"
|
| 67 |
+
# " - {other_agent} proposed: {last_split_coagent} coins\n"
|
| 68 |
+
# " - {other_agent} earned: {last_points_coagent} points\n"
|
| 69 |
+
# " - Round Complete.\n"
|
| 70 |
+
# )
|
| 71 |
+
self.last_round_prompt = "In the previous round, {other_agent} had a {last_hand_value_coagent} hand and proposed {last_split_coagent} coins.\n"
|
| 72 |
+
if self.proposal_start_end_format:
|
| 73 |
+
self.send_split_prompt = (
|
| 74 |
+
"Submit your proposal\n"
|
| 75 |
+
"Respond with <<proposal_start>> x <<proposal_end>> where x is an integer in [0, 10]."
|
| 76 |
+
)
|
| 77 |
+
else:
|
| 78 |
+
self.send_split_prompt = (
|
| 79 |
+
"Submit your proposal\n"
|
| 80 |
+
"Respond with <coins_to_self> x </coins_to_self> where x is an integer in [0, 10]."
|
| 81 |
+
)
|
| 82 |
+
self.wait_for_message_prompt = "Wait for {other_agent} to send a message..."
|
| 83 |
+
# self.wait_for_message_prompt = ""
|
| 84 |
+
self.last_message_prompt = "{other_agent} said: {last_message}"
|
| 85 |
+
if self.message_start_end_format:
|
| 86 |
+
self.send_message_prompt = f"Send your message now in <<message_start>>...<<message_end>> (<={self.num_message_chars} chars)."
|
| 87 |
+
else:
|
| 88 |
+
self.send_message_prompt = f"Send your message now in <message>...</message> (<={self.num_message_chars} chars)."
|
| 89 |
+
|
| 90 |
+
def get_message_regex(self, observation: TrustAndSplitRPSObs) -> str:
|
| 91 |
+
if self.message_start_end_format:
|
| 92 |
+
return (
|
| 93 |
+
rf"<<message_start>>[\s\S]{{0,{self.num_message_chars}}}<<message_end>>"
|
| 94 |
+
)
|
| 95 |
+
else:
|
| 96 |
+
return rf"<message>[\s\S]{{0,{self.num_message_chars}}}</message>"
|
| 97 |
+
|
| 98 |
+
def get_split_regex(self, observation: TrustAndSplitRPSObs) -> str:
|
| 99 |
+
if self.proposal_start_end_format:
|
| 100 |
+
return r"<<proposal_start>> ?(10|[0-9]) ?<<proposal_end>>"
|
| 101 |
+
else:
|
| 102 |
+
return r"<coins_to_self> ?(10|[0-9]) ?</coins_to_self>"
|
| 103 |
+
|
| 104 |
+
def get_split_action(
|
| 105 |
+
self, policy_output: str, observation: TrustAndSplitRPSObs
|
| 106 |
+
) -> Split:
|
| 107 |
+
import re as _re
|
| 108 |
+
|
| 109 |
+
if self.proposal_start_end_format:
|
| 110 |
+
m = _re.search(
|
| 111 |
+
r"<<proposal_start>> ?(10|[0-9]) ?<<proposal_end>>", policy_output
|
| 112 |
+
)
|
| 113 |
+
else:
|
| 114 |
+
m = _re.search(
|
| 115 |
+
r"<coins_to_self> ?(10|[0-9]) ?</coins_to_self>", policy_output
|
| 116 |
+
)
|
| 117 |
+
coins_int = int(m.group(1)) if m else int(policy_output)
|
| 118 |
+
return Split(items_given_to_self={"coins": coins_int})
|
src_code_for_reproducibility/markov_games/negotiation/tas_rps_simulation.py
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Trust-and-Split simulation.
|
| 3 |
+
|
| 4 |
+
This environment models a simple bargaining game over 10 coins with messaging.
|
| 5 |
+
Agents are assigned rock/paper/scissors hands, with the winner getting value 10 per coin
|
| 6 |
+
and the loser getting value 1 per coin. Agents alternate sending messages for a fixed
|
| 7 |
+
number of turns per round and then each submits a split proposal indicating how many
|
| 8 |
+
coins they keep for themselves. Rewards are proportional if the proposed totals exceed 10.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import copy
|
| 12 |
+
from dataclasses import dataclass
|
| 13 |
+
from typing import Any, Dict, List, Literal, Tuple
|
| 14 |
+
|
| 15 |
+
from numpy.random import default_rng
|
| 16 |
+
|
| 17 |
+
from mllm.markov_games.negotiation.nego_simulation import (
|
| 18 |
+
Message,
|
| 19 |
+
NegotiationObs,
|
| 20 |
+
NegotiationSimulation,
|
| 21 |
+
NegotiationState,
|
| 22 |
+
Split,
|
| 23 |
+
compute_tas_style_rewards,
|
| 24 |
+
)
|
| 25 |
+
from mllm.markov_games.rollout_tree import SimulationStepLog
|
| 26 |
+
|
| 27 |
+
AgentId = str
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _get_rps_winner(
|
| 31 |
+
hand1: Literal["rock", "paper", "scissors"],
|
| 32 |
+
hand2: Literal["rock", "paper", "scissors"],
|
| 33 |
+
) -> Literal["rock", "paper", "scissors"]:
|
| 34 |
+
"""Determine winner of rock-paper-scissors between two hands."""
|
| 35 |
+
if hand1 == hand2:
|
| 36 |
+
raise ValueError("Hands should be different")
|
| 37 |
+
if (
|
| 38 |
+
(hand1 == "rock" and hand2 == "scissors")
|
| 39 |
+
or (hand1 == "paper" and hand2 == "rock")
|
| 40 |
+
or (hand1 == "scissors" and hand2 == "paper")
|
| 41 |
+
):
|
| 42 |
+
return hand1
|
| 43 |
+
else:
|
| 44 |
+
return hand2
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@dataclass
|
| 48 |
+
class TrustAndSplitRPSState(NegotiationState):
|
| 49 |
+
hands: Dict[
|
| 50 |
+
AgentId, Literal["rock", "paper", "scissors"]
|
| 51 |
+
] # rock, paper, or scissors
|
| 52 |
+
previous_hands: Dict[AgentId, Literal["rock", "paper", "scissors"]] | None
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@dataclass
|
| 56 |
+
class TrustAndSplitRPSObs(NegotiationObs):
|
| 57 |
+
hand: Literal["rock", "paper", "scissors"]
|
| 58 |
+
last_hand_agent: Literal["rock", "paper", "scissors"] | None
|
| 59 |
+
last_hand_coagent: Literal["rock", "paper", "scissors"] | None
|
| 60 |
+
last_hand_value_coagent: Literal["upper", "lower"] | None
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class TrustAndSplitRPSSimulation(NegotiationSimulation):
|
| 64 |
+
def __init__(
|
| 65 |
+
self,
|
| 66 |
+
alternating_hands: bool = False,
|
| 67 |
+
alternating_mix_ratio: float = None,
|
| 68 |
+
*args,
|
| 69 |
+
**kwargs,
|
| 70 |
+
):
|
| 71 |
+
self.alternating_hands = alternating_hands
|
| 72 |
+
self.alternating_mix_ratio = alternating_mix_ratio
|
| 73 |
+
super().__init__(*args, **kwargs)
|
| 74 |
+
if self.alternating_mix_ratio is not None:
|
| 75 |
+
if self.rng.random() < self.alternating_mix_ratio:
|
| 76 |
+
self.alternating_hands = True
|
| 77 |
+
else:
|
| 78 |
+
self.alternating_hands = False
|
| 79 |
+
|
| 80 |
+
def _sample_hands_and_values(
|
| 81 |
+
self,
|
| 82 |
+
alternate_hands: bool = False,
|
| 83 |
+
) -> Tuple[Dict[AgentId, str], Dict[AgentId, float]]:
|
| 84 |
+
hands = ["rock", "paper", "scissors"]
|
| 85 |
+
if alternate_hands:
|
| 86 |
+
previous_hands = list(self.state.previous_hands.values())
|
| 87 |
+
hand1, hand2 = self.rng.choice(hands, size=2, replace=False)
|
| 88 |
+
winner = _get_rps_winner(hand1, hand2)
|
| 89 |
+
loser = hand1 if winner == hand2 else hand2
|
| 90 |
+
previous_winner = _get_rps_winner(previous_hands[0], previous_hands[1])
|
| 91 |
+
agent_hands, values = {}, {}
|
| 92 |
+
for agent_id in self.agent_ids:
|
| 93 |
+
if self.state.previous_hands[agent_id] == previous_winner:
|
| 94 |
+
agent_hands[agent_id] = loser
|
| 95 |
+
values[agent_id] = 1.0
|
| 96 |
+
else:
|
| 97 |
+
agent_hands[agent_id] = winner
|
| 98 |
+
values[agent_id] = 10.0
|
| 99 |
+
return agent_hands, values
|
| 100 |
+
else:
|
| 101 |
+
# Assign different hands to each agent
|
| 102 |
+
hand1, hand2 = self.rng.choice(hands, size=2, replace=False)
|
| 103 |
+
|
| 104 |
+
agent_hands = {self.agent_ids[0]: hand1, self.agent_ids[1]: hand2}
|
| 105 |
+
|
| 106 |
+
# Determine winner and assign values
|
| 107 |
+
winner = _get_rps_winner(hand1, hand2)
|
| 108 |
+
values = {}
|
| 109 |
+
for agent_id in self.agent_ids:
|
| 110 |
+
if agent_hands[agent_id] == winner:
|
| 111 |
+
values[agent_id] = 10.0 # Winner gets value 10
|
| 112 |
+
else:
|
| 113 |
+
values[agent_id] = 1.0 # Loser gets value 1
|
| 114 |
+
|
| 115 |
+
return agent_hands, values
|
| 116 |
+
|
| 117 |
+
def set_new_round_of_variant(self):
|
| 118 |
+
self.state.previous_hands = copy.deepcopy(self.state.hands)
|
| 119 |
+
new_hands, new_values = self._sample_hands_and_values(
|
| 120 |
+
alternate_hands=self.alternating_hands
|
| 121 |
+
)
|
| 122 |
+
self.state.hands = new_hands
|
| 123 |
+
self.state.values = new_values
|
| 124 |
+
# Quantities are constant in TAS
|
| 125 |
+
self.state.quantities = {"coins": 10}
|
| 126 |
+
self.state.split_phase = False
|
| 127 |
+
|
| 128 |
+
def get_info_of_variant(
|
| 129 |
+
self, state: NegotiationState, actions: Dict[AgentId, Any]
|
| 130 |
+
) -> Dict[str, Any]:
|
| 131 |
+
return {
|
| 132 |
+
"quantities": copy.deepcopy(state.quantities),
|
| 133 |
+
"hands": copy.deepcopy(state.hands),
|
| 134 |
+
"values": copy.deepcopy(state.values),
|
| 135 |
+
"previous_hands": copy.deepcopy(state.previous_hands),
|
| 136 |
+
"previous_values": copy.deepcopy(state.previous_values),
|
| 137 |
+
"splits": copy.deepcopy(state.splits),
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
def get_rewards(self, splits: Dict[AgentId, Split]) -> Dict[AgentId, float]:
|
| 141 |
+
return compute_tas_style_rewards(
|
| 142 |
+
self.agent_ids, self.state.values, splits, self.state.quantities
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
def get_obs_agent(self, agent_id):
|
| 146 |
+
"""Returns observation for agent_id"""
|
| 147 |
+
other_id = self._other(agent_id)
|
| 148 |
+
last_value_coagent = (
|
| 149 |
+
None
|
| 150 |
+
if self.state.previous_values is None
|
| 151 |
+
else self.state.previous_values.get(other_id)
|
| 152 |
+
)
|
| 153 |
+
last_hand_coagent = (
|
| 154 |
+
None
|
| 155 |
+
if self.state.previous_hands is None
|
| 156 |
+
else self.state.previous_hands.get(other_id)
|
| 157 |
+
)
|
| 158 |
+
last_points_coagent = (
|
| 159 |
+
None
|
| 160 |
+
if self.state.previous_points is None
|
| 161 |
+
else round(self.state.previous_points.get(other_id), 1)
|
| 162 |
+
)
|
| 163 |
+
last_value_agent = (
|
| 164 |
+
None
|
| 165 |
+
if self.state.previous_values is None
|
| 166 |
+
else self.state.previous_values.get(agent_id)
|
| 167 |
+
)
|
| 168 |
+
last_hand_agent = (
|
| 169 |
+
None
|
| 170 |
+
if self.state.previous_hands is None
|
| 171 |
+
else self.state.previous_hands.get(agent_id)
|
| 172 |
+
)
|
| 173 |
+
last_points_agent = (
|
| 174 |
+
None
|
| 175 |
+
if self.state.previous_points is None
|
| 176 |
+
else round(self.state.previous_points.get(agent_id), 1)
|
| 177 |
+
)
|
| 178 |
+
last_split_coagent = None
|
| 179 |
+
last_split_agent = None
|
| 180 |
+
if self.state.previous_splits is not None:
|
| 181 |
+
last_split_coagent = self.state.previous_splits[
|
| 182 |
+
other_id
|
| 183 |
+
].items_given_to_self["coins"]
|
| 184 |
+
last_split_agent = self.state.previous_splits[agent_id].items_given_to_self[
|
| 185 |
+
"coins"
|
| 186 |
+
]
|
| 187 |
+
if last_hand_agent is None or last_hand_coagent is None:
|
| 188 |
+
last_hand_value_coagent = None
|
| 189 |
+
else:
|
| 190 |
+
winner = _get_rps_winner(last_hand_agent, last_hand_coagent)
|
| 191 |
+
last_hand_value_coagent = (
|
| 192 |
+
"upper" if winner == last_hand_coagent else "lower"
|
| 193 |
+
)
|
| 194 |
+
obs = TrustAndSplitRPSObs(
|
| 195 |
+
round_nb=self.state.round_nb,
|
| 196 |
+
last_message=self.state.last_message,
|
| 197 |
+
quota_messages_per_agent_per_round=self.quota_messages_per_agent_per_round,
|
| 198 |
+
current_agent=self.state.current_agent,
|
| 199 |
+
other_agent=self.agent_id_to_name[other_id],
|
| 200 |
+
quantities={"coins": 10},
|
| 201 |
+
item_types=self.item_types,
|
| 202 |
+
value=self.state.values[agent_id],
|
| 203 |
+
split_phase=self.state.split_phase,
|
| 204 |
+
last_split_agent=last_split_agent,
|
| 205 |
+
last_value_agent=last_value_agent,
|
| 206 |
+
last_points_agent=last_points_agent,
|
| 207 |
+
last_split_coagent=last_split_coagent,
|
| 208 |
+
last_value_coagent=last_value_coagent,
|
| 209 |
+
last_points_coagent=last_points_coagent,
|
| 210 |
+
hand=self.state.hands[agent_id],
|
| 211 |
+
last_hand_coagent=last_hand_coagent,
|
| 212 |
+
last_hand_agent=last_hand_agent,
|
| 213 |
+
last_quantities=self.state.previous_quantities,
|
| 214 |
+
last_hand_value_coagent=last_hand_value_coagent,
|
| 215 |
+
)
|
| 216 |
+
return obs
|
| 217 |
+
|
| 218 |
+
def get_state(self):
|
| 219 |
+
return self.state
|
| 220 |
+
|
| 221 |
+
def get_safe_copy(self):
|
| 222 |
+
"""Return a safe copy of the simulation."""
|
| 223 |
+
simulation_copy = copy.copy(self)
|
| 224 |
+
simulation_copy.state = copy.deepcopy(self.state)
|
| 225 |
+
return simulation_copy
|
| 226 |
+
|
| 227 |
+
def reset(self):
|
| 228 |
+
"""Initialize and return initial observations"""
|
| 229 |
+
# Decide starting agent alternating across resets for determinism
|
| 230 |
+
start_agent = self.agent_ids[self._starting_agent_index]
|
| 231 |
+
hands, values = self._sample_hands_and_values()
|
| 232 |
+
self.state = TrustAndSplitRPSState(
|
| 233 |
+
round_nb=0,
|
| 234 |
+
last_message="",
|
| 235 |
+
current_agent=start_agent,
|
| 236 |
+
quantities={"coins": 10},
|
| 237 |
+
values=values,
|
| 238 |
+
splits={aid: None for aid in self.agent_ids},
|
| 239 |
+
nb_messages_sent={aid: 0 for aid in self.agent_ids},
|
| 240 |
+
previous_values=None,
|
| 241 |
+
previous_splits=None,
|
| 242 |
+
previous_points=None,
|
| 243 |
+
split_phase=False,
|
| 244 |
+
hands=hands,
|
| 245 |
+
previous_hands=None,
|
| 246 |
+
previous_quantities=None,
|
| 247 |
+
)
|
| 248 |
+
return self.get_obs()
|
src_code_for_reproducibility/markov_games/negotiation/tas_simple_agent.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mllm.markov_games.negotiation.nego_agent import NegotiationAgent
|
| 2 |
+
from mllm.markov_games.negotiation.nego_simulation import Split
|
| 3 |
+
from mllm.markov_games.negotiation.tas_simple_simulation import TrustAndSplitSimpleObs
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TrustAndSplitSimpleAgent(NegotiationAgent):
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
num_message_chars,
|
| 10 |
+
message_start_end_format: bool = False,
|
| 11 |
+
proposal_start_end_format: bool = False,
|
| 12 |
+
*args,
|
| 13 |
+
**kwargs,
|
| 14 |
+
):
|
| 15 |
+
self.num_message_chars = num_message_chars
|
| 16 |
+
self.message_start_end_format = message_start_end_format
|
| 17 |
+
self.proposal_start_end_format = proposal_start_end_format
|
| 18 |
+
super().__init__(*args, **kwargs)
|
| 19 |
+
self.intro_prompt = (
|
| 20 |
+
"Welcome to an iterated game. You are {agent}. The other agent is {other_agent}.\n"
|
| 21 |
+
"Setup:\n"
|
| 22 |
+
"1. The game has multiple independent rounds.\n"
|
| 23 |
+
"2. In each round, there are 10 coins to split between the two agents.\n"
|
| 24 |
+
"3. Both agents are assigned a per-coin value between 1 and 10 (inclusive) in each round.\n"
|
| 25 |
+
"4. You can only observe your own per-coin value.\n"
|
| 26 |
+
"5. Because assignments are random, both agents are equally likely to have same expected per-coin value.\n"
|
| 27 |
+
"\n"
|
| 28 |
+
"Protocol:\n"
|
| 29 |
+
"1. At the start of the round, one agent begins the conversation. The starting role alternates each round.\n"
|
| 30 |
+
"2. Agents exchange a short chat ({quota_messages_per_agent_per_round} messages per round per agent) to negotiate how to split the coins.\n"
|
| 31 |
+
" - Use this chat to communicate your private per-coin value to make informed proposals.\n"
|
| 32 |
+
"3. After the chat, both agents simultaneously propose how many coins they keep.\n"
|
| 33 |
+
"4. If the total sum of proposals is less than or equal to 10, both agents receive their proposals.\n"
|
| 34 |
+
"5. If the total sum of proposals exceeds 10, the coins are allocated proportionally.\n"
|
| 35 |
+
"6. Your points for the round = (coins you receive) x (your per-coin value for that round). \n"
|
| 36 |
+
"7. Points are accumulated across rounds.\n"
|
| 37 |
+
"Your goal: {goal}\n"
|
| 38 |
+
)
|
| 39 |
+
self.new_round_prompt = (
|
| 40 |
+
"A New Round Begins\n"
|
| 41 |
+
"Your per-coin value is {value}. You don't know {other_agent}'s value yet.\n"
|
| 42 |
+
)
|
| 43 |
+
self.last_round_prompt = "In the previous round, {other_agent} had a {last_value_str_coagent} value and proposed {last_split_coagent} coins.\n"
|
| 44 |
+
if self.proposal_start_end_format:
|
| 45 |
+
self.send_split_prompt = (
|
| 46 |
+
"Submit your proposal\n"
|
| 47 |
+
"Respond with <<proposal_start>> x <<proposal_end>> where x is an integer in [0, 10]."
|
| 48 |
+
)
|
| 49 |
+
else:
|
| 50 |
+
self.send_split_prompt = (
|
| 51 |
+
"Submit your proposal\n"
|
| 52 |
+
"Respond with <coins_to_self> x </coins_to_self> where x is an integer in [0, 10]."
|
| 53 |
+
)
|
| 54 |
+
self.wait_for_message_prompt = "Wait for {other_agent} to send a message..."
|
| 55 |
+
# self.wait_for_message_prompt = ""
|
| 56 |
+
self.last_message_prompt = "{other_agent} said: {last_message}"
|
| 57 |
+
if self.message_start_end_format:
|
| 58 |
+
self.send_message_prompt = f"Send your message now in <<message_start>>...<<message_end>> (<={self.num_message_chars} chars)."
|
| 59 |
+
else:
|
| 60 |
+
self.send_message_prompt = f"Send your message now in <message>...</message> (<={self.num_message_chars} chars)."
|
| 61 |
+
|
| 62 |
+
def get_message_regex(self, observation: TrustAndSplitSimpleObs) -> str:
|
| 63 |
+
if self.message_start_end_format:
|
| 64 |
+
return (
|
| 65 |
+
rf"<<message_start>>[\s\S]{{0,{self.num_message_chars}}}<<message_end>>"
|
| 66 |
+
)
|
| 67 |
+
else:
|
| 68 |
+
return rf"<message>[\s\S]{{0,{self.num_message_chars}}}</message>"
|
| 69 |
+
|
| 70 |
+
def get_split_regex(self, observation: TrustAndSplitSimpleObs) -> str:
|
| 71 |
+
if self.proposal_start_end_format:
|
| 72 |
+
return r"<<proposal_start>> ?(10|[0-9]) ?<<proposal_end>>"
|
| 73 |
+
else:
|
| 74 |
+
return r"<coins_to_self> ?(10|[0-9]) ?</coins_to_self>"
|
| 75 |
+
|
| 76 |
+
def get_split_action(
|
| 77 |
+
self, policy_output: str, observation: TrustAndSplitSimpleObs
|
| 78 |
+
) -> Split:
|
| 79 |
+
import re as _re
|
| 80 |
+
|
| 81 |
+
if self.proposal_start_end_format:
|
| 82 |
+
m = _re.search(
|
| 83 |
+
r"<<proposal_start>> ?(10|[0-9]) ?<<proposal_end>>", policy_output
|
| 84 |
+
)
|
| 85 |
+
else:
|
| 86 |
+
m = _re.search(
|
| 87 |
+
r"<coins_to_self> ?(10|[0-9]) ?</coins_to_self>", policy_output
|
| 88 |
+
)
|
| 89 |
+
coins_int = int(m.group(1)) if m else int(policy_output)
|
| 90 |
+
return Split(items_given_to_self={"coins": coins_int})
|
src_code_for_reproducibility/markov_games/negotiation/tas_simple_simulation.py
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import copy
|
| 2 |
+
from collections import defaultdict
|
| 3 |
+
from dataclasses import dataclass
|
| 4 |
+
from typing import Any, Dict, List, Literal
|
| 5 |
+
|
| 6 |
+
from numpy.random import default_rng
|
| 7 |
+
|
| 8 |
+
from mllm.markov_games.negotiation.nego_simulation import (
|
| 9 |
+
NegotiationObs,
|
| 10 |
+
NegotiationSimulation,
|
| 11 |
+
NegotiationState,
|
| 12 |
+
Split,
|
| 13 |
+
compute_tas_style_rewards,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
AgentId = str
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class TrustAndSplitSimpleState(NegotiationState):
|
| 21 |
+
pass
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@dataclass
|
| 25 |
+
class TrustAndSplitSimpleObs(NegotiationObs):
|
| 26 |
+
last_value_str_coagent: str | None
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class TrustAndSplitSimpleSimulation(NegotiationSimulation):
|
| 30 |
+
def __init__(
|
| 31 |
+
self,
|
| 32 |
+
game_type: Literal["10-1-exclusive", "1-to-10"] = "1-to-10",
|
| 33 |
+
dist_type: Literal["uniform", "bimodal"] = "uniform",
|
| 34 |
+
beta_dist_alpha: float = 0.1,
|
| 35 |
+
beta_dist_beta: float = 0.1,
|
| 36 |
+
*args,
|
| 37 |
+
**kwargs,
|
| 38 |
+
):
|
| 39 |
+
self.game_type = game_type
|
| 40 |
+
self.dist_type = dist_type
|
| 41 |
+
self.beta_dist_alpha = beta_dist_alpha
|
| 42 |
+
self.beta_dist_beta = beta_dist_beta
|
| 43 |
+
super().__init__(*args, **kwargs)
|
| 44 |
+
|
| 45 |
+
def _sample_values(self) -> Dict[AgentId, dict]:
|
| 46 |
+
values = {}
|
| 47 |
+
while True:
|
| 48 |
+
if self.game_type == "10-1-exclusive":
|
| 49 |
+
v = int(self.rng.choice([1, 10]))
|
| 50 |
+
values[self.agent_ids[0]] = v
|
| 51 |
+
values[self.agent_ids[1]] = 10 if v == 1 else 1
|
| 52 |
+
elif self.game_type == "1-to-10":
|
| 53 |
+
for aid in self.agent_ids:
|
| 54 |
+
if self.dist_type == "uniform":
|
| 55 |
+
values[aid] = int(self.rng.integers(1, 11))
|
| 56 |
+
elif self.dist_type == "bimodal":
|
| 57 |
+
alpha, beta = self.beta_dist_alpha, self.beta_dist_beta
|
| 58 |
+
values[aid] = int(round(self.rng.beta(alpha, beta) * 9) + 1)
|
| 59 |
+
if len(set(values.values())) != 1:
|
| 60 |
+
break
|
| 61 |
+
return values
|
| 62 |
+
|
| 63 |
+
def _sample_quantities(self) -> Dict[str, int]:
|
| 64 |
+
return {"coins": 10}
|
| 65 |
+
|
| 66 |
+
def set_new_round_of_variant(self):
|
| 67 |
+
self.state.quantities = self._sample_quantities()
|
| 68 |
+
self.state.values = self._sample_values()
|
| 69 |
+
self.state.split_phase = False
|
| 70 |
+
|
| 71 |
+
def get_info_of_variant(
|
| 72 |
+
self, state: NegotiationState, actions: Dict[AgentId, Any]
|
| 73 |
+
) -> Dict[str, Any]:
|
| 74 |
+
return {
|
| 75 |
+
"quantities": copy.deepcopy(state.quantities),
|
| 76 |
+
"values": copy.deepcopy(state.values),
|
| 77 |
+
# "previous_values": copy.deepcopy(state.previous_values),
|
| 78 |
+
"splits": copy.deepcopy(state.splits),
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
def get_rewards(self, splits: Dict[AgentId, Split]) -> Dict[AgentId, float]:
|
| 82 |
+
return compute_tas_style_rewards(
|
| 83 |
+
self.agent_ids, self.state.values, splits, self.state.quantities
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def get_obs(self):
|
| 87 |
+
return {agent_id: self.get_obs_agent(agent_id) for agent_id in self.agent_ids}
|
| 88 |
+
|
| 89 |
+
def get_obs_agent(self, agent_id):
|
| 90 |
+
other_id = self._other(agent_id)
|
| 91 |
+
last_value_coagent = (
|
| 92 |
+
None
|
| 93 |
+
if self.state.previous_values is None
|
| 94 |
+
else self.state.previous_values.get(other_id)
|
| 95 |
+
)
|
| 96 |
+
last_points_coagent = (
|
| 97 |
+
None
|
| 98 |
+
if self.state.previous_points is None
|
| 99 |
+
else round(self.state.previous_points.get(other_id), 1)
|
| 100 |
+
)
|
| 101 |
+
last_value_agent = (
|
| 102 |
+
None
|
| 103 |
+
if self.state.previous_values is None
|
| 104 |
+
else self.state.previous_values.get(agent_id)
|
| 105 |
+
)
|
| 106 |
+
last_points_agent = (
|
| 107 |
+
None
|
| 108 |
+
if self.state.previous_points is None
|
| 109 |
+
else round(self.state.previous_points.get(agent_id), 1)
|
| 110 |
+
)
|
| 111 |
+
last_split_coagent = None
|
| 112 |
+
last_split_agent = None
|
| 113 |
+
if self.state.previous_splits is not None:
|
| 114 |
+
last_split_coagent = self.state.previous_splits[
|
| 115 |
+
other_id
|
| 116 |
+
].items_given_to_self["coins"]
|
| 117 |
+
last_split_agent = self.state.previous_splits[agent_id].items_given_to_self[
|
| 118 |
+
"coins"
|
| 119 |
+
]
|
| 120 |
+
if last_value_agent is None or last_value_coagent is None:
|
| 121 |
+
last_value_str_coagent = None
|
| 122 |
+
else:
|
| 123 |
+
if last_value_coagent > last_value_agent:
|
| 124 |
+
last_value_str_coagent = "higher"
|
| 125 |
+
elif last_value_coagent < last_value_agent:
|
| 126 |
+
last_value_str_coagent = "lower"
|
| 127 |
+
else:
|
| 128 |
+
raise ValueError("Should not be equal values")
|
| 129 |
+
|
| 130 |
+
obs = TrustAndSplitSimpleObs(
|
| 131 |
+
round_nb=self.state.round_nb,
|
| 132 |
+
last_message=self.state.last_message,
|
| 133 |
+
quota_messages_per_agent_per_round=self.quota_messages_per_agent_per_round,
|
| 134 |
+
current_agent=self.state.current_agent,
|
| 135 |
+
other_agent=self.agent_id_to_name[other_id],
|
| 136 |
+
quantities=self.state.quantities,
|
| 137 |
+
item_types=self.item_types,
|
| 138 |
+
value=self.state.values[agent_id],
|
| 139 |
+
split_phase=self.state.split_phase,
|
| 140 |
+
last_split_agent=last_split_agent,
|
| 141 |
+
last_value_agent=last_value_agent,
|
| 142 |
+
last_points_agent=last_points_agent,
|
| 143 |
+
last_split_coagent=last_split_coagent,
|
| 144 |
+
last_value_coagent=last_value_coagent,
|
| 145 |
+
last_points_coagent=last_points_coagent,
|
| 146 |
+
last_quantities=self.state.previous_quantities,
|
| 147 |
+
last_value_str_coagent=last_value_str_coagent,
|
| 148 |
+
)
|
| 149 |
+
return obs
|
| 150 |
+
|
| 151 |
+
def reset(self):
|
| 152 |
+
start_agent = self.agent_ids[self._starting_agent_index]
|
| 153 |
+
quantities = self._sample_quantities()
|
| 154 |
+
values = self._sample_values()
|
| 155 |
+
self.state = TrustAndSplitSimpleState(
|
| 156 |
+
round_nb=0,
|
| 157 |
+
last_message="",
|
| 158 |
+
current_agent=start_agent,
|
| 159 |
+
quantities=quantities,
|
| 160 |
+
values=values,
|
| 161 |
+
previous_values=None,
|
| 162 |
+
splits={aid: None for aid in self.agent_ids},
|
| 163 |
+
nb_messages_sent={aid: 0 for aid in self.agent_ids},
|
| 164 |
+
split_phase=False,
|
| 165 |
+
previous_splits=None,
|
| 166 |
+
previous_points=None,
|
| 167 |
+
previous_quantities=None,
|
| 168 |
+
)
|
| 169 |
+
return self.get_obs()
|
src_code_for_reproducibility/markov_games/negotiation/tas_simulation.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import copy
|
| 2 |
+
from collections import defaultdict
|
| 3 |
+
from dataclasses import dataclass
|
| 4 |
+
from typing import Any, Dict, List, Literal
|
| 5 |
+
|
| 6 |
+
from numpy.random import default_rng
|
| 7 |
+
|
| 8 |
+
from mllm.markov_games.negotiation.nego_simulation import (
|
| 9 |
+
NegotiationObs,
|
| 10 |
+
NegotiationSimulation,
|
| 11 |
+
NegotiationState,
|
| 12 |
+
Split,
|
| 13 |
+
compute_tas_style_rewards,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
AgentId = str
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class TrustAndSplitState(NegotiationState):
|
| 21 |
+
pass
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@dataclass
|
| 25 |
+
class TrustAndSplitObs(NegotiationObs):
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class TrustAndSplitSimulation(NegotiationSimulation):
|
| 30 |
+
def __init__(
|
| 31 |
+
self,
|
| 32 |
+
game_type: Literal["10-1-exclusive", "10-1-ties", "1-to-20"] = "1-to-20",
|
| 33 |
+
same_round_value: bool = True,
|
| 34 |
+
atleast_one_conflict: bool = False,
|
| 35 |
+
*args,
|
| 36 |
+
**kwargs,
|
| 37 |
+
):
|
| 38 |
+
self.game_type = game_type
|
| 39 |
+
self.same_round_value = same_round_value
|
| 40 |
+
self.atleast_one_conflict = atleast_one_conflict
|
| 41 |
+
super().__init__(*args, **kwargs)
|
| 42 |
+
|
| 43 |
+
def _sample_values(self) -> Dict[AgentId, dict]:
|
| 44 |
+
values = defaultdict(dict)
|
| 45 |
+
if self.state is None:
|
| 46 |
+
item_types = self.item_types
|
| 47 |
+
else:
|
| 48 |
+
item_types = list(self.state.quantities.keys())
|
| 49 |
+
while True:
|
| 50 |
+
for item in item_types:
|
| 51 |
+
if self.game_type == "10-1-exclusive":
|
| 52 |
+
v = int(self.rng.choice([1, 10]))
|
| 53 |
+
values[self.agent_ids[0]][item] = v
|
| 54 |
+
values[self.agent_ids[1]][item] = 10 if v == 1 else 1
|
| 55 |
+
elif self.game_type == "10-1-ties":
|
| 56 |
+
for aid in self.agent_ids:
|
| 57 |
+
values[aid][item] = int(self.rng.choice([1, 10]))
|
| 58 |
+
elif self.game_type == "1-to-20":
|
| 59 |
+
for aid in self.agent_ids:
|
| 60 |
+
values[aid][item] = int(self.rng.integers(1, 21))
|
| 61 |
+
agent_values = [sum(v.values()) for v in values.values()]
|
| 62 |
+
if self.atleast_one_conflict:
|
| 63 |
+
has_conflict = False
|
| 64 |
+
for item in item_types:
|
| 65 |
+
agent_values_for_item = [
|
| 66 |
+
values[aid][item] for aid in self.agent_ids
|
| 67 |
+
]
|
| 68 |
+
if (
|
| 69 |
+
len(set(agent_values_for_item)) > 1
|
| 70 |
+
): # Different values for this item
|
| 71 |
+
has_conflict = True
|
| 72 |
+
break
|
| 73 |
+
if not has_conflict:
|
| 74 |
+
continue
|
| 75 |
+
if len(set(agent_values)) == 1 or not self.same_round_value:
|
| 76 |
+
break
|
| 77 |
+
return values
|
| 78 |
+
|
| 79 |
+
def _sample_quantities(self) -> Dict[str, int]:
|
| 80 |
+
return {item.lower(): 10 for item in self.item_types}
|
| 81 |
+
|
| 82 |
+
def set_new_round_of_variant(self):
|
| 83 |
+
self.state.quantities = self._sample_quantities()
|
| 84 |
+
self.state.values = self._sample_values()
|
| 85 |
+
self.state.split_phase = False
|
| 86 |
+
|
| 87 |
+
def get_info_of_variant(
|
| 88 |
+
self, state: NegotiationState, actions: Dict[AgentId, Any]
|
| 89 |
+
) -> Dict[str, Any]:
|
| 90 |
+
return {
|
| 91 |
+
"quantities": copy.deepcopy(state.quantities),
|
| 92 |
+
"values": copy.deepcopy(state.values),
|
| 93 |
+
# "previous_values": copy.deepcopy(state.previous_values),
|
| 94 |
+
"splits": copy.deepcopy(state.splits),
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
def get_rewards(self, splits: Dict[AgentId, Split]) -> Dict[AgentId, float]:
|
| 98 |
+
return compute_tas_style_rewards(
|
| 99 |
+
self.agent_ids, self.state.values, splits, self.state.quantities
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
def get_obs(self):
|
| 103 |
+
return {agent_id: self.get_obs_agent(agent_id) for agent_id in self.agent_ids}
|
| 104 |
+
|
| 105 |
+
def get_obs_agent(self, agent_id):
|
| 106 |
+
other_id = self._other(agent_id)
|
| 107 |
+
last_value_coagent = (
|
| 108 |
+
None
|
| 109 |
+
if self.state.previous_values is None
|
| 110 |
+
else self.state.previous_values.get(other_id)
|
| 111 |
+
)
|
| 112 |
+
last_points_coagent = (
|
| 113 |
+
None
|
| 114 |
+
if self.state.previous_points is None
|
| 115 |
+
else round(self.state.previous_points.get(other_id), 1)
|
| 116 |
+
)
|
| 117 |
+
last_value_agent = (
|
| 118 |
+
None
|
| 119 |
+
if self.state.previous_values is None
|
| 120 |
+
else self.state.previous_values.get(agent_id)
|
| 121 |
+
)
|
| 122 |
+
last_points_agent = (
|
| 123 |
+
None
|
| 124 |
+
if self.state.previous_points is None
|
| 125 |
+
else round(self.state.previous_points.get(agent_id), 1)
|
| 126 |
+
)
|
| 127 |
+
last_split_coagent = None
|
| 128 |
+
last_split_agent = None
|
| 129 |
+
if self.state.previous_splits is not None:
|
| 130 |
+
last_split_coagent = self.state.previous_splits[
|
| 131 |
+
other_id
|
| 132 |
+
].items_given_to_self
|
| 133 |
+
last_split_agent = self.state.previous_splits[agent_id].items_given_to_self
|
| 134 |
+
obs = TrustAndSplitObs(
|
| 135 |
+
round_nb=self.state.round_nb,
|
| 136 |
+
last_message=self.state.last_message,
|
| 137 |
+
quota_messages_per_agent_per_round=self.quota_messages_per_agent_per_round,
|
| 138 |
+
current_agent=self.state.current_agent,
|
| 139 |
+
other_agent=self.agent_id_to_name[other_id],
|
| 140 |
+
quantities=self.state.quantities,
|
| 141 |
+
item_types=self.item_types,
|
| 142 |
+
value=self.state.values[agent_id],
|
| 143 |
+
split_phase=self.state.split_phase,
|
| 144 |
+
last_split_agent=last_split_agent,
|
| 145 |
+
last_value_agent=last_value_agent,
|
| 146 |
+
last_points_agent=last_points_agent,
|
| 147 |
+
last_split_coagent=last_split_coagent,
|
| 148 |
+
last_value_coagent=last_value_coagent,
|
| 149 |
+
last_points_coagent=last_points_coagent,
|
| 150 |
+
last_quantities=self.state.previous_quantities,
|
| 151 |
+
)
|
| 152 |
+
return obs
|
| 153 |
+
|
| 154 |
+
def reset(self):
|
| 155 |
+
start_agent = self.agent_ids[self._starting_agent_index]
|
| 156 |
+
quantities = self._sample_quantities()
|
| 157 |
+
values = self._sample_values()
|
| 158 |
+
self.state = TrustAndSplitState(
|
| 159 |
+
round_nb=0,
|
| 160 |
+
last_message="",
|
| 161 |
+
current_agent=start_agent,
|
| 162 |
+
quantities=quantities,
|
| 163 |
+
values=values,
|
| 164 |
+
previous_values=None,
|
| 165 |
+
splits={aid: None for aid in self.agent_ids},
|
| 166 |
+
nb_messages_sent={aid: 0 for aid in self.agent_ids},
|
| 167 |
+
split_phase=False,
|
| 168 |
+
previous_splits=None,
|
| 169 |
+
previous_points=None,
|
| 170 |
+
previous_quantities=None,
|
| 171 |
+
)
|
| 172 |
+
return self.get_obs()
|
src_code_for_reproducibility/markov_games/run_markov_games.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from collections.abc import Callable
|
| 3 |
+
from dataclasses import dataclass
|
| 4 |
+
|
| 5 |
+
from torch._C import ClassType
|
| 6 |
+
|
| 7 |
+
from mllm.markov_games.markov_game import MarkovGame
|
| 8 |
+
from mllm.markov_games.rollout_tree import RolloutTreeRootNode
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
async def run_markov_games(
|
| 12 |
+
runner: Callable[[MarkovGame], RolloutTreeRootNode],
|
| 13 |
+
runner_kwargs: dict,
|
| 14 |
+
output_folder: str,
|
| 15 |
+
markov_games: list[MarkovGame],
|
| 16 |
+
) -> list[RolloutTreeRootNode]:
|
| 17 |
+
tasks = []
|
| 18 |
+
for mg in markov_games:
|
| 19 |
+
tasks.append(
|
| 20 |
+
asyncio.create_task(
|
| 21 |
+
runner(markov_game=mg, output_folder=output_folder, **runner_kwargs)
|
| 22 |
+
)
|
| 23 |
+
)
|
| 24 |
+
return await asyncio.gather(*tasks)
|
src_code_for_reproducibility/markov_games/vine_ppo.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from anytree import Node, RenderTree
|
| 2 |
+
from anytree.exporter import DotExporter
|
| 3 |
+
import os.path
|
| 4 |
+
import asyncio
|
| 5 |
+
from mllm.markov_games.markov_game import MarkovGame
|
| 6 |
+
|
| 7 |
+
async def VinePPORunner(
|
| 8 |
+
markov_game: MarkovGame,
|
| 9 |
+
**kwargs):
|
| 10 |
+
pass
|
src_code_for_reproducibility/models/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (155 Bytes). View file
|
|
|
src_code_for_reproducibility/models/__pycache__/adapter_training_wrapper.cpython-312.pyc
ADDED
|
Binary file (4.92 kB). View file
|
|
|
src_code_for_reproducibility/models/__pycache__/human_policy.cpython-312.pyc
ADDED
|
Binary file (11.9 kB). View file
|
|
|
src_code_for_reproducibility/models/__pycache__/inference_backend.cpython-312.pyc
ADDED
|
Binary file (2.24 kB). View file
|
|
|
src_code_for_reproducibility/models/__pycache__/inference_backend_dummy.cpython-312.pyc
ADDED
|
Binary file (2.34 kB). View file
|
|
|
src_code_for_reproducibility/models/__pycache__/inference_backend_sglang.cpython-312.pyc
ADDED
|
Binary file (3.67 kB). View file
|
|
|
src_code_for_reproducibility/models/__pycache__/inference_backend_vllm.cpython-312.pyc
ADDED
|
Binary file (4.98 kB). View file
|
|
|
src_code_for_reproducibility/models/__pycache__/large_language_model_api.cpython-312.pyc
ADDED
|
Binary file (6.94 kB). View file
|
|
|
src_code_for_reproducibility/models/__pycache__/large_language_model_local.cpython-312.pyc
ADDED
|
Binary file (16.7 kB). View file
|
|
|
src_code_for_reproducibility/models/__pycache__/scalar_critic.cpython-312.pyc
ADDED
|
Binary file (3.21 kB). View file
|
|
|
src_code_for_reproducibility/training/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (157 Bytes). View file
|
|
|
src_code_for_reproducibility/training/__pycache__/annealing_methods.cpython-312.pyc
ADDED
|
Binary file (502 Bytes). View file
|
|
|