Created
August 31, 2022 03:10
-
-
Save grosscol/8da0ee85add98914519ef9b6a1c45e2f to your computer and use it in GitHub Desktop.
Making a simple agent meeting simulation on Discord
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate single round of meetings. | |
# from a data frame with `agentNo` and `state` cols. | |
generate_meetings <- function(agent_df){ | |
# 1 Make a list of Buyer agents | |
is_a_buyer <- agent_df$state == "Buyer" | |
buyers_ids <- agent_df[is_a_buyer, c('agentNo')] | |
# 2 Make list of Seller agents | |
is_a_seller <- agent_df$state == "Seller" | |
sellers_ids <- agent_df[is_a_seller, c('agentNo')] | |
# 3 Make a random selection of buyer agents | |
rnd_buyer_ids <- sample(buyers_ids) | |
# 4 Make a random selection of seller | |
rnd_seller_ids <- sample(sellers_ids) | |
# 5 Make a list of meetings | |
meetings <- data.frame(buyer_id = rnd_buyer_ids, | |
seller_id = rnd_seller_ids) | |
return(meetings) | |
} | |
# Did a meeting result in a deal? | |
# Takes meeting data frame of buyer and seller ids | |
# Takes agent frame for looking up prices | |
dealings <- function(meet_df, agent_df){ | |
buyer_idxs <- match(meet_df$buyer_id, agent_df$agentNo ) | |
seller_idxs <- match(meet_df$seller_id, agent_df$agentNo ) | |
# Get prices by index for buyers and sellers | |
buyer_prices <- agent_df[buyer_idxs, c('Price')] | |
seller_prices <- agent_df[seller_idxs, c('Price')] | |
# Determine which deals are done | |
meet_df$deal_done <- buyer_prices == seller_prices | |
return(meet_df) | |
} | |
# Given a set of deals and the agents, | |
# return a copy of the agents with updated statuses | |
update_agents <- function(deal_df, agent_df){ | |
# Trim to done matches | |
d_matches <- deal_df[ deal_df$deal_done == TRUE, ] | |
# concatenate all the agent ids (buyers and sellers) | |
ids_to_update <- c(d_matches$buyer_id, d_matches$seller_id) | |
# Get row indexes of agents using the id | |
id_idxs <- match(ids_to_update, agent_df$agentNo) | |
# Change the state of the rows of agentIds who had a deal | |
agent_df$state[id_idxs] <- "dealDone" | |
# Return copy of updated agents data frame | |
return(agent_df) | |
} | |
# Generate Agents | |
set.seed(5) | |
Population <- 20 | |
Rounds <- 10 | |
Agents = data.frame(agentNo = seq(Population), | |
state = rep(c("Buyer","Seller"),each = Population/2), | |
Mix = (1), | |
Price = round(runif(Population, 5, 10 )) | |
) | |
# Run ten rounds of meetings | |
set.seed(234) | |
agents_df <- Agents | |
for( itt in 1:10 ){ | |
print(itt) | |
loop_meets <- generate_meetings(agents_df) | |
loop_deals <- dealings(loop_meets, agents_df) | |
# overwrite agent state for next round | |
agents_df <- update_agents(loop_deals, agents_df) | |
print(agents_df) | |
} | |
#Examine result | |
agents_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment