I'm looking to create a mathematical programming model, it's probably simple but it's been a long time since I've worked this kind of problem and I'm stuck. Here's a story problem to tee this up:

Say a teacher has 4 toys for her 10 students. In a given day, she counts whether or not student *i* played with toy *j*.

```
toys <- tibble::tribble(
~student, ~toy_a, ~toy_b, ~toy_c, ~toy_d,
1L, 1L, 1L, 0L, 0L,
2L, 0L, 0L, 1L, 0L,
3L, 0L, 1L, 1L, 1L,
4L, 1L, 1L, 1L, 1L,
5L, 1L, 1L, 0L, 0L,
6L, 0L, 0L, 0L, 0L,
7L, 1L, 0L, 0L, 1L,
8L, 0L, 1L, 1L, 0L,
9L, 1L, 0L, 0L, 1L,
10L, 1L, 1L, 1L, 0L
)
```

The teacher would like to pick the optimal set of two (an arbitrary number) toys out of the four, such that the largest percentage of students are satisfied. A given student will be satisfied with the set of two toys if he played with at least one (another arbitrary number) of the toys.

Here's how I've set this up in my head...

- There are 4 "decision variables", one for each toy, representing whether or not the toy is included in the new set.
- It seems that these decision variables could be represented by a numeric vector of ones and zeros.
- The the thing we're trying to optimize is the proportion of students satisfied with the new set, this value will of course be bound between 0 and 1.

```
toys_m <- as.matrix(toys[-1])
mean_toy_satisfaction <- function(toys.m = toys_m, included, min.play = 1) {
# this "toggles on/off" toys in the set
toys.m <- toys.m %*% diag(included)
# how many toys did a given student play with in this set?
num.toys.played <- rowSums(toys.m)
# meet satisfaction criteria?
satisfied <- num.toys.played >= min.play
mean(satisfied)
}
# example where toys A and B make it into the set
mean_toy_satisfaction(
toys.m = toys_m,
included = c(1, 1, 0, 0),
min.play = 1
)
#> [1] 0.8
```

So, the variable which needs optimizing is the vector passed to the `included`

argument to the above function. Again, this seems like it should be rather easy to implement (and it is, if you want to use Excel Solver ).

I've looked through a number of the optimization packages, but I'm honestly not sure where to start, if anyone could point me in the right direction or help frame the problem better I'd greatly appreciate it.