Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

A (one dimensional) cellular automaton is a function1 F : Σ → Σ with the property that there is a K > 0 such that F (x)i depends only on the 2K + 1 coordinates xi−K , xi−K+1, . . . , xi−1, xi, xi+1, . . . , xi+K . A periodic point of σ is any x such that σ^p (x) = x for some p ∈ N, and a periodic point of F is any x such that F^q (x) = x for some q ∈ N. Given a cellular automaton F, a point x ∈ Σ is jointly periodic if there are p, q ∈ N such that σ^p (x) = F^q (x) = x, that is, it is a periodic point under both functions.

This project aims to explore the nature of one-dimensional Cellular Automata, in the hope of finding the structure of cellular automata through its periodic points.

2034 views
License: MIT
ubuntu2004
1
import java.time.Duration;
2
import java.time.Instant;
3
import java.util.*;
4
import java.util.function.Function;
5
6
public class FDense {
7
private static int N = 2; // Size of the alphabet
8
private static HashMap<Integer, Integer> Rule= new HashMap<Integer, Integer>();
9
10
private static HashMap<Integer, Integer> createHashMap(char[] rule, int k) {
11
HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
12
int n = (int) Math.pow(2, k); // Total number of input patterns
13
14
// Iterate over all input patterns and store the output in the HashMap
15
for (int i = 0; i < n; i++) {
16
hashMap.put(i, Integer.parseInt(String.valueOf(rule[i]).trim()));
17
}
18
19
return hashMap;
20
}
21
22
private static List<String> generateWords(int length) {
23
List<String> words = new ArrayList<>();
24
generateWordsHelper("", length, words);
25
return words;
26
}
27
28
private static void generateWordsHelper(String prefix, int length, List<String> words) {
29
if (prefix.length() == length) {
30
words.add(prefix);
31
} else {
32
for (int i = 0; i < N; i++) {
33
generateWordsHelper(prefix + i, length, words);
34
}
35
}
36
}
37
38
public static List<String> applyRule(List<String> words, int span) {
39
List<String> newWords = new ArrayList<>();
40
int key;
41
for (String word : words) {
42
StringBuilder newWord = new StringBuilder();
43
for (int i = 0; i < word.length(); i++) {
44
String pattern = "";
45
if(i == word.length() - span + 1) {
46
pattern += word.charAt(i);
47
pattern += word.charAt(i+1);
48
pattern += word.charAt(i+2);
49
pattern += word.charAt(0);
50
} else if (i == word.length() - span + 2) {
51
pattern += word.charAt(i);
52
pattern += word.charAt(i+1);
53
pattern += word.charAt(0);
54
pattern += word.charAt(1);
55
} else if (i == word.length() - span + 3) {
56
pattern += word.charAt(i);
57
pattern += word.charAt(0);
58
pattern += word.charAt(1);
59
pattern += word.charAt(2);
60
} else {
61
pattern += word.charAt(i);
62
pattern += word.charAt(i+1);
63
pattern += word.charAt(i+2);
64
pattern += word.charAt(i+3);
65
}
66
67
// apply the rule to the input pattern and append the result to the new word
68
key = Integer.parseInt(pattern, 2);
69
int output = Rule.get(key);
70
newWord.append(output);
71
}
72
newWords.add(newWord.toString());
73
}
74
return newWords;
75
}
76
77
78
public static String applyRule(String word, int span) {
79
int key;
80
String newWord = "";
81
for (int i = 0; i < word.length(); i++) {
82
String pattern = "";
83
if (i == word.length() - span + 1) {
84
pattern += word.charAt(i);
85
pattern += word.charAt(i + 1);
86
pattern += word.charAt(i + 2);
87
pattern += word.charAt(i + 3);
88
pattern += word.charAt(i + 4);
89
pattern += word.charAt(0);
90
} else if (i == word.length() - span + 2) {
91
pattern += word.charAt(i);
92
pattern += word.charAt(i + 1);
93
pattern += word.charAt(i + 2);
94
pattern += word.charAt(i + 3);
95
pattern += word.charAt(0);
96
pattern += word.charAt(1);
97
} else if (i == word.length() - span + 3) {
98
pattern += word.charAt(i);
99
pattern += word.charAt(i + 1);
100
pattern += word.charAt(i + 2);
101
pattern += word.charAt(0);
102
pattern += word.charAt(1);
103
pattern += word.charAt(2);
104
} else if (i == word.length() - span + 4) {
105
pattern += word.charAt(i);
106
pattern += word.charAt(i + 1);
107
pattern += word.charAt(0);
108
pattern += word.charAt(1);
109
pattern += word.charAt(2);
110
pattern += word.charAt(3);
111
} else if (i == word.length() -span + 5) {
112
pattern += word.charAt(i);
113
pattern += word.charAt(0);
114
pattern += word.charAt(1);
115
pattern += word.charAt(2);
116
pattern += word.charAt(3);
117
pattern += word.charAt(4);
118
} else {
119
pattern += word.charAt(i);
120
pattern += word.charAt(i + 1);
121
pattern += word.charAt(i + 2);
122
pattern += word.charAt(i + 3);
123
pattern += word.charAt(i + 4);
124
pattern += word.charAt(i + 5);
125
}
126
127
// apply the rule to the input pattern and append the result to the new word
128
key = Integer.parseInt(pattern, 2);
129
newWord += Rule.get(key);
130
}
131
132
return newWord;
133
}
134
135
public static void main(String[] args) {
136
int k = 15;
137
int m = 10;
138
int span = 6;
139
140
List<String> originalWords = new ArrayList<>();
141
List<String> wordsWithKperiod = new ArrayList<>();
142
List<String> allWords = new ArrayList<>();
143
144
originalWords = generateWords(m);
145
146
//System.out.println(originalWords);
147
148
wordsWithKperiod = generateWords(k-m);
149
150
for (int i = 0; i < originalWords.size(); i++) {
151
for (int j = 0; j < wordsWithKperiod.size(); j++) {
152
allWords.add(originalWords.get(i) + wordsWithKperiod.get(j));
153
}
154
}
155
156
157
/*
158
for (int i = 0; i < originalWords.size(); i++) {
159
for (int j = 0; j < wordsWithKperiod.size(); j++) {
160
String newWord = originalWords.get(i) + wordsWithKperiod.get(j);
161
newWord += newWord.substring(0, span); // add first k characters to end of string
162
allWords.add(newWord);
163
}
164
}
165
*/
166
167
//testing
168
System.out.println(allWords);
169
170
String tabularRule = "1010101001010101010101010101010101010101101010101010101010101010";
171
Rule = createHashMap(tabularRule.toCharArray(), span);
172
System.out.println(Rule);
173
174
175
int mdense = 0;
176
177
for(int z = 0; z < allWords.size(); z++) {
178
String newWord = applyRule(allWords.get(z), span);
179
while(!newWord.equals(allWords.get(z))) {
180
newWord = applyRule(newWord, span);
181
}
182
mdense += 1;
183
}
184
185
System.out.println(mdense);
186
187
}
188
}
189