Skip to content

Commit

Permalink
🐛 fix(voter_agent.py-main.py): fixed issue with saving agents
Browse files Browse the repository at this point in the history
  • Loading branch information
Sneheel Sarangi committed Dec 10, 2024
1 parent f7764ca commit a8f3654
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 50 deletions.
9 changes: 5 additions & 4 deletions examples/election/src/election_sim/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
SimpleGameRunner,
build_agent_with_memories,
init_objects,
save_to_json,
sort_agents,
)
from sim_utils.misc_sim_utils import event_logger, post_analysis
Expand Down Expand Up @@ -362,11 +363,11 @@ def run_sim(
eval_event_logger.episode_idx = i
action_event_logger.episode_idx = i
for player in players:
player_dir = os.path.join("../player_data", player.name)
player_dir = os.path.join("output/player_checkpoints", player.name)
os.makedirs(player_dir, exist_ok=True)
file_path = os.path.join(player_dir, f"Episode_{i}.json")
# Get JSON data from player
json_data = player.save_to_json()
json_data = save_to_json(player)
with open(file_path, "w") as file:
file.write(json.dumps(json_data, indent=4))
deploy_surveys(players, eval_config, eval_event_logger)
Expand Down Expand Up @@ -413,8 +414,8 @@ def run_sim(
# experiment_name = "bias"
# experiment_name = "malicious"
experiment_name = args.exp
N = 100
# N = 20
# N = 100
N = 20
survey = "None.Big5"
# survey = "Costa_et_al_JPersAssess_2021.Schwartz"
config_name = f"N{N}_{survey.split('.')[0]}_{survey.split('.')[1]}_{experiment_name}.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ def build_agent(
observation_summary,
relevant_memories,
self_perception,
opinions_on_candidate,
*opinions_on_candidate,
time_display,
# Components that do not provide pre_act context.
identity_characteristics,
relevant_opinions,
*relevant_opinions,
]

components_of_agent = {
Expand Down
44 changes: 44 additions & 0 deletions examples/election/src/election_sim/sim_utils/concordia_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,55 @@
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
sys.path.insert(0, project_root)

import json

from concordia.agents import entity_agent_with_logging
from concordia.typing import entity_component
from scenario_agents import candidate_agent, voter_agent

from mastodon_sim.concordia import triggering


def save_to_json(
agent: entity_agent_with_logging.EntityAgentWithLogging,
) -> str:
"""Saves an agent to JSON data.
This function saves the agent's state to a JSON string, which can be loaded
afterwards with `rebuild_from_json`. The JSON data
includes the state of the agent's context components, act component, memory,
agent name and the initial config. The clock, model and embedder are not
saved and will have to be provided when the agent is rebuilt. The agent must
be in the `READY` phase to be saved.
Args:
agent: The agent to save.
Returns
-------
A JSON string representing the agent's state.
Raises
------
ValueError: If the agent is not in the READY phase.
"""
if agent.get_phase() != entity_component.Phase.READY:
raise ValueError("The agent must be in the `READY` phase to be saved.")

data = {
component_name: agent.get_component(component_name).get_state()
for component_name in agent.get_all_context_components()
}

data["act_component"] = agent.get_act_component().get_state()

config = agent.get_config()
if config is not None:
data["agent_config"] = config.to_dict()

return json.dumps(data)


def init_objects(model, embedder, shared_memories, clock):
shared_context = model.sample_text( # TODO: deprecated?
"Summarize the following passage in a concise and insightful fashion. "
Expand Down
Loading

0 comments on commit a8f3654

Please sign in to comment.