-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycle-detection.R
117 lines (89 loc) · 2.18 KB
/
cycle-detection.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
source('ca.R')
#library(igraph)
bin2dec <- function(x)
{
sum(x * 2^(rev(seq_along(x)) - 1))
}
dec2bin <- function(x, k, n)
{
b <- numeric(n)
j <- 1
while(x > 0)
{
b[j] <- x %% k # modular division (remainder)
j <- j + 1
x <- x %/% k # integer division (quocient)
}
rev(b)
}
N_cells <- 7
main <- function(N_cells, k=2)
{
n <- k**N_cells
check <- logical(n)
nstate.next <- numeric(N_cells)
adj.mat <- matrix(logical(n**2), ncol=n, nrow=n)
cycles <- vector()
# Percorrer todas as possibilidades
for (i in 0:(n-1))
{
#cat('Seed: ', i, '\n')
if (check[i+1] == FALSE)
{
#i_cycle <- 1
check[i+1] <- TRUE
seed <- dec2bin(i, k, N_cells)
cycle <- i
nstate.next <- nextStateGen(seed)
nstate.prev <- nstate.next
cycle <- c(cycle, bin2dec(nstate.next) )
adj.mat[bin2dec(seed)+1,bin2dec(nstate.next)+1] <- 1
#cat(bin2dec(seed), ' ', bin2dec(nstate.next), ' ')
while (check[bin2dec(nstate.next) + 1] == FALSE)
{
check[bin2dec(nstate.prev) + 1] <- TRUE
nstate.next <- nextStateGen(nstate.prev)
adj.mat[bin2dec(nstate.prev)+1,bin2dec(nstate.next)+1] <- 1
#cat(bin2dec(nstate.next), ' ')
nstate.prev <- nstate.next
cycle <- c(cycle, bin2dec(nstate.next) )
}
cycle_check <- which(cycle == tail(cycle,1))
if (length(cycle_check) > 1)
{
cycles <- c(cycles, cycle_check[2] - cycle_check[1])
#cat('\n')
#cat('ciclo tamanho: ', cycle_check[2] - cycle_check[1], '\n')
}
#cat('\n')
}
}
#ratio <- max(cycles)/n
#cat("total, min, 1st, median, 3rd, max, ratio\n")
#cat(length(cycles), quantile(cycles), ratio)
#cat("\n")
#if (ratio >= 0.5)
#{cat("Regra boa: ",rules,"\n")}
g <- graph.adjacency(adj.mat)
#summary(g)
#L <- layout.fruchterman.reingold(g)
#plot(g,layout=L)
#plot(g)
tkplot(g)
cycles
}
go <- function()
{
N_cells <- 12
d <- data.frame()
filename <- sprintf('quartis-n%d-r%d-t%d.csv', N_cells, r, totalistic)
for(i_rule in 1:(k**(k**(2*r+1)) / 4 ) )
#for(i_rule in 1:100)
{
cat('Regra', i_rule, '\n')
rules <- i_rule
cycles <- main(N_cells)
d <- rbind(d, data.frame(t(c(i_rule, length(cycles), t(quantile(cycles)), max(cycles)/(k**N_cells)))))
}
write.csv(d, file=filename)
}