---
title: "Application to simple datasets"
output: 
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 2
vignette: >
  %\VignetteIndexEntry{Application to simple datasets}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(grouper)
library(ompr)
library(ompr.roi)
#library(ROI.plugin.gurobi)
library(ROI.plugin.glpk)
```

# Introduction 

This vignette illustrates the use of the package on simple datasets, for which
the optimal solutions are apparent from inspection.

For each dataset below, we will use 2 workflows to run the optimisation. The 
first workflow does not make use of the wrapper function `extract_info()` and 
instead uses `extract_student_info()` and `extract_params_yaml()`.
The alternative uses `extract_info()` as the extraction
wrapper; for diversity and preference models it also demonstrates direct
parameter supply in `prepare_model()` (without YAML).

# Diversity-Based Assignment

## Dataset 001 (diversity only)

The first dataset comprises just 4 students. Here is what it looks like. The
name of this dataset indicates that it is for the diversity-based-assignment
(dba) model and that it consists of the group composition (gc) information.

```{r}
dba_gc_ex001
```

It is intuitive that an assignment into two groups of size two, based on the
diversity of majors alone, should assign students 1 and 3 into the first 
group and the remaining two students into another group.

The corresponding YAML `dba_gc_ex001.yml` file for this exercise consists of the
following lines:

```{r echo=FALSE, comment=''}
cat(readLines(system.file("extdata",  "dba_params_ex001.yml",   
                          package = "grouper")),  sep = '\n')
```

To run the assignment using only the primary major (ignoring the skill), we 
can use the following commands. We can use either the gurobi solver, or the 
glpk solver for this example. Both are equally fast.

```{r}
# indicate appropriate columns using integer ids.
df_ex001_list <- extract_student_info(dba_gc_ex001, "diversity",
                                  demographic_cols = 2, skills = 3, 
                                  self_formed_groups = 4)
yaml_ex001_list <- extract_params_yaml(system.file("extdata", 
                                             "dba_params_ex001.yml",  
                                             package = "grouper"),
                                       "diversity")
m1 <- prepare_model(df_ex001_list, yaml_ex001_list, assignment="diversity",
                    w1=1.0, w2=0.0)
#result3 <- solve_model(m1, with_ROI(solver="gurobi"))
result3 <- solve_model(m1, with_ROI(solver="glpk"))
assign_groups(result3, assignment = "diversity", dframe=dba_gc_ex001, 
              group_names="groups")
```

Alternative workflow (wrapper + direct parameters):

```{r}
df_ex001_alt <- extract_info(
  assignment = "diversity",
  dframe = dba_gc_ex001,
  demographic_cols = 2,
  skills = 3,
  self_formed_groups = 4
)

m1_alt <- prepare_model(
  df_ex001_alt,
  assignment = "diversity",
  w1 = 1.0,
  w2 = 0.0,
  n_topics = 2,
  nmin = 2,
  nmax = 2,
  rmin = 1,
  rmax = 1
)

result3_alt <- solve_model(m1_alt, with_ROI(solver = "glpk"))
assign_groups(
  model_result = result3_alt,
  assignment = "diversity",
  dframe = dba_gc_ex001,
  group_names = "groups"
)
```

We can see that students 2 and 3 have been assigned to topic 1, repetition 1. Students 
1 and 4 have been assigned to topic 2, repetition 1.

## Dataset 001 (skills only)

```{r}
# indicate appropriate columns using integer ids.
df_ex001_list <- extract_student_info(dba_gc_ex001, "diversity",
                                  demographic_cols = 2, skills = 3, 
                                  self_formed_groups = 4)
yaml_ex001_list <- extract_params_yaml(system.file("extdata", 
                                             "dba_params_ex001.yml",  
                                             package = "grouper"),
                                       "diversity")
m1a <- prepare_model(df_ex001_list, yaml_ex001_list, assignment="diversity",
                    w1=0.0, w2=1.0)
#result3 <- solve_model(m1a, with_ROI(solver="gurobi"))
result3 <- solve_model(m1a, with_ROI(solver="glpk"))

assign_groups(result3, assignment = "diversity", dframe=dba_gc_ex001, 
              group_names="groups")

get_solution(result3, smin)
get_solution(result3, smax)
```

Alternative workflow (wrapper + direct parameters):

```{r}
df_ex001_skill_alt <- extract_info(
  assignment = "diversity",
  dframe = dba_gc_ex001,
  demographic_cols = 2,
  skills = 3,
  self_formed_groups = 4
)

m1a_alt <- prepare_model(
  df_ex001_skill_alt,
  assignment = "diversity",
  w1 = 0.0,
  w2 = 1.0,
  n_topics = 2,
  nmin = 2,
  nmax = 2,
  rmin = 1,
  rmax = 1
)

