add sampler for symmetric groups
Browse files- automata.py +84 -1
automata.py
CHANGED
@@ -22,6 +22,8 @@ import itertools
|
|
22 |
import datasets
|
23 |
import numpy as np
|
24 |
|
|
|
|
|
25 |
|
26 |
_CITATION = """\
|
27 |
"""
|
@@ -159,8 +161,89 @@ class FlipFlopSampler(AutomatonSampler):
|
|
159 |
return x, self.f(x)
|
160 |
|
161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
dataset_map = {
|
163 |
'parity': ParitySampler,
|
164 |
'flipflop': FlipFlopSampler,
|
|
|
165 |
# TODO: more datasets
|
166 |
-
}
|
|
|
22 |
import datasets
|
23 |
import numpy as np
|
24 |
|
25 |
+
# Local imports
|
26 |
+
# from symmetric import SymmetricSampler
|
27 |
|
28 |
_CITATION = """\
|
29 |
"""
|
|
|
161 |
return x, self.f(x)
|
162 |
|
163 |
|
164 |
+
class SymmetricSampler(AutomatonSampler):
|
165 |
+
"""
|
166 |
+
TODO: add options for labels as functions of states
|
167 |
+
- parity (whether a state is even): this may need packages (e.g. Permutation from sympy)
|
168 |
+
- position / toggle: for S3 ~ D6, we can add labels for substructures as in Dihedral groups.
|
169 |
+
"""
|
170 |
+
def __init__(self, data_config):
|
171 |
+
super().__init__(data_config)
|
172 |
+
self.name = 'symmetric'
|
173 |
+
self.data_config = data_config
|
174 |
+
|
175 |
+
if 'n' not in data_config:
|
176 |
+
data_config['n'] = 5 # Default to S5
|
177 |
+
if 'n_actions' not in data_config:
|
178 |
+
data_config['n_actions'] = 3
|
179 |
+
if 'label_type' not in data_config:
|
180 |
+
# Options: 'state', 'first_chair'
|
181 |
+
data_config['label_type'] = 'state'
|
182 |
+
|
183 |
+
self.n = data_config['n']
|
184 |
+
self.label_type = data_config['label_type']
|
185 |
+
|
186 |
+
"""
|
187 |
+
Get states
|
188 |
+
"""
|
189 |
+
self.state_encode = lambda state: ''.join([str(int(each)) for each in state])
|
190 |
+
self.state_label_map = {}
|
191 |
+
for si, state in enumerate(itertools.permutations(range(self.n))):
|
192 |
+
enc = self.state_encode(state)
|
193 |
+
self.state_label_map[enc] = si
|
194 |
+
|
195 |
+
"""
|
196 |
+
Get actions (3 defaults: id, shift-by-1, swap-first-two)
|
197 |
+
"""
|
198 |
+
self.n_actions = data_config['n_actions']
|
199 |
+
self.actions = {0: np.eye(self.n)}
|
200 |
+
# shift all elements to the right by 1
|
201 |
+
shift_idx = list(range(1, self.n)) + [0]
|
202 |
+
self.actions[1] = np.eye(self.n)[shift_idx]
|
203 |
+
# swap the first 2 elements
|
204 |
+
shift_idx = [1, 0] + list(range(2, self.n))
|
205 |
+
self.actions[2] = np.eye(self.n)[shift_idx]
|
206 |
+
|
207 |
+
if self.n_actions > 3:
|
208 |
+
# add permutations in the order given by itertools.permutations
|
209 |
+
self.all_permutations = list(itertools.permutations(range(self.n)))[1:]
|
210 |
+
cnt = 2
|
211 |
+
for each in self.all_permutations:
|
212 |
+
action = np.eye(self.n)[list(each)]
|
213 |
+
if np.linalg.norm(action - self.actions[0]) == 0:
|
214 |
+
continue
|
215 |
+
elif np.linalg.norm(action - self.actions[1]) == 0:
|
216 |
+
continue
|
217 |
+
self.actions[cnt] = action
|
218 |
+
cnt += 1
|
219 |
+
if cnt == self.n_actions: break
|
220 |
+
|
221 |
+
def get_state_label(self, state):
|
222 |
+
enc = self.state_encode(state)
|
223 |
+
return self.state_label_map[enc]
|
224 |
+
|
225 |
+
def f(self, x):
|
226 |
+
curr_state = np.arange(self.n)
|
227 |
+
labels = []
|
228 |
+
for action in x:
|
229 |
+
curr_state = self.actions[action].dot(curr_state)
|
230 |
+
|
231 |
+
if self.label_type == 'state':
|
232 |
+
labels += self.get_state_label(curr_state),
|
233 |
+
elif self.label_type == 'first_chair':
|
234 |
+
labels += curr_state[0],
|
235 |
+
|
236 |
+
return np.array(labels)
|
237 |
+
|
238 |
+
def sample(self):
|
239 |
+
x = np.random.choice(range(self.n_actions), replace=True, size=self.T)
|
240 |
+
|
241 |
+
return x, self.f(x)
|
242 |
+
|
243 |
+
|
244 |
dataset_map = {
|
245 |
'parity': ParitySampler,
|
246 |
'flipflop': FlipFlopSampler,
|
247 |
+
'symmetric': SymmetricSampler,
|
248 |
# TODO: more datasets
|
249 |
+
}
|