add Dihedral groups
Browse files- automata.py +91 -0
automata.py
CHANGED
@@ -512,11 +512,102 @@ class CyclicSampler(AutomatonSampler):
|
|
512 |
return x, self.f(x)
|
513 |
|
514 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
515 |
|
516 |
dataset_map = {
|
517 |
'abab': ABABSampler,
|
518 |
'alternating': AlternatingSampler,
|
519 |
'cyclic': CyclicSampler,
|
|
|
520 |
'flipflop': FlipFlopSampler,
|
521 |
'gridworld': GridworldSampler,
|
522 |
'parity': ParitySampler,
|
|
|
512 |
return x, self.f(x)
|
513 |
|
514 |
|
515 |
+
class DihedralSampler(AutomatonSampler):
|
516 |
+
def __init__(self, data_config):
|
517 |
+
super().__init__(data_config)
|
518 |
+
|
519 |
+
if 'n' not in data_config:
|
520 |
+
data_config['n'] = 5
|
521 |
+
self.n = data_config['n']
|
522 |
+
|
523 |
+
if 'label_type' not in data_config:
|
524 |
+
# Options: 'state', 'toggle', 'position'
|
525 |
+
data_config['label_type'] = 'state'
|
526 |
+
self.label_type = data_config['label_type']
|
527 |
+
|
528 |
+
"""
|
529 |
+
2 actions: toggle, or shift by 1 position (direction determined by the toggle).
|
530 |
+
"""
|
531 |
+
self.n_actions = 2
|
532 |
+
self.actions = {}
|
533 |
+
# shift all elements to the left (counter-clockwise) by 1
|
534 |
+
shift_idx = list(range(1, self.n)) + [0]
|
535 |
+
self.actions[0] = np.eye(self.n)[shift_idx]
|
536 |
+
# shift all elements to the right (closewise) by 1
|
537 |
+
shift_idx = [self.n-1] + list(range(self.n-1))
|
538 |
+
self.actions[1] = np.eye(self.n)[shift_idx]
|
539 |
+
|
540 |
+
self.__info__ = 'Dihedral group of order 2n, where n={self.n}:\n' \
|
541 |
+
+f"- Inputs: binary tokens:\n" \
|
542 |
+
+ " 0 for toggle, i.e. change direction in the n-cycle;\n" \
|
543 |
+
+ " 1 for drive, i.e. move forward 1 step on the n-cycle.\n" \
|
544 |
+
+ "- Labels: depending on the label_type.\n" \
|
545 |
+
+ "- Config:\n" \
|
546 |
+
+ " - n (int): size of the 'cycle'; i.e. there are 2n states considering also the toggle bit.\n" \
|
547 |
+
+ " - label_type (str): choosing from the following options: \n" \
|
548 |
+
+ " - 'state': the state id, i.e. considering both toggle and position. \n" \
|
549 |
+
+ " - 'toggle': the toggle bit (in {0, 1}). \n" \
|
550 |
+
+ " - 'position': the position on the n-cycle (in [n]).\n" \
|
551 |
+
+ self.__info__
|
552 |
+
|
553 |
+
def f_sequential(self, x):
|
554 |
+
# sanity check: sequential solution
|
555 |
+
position = np.arange(self.n)
|
556 |
+
states = []
|
557 |
+
toggle = 0 # i.e. parity
|
558 |
+
for action in x:
|
559 |
+
if action == 0:
|
560 |
+
# toggle direction
|
561 |
+
toggle = 1 - toggle
|
562 |
+
else:
|
563 |
+
# drive by 1
|
564 |
+
position = self.actions[toggle].dot(position)
|
565 |
+
states += (toggle, position[0]),
|
566 |
+
return states
|
567 |
+
|
568 |
+
def f(self, x):
|
569 |
+
# Parallel solution
|
570 |
+
|
571 |
+
# Get toggles: a parity task on the toggle bit
|
572 |
+
toggles = (x == 0).astype(np.int64)
|
573 |
+
toggle_status = np.cumsum(toggles) % 2
|
574 |
+
|
575 |
+
# Get positions: a directed modular counter
|
576 |
+
directions = (-1)**toggle_status
|
577 |
+
directed_drives = (x != 0).astype(np.int64) * directions
|
578 |
+
positions = np.cumsum(directed_drives) % self.n
|
579 |
+
|
580 |
+
if self.label_type == 'state':
|
581 |
+
labels = [self.get_state_label(each) for each in zip(toggle_status, positions)]
|
582 |
+
return np.array(labels).astype(np.int64)
|
583 |
+
elif self.label_type == 'toggle':
|
584 |
+
return toggle_status
|
585 |
+
elif self.label_type == 'positions':
|
586 |
+
return positions
|
587 |
+
|
588 |
+
def get_state_label(self, state):
|
589 |
+
"""
|
590 |
+
toggle in {0,1}
|
591 |
+
position in [k]
|
592 |
+
"""
|
593 |
+
toggle, position = state
|
594 |
+
label = self.n*toggle + position
|
595 |
+
return label
|
596 |
+
|
597 |
+
def sample(self):
|
598 |
+
T = self.sample_length()
|
599 |
+
x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)
|
600 |
+
|
601 |
+
return x, self.f(x)
|
602 |
+
|
603 |
+
|
604 |
+
|
605 |
|
606 |
dataset_map = {
|
607 |
'abab': ABABSampler,
|
608 |
'alternating': AlternatingSampler,
|
609 |
'cyclic': CyclicSampler,
|
610 |
+
'dihedral': DihedralSampler,
|
611 |
'flipflop': FlipFlopSampler,
|
612 |
'gridworld': GridworldSampler,
|
613 |
'parity': ParitySampler,
|