result3_skill_alt <- solve_model(m1a_alt, with_ROI(solver = "glpk"))

assign_groups(
  model_result = result3_skill_alt,
  assignment = "diversity",
  dframe = dba_gc_ex001,
  group_names = "groups"
)

get_solution(result3_skill_alt, smin)
get_solution(result3_skill_alt, smax)
```

We can see that students 1 and 2 have been assigned to topic 1, repetition 1. Students 
3 and 4 have been assigned to topic 2, repetition 1.

## Dataset 003

This dataset demonstrates the use of a custom dissimilarity matrix instead of
using the default Gower distance from the
[cluster](https://cran.r-project.org/package=cluster) package.

```{r}
dba_gc_ex003
```

Now consider a situation where we wish to consider years 1 and 2 different from
years 3 and 4, and `math` and `dsds` (STEM majors) to be different from `elts`
and `history` (non-STEM majors). For each difference, we assign a score of 1.

This means that students 1 and 2 would have a dissimilarity score of 1 due to 
their difference in majors. Students 1 and 3 would also have a score of 1, but 
due to their difference in years. Students 1 and 4 would have score of 2, due to
their differences in majors and in years. The overall dissimilarity matrix would 
be:

```{r}
d_mat <- matrix(c(0, 1, 1, 2,
                  1, 0, 2, 1,
                  1, 2, 0, 1,
                  2, 1, 1, 0), nrow=4, byrow = TRUE)
```

To run the optimisation for this model, we can execute the following code:

```{r}
df_ex003_list <- extract_student_info(dba_gc_ex003, "diversity",
                                skills = NULL,
                                self_formed_groups = 3,
                                d_mat=d_mat)
yaml_ex003_list <- extract_params_yaml(system.file("extdata",   
                                                   "dba_params_ex003.yml",   
                                                   package = "grouper"), 
                                       "diversity")
m3 <- prepare_model(df_ex003_list, yaml_ex003_list, w1=1.0, w2=0.0)
result <- solve_model(m3, with_ROI(solver="glpk", verbose=TRUE))

assign_groups(result, "diversity", dba_gc_ex003, group_names="self_groups")
```

Alternative workflow (wrapper + direct parameters):

```{r}
df_ex003_alt <- extract_info(
  assignment = "diversity",
  dframe = dba_gc_ex003,
  skills = NULL,
  self_formed_groups = 3,
  d_mat = d_mat
)

m3_alt <- prepare_model(
  df_ex003_alt,
  assignment = "diversity",
  w1 = 1.0,
  w2 = 0.0,
  n_topics = 2,
  nmin = 2,
  nmax = 2,
  rmin = 1,
  rmax = 1
)

result_alt <- solve_model(m3_alt, with_ROI(solver = "glpk", verbose = TRUE))

assign_groups(
  model_result = result_alt,
  assignment = "diversity",
  dframe = dba_gc_ex003,
  group_names = "self_groups"
)
```

As you can see, the members of the two groups have maximal difference between 
them - they differ in terms of their year, and in terms of their major.

## Dataset 004

In this example, we demonstrate that `grouper` provides the flexibility to constrain
group sizes for individual topics. This could be useful in a situation where a 
particular project topic may require a larger group.

The dataset we use contains only skill levels (Python skills, higher corresponding
to more skill).

```{r}
dba_gc_ex004
```

Suppose we wish to assign the students to two topics, but the second topic requires
3 members, and the first requires only 2. In this example, we only utilise the 
skill levels; no demographic variables are included in the objective function.

```{r}
df_ex004_list <- extract_student_info(dba_gc_ex004, 
                                      skills = 2, 
                                      self_formed_groups = 3, 
                                      d_mat=matrix(0, 5, 5))
yaml_ex004_list <- extract_params_yaml(system.file("extdata",    
                                                   "dba_params_ex004.yml",   
                                                   package = "grouper"),  
                                       "diversity")
m4 <- prepare_model(df_ex004_list, yaml_ex004_list, w1=0.0, w2=1.0)
result <- solve_model(m4, with_ROI(solver="glpk", verbose=TRUE))
assign_groups(result, "diversity", dba_gc_ex004, group_names="self_groups")
```

Alternative workflow (wrapper + direct parameters):

```{r}
df_ex004_alt <- extract_info(
  assignment = "diversity",
  dframe = dba_gc_ex004,
  skills = 2,
  self_formed_groups = 3,
  d_mat = matrix(0, 5, 5)
)

