1

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

Uwe
  • 41,420
  • 11
  • 90
  • 134
  • Why is the fourth example correct? It doesn't have "consecutive ones" at the start or at the end. And the third example has consecutive zeroes at the end. That appears incorrect as well... – Limey Jul 03 '20 at 10:03
  • I re-expressed the constraints. I hope now it makes sense – Psyndrom Ventura Jul 03 '20 at 10:09
  • I don't understand your question. Your create permutations of `c("zero","one")` and then replace with 0, 0 and 1, 1. What's the point? Why not start with `c(0, 1)` ? – Ronak Shah Jul 03 '20 at 12:31
  • I do this because I want at least 2 zeros between 0-1 switches. Also the total number of permutations for a target vector of length 24 is 17M and I dont think this manageable. – Psyndrom Ventura Jul 04 '20 at 06:28
  • 17M should be pretty manageable – MichaelChirico Jul 04 '20 at 10:56
  • 2
    If you use the `bit` package, you can store 0/1 as single bit instead of 32-bit integer. Then 17M * 24 elements is 48.6 Mb. Even if you use `integer` storage, it's about 1.5 Gb – MichaelChirico Jul 04 '20 at 10:59

1 Answers1

1

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"
chinsoon12
  • 25,005
  • 4
  • 25
  • 35