R: Optimally Sharing Cookies Within Groups of Friends
Asked Answered
S

0

7

I am working with the R programming language.

Suppose there are 100 people - each person is denoted with an ID from 1:100. Each person can be friends with other people. The dataset can be represented in graph/network format and looks something like this:

# Set the seed for reproducibility
set.seed(123)

# Generate a vector of ID's from 1 to 100
ids <- 1:100

# Initialize an empty data frame to store the "from" and "to" values
edges <- data.frame(from=integer(), to=integer(), stringsAsFactors=FALSE)

# Iterate through the ID's
for(id in ids) {
    # Randomly select a minimum of 1 and a maximum of 8 neighbors for the current ID
    neighbors <- sample(ids[ids != id], size=sample(1:8, size=1))
    
    # Add a new row to the data frame for each "to" value
    for(neighbor in neighbors) {
        edges <- rbind(edges, data.frame(from=id, to=neighbor))
    }
}

As we can see, the data can be visualized to reveal the graph/network format:

library(igraph)
library(visNetwork)


# Convert the data frame to an igraph object
g <- graph_from_data_frame(edges, directed=FALSE)

# Plot the graph
plot(g)

# Optional visualization
#visIgraph(g)

enter image description here

Now, suppose each person in this dataset has has a certain number of cookies. This looks something like this:

set.seed(123)

cookies = data.frame(id = 1:100, number_of_cookies = c(abs(as.integer(rnorm(25, 15, 5))), abs(as.integer(rnorm(75, 5, 5)))))
                 

Here is my question:

  • I want to make sure that no person in this dataset has less than 12 cookies - that is, if someone has less than 12 cookies, they can "pool" together their cookies with their neighbors (e.g. first-degree neighbors, second-degree neighbors, ... n-th degree neighbors ... until this condition is satisfied) and see if they now have more than 12 cookies
  • However, I also want to make sure that during this pooling process, no pooled group of friends has more than 20 cookies (i.e. this might require having to "un-pool" neighbors that were previously pooled together)
  • And finally, if someone is in a group with some other people - this person can not be then placed into another group (i.e. no "double dipping")

I wrote a function that takes an ID as an input and then returns the total number of cookies for this ID and all of this ID's neighbors:

library(data.table)
library(dplyr)


sum_cookies_for_id <- function(id) {
    
    # Get the connected IDs for the given ID
    connected_ids <- c(id, edges[edges$from == id | edges$to == id, "to"])
    
    # Sum the number of cookies for all connected IDs
    sum(cookies[cookies$id %in% connected_ids, "number_of_cookies"])
}

# Test the function
sum_cookies_for_id(23)

But beyond this, I am not sure how to continue.

Can someone please show me how I might be able to continue writing the code for this problem? Can Dynamic Programming be used in such an example?

Thanks!

Notes:

  • I think that this problem might have a "stochastic" nature - that is, depending on which ID you begin with and which other ID's you group this specific ID with ... might eventually lead to fewer or more people with less than 12 cookies
  • I think that writing a "greedy" algorithm that performs random groupings might be the easiest option for such a problem?
  • In the future, I would be interested in seeing how more "sophisticated algorithms" (e.g. Genetic Algorithm) could be used to make groupings such that the fewest number of people with less than 12 cookies are left behind.
  • Food for Thought: Is it possible that perhaps some pre-existing graph/network clustering algorithm could be used for this problem while taking into consideration these total/sum constraints (e.g. Louvain Community Detection)?
Sylvanus answered 29/12, 2022 at 2:59 Comment(12)
As it is hard to eat one's nth degree neighbor's cookie, the concept of pooling such that a group might contain, at minimum 12 (cookies) however much dispersed, seems a game theory based simulation of impoverishment. Beyond pooling, what other mechanisms of exchange do you envision, or have I missed something above? Might a widely dispersed group start only with 12 and have to dig their way out of the well...And perhaps, among neighbors, you have a one off 'kill you for your cookies' card you can play, but when?Cantrell
@ Chris: thank you for your reply! I just wanted to create a generic question which makes use of WHILE LOOPS that randomly groups neighbors together while checking if different conditions (i.e. >12 and <20) are satisfied and updating a "list" of eligible neighbors. I never thought that this might be a Game Theory problem - but thank you for this insight!Sylvanus
So, rather than random, is it socially stratified such that a given ID only meets others within some range, say +/- three cookies of their number of cookies, and, as in poker, if you pull your kill card and they have more, either they get all your cookies or they are distributed around the pool, less, you get them, tied, no loss of cookies, just the kill card, and forever a fugitive from contact. And enviably strange and difficult to simulate, as number of cookies is secret, up to a point. You might know how many cookies Joe's group has, but how many Joe, his secret...Cantrell
@ Chris: Thank you for your reply! I don't really know much about Poker ... maybe I should I start reading about this. I have a feeling that this problem might be a lot more difficult than I previously anticipated. Note that I am not looking for globally optimal groupings...I am just trying to do "better" than I currently have (i.e. at least form some groupings). I will start reading more. Thank you so much!Sylvanus
Due to your graphing problem, you're presenting rather more that a typical while loop question admits. I'll stop pestering with off the point comments and remember to delete later. And it isn't poker per se, just another potential 'rule' about why and who might meet, were this is a game context. But then, there's partying in Time Square, and you never know who you'll meet there that wants your cookies...Happy New Year, and be Safe.Cantrell
@ Chris: Thank you for your reply! Haha, I have never made it down to Time Square before, is it cold during there? Let's hope that we live in "integer land" in which individual cookies can't be broken into smaller pieces and make this problem skyrocket ... . I was looking at your profile here on StackOverflow, you ask some good questions and post some good answers! Happy New Year my friend - wishing you all the best and hope that next year around this time, we will have looked back and realized how many new things we have learned!Sylvanus
And I'm all for that. It could be another 'rule', in addition to one kill, one awkward interaction with a non-+/-three-er whose results are a dice roll. And yes, it is generally plenty darn cold.Cantrell
As a personal comment - an idea I had long ago. If both of us are on an island and I have 1 cookie - if I eat the whole cookie, I will maximize some utility function (e.g. calories) in the short term - but if i share half with you, there might be a greater probability that you will share with me tomorrow should the roles be reversed ... and this way, if we meet a tiger, there will be two us which will increase the odds of our individual survivals. but perhaps I could still accomplish both of these by only giving you 40% of my cookie... or even 37% of my cookie? How much should I share? LOLSylvanus
Usefully, the tiger isn't interested in cookies, and it's presence tends to focus the mind on the immediate. I find, in breaking cookies with my wife, that perception of +/- 10% is sufficiently close to half. I suppose at 37, I'd notice. But then there's the phrase ' that's the way...' And if you were using stones rather than cookies, the cost function of carrying, Easter Island.Cantrell
I don't the problem is fully defined. Is there a mechanism for a person or group to get rid of a cookie? If that's the case, then it seems to me that the group can count how many cookies they have and simply discard cookies until 20 remain.Heterogenesis
with the function you gave OP, people can (and do) start out with more than 20 cookiesHumblebee
@Sylvanus you never replied to my message on this one!Humblebee

© 2022 - 2024 — McMap. All rights reserved.