m4_alt <- prepare_model(
  df_ex004_alt,
  assignment = "diversity",
  w1 = 0.0,
  w2 = 1.0,
  n_topics = 2,
  nmin = c(2, 3),
  nmax = c(2, 3),
  rmin = 1,
  rmax = 1
)

result4_alt <- solve_model(m4_alt, with_ROI(solver = "glpk", verbose = TRUE))
assign_groups(
  model_result = result4_alt,
  assignment = "diversity",
  dframe = dba_gc_ex004,
  group_names = "self_groups"
)
```

Due to the constraints, topic 2 was assigned 3 members, while preserving the 
total skill level in each group (to be 4).

# Preference-Based Assignment

## Dataset 002

The second datasets comprises 8 students. Here is a listing of the dataset:

```{r}
pba_gc_ex002
```

Each student is in a self-formed group of size 2, indicated via the `grouping`
column. Suppose that, for this set of students, the instructor wishes to assign
students into two topics, with each topic having two sub-groups. This requires
the preference matrix to have 4 columns - one for each topic-subgroup
combination. Remember that the ordering of topics/subtopics should be:

T1S1, T2S1, T1S2, T2S2

There should be 4 rows in the preference matrix - one for each self-formed
group.

```{r}
pba_prefmat_ex002
```

It is possible to assign each self-formed group to its optimal choice of
topic-subtopic combination. In our solution, we should see that group 1 is
assigned to subtopic 1 of topic 1, group 2 is assigned to sub-topic 1 of topic
2, and so on.

```{r}
df_ex002_list <- extract_student_info(pba_gc_ex002, "preference", 
                                      self_formed_groups = 2, 
                                      pref_mat = pba_prefmat_ex002)
yaml_ex002_list <- extract_params_yaml(system.file("extdata", 
                                             "pba_params_ex002.yml",  
                                             package = "grouper"),
                                       "preference")
m2 <- prepare_model(df_ex002_list, yaml_ex002_list, "preference")

#result2 <- solve_model(m2, with_ROI(solver="gurobi"))
result2 <- solve_model(m2, with_ROI(solver="glpk"))
assign_groups(result2, assignment = "preference", 
              dframe=pba_gc_ex002, yaml_ex002_list, 
              group_names="grouping")
```

Alternative workflow (wrapper + direct parameters):

```{r}
df_ex002_alt <- extract_info(
  assignment = "preference",
  dframe = pba_gc_ex002,
  self_formed_groups = 2,
  pref_mat = pba_prefmat_ex002
)

m2_alt <- prepare_model(
  df_ex002_alt,
  assignment = "preference",
  n_topics = 2,
  B = 2,
  nmin = 2,
  nmax = 2,
  rmin = 1,
  rmax = 1
)

result2_alt <- solve_model(m2_alt, with_ROI(solver = "glpk"))
assign_groups(
  model_result = result2_alt,
  assignment = "preference",
  dframe = pba_gc_ex002,
  params_list = list(n_topics = 2, B = 2),
  group_names = "grouping"
)
```

# Multi-role Workload Assignment

This example has four individuals and four courses, with
`past_ta + past_gr = 4` for each individual.

```{r}
multirole_students_ex001
multirole_prefmat_ex001
multirole_demand_ex001
```

The demand matrix intentionally excludes E. Because TA and GR demand are below
total capacity, `e_mode = "rr"` generates the remaining E demand.

```{r, include = FALSE}
c_sem <- 4
```

The direct workflow uses `extract_multirole_info()` and
`prepare_multirole_model()`. The example preference matrix is used for TA;
the GR terms are disabled in this first example.

```{r}
multirole_ex001 <- extract_multirole_info(
  student_df = multirole_students_ex001,
  d_mat = multirole_demand_ex001,
  p_ta_mat = multirole_prefmat_ex001,
  e_mode = "rr",
  C = c_sem
)

multirole_ex001$d
```

```{r}
m_multirole_ex001 <- prepare_multirole_model(
  multirole_ex001,
  ta_protected_max = 1,
  ta_min = 1, ta_max = 1,
  gr_min = 1, gr_max = 1,
  alpha_ta = 2, beta_ta = 1, phi = 1, rho_ta = 10,
  alpha_gr = NULL, beta_gr = NULL, rho_gr = NULL
)

result_multirole_ex001 <- solve_model(
  m_multirole_ex001,
  with_ROI(solver = "glpk")
)

