Generate recommendations for the given agent based on two-hop connections.
Note
The recommendation score for a candidate agent is based on:
1. The number of two-hop paths from the given agent to the candidate (higher is better).
2. The number of followers the candidate agent has (higher is better).
3. A random tie-breaker, when self.is_randomized is True (controlled by self.temperature).
Agents that are already followed by the given agent are excluded from the recommendations.
Social Networks
Social Network Base Class
Bases:
objectSocial Network class.
The SocialNetwork class represents a social network environment where agents can tweet messages and follow/unfollow other agents.
config (dict[str, Any])
registered_classes (list[Type])
prng (Random)
Add a new agent to the SocialNetwork.
agent_id (int) – The agent ID for the agent to be added.
agent_name (str)
None
Note
See also: econsimulacra.envs.base.Environment._generate_agents
Append a new message to the agent’s existing tweet. This represents the agent tweeting a message in the social network.
agent_id (int) – The agent ID of the agent that tweeted.
message (str) – The content of the tweet.
None
Note
See also: econsimulacra.envs.base.Environment._act_in_social_network
Update the social network to reflect that the agent with agent_id follows the agent with target_agent_id.
agent_id (int) – The agent IDthat wants to follow another agent.
target_agent_id (int) – The agent ID to be followed.
None
Note
See also: econsimulacra.envs.base.Environment._act_in_social_network
Update the social network to reflect that the agent with agent_id unfollows the agent with target_agent_id.
agent_id (int) – The agent IDthat wants to unfollow another agent.
target_agent_id (int) – The agent ID to be unfollowed.
None
Note
See also: econsimulacra.envs.base.Environment._act_in_social_network
Get the agent IDs that follow the agent with the given agent_id.
agent_id (int) – The agent ID for which to get the followers.
A set of agent IDs that follow the given agent.
set[int]
Get the number of followers for the agent with the given agent_id.
agent_id (int) – The agent ID for which to get the number of followers.
The number of followers for the given agent.
int
Get the follow cap of the social network.
The follow cap of the social network, or None if not set.
int, optional
Get the allowed number of follows for the agent with the given agent_id, which is the remaining number of follows the agent can make before reaching the follow cap.
agent_id (int) – The agent ID for which to get the allowed number of follows.
The allowed number of follows for the given agent, or None if there is no follow cap.
int, optional
Get the agent IDs that the agent with the given agent_id follows.
agent_id (int) – The agent ID for which to get the follows.
A set of agent IDs that the given agent follows.
set[int]
Get the number of follows for the agent with the given agent_id.
agent_id (int) – The agent ID for which to get the number of follows.
The number of follows for the given agent.
int
Get the latest tweet of the agent with the given agent_id.
agent_id (int) – The agent ID for which to get the latest tweet.
The latest tweet of the given agent.
str
Get a list of recommended agent IDs for the agent with the given agent_id to follow, based on the recommender system.
agent_id (int) – The agent ID for which to get the recommended follows.
A list of recommended agent IDs for the given agent to follow.
list[int]
Note
The recommendations are generated based on the recommender system’s algorithm. See also: econsimulacra.social_networks.recsys.RecommenderSystem, econsimulacra.envs.obs_providers.RecommendedFollowsProvider
Recommender System
Bases:
ABCBase class for recommender systems in the social network environment (abstract class).
Recommender systems are responsible for generating personalized recommendations for agents in the social network, such as which other agents to follow. They can also implement event hooks to update their internal state based on changes in the social network, such as when agents follow or unfollow each other, or tweet. .get_recommendations(agent_id) is the only method that must be implemented by subclasses, while the event hooks are optional to implement based on the specific recommendation algorithm.
config (dict[str, Any])
prng (Random)
Inject the social network instance into the recommender system.
social_network (SocialNetwork) – The social network instance to bind to the recommender system.
None
Note
This method is called when the SocialNetwork is initialized, allowing the recommender system to access the social network’s state and subscribe to its events. See also: econsimulacra.envs.social_networks.base.SocialNetwork.__init__
agent_id (int)
list[Any]
Event hook called when a new agent is added to the social network.
agent_id (int) – The unique identifier of the agent that was added.
agent_name (str) – The name of the agent that was added.
None
Note
econsimulacra.envs.social_networks.base.SocialNetwork.add_agent
Event hook called when an agent follows another agent in the social network.
agent_id (int) – The unique identifier of the agent that initiated the follow action.
target_agent_id (int) – The unique identifier of the agent that is being followed.
None
Note
econsimulacra.envs.social_networks.base.SocialNetwork.follow_agent
Event hook called when an agent unfollows another agent in the social network.
agent_id (int) – The unique identifier of the agent that initiated the unfollow action.
target_agent_id (int) – The unique identifier of the agent that is being unfollowed.
None
Note
econsimulacra.envs.social_networks.base.SocialNetwork.unfollow_agent
Event hook called when an agent tweets in the social network.
agent_id (int) – The unique identifier of the agent that tweeted.
message (str) – The content of the tweet.
None
Note
econsimulacra.envs.social_networks.base.SocialNetwork.tweet
Bases:
RecommenderSystemA simple recommender system that recommends agents based on two-hop connections in the social network.
config (dict[str, Any])
prng (Random)
Event hook called when a new agent is added to the social network.
agent_id (int) – The unique identifier of the agent that was added.
agent_name (str) – The name of the agent that was added.
None
Event hook called when an agent follows another agent in the social network.
agent_id (int) – The unique identifier of the agent that is following.
target_agent_id (int) – The unique identifier of the agent being followed.
None
Note
When agent_id follows target_agent_id, we update the two-hop follow counts for
agent_id -> target_agent_id -> XandX -> agent_id -> target_agent_idfor all relevant intermediate agents X.Event hook called when an agent unfollows another agent in the social network.
agent_id (int) – The unique identifier of the agent that is unfollowing.
target_agent_id (int) – The unique identifier of the agent being unfollowed.
None
Note
When agent_id unfollows target_agent_id, we update the two-hop follow counts for
agent_id -> target_agent_id -> XandX -> agent_id -> target_agent_idfor all relevant intermediate agents X.Generate recommendations for the given agent based on two-hop connections.
agent_id (int) – The unique identifier of the agent for whom to generate recommendations.
list[dict[str, Any]]
Note
The recommendation score for a candidate agent is based on: 1. The number of two-hop paths from the given agent to the candidate (higher is better). 2. The number of followers the candidate agent has (higher is better). 3. A random tie-breaker, when
self.is_randomizedis True (controlled byself.temperature).Agents that are already followed by the given agent are excluded from the recommendations.