Title: | Generate social roulette partitions, i.e. divide people into groups of predefined size |
---|---|
Description: | Partition individuals into groups of a predefined size. Different ways of partitioning are provided, one based on the maximally diverse grouping problem solver by Lai and Hao (2016) tries to maximize the overall distance of how far back in time individuals have been in the same group previously. |
Authors: | Michael Höhle [aut, cre], Jin-Kao Hao [aut], Xiangjing Lai [aut] |
Maintainer: | Michael Höhle <[email protected]> |
License: | GPL-3 |
Version: | 0.1.0 |
Built: | 2024-11-14 03:59:23 UTC |
Source: | https://github.com/mhoehle/socialroulette |
Read output from the Lai and Hao (2016) MDGP solver
mdgp_read_solutionfile(file_name)
mdgp_read_solutionfile(file_name)
file_name |
Name of the solution file |
data.frame with two clumns, idx (index) and group
Given a distance metric for each possible pair in the frame of n individuals, find a partition into as many groups as possible with group size at least m. This is an instance of the maximallz diverse grouping problem, which we use the algorithm by Lai and Hao (2016) to solve.
mdgp_solver(mdgp_format_file, time_limit = 15)
mdgp_solver(mdgp_format_file, time_limit = 15)
mdgp_format_file |
Path to the file containing the MDGP specification in mdgplib format |
time_limit |
Number of seconds to iteratively optimize each run. The larger the number of participants to group, the larger this value should be. Rule of thumb: time_limit = exp(0.5 + 0.0025*n) |
The maximally diverse grouping problem is about partitioning individuals
into groups of size at least
while maximizing a sum of utility values computed
by summing the utility
over all individuals
,
partitioned into the same group.
and summing this quantity over all groups. More formally, let
denote the number of
time unit (typically days) ago, that individual
and
were in the same group.
Note: This distance is derived by looking at the previous partitions and it is a matter
of definition what this value should be, if
and
have not previously been
in the same group. Let
denote the resulting number of groups where
denotes integer division.
For a given partition let
be an indicator variable, which is 1, if
is assigned into
group
and zero otherwise.
A solver of the maximally diverse grouping problem now tries to maximize
subject to the conditions
where is the size of group
, i.e.
. We shall adopt the convention
that group sizes are determined by assigning the labels
to all individuals and then count
how often each label occurs. This means, e.g., that for
and
we get
group with 7 members.
Note: The code calls C++ code by Xiangjing Lai and Jin-Kao Hao. This is an R adapted version of the C++ code available from http://www.info.univ-angers.fr/ Note: several temporary files are generated using 'tempfile()'.
File name of the solution file
Xiangjing Lai and Jin-Kao Hao, R interface by M. Höhle
Xiangjing Lai and Jin-Kao Hao (2016). *Iterated maxima search for the maximally diverse grouping problem*. European Journal of Operational Research, 254(3), pp. 780-800, https://doi.org/10.1016/j.ejor.2016.05.018
Generate a tempfile()
containing the appropriate specification formed from current_frame
and past_partitions
.
mdgp_write_specfile(current_frame, past_partitions, m)
mdgp_write_specfile(current_frame, past_partitions, m)
current_frame |
A pair-distance tibble. The frame needs to contain a column denoted 'id' |
past_partitions |
A named list of partitions, where the names correspond to the date when the partition was used. |
m |
The minimum group size, i.e. all groups have size at least m |
The specification format has the following header line
Here, each group size is repeated twice, because the solver has the opportunity to specify a lower as well as an
upper boundary for each group size. The keyword 'ds' for the solver in the above means that the groups can be of different sizes as determined by the respective
.
File name of the generated specification file
frame <- tibble::tibble(id=sprintf("id%.2d", 1:5), date=as.Date("2021-04-28")) past_partitions <- list("2021-04-21"=list(c("id02", "id03", "id04"), c("id01", "id05"))) spec_file <- socialroulette:::mdgp_write_specfile(frame, past_partitions=past_partitions, m=2) cat(stringr::str_c(readLines(spec_file), collapse="\n"))
frame <- tibble::tibble(id=sprintf("id%.2d", 1:5), date=as.Date("2021-04-28")) past_partitions <- list("2021-04-21"=list(c("id02", "id03", "id04"), c("id01", "id05"))) spec_file <- socialroulette:::mdgp_write_specfile(frame, past_partitions=past_partitions, m=2) cat(stringr::str_c(readLines(spec_file), collapse="\n"))
The function works by splitting the pairs data.frame by date and then apply the internal function pairs_to_partition to the resulting sub data.frame.
pairs_to_partitions(pairs)
pairs_to_partitions(pairs)
pairs |
A |
pairs_to_partition
partitions <- list("2021-04-21"=list(c("id02", "id03", "id04"), c("id01", "id05")), "2021-04-28"=list(c("id03", "id04", "id05"), c("id01", "id02"))) partitions2 <- partitions %>% socialroulette::partitions_to_pairs() %>% socialroulette::pairs_to_partitions() all.equal(partitions, partitions2)
partitions <- list("2021-04-21"=list(c("id02", "id03", "id04"), c("id01", "id05")), "2021-04-28"=list(c("id03", "id04", "id05"), c("id01", "id02"))) partitions2 <- partitions %>% socialroulette::partitions_to_pairs() %>% socialroulette::pairs_to_partitions() all.equal(partitions, partitions2)
The resulting tibble
will have an additional column group
partition_to_frame(l)
partition_to_frame(l)
l |
A partition, i.e. list of vectors, where each vector contains all individuals in the corresponding group |
frame The frame with 'id' and 'group' columns to convert
round1 <- list(c("id02", "id03", "id04"), c("id05", "id01")) socialroulette::partition_to_frame(round1)
round1 <- list(c("id02", "id03", "id04"), c("id05", "id01")) socialroulette::partition_to_frame(round1)
Convert a list of partitions into pairs format including a distance metric
partitions_to_distance(current_frame, past_partitions)
partitions_to_distance(current_frame, past_partitions)
current_frame |
The current frame |
past_partitions |
List of previous partitions |
frame <- tibble::tibble(id=sprintf("id%.2d", 1:5), date=as.Date("2021-04-28")) partitions <- list("2021-04-14"=list(c("id02", "id03", "id04"), c("id01", "id05")), "2021-04-21"=list(c("id03", "id04", "id05"), c("id01", "id02"))) socialroulette::partitions_to_distance(frame, partitions)
frame <- tibble::tibble(id=sprintf("id%.2d", 1:5), date=as.Date("2021-04-28")) partitions <- list("2021-04-14"=list(c("id02", "id03", "id04"), c("id01", "id05")), "2021-04-21"=list(c("id03", "id04", "id05"), c("id01", "id02"))) socialroulette::partitions_to_distance(frame, partitions)
The name of the list contain the date at which each partition was used.
partitions_to_pairs(partitions)
partitions_to_pairs(partitions)
partitions |
A list of past partitions, i.e. a named list where each entry is a partition and the names reflect the dates the partition were used |
partitions <- list("2021-04-21"=list(c("id02", "id03", "id04"), c("id05", "id01")), "2021-04-28"=list(c("id05", "id03", "id04"), c("id02", "id01"))) socialroulette::partitions_to_pairs(partitions)
partitions <- list("2021-04-21"=list(c("id02", "id03", "id04"), c("id05", "id01")), "2021-04-28"=list(c("id05", "id03", "id04"), c("id02", "id01"))) socialroulette::partitions_to_pairs(partitions)
One can either use simple random sampling (srs) to generate the partition or solve the maximally diverse grouping problem (mdgp) using the algorithm by Lai and Hao (2016) in order to maximize time since last meets over all groups.
rsocialroulette( current_frame, past_partitions = NULL, m, algorithm = c("mdgp", "srs"), ... )
rsocialroulette( current_frame, past_partitions = NULL, m, algorithm = c("mdgp", "srs"), ... )
current_frame |
A tibble containing the participants of the current round, i.e. it has a column 'id' containing a unique identifier and a 'date' column representing the date of the partition. |
past_partitions |
A list of partition lists each named by the date the partition was used (this is used to determine temporal distance to last meeting) |
m |
minimum group size, i.e. all groups will be at least size m. |
algorithm |
String specifying either "srs" (simple random sampling - DEFAULT) or "mdgp" (maximally diverse grouping problem) |
... |
Additional arguments to be sent to the solver |
A partitioning of current_frame maximizing the overall sum of gossip to be exchanged.
Höhle M (2021), Long time, no see: Virtual Lunch Roulette, Blog post, https://staff.math.su.se/hoehle/blog/2021/04/04/socialsamp.html
Xiangjing Lai and Jin-Kao Hao (2016). *Iterated maxima search for the maximally diverse grouping problem*. European Journal of Operational Research, 254(3), pp. 780-800, https://doi.org/10.1016/j.ejor.2016.05.018
mdgp_solver
today <- Sys.Date() frame <- tibble::tibble( id=sprintf("id%.02d",1:4), date=today) round1 <- rsocialroulette(current_frame = frame, m=2, algorithm="srs") round1 #Generate list of past partitions past_partitions <- list(round1) %>% setNames(today) frame2 <- frame %>% dplyr::mutate(date = today+7) round2 <- rsocialroulette(current_frame = frame2, past_partitions=past_partitions, m=2, algorithm="mdgp") round2
today <- Sys.Date() frame <- tibble::tibble( id=sprintf("id%.02d",1:4), date=today) round1 <- rsocialroulette(current_frame = frame, m=2, algorithm="srs") round1 #Generate list of past partitions past_partitions <- list(round1) %>% setNames(today) frame2 <- frame %>% dplyr::mutate(date = today+7) round2 <- rsocialroulette(current_frame = frame2, past_partitions=past_partitions, m=2, algorithm="mdgp") round2
socialroulette: A package to generate social groupings
Description
A package for partitioning individuals into groups of a pre-specified size. This can be as simple as using simple random sampling (srs) to divide n individuals into groups of size at least m. If one keeps track of the past partitions then an additional aim can be to try to maximize the time that people have a reunion, i.e. end up in the same group. This boils down to an instance of the maximally diverse grouping problem. See the [package website](https://hoehleatsu.github.io/socialroulette/) for more information, documentation and examples.
Details
A side effect of the package is that it provides a maximally diverse grouping problem solver, which can in principle be used for other purposes. As a consequence, internal functionality is exposed using export statements.