# generates matrix based on given perm number and size # determine number of connections for given permutation mxGetPerm <- function(rowcol,perm){ perms = 0 size = rowcol*rowcol print(paste("getting model for permutation ", perm, " of ",2^size)) for(k in c(0:size)) { perms = perms+choose(size,k) if(perm <= perms) { nconn = k break } } print(paste("number of connections: ",nconn)) range = 0 connslots = vector(length=nconn, mode="list") for(i in c(0:(nconn-1))) { range = range+choose(size,i) } perm = perm-range print(paste("secondary perm is ", perm)) nslotranges = list() n = 1 nslotranges[[n]] = vector(length=(size-(nconn-1)), mode="list") for(r in c(1:(size-(nconn-1)))) { nslotranges[[n]][r] = choose((size-r),nconn-n) } range = 0 for(s in c(1:length(nslotranges[[n]]))) { curr_range = as.numeric(nslotranges[[n]][s]) range = range+curr_range if(perm <= range) { connslots[[n]] = s # changed to 1 indexing break } } rangegroup = connslots[[n]] if(rangegroup>1) { for(y in 1:(rangegroup-1)) { perm = perm-as.numeric(nslotranges[[n]][y]) } } # initialize matrix, determine new permutation # index based on range for number of connections # and first non-zero index within the matrix idata = list() istart = connslots[[1]] istop = istart+(nconn-1) for(x in 1:size) { if(x=istart && x<=istop) idata[[x]] = 1 if(x>istop) idata[[x]] = 0 } print(paste("perm is now ",perm)) print(paste("bit1 slot is ",connslots[[n]])) perm_array = mxPerm(perm,nconn,idata) return(perm_array) } # ------------------------------------------ # iterate thru matrices in subset # until matching index is found # that is the matrix to be optimized find_n <- function(n,idata){ cnt = 0 for(j in c(1:length(idata))) { if(idata[[j]] == 1) cnt = cnt+1 if(cnt == n) return(j) } return(j) } find_shift_elmt <- function(connections,idata){ x = connections-1 while(x>=1) { curr_n = find_n(x,idata) if(idata[[curr_n+1]] == 0) return(curr_n) x = x-1 } return(0) } insert_zero <- function(pos,connections,alldata) { size = length(alldata[[1]]) if( pos+1>size || alldata[[1]][pos+1] != 0) { selmt_pos = find_shift_elmt(connections, alldata[[1]]) pos = selmt_pos; } bitcount = 0; alldata[[2]] = alldata[[1]] i = size; # set position of interest alldata[[1]][pos] = 0; # set those to left which should # be static but keep track of them if(pos>1){ for(k in c(1:(pos-1))) { alldata[[1]][k] = alldata[[2]][k]; if(alldata[[1]][k] == 1){ bitcount = bitcount+1; } } } # now remaining bits not set on left # are set immediately to the right rbits = connections-bitcount rcnt = 0 newpos = pos+1 while(rcnt