dymium-org / core

An R package for microsimulation modelling
https://core.dymium.org
GNU General Public License v3.0
25 stars 1 forks source link

Market class #6

Closed asiripanich closed 4 years ago

asiripanich commented 4 years ago

Market class should have search and matching strategies that can be swapped. This is needed to micro-simulate a labour market, housing market, etc.

Constructor..

Market$new(searchStrategy = SearchStrategyA$new(...), 
           matchStrategy = MatchStrategyA$new(...))

Matching strategies

Search strategies

Components

Resources

asiripanich commented 4 years ago

Can the collections package can be used to efficiently manage the microsimulation queue during search and matching than vector()?

asiripanich commented 4 years ago

Should this be a function instead of a class? I think it would be less ambiguous to the users if this is a function.

asiripanich commented 4 years ago

A snippet of code.

What I'm trying to do here is to convert MatchingMarket from a R6Class to a function but first I need to figure out how to include a scoring function (to calculate the matching scores for each agent to the agents in its choice-set) that is both performant and easy to understand.

n_A <- 100
n_B <- 100

agent_A <- 
  data.table(
    id = 1:n_A,
    age = round(runif(n_A) * 30 + 20),
    attitude = runif(n_A) * 10
  )
agent_A

agent_B <- 
  data.table(
    id = 1:n_B,
    age = round(runif(n_B) * 30 + 20),
    attitude = runif(n_A) * 10
  )
agent_B

crossjoin <- 
  CJ(id.a = agent_A$id, id.b = agent_B$id) %>%
  merge(., agent_A, by.x = "id.a", by.y = "id") %>%
  merge(., agent_B, by.x = "id.b", by.y = "id", suffixes = c(".a", '.b')) 
crossjoin

score <- function(data) {
  with(data, abs((age.a - age.b)) + abs((attitude.a - attitude.b)^2) + 5)  
}

score(crossjoin)

mymatch <- function(data_a, data_b, score_a, score_b) {

  # create a choiceset
  # ...

  # check the scoring function

  # join data of the agent of its choiceset
  crossjoin <- 
    CJ(id.a = agent_A$id, id.b = agent_B$id) %>%
    merge(., agent_A, by.x = "id.a", by.y = "id") %>%
    merge(., agent_B, by.x = "id.b", by.y = "id", suffixes = c(".a", '.b')) 

  # 
}

# check for variables in the scoring function. 
# knowing what variables are required in the scoring function can reduce the size of the merged data
strsplit(paste(deparse(score), collapse = ";"), "\\(|\\)|\\;|\\{|\\}|\\s|\\-|\\+|\\,") %>%
  unlist() %>%
  .[. != ""] %>%
  grep(pattern = "\\w+(?:\\.+\\w+)+", x = ., value = T)
asiripanich commented 4 years ago

see #85.