assign_job(
  result_multirole_ex001,
  student_df = multirole_students_ex001,
  course_codes = rownames(multirole_demand_ex001),
  name_col = "Name"
)
```

## Full TA and GR workflow

The following wrapper workflow activates every objective component. For
simplicity, the same example preference matrix is used for TA and GR, although
the two matrices can be different in practice. Year 1 receives TA protection,
while Year 2 receives GR protection.

```{r}
full_multirole_inputs <- extract_info(
  assignment = "multirole",
  student_df = multirole_students_ex001,
  d_mat = multirole_demand_ex001,
  p_ta_mat = multirole_prefmat_ex001,
  p_gr_mat = multirole_prefmat_ex001,
  e_mode = "rr",
  C = c_sem
)

full_multirole_model <- prepare_model(
  full_multirole_inputs,
  assignment = "multirole",
  protected_year_ta = 1,
  protected_year_gr = 2,
  ta_protected_max = 1,
  gr_protected_max = 1,
  alpha_ta = 2,
  alpha_gr = 2,
  beta_ta = 1,
  beta_gr = 1,
  phi = 1,
  rho_ta = 10,
  rho_gr = 10
)

full_multirole_result <- solve_assignment(
  model = full_multirole_model,
  assignment = "multirole",
  solver = "glpk",
  student_df = multirole_students_ex001,
  course_codes = rownames(multirole_demand_ex001),
  name_col = "Name",
  verbose = FALSE
)

full_multirole_result$output
```

The resulting current-semester workload totals confirm that TA, GR, and E are
all allocated:

```{r, echo = FALSE}
full_assignment <- full_multirole_result$output
full_ta_cols <- grepl("-t$", names(full_assignment))
full_gr_cols <- grepl("-g$", names(full_assignment))
full_e_cols <- grepl("-e$", names(full_assignment))

data.frame(
  Name = full_assignment$Name,
  TA = rowSums(full_assignment[, full_ta_cols, drop = FALSE]),
  GR = rowSums(full_assignment[, full_gr_cols, drop = FALSE]),
  E = rowSums(full_assignment[, full_e_cols, drop = FALSE]),
  current_total = rowSums(
    full_assignment[
      , full_ta_cols | full_gr_cols | full_e_cols,
      drop = FALSE
    ]
  )
)
```

## Single-semester

The model enforces an annual total of `2 * C` for every individual. For a
single-semester allocation, set `single_semester = TRUE` during extraction.
Only `student_id` and `year` are then required as the first two columns.
Extraction ignores any supplied `past_ta` and `past_gr` columns and generates
`t1 = 0` and `g1 = C` for every individual.

The synthetic `g1 = C` values are uniform, so they shift every annual GR
workload by the same amount and do not change the GR spread. They fill one
semester of the annual equality and leave exactly `C` units per individual for
the current TA, GR, and E allocation. Capacity is supplied once during
extraction and is stored in the resulting input list for model preparation.

```{r}
multirole_students_ex001_sem <- multirole_students_ex001[
  , c("student_id", "year", "Name")
]

multirole_ex001_sem <- extract_multirole_info(
  student_df = multirole_students_ex001_sem,
  d_mat = multirole_demand_ex001,
  p_ta_mat = multirole_prefmat_ex001,
  e_mode = "rr",
  C = c_sem,
  single_semester = TRUE
)

m_multirole_ex001_sem <- prepare_model(
  multirole_ex001_sem,
  assignment = "multirole",
  ta_protected_max = 1,
  ta_min = 1, ta_max = 1,
  gr_min = 1, gr_max = 1
)

result_multirole_ex001_sem <- solve_model(
  m_multirole_ex001_sem,
  with_ROI(solver = "glpk")
)

job_multirole_ex001_sem <- assign_job(
  result_multirole_ex001_sem,
  student_df = multirole_students_ex001_sem,
  course_codes = rownames(multirole_demand_ex001),
  name_col = "Name"
)

job_multirole_ex001_sem
```

```{r}
ta_cols <- grepl("-t$", names(job_multirole_ex001_sem))
gr_cols <- grepl("-g$", names(job_multirole_ex001_sem))
e_cols <- grepl("-e$", names(job_multirole_ex001_sem))

data.frame(
  Name = job_multirole_ex001_sem$Name,
  TA = rowSums(job_multirole_ex001_sem[, ta_cols, drop = FALSE]),
  GR = rowSums(job_multirole_ex001_sem[, gr_cols, drop = FALSE]),
  E = rowSums(job_multirole_ex001_sem[, e_cols, drop = FALSE]),
  current_total = rowSums(
    job_multirole_ex001_sem[, ta_cols | gr_cols | e_cols, drop = FALSE]
  )
)
```

Each objective weight can be disabled with `NULL` or zero. Its objective
expression is omitted, and spread or protection variables and constraints are
also omitted when their corresponding weights are disabled.

Standalone constructors include `prepare_diversity_model()`,
`prepare_preference_model()`, and `prepare_multirole_model()`.
