我有一套解決方案,但我試圖通過使用最大重疊來決議它們以使它們彼此更加不同。這是解決方案集(以及零和一的視圖 - 它們是指示是否選擇了 AF 的二進制變數),我的偽代碼嘗試解決它(代碼下方有更多詳細資訊):
solutions <- list(c(1, 0, 0, 1, 1, 1), c(0, 1, 0, 0, 1, 1), c(0, 1, 0, 1, 1, 1), c(1, 0, 0, 1, 0, 1))
1 0 0 1 1 1
0 1 0 0 1 1
0 1 0 1 1 1
1 0 0 1 0 1
# Pseudocode attempt
k = 2 #max allowed overlaps
i = 1 #counter
final_solutions <- list() #list of final solutions that are valid
while (i <= length(solutions)) {
if (i == 1) { #putting the first solution in, no checking
final_solutions[i] <- solutions[[i]]
}
j = 0 #number of overlaps
for (member in final_solutions) { #iterate through all solutions that have been validated
if overlap(solution[[i]], member) > k {
j = j 1
}
}
if (j == 0) { final_solutions <- append(final_solutions, solution[[i]]) }
}
基本上,我現在正在嘗試遍歷此串列,并且只保留與集合中的其他解決方案最多共享兩個常見 1 的解決方案。
所以,首先我會采用第一個解決方案 (1, 0, 0, 1, 1, 1) 并將其添加到我的集合中。
接下來我會看看 (0, 1, 0, 0, 1, 1)。它與第一個解決方案共享最后兩個位置的 1,所以這很好,它會被添加。
Next, I look at the third solution, (0, 1, 0, 1, 1, 1). It shares the 1 in the last three positions with the first solution, as well as the second 1 and the last two 1's with the second solution. It is not added to the set.
Finally, looking at the fourth solution (1, 0, 0, 1, 0, 1) - its three 1's are shared with the first solution, so it is not added.
I'm thinking something like the pseudocode above but would appreciate any help / help with fixing that syntax as I'm not very good with lists!
uj5u.com熱心網友回復:
如果您想要一種遞回方式,這里可能是通過定義遞回函式的一個選項f
f <- function(lst) {
if (length(lst) == 1) {
return(lst)
}
nlst <- tail(lst, 1)
plst <- head(lst, -1)
v <- Recall(plst)
if (all(nlst[[1]] %*% do.call(cbind,plst) <= 2)) {
v <- c(v, nlst)
}
v
}
你會看到
> f(solutions)
[[1]]
[1] 1 0 0 1 1 1
[[2]]
[1] 0 1 0 0 1 1
或者,我們可以使用Reduce但遵循相同的想法
Reduce(
function(x, y) {
if (all(y %*% do.call(cbind, x) <= 2)) {
return(c(x, list(y)))
}
x
},
solutions[-1],
init = solutions[1]
)
這使
[[1]]
[1] 1 0 0 1 1 1
[[2]]
[1] 0 1 0 0 1 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/434918.html
