简体   繁体   中英

Compute permutations following specific constraints

I want to store all feasible permutations for a target vector of size 24, that consists of (0,1).

For memory efficiency I use the below:

Test = data.table(permutations(n = 2,r = 12,v = c("zero","one"),repeats.allowed = T))
Test[, names(Test) := lapply(.SD, function(x) gsub("zero", "0,0", x))]
Test[, names(Test) := lapply(.SD, function(x) gsub("one", "1,1", x))]

Final output should adhere the following:

  1. Minimum number of consecutive 1's is 8 and maximum is 15 in the middle of the vector.
  2. At the start/end only consecutive 1's are permitted to be less than 8
  3. two 0's between consecutive 1's

Examples:

c(1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1) - Correct

c(1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1) - Correct

c(1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0) - Correct

c(0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0) - Correct

c(0,0,1,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1) - Wrong: only 2 consecutive 1's

c(0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,1,1,0,1,1) - Wrong: only one 0 between consecutive 1's at the end of the vector

Not sure if I fully understand, for large number of consecutive ones in the middle, here is something to get started:

N <- 24

k <- 15L
npad <- 2L
m <- N - k - 2*npad
apply(expand.grid(0L:1L, 0L:1L, 1L:m), 1L, function(x) {
    y <- rep.int(c(x["Var1"], 0L, 1L, 0L, x["Var2"]), 
        c(x["Var3"], 2L, k, 2L, m - x["Var3"]))
    paste(y, collapse="")
})

output for k=15 :

 [1] "000111111111111111000000" "100111111111111111000000" "000111111111111111001111" "100111111111111111001111"
 [5] "000011111111111111100000" "110011111111111111100000" "000011111111111111100111" "110011111111111111100111"
 [9] "000001111111111111110000" "111001111111111111110000" "000001111111111111110011" "111001111111111111110011"
[13] "000000111111111111111000" "111100111111111111111000" "000000111111111111111001" "111100111111111111111001"
[17] "000000011111111111111100" "111110011111111111111100" "000000011111111111111100" "111110011111111111111100"

Is 111111111111111000000000 and 011111111111111100000000 required as well? If yes, there is a need to loop the above for values of 0L and 1L instead of just 2L

output for k=8 :

 [1] "000111111110000000000000" "100111111110000000000000" "000111111110011111111111" "100111111110011111111111"
 [5] "000011111111000000000000" "110011111111000000000000" "000011111111001111111111" "110011111111001111111111"
 [9] "000001111111100000000000" "111001111111100000000000" "000001111111100111111111" "111001111111100111111111"
[13] "000000111111110000000000" "111100111111110000000000" "000000111111110011111111" "111100111111110011111111"
[17] "000000011111111000000000" "111110011111111000000000" "000000011111111001111111" "111110011111111001111111"
[21] "000000001111111100000000" "111111001111111100000000" "000000001111111100111111" "111111001111111100111111"
[25] "000000000111111110000000" "111111100111111110000000" "000000000111111110011111" "111111100111111110011111"
[29] "000000000011111111000000" "111111110011111111000000" "000000000011111111001111" "111111110011111111001111"
[33] "000000000001111111100000" "111111111001111111100000" "000000000001111111100111" "111111111001111111100111"
[37] "000000000000111111110000" "111111111100111111110000" "000000000000111111110011" "111111111100111111110011"
[41] "000000000000011111111000" "111111111110011111111000" "000000000000011111111001" "111111111110011111111001"
[45] "000000000000001111111100" "111111111111001111111100" "000000000000001111111100" "111111111111001111111100"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM