我正在努力实现lpSolve解决方案,以优化假设的日常幻想棒球问题。我在应用最后一个约束时遇到了麻烦:
举例来说,假设您有一个包含1000个球员的数据框,其中包含积分,成本,位置和球队,并且您正在尝试最大化平均积分:
library(tidyverse)
library(lpSolve)
set.seed(123)
df <- data_frame(avg_points = sample(5:45,1000, replace = T),
cost = sample(3:45,1000, replace = T),
position = sample(c("P","C","1B","2B","3B","SS","OF"),1000, replace = T),
team = sample(LETTERS,1000, replace = T)) %>% mutate(id = row_number())
head(df)
# A tibble: 6 x 5
# avg_points cost position team id
# <int> <int> <chr> <chr> <int>
#1 17 13 2B Y 1
#2 39 45 1B P 2
#3 29 33 1B C 3
#4 38 31 …Run Code Online (Sandbox Code Playgroud)