1
我有一個優化問題,提供了一些問題。RGLPK約束問題
當我運行下面的代碼時,我得到了一個最佳解決方案。
objective <- DK$DK.proj
objective.variable.types <- rep("B", length(DK$player))
constraint.matrix <- rbind(as.numeric(DK$pos == "SP"),
as.numeric(DK$pos == "C"),
as.numeric(DK$pos == "1B"),
as.numeric(DK$pos == "2B"),
as.numeric(DK$pos == "SS"),
as.numeric(DK$pos == "3B"),
as.numeric(DK$pos == "OF"),
DK$salary)
constraint.directions <- c(
"==", # SP
"==", # C
"==", # 1B
"==", # 2B
"==", # SS
"==", # 3B
"==", # OF,
"<=") # max salary
rhs <- c(
2, # SP
1, # C
1, # 1B
1, # 2B
1, # SS
1, # 3B
3, # OF
50000) # max salary
Rglpk_solve_LP(obj = objective, mat = constraint.matrix, dir = constraint.directions, rhs = rhs,
types = objective.variable.types, max = TRUE)
但問題是一些球員在數據框架中被重複,因爲他們有多個職位。有兩行同一個玩家,只有一個不同的位置列,但我只能使用每個玩家一次。所以我加了下面的約束。
objective <- DK$DK.proj
objective.variable.types <- rep("B", length(DK$player))
constraint.matrix <- rbind(as.numeric(DK$pos == "SP"),
as.numeric(DK$pos == "C"),
as.numeric(DK$pos == "1B"),
as.numeric(DK$pos == "2B"),
as.numeric(DK$pos == "SS"),
as.numeric(DK$pos == "3B"),
as.numeric(DK$pos == "OF"),
DK$salary,
n_distinct(DK$player))
constraint.directions <- c(
"==", # SP
"==", # C
"==", # 1B
"==", # 2B
"==", # SS
"==", # 3B
"==", # OF,
"<=", # max salary
"==") # max players
rhs <- c(
2, # SP
1, # C
1, # 1B
1, # 2B
1, # SS
1, # 3B
3, # OF
50000, # max salary
10) # max players
Rglpk_solve_LP(obj = objective, mat = constraint.matrix, dir = constraint.directions, rhs = rhs,
types = objective.variable.types, max = TRUE)
現在我無法得到一個可行的解決方案。關於我應該改變約束的任何建議,以便我可以始終擁有10個獨特的,非重複的球員,同時遵循其他約束條件?
太棒了,謝謝你的工作很棒! – NateN