我與各種auction algorithms工作評估ň項目分配 到ň代理通過競價機制,使得每個代理被分配到恰好一個項目,並且每個項目只能分配給一個代理。我想通過將它們與暴力方法進行比較來評估我正在測試的算法的性能。比較是 通過價值分配的總和,我試圖最大化。強力運算拍賣算法
set.seed(1)
#Assume:
n <- 3
agents <- 1:3 # agent IDs
items <-1:3 # item IDs
# each agent has a preference score for each item
# create agent x item matrix w/ cells containing pref score
(m <- matrix(data = round(runif(9, 0, 1),3), nrow = n, ncol = n))
## [,1] [,2] [,3]
## [1,] 0.266 0.908 0.945
## [2,] 0.372 0.202 0.661
## [3,] 0.573 0.898 0.629
# Given these sample data, here is one possible assignment
s <- data.frame(agent = agents, item = NA, score = NA)
# assign item & corresponding score to agent
s[1,"item"] <- 1; s[1,"score"] <- m[1,1]
s[2,"item"] <- 2; s[2,"score"] <- m[2,2]
s[3,"item"] <- 1; s[3,"score"] <- m[3,3]
s
## agent item score
## 1 1 1 0.266
## 2 2 2 0.202
## 3 3 1 0.629
# The value/score of this particular assignment s
(total_score <- sum(s$score))
## [1] 1.097
我想要做的是,給我的代理和項目載體是創建包含成員項任務的每一個可能的組合的數據結構。根據我的計算,應該有階乘(n)可能的組合。因此在例子中,最終結構應該有6行。
這是我想要的符號表示。每一行對應一個特定的全面分配,其中代理是列及其相應的項目將單元格的值:
# a1 a2 a3 <- cols are agents
# ______________
# s1 | 1 2 3 <- corresponds to assignment s above
# s2 | 1 3 2
# s3 | 2 1 3
# s4 | 2 3 1
# s5 | 3 2 1
# s6 | 3 1 2
我不清楚爲ň任何正值一般實現這一目標的最佳途徑。我嘗試過expand.grid()
,但似乎不符合我想要實現的 。有沒有我可以使用的功能呢,還是有人對我可以實現的算法有任何建議?
我想知道expand.grid可能對你有幫助。 –
[R:可以生成所有向量沒有重複元素的排列]的重複(http://stackoverflow.com/questions/14704039/r-generate-all-permutations-of-vector-without-duplicated-elements) –