这是一个大矩阵的一部分(维度大约为:1,000-1,000,000行 x 100 - 1,000列):
scen_1 scen_2 scen_3 scen_4 ...
...
9 3.262275 0.000000 0.00000 0.0000000 ...
10 2.843631 0.000000 1.22636 1.0559217 ...
11 0.000000 0.000000 0.00000 0.9836209 ...
12 2.572686 0.000000 0.00000 1.1000293 ...
13 0.000000 0.000000 0.00000 0.0000000 ...
14 0.611070 1.478159 0.00000 0.0000000 ...
15 0.000000 0.000000 0.00000 0.0000000 ...
16 0.000000 0.000000 0.00000 1.0146529 ...
...
现在,我想选择n行,在每列获得最大值后,有最高的总和,因此行之间要很好地互补。例如,我选择第9和第10行,我得到组合(最大值)向量
3.262275 0.00000 1.22636 1.0559217
,总和为5.5445567
。而如果我选择第14和第16行,我会得到0.611070 1.478159 0.00000 1.0146529
,总和为3.1038819
,因此第一种选择更好。对于上述示例,n为3的解决方法将是10、14和9行。我希望我能解释清楚问题。
我的方法是首先选择行总和最高的行,然后选择增加最高附加值的行。但我强烈感觉这并不总是给出最佳解决方案。由于矩阵的大小,计算所有可能的组合是不可行的。遗传算法是否可行?还是有更简单的方法?
谢谢。
编辑:
为了更容易理解,这里是一个MWE:
# Create example matrix
mat <- matrix(c(1.562275, 0.000000, 0.00000, 0.0000000,2.843631, 0.000000, 1.22636, 1.0559217,0.000000, 0.000000, 0.00000, 0.9836209,1.572686, 0.000000, 0.00000, 1.8000293,0.000000, 0.000000, 0.00000, 0.0000000,1.611070, 1.478159, 0.00000, 0.0000000,0.000000, 0.000000, 0.00000, 0.0000000,0.000000, 0.000000, 0.00000, 1.0146529), byrow = TRUE, ncol = 4, dimnames = list(c(9:16), c("scen_1", "scen_2", "scen_3", "scen_4")))
# Function to evaluate each combination of rows (this value should be maximized)
get_combined_max_value_sum <- function(choosen_rows){
# Select rows
sel_mat <- mat[choosen_rows,]
# calculate columwise max
max_mat <- apply(sel_mat, 2, max)
# Sum the values
return(sum(max_mat))
}
# I am looking for the function best_rows() which returns the rows, which gives the
# maximum value (or at least a close guess) for the get_combined_max_value_sum()
# function
best_rows <- function(n_rows){
result <- vector()
# do some magic
return(result) # vector with length n_row for the "best" rows.
}
# ------------------------------------------------
# @ slamballais
# The rows with the highest rowise sum (10 & 12)
get_combined_max_value_sum(c("10","12"))
# get a lower score then row 9 and 13
get_combined_max_value_sum(c("10","14"))
rowSums
来获取每行的总和,然后将行从大到小排序吗?然后你可以选择前 N 行,其中 N 是你想要的任何值。 - slamballaismax
吗?max_mat <- apply(sel_mat, 1, max)
。另外,为了澄清,你不是在寻找检查每个值组合的答案吗? - Ronak Shah