当前位置: 首页 > 知识库问答 >
问题:

在其他变量的多个组合中最小化列的总和

程振濂
2023-03-14

这一次我的头撞到了墙上。。。希望能得到一些帮助。

我将在下面用模拟数据解释一个例子,但本质上,我试图在其他变量的多个组合中最小化列的总和。

问:在每个团队trailhead中,通过查看team_runners_at_departureother_runners_at_departure中跑步者的不同组合,让complete_team中的所有跑步者完成比赛(total_hours)的最快方法是什么?

下面数据集的每一行代表一场比赛的完整“跑步”。变量包括:

  • 出发时团队跑步者:这一行数据中跑步和计时的人

建立一个故事:假设我有关于一个跑步团队成员的不同组合完成一次试跑需要多长时间的信息。因此,一个跑步团队可能有两个人,他们可以单独或一起跑步,这将是三种组合,每个组合需要不同的小时数。

我想知道,对于他们可能跑的每一条赛道,团队完成跑步的最快方式是什么——是一起跑,还是分开跑,等等?因此,需要知道多个运行时间(即数据集中的行)并按组将其最小化。

然而,棘手的是其他跑步者也在场。假设他们对团队成员的影响不同,所以我最终想知道所有可能的组合:跑步团队成员和其他可能的跑步者的组合,完成每个完整团队的最快方式是什么。同样,这需要知道一个团队中多组跑步者的时间,并按组最小化总时间。

R的数据:

structure(list(team_runners_at_departure = c("adam", "adam", 
"adam", "adam", "adam", "adam", "adam", "adam", "adam, brittany", 
"adam, brittany", "adam, brittany", "adam, brittany", "adam, brittany", 
"adam, brittany", "adam, brittany", "adam, brittany", "adam, mike", 
"adam, mike", "adam, mike", "adam, mike", "adam, mike", "adam, mike", 
"adam, mike", "adam, mike", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"brittany", "brittany", "brittany", "brittany", "brittany", "brittany", 
"brittany", "brittany", "mike", "mike", "mike", "mike", "mike", 
"mike", "mike", "mike", "mike, brittany", "mike, brittany", "mike, brittany", 
"mike, brittany", "mike, brittany", "mike, brittany", "mike, brittany", 
"mike, brittany", "brad", "brad", "brad", "brad", "brad", "brad", 
"brad", "brad", "brad, hailey", "brad, hailey", "brad, hailey", 
"brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
"brad, hailey", "hailey", "hailey", "hailey", "hailey", "hailey", 
"hailey", "hailey", "hailey"), other_runners_at_departure = c("fred", 
"fred", "fred, gina", "fred, gina", "gina", "gina", "none", "none", 
"fred", "fred", "fred, gina", "fred, gina", "gina", "gina", "none", 
"none", "fred", "fred", "fred, gina", "fred, gina", "gina", "gina", 
"none", "none", "fred", "fred", "fred, gina", "fred, gina", "gina", 
"gina", "none", "none", "fred", "fred", "fred, gina", "fred, gina", 
"gina", "gina", "none", "none", "fred", "fred", "fred, gina", 
"fred, gina", "gina", "gina", "none", "none", "fred", "fred", 
"fred, gina", "fred, gina", "gina", "gina", "none", "none", "amy", 
"amy", "john", "john", "john, amy", "john, amy", "none", "none", 
"amy", "amy", "john", "john", "john, amy", "john, amy", "none", 
"none", "amy", "amy", "john", "john", "john, amy", "john, amy", 
"none", "none"), all_runners_at_departure = c("adam fred", "adam fred", 
"adam fred gina", "adam fred gina", "adam gina", "adam gina", 
"adam", "adam", "adam brittany fred", "adam brittany fred", "adam brittany fred gina", 
"adam brittany fred gina", "adam brittany gina", "adam brittany gina", 
"adam brittany", "adam brittany", "adam mike fred", "adam mike fred", 
"adam mike fred gina", "adam mike fred gina", "adam mike gina", 
"adam mike gina", "adam mike", "adam mike", "adam mike brittany fred", 
"adam mike brittany fred", "adam mike brittany fred gina", "adam mike brittany fred gina", 
"adam mike brittany gina", "adam mike brittany gina", "adam mike brittany", 
"adam mike brittany", "brittany fred", "brittany fred", "brittany fred gina", 
"brittany fred gina", "brittany gina", "brittany gina", "brittany", 
"brittany", "mike fred", "mike fred", "mike fred gina", "mike fred gina", 
"mike gina", "mike gina", "mike", "mike", "mike brittany fred", 
"mike brittany fred", "mike brittany fred gina", "mike brittany fred gina", 
"mike brittany gina", "mike brittany gina", "mike brittany", 
"mike brittany", "brad amy", "brad amy", "brad john", "brad john", 
"brad john amy", "brad john amy", "brad", "brad", "brad hailey amy", 
"brad hailey amy", "brad hailey john", "brad hailey john", "brad hailey john amy", 
"brad hailey john amy", "brad hailey", "brad hailey", "hailey amy", 
"hailey amy", "hailey john", "hailey john", "hailey john amy", 
"hailey john amy", "hailey", "hailey"), complete_team = c("adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
"adam, mike, brittany", "brad, hailey", "brad, hailey", "brad, hailey", 
"brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
"brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
"brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
"brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
"brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
"brad, hailey"), team = c("team1", "team1", "team1", "team1", 
"team1", "team1", "team1", "team1", "team1", "team1", "team1", 
"team1", "team1", "team1", "team1", "team1", "team1", "team1", 
"team1", "team1", "team1", "team1", "team1", "team1", "team1", 
"team1", "team1", "team1", "team1", "team1", "team1", "team1", 
"team1", "team1", "team1", "team1", "team1", "team1", "team1", 
"team1", "team1", "team1", "team1", "team1", "team1", "team1", 
"team1", "team1", "team1", "team1", "team1", "team1", "team1", 
"team1", "team1", "team1", "team2", "team2", "team2", "team2", 
"team2", "team2", "team2", "team2", "team2", "team2", "team2", 
"team2", "team2", "team2", "team2", "team2", "team2", "team2", 
"team2", "team2", "team2", "team2", "team2", "team2"), trailhead_start = c("trail_one", 
"trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
"trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
"trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
"trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
"trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
"trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
"trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
"trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
"trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
"trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
"trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
"trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
"trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
"trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
"trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
"trail_one", "trail_two", "trail_one", "trail_two"), total_hours = c(10L, 
7L, 6L, 3L, 9L, 10L, 7L, 6L, 6L, 4L, 7L, 6L, 2L, 7L, 7L, 7L, 
8L, 2L, 3L, 3L, 8L, 2L, 9L, 2L, 3L, 4L, 4L, 4L, 5L, 7L, 9L, 4L, 
2L, 6L, 7L, 1L, 6L, 9L, 9L, 9L, 6L, 8L, 7L, 1L, 9L, 6L, 4L, 8L, 
3L, 4L, 3L, 3L, 4L, 5L, 7L, 4L, 3L, 9L, 8L, 6L, 2L, 5L, 3L, 5L, 
5L, 4L, 5L, 9L, 10L, 9L, 10L, 5L, 10L, 10L, 7L, 8L, 9L, 10L, 
7L, 3L)), row.names = c(NA, -80L), class = c("tbl_df", "tbl", 
"data.frame"))

