2016-07-26 120 views
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個獨特的,非重複的球員,同時遵循其他約束條件?

回答

1

問題是你的約束矩陣沒有做你認爲它是什麼。如果查看您編碼的約束矩陣,您會看到底行包含的所有條目等於DK數據框中唯一播放器的數量。當約束矩陣乘以解向量時必須實現線性規劃約束。解矢量中的單個1和矩陣乘法後的結果條目將是總唯一播放器的數量。所以約束不能實現,也不會收斂。

這是一個破解工作。這並不美觀,但它完成了工作。

objective <- DK$DK.proj 
playerMatrix <- lapply(unique(DK$player), function(name) as.numeric(DK$player==name)) 
playerMatrix <- t(matrix(unlist(playerMatrix), ncol=n_distinct(DK$player))) 

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, 
          playerMatrix) 
constraint.directions <- c(
    "==", # SP 
    "==", # C 
    "==", # 1B 
    "==", # 2B 
    "==", # SS 
    "==", # 3B 
    "==", # OF, 
    "<=", # max salary 
    rep("<=", nrow(playerMatrix))) # unique players 

rhs <- c(
    2, # SP 
    1, # C 
    1, # 1B 
    1, # 2B 
    1, # SS 
    1, # 3B 
    3, # OF 
    50000, # max salary 
    rep(1, nrow(playerMatrix))) # unique players 

    sol <- Rglpk_solve_LP(obj = objective, mat = constraint.matrix, 
      dir = constraint.directions, rhs = rhs, 
      types = objective.variable.types, max = TRUE) 

玩家矩陣與唯一玩傢俱有相同的行數並跟蹤確保沒有多於一次選擇唯一玩家。

+0

太棒了,謝謝你的工作很棒! – NateN