共有1个答案

徐友樵
2023-03-14

鉴于当前的信息,我尝试了以下方法:创建数据:

require(dplyr)
df <- structure(list(team_components_at_departure = c("adam", "adam", 
                                                "adam", "adam", "adam", "adam", "adam", "adam", "adam, brittany", 
                                                "adam, brittany", "adam, brittany", "adam, brittany", "adam, brittany", 
                                                "adam, brittany", "adam, brittany", "adam, brittany", "adam, mike", 
                                                "adam, mike", "adam, mike", "adam, mike", "adam, mike", "adam, mike", 
                                                "adam, mike", "adam, mike", "adam, mike, brittany", "adam, mike, brittany", 
                                                "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                                "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                                "brittany", "brittany", "brittany", "brittany", "brittany", "brittany", 
                                                "brittany", "brittany", "mike", "mike", "mike", "mike", "mike", 
                                                "mike", "mike", "mike", "mike, brittany", "mike, brittany", "mike, brittany", 
                                                "mike, brittany", "mike, brittany", "mike, brittany", "mike, brittany", 
                                                "mike, brittany", "brad", "brad", "brad", "brad", "brad", "brad", 
                                                "brad", "brad", "brad, hailey", "brad, hailey", "brad, hailey", 
                                                "brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
                                                "brad, hailey", "hailey", "hailey", "hailey", "hailey", "hailey", 
                                                "hailey", "hailey", "hailey"), 
               other_team_members_at_departure = c("fred", 
                                                   "fred", "fred, gina", "fred, gina", "gina", "gina", "none", "none", 
                                                   "fred", "fred", "fred, gina", "fred, gina", "gina", "gina", "none", 
                                                   "none", "fred", "fred", "fred, gina", "fred, gina", "gina", "gina", 
                                                   "none", "none", "fred", "fred", "fred, gina", "fred, gina", "gina", 
                                                   "gina", "none", "none", "fred", "fred", "fred, gina", "fred, gina", 
                                                   "gina", "gina", "none", "none", "fred", "fred", "fred, gina", 
                                                   "fred, gina", "gina", "gina", "none", "none", "fred", "fred", 
                                                   "fred, gina", "fred, gina", "gina", "gina", "none", "none", "amy", 
                                                   "amy", "john", "john", "john, amy", "john, amy", "none", "none", 
                                                   "amy", "amy", "john", "john", "john, amy", "john, amy", "none", 
                                                   "none", "amy", "amy", "john", "john", "john, amy", "john, amy", 
                                                   "none", "none"), 
               all_team_mems_at_departure = c("adam fred", 
                                              "adam fred", "adam fred gina", "adam fred gina", "adam gina", 
                                              "adam gina", "adam", "adam", "adam brittany fred", "adam brittany fred", 
                                              "adam brittany fred gina", "adam brittany fred gina", "adam brittany gina", 
                                              "adam brittany gina", "adam brittany", "adam brittany", "adam mike fred", 
                                              "adam mike fred", "adam mike fred gina", "adam mike fred gina", 
                                              "adam mike gina", "adam mike gina", "adam mike", "adam mike", 
                                              "adam mike brittany fred", "adam mike brittany fred", "adam mike brittany fred gina", 
                                              "adam mike brittany fred gina", "adam mike brittany gina", "adam mike brittany gina", 
                                              "adam mike brittany", "adam mike brittany", "brittany fred", 
                                              "brittany fred", "brittany fred gina", "brittany fred gina", 
                                              "brittany gina", "brittany gina", "brittany", "brittany", "mike fred", 
                                              "mike fred", "mike fred gina", "mike fred gina", "mike gina", 
                                              "mike gina", "mike", "mike", "mike brittany fred", "mike brittany fred", 
                                              "mike brittany fred gina", "mike brittany fred gina", "mike brittany gina", 
                                              "mike brittany gina", "mike brittany", "mike brittany", "brad amy", 
                                              "brad amy", "brad john", "brad john", "brad john amy", "brad john amy", 
                                              "brad", "brad", "brad hailey amy", "brad hailey amy", "brad hailey john", 
                                              "brad hailey john", "brad hailey john amy", "brad hailey john amy", 
                                              "brad hailey", "brad hailey", "hailey amy", "hailey amy", "hailey john", 
                                              "hailey john", "hailey john amy", "hailey john amy", "hailey", 
                                              "hailey"), 
               complete_team = c("adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "adam, mike, brittany", "adam, mike, brittany", "adam, mike, brittany", 
                                 "brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
                                 "brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
                                 "brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
                                 "brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
                                 "brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey", 
                                 "brad, hailey", "brad, hailey", "brad, hailey", "brad, hailey"
               ), 
               team = c("team1", "team1", "team1", "team1", "team1", "team1", 
                        "team1", "team1", "team1", "team1", "team1", "team1", "team1", 
                        "team1", "team1", "team1", "team1", "team1", "team1", "team1", 
                        "team1", "team1", "team1", "team1", "team1", "team1", "team1", 
                        "team1", "team1", "team1", "team1", "team1", "team1", "team1", 
                        "team1", "team1", "team1", "team1", "team1", "team1", "team1", 
                        "team1", "team1", "team1", "team1", "team1", "team1", "team1", 
                        "team1", "team1", "team1", "team1", "team1", "team1", "team1", 
                        "team1", "team2", "team2", "team2", "team2", "team2", "team2", 
                        "team2", "team2", "team2", "team2", "team2", "team2", "team2", 
                        "team2", "team2", "team2", "team2", "team2", "team2", "team2", 
                        "team2", "team2", "team2", "team2"), 
               trailhead = c("trail_one", 
                             "trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
                             "trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
                             "trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
                             "trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
                             "trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
                             "trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
                             "trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
                             "trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
                             "trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
                             "trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
                             "trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
                             "trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
                             "trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
                             "trail_one", "trail_two", "trail_one", "trail_two", "trail_one", 
                             "trail_two", "trail_one", "trail_two", "trail_one", "trail_two", 
                             "trail_one", "trail_two", "trail_one", "trail_two"), 
               total_hours = c(10L, 
                               7L, 6L, 3L, 9L, 10L, 7L, 6L, 6L, 4L, 7L, 6L, 2L, 7L, 7L, 7L, 
                               8L, 2L, 3L, 3L, 8L, 2L, 9L, 2L, 3L, 4L, 4L, 4L, 5L, 7L, 9L, 4L, 
                               2L, 6L, 7L, 1L, 6L, 9L, 9L, 9L, 6L, 8L, 7L, 1L, 9L, 6L, 4L, 8L, 
                               3L, 4L, 3L, 3L, 4L, 5L, 7L, 4L, 3L, 9L, 8L, 6L, 2L, 5L, 3L, 5L, 
                               5L, 4L, 5L, 9L, 10L, 9L, 10L, 5L, 10L, 10L, 7L, 8L, 9L, 10L, 
                               7L, 3L)), 
          row.names = c(NA, -80L), 
          class = c("tbl_df", "tbl", 
                    "data.frame"))

使用此数据结构,计算每个成员组合的总比赛时间:

df <- as.data.frame(df)
dfs <- split(df, df$all_team_mems_at_departure)
times <- sapply(dfs, function(df) sum(df$total_hours))

在您给出的这个示例数据中,如果您检查dfs,则取决于完全相同的行,除了始终为trail\u one和trail\u two以及总小时数的trailhead。虽然总的_小时是如此不同,例如我假设的7小时和1小时,但必须将它们相加才能得到总的赛道/比赛时间。

然后使用此信息查找最小时间组:

names(times[times == min(times)])
## [1] "adam mike fred gina"     "mike brittany fred gina"
 类似资料:
  • 我有一个元素数组< code>[(A1,B1),...,(An,Bn)](都是正浮点和Bi 毫无疑问,我可以尝试所有这些方法,并选择一个给出最小和的方法(这将给出正确的结果O(n!)). 我尝试将总和更改为并尝试使用贪婪算法,该算法在每个步骤上获取最大的Ai元素(这不会产生正确的结果)。 现在,当我看最新的方程时,我觉得这里我看到了最优子结构,因此我必须使用动态编程,但我无法找出正确的方向。有什么

  • 给定一个正整数数组,找出子集的最小数目,其中: 子集中每个元素的总和不超过一个值,k。 数组中的每个元素在任何子集中只使用一次 数组中的所有值都必须出现在任何子集中。 基本上,一个“填充”算法,但需要最小化容器,并需要确保所有东西都被填满。我目前的想法是按降序排序,当总和超过k时开始创建集合,开始下一个,但不确定什么是更好的方法。 编辑: Ex: 在输出集合中,所有1-5都被使用,并且在集合中仅使

  • 问题内容: 表_1 D_ID整数 Deposit_amt整数 表_2 Total_ID Total_amt整数 是否有可能写一个SELECT语句来找到所有的行,其总和为中。两个表中都有多个行。 假设第一行中有一个。我想知道,在与行2,6,12求和= 100,行2,3,42求和= 100,等。 帮助表示赞赏。让我知道是否需要澄清。 我问这个问题,因为某人作为工作的一部分,拥有交易清单和总计清单,她需

  • 以下是经典的实践中的一致性: 当线程A写入一个易失性变量,随后线程B读取相同的变量时,A在写入易失性变量之前可见的所有变量的值在读取易失性变量后变得对B可见。 我不确定我真的能理解这句话。例如,在这种情况下,所有变量的含义是什么?这是否意味着使用对使用非volatile变量也有副作用<在我看来,这句话有一些我无法理解的微妙含义<有什么帮助吗?

  • 我有一个问题要用java来解决。 我的代码有三个变量:a、b、c,分别是双精度的。所有变量之和必须等于1。 我需要测试这些变量中所有可能的组合,值从0.10变化 示例: 组合1: 组合2: 然后继续。 是否有任何框架或库来自动化这种测试?

  • 问题内容: 我需要在其他班上变数。我怎样才能做到这一点? 所以我尝试在Textcl.class中使用,但是得到了。 问题答案: 您将得到空值,因为inString从未按Robert Kilar在注释中正确指出的那样进行初始化。 您可以通过使用类名来引用静态变量。 示例ClassName.variablename。就你而言 运行您的主类。当您运行inString时,将在该类的构造函数中对其进行初始化