File size: 27,185 Bytes
8453f96
03bcb43
8453f96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d59b4
e5af3c1
59a70ca
8453f96
 
 
d262e6f
 
 
 
 
 
 
8453f96
 
dbf2014
 
 
 
 
 
8453f96
 
 
dbf2014
8453f96
 
 
 
 
 
 
 
dbf2014
8453f96
 
 
 
 
be8b808
8453f96
 
 
 
 
be8b808
 
6a23009
 
d262e6f
be8b808
 
 
dbf2014
5377ed8
8453f96
 
 
0a3368a
 
8453f96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d59b4
e572ba6
87d59b4
dbf2014
8453f96
0a3368a
 
8453f96
 
dbf2014
b0d21d5
 
 
 
 
6a23009
b0d21d5
 
 
 
2a180d8
b0d21d5
 
 
d262e6f
b0d21d5
 
 
8453f96
b0d21d5
 
 
8453f96
b0d21d5
 
 
 
 
8453f96
b0d21d5
 
d262e6f
b0d21d5
 
 
 
6a23009
b0d21d5
 
d262e6f
b0d21d5
d262e6f
b0d21d5
 
d262e6f
b0d21d5
 
 
 
470476a
b0d21d5
 
 
 
 
6a23009
b0d21d5
 
6a23009
b0d21d5
 
 
 
d262e6f
b0d21d5
 
 
 
8453f96
b0d21d5
 
 
 
 
8453f96
b0d21d5
 
8453f96
b0d21d5
 
 
 
 
 
6a23009
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8453f96
 
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf2b634
 
 
 
 
 
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59a70ca
470476a
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aca0f6d
b0d21d5
 
 
 
59a70ca
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59a70ca
 
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59a70ca
b0d21d5
 
 
 
 
 
59a70ca
b0d21d5
 
 
59a70ca
b0d21d5
 
 
 
 
59a70ca
b0d21d5
 
 
 
 
59a70ca
b0d21d5
 
 
59a70ca
b0d21d5
3555211
aca0f6d
b0d21d5
aca0f6d
b0d21d5
 
 
aca0f6d
b0d21d5
 
6a23009
b0d21d5
6a23009
b0d21d5
 
 
 
 
 
 
 
aca0f6d
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aca0f6d
59a70ca
b0d21d5
59a70ca
b0d21d5
59a70ca
b0d21d5
 
59a70ca
b0d21d5
aca0f6d
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
aca0f6d
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aca0f6d
 
dbf2014
b0d21d5
 
470476a
b0d21d5
 
 
470476a
b0d21d5
 
 
 
 
 
 
 
 
 
 
470476a
b0d21d5
 
 
 
 
 
 
470476a
b0d21d5
 
470476a
b0d21d5
 
 
470476a
b0d21d5
470476a
 
dbf2014
b0d21d5
 
eda7501
b0d21d5
 
 
eda7501
b0d21d5
 
 
 
eda7501
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eda7501
b0d21d5
 
 
eda7501
b0d21d5
eda7501
 
dbf2014
b0d21d5
 
 
cf2b634
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47341b7
dbf2014
b0d21d5
 
0c45ea8
b0d21d5
 
 
 
 
0c45ea8
cf2b634
b0d21d5
cf2b634
2a180d8
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf2b634
 
b0d21d5
 
cf2b634
b0d21d5
 
 
 
cf2b634
b0d21d5
 
 
cf2b634
b0d21d5
 
 
2a180d8
 
 
8453f96
b0d21d5
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# Copyright 2020 The HuggingFace Datasets Authors.
# Copyright 2023 Bingbin Liu, Jordan Ash, Surbhi Goel, Akshay Krishnamurthy, and Cyril Zhang.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import csv
import json
import os
import itertools
import math
from sympy.combinatorics.permutations import Permutation

import datasets
import numpy as np
from copy import copy

# check python version
import sys
major, minor = sys.version_info[:2]
version = major + 0.1*minor
OLD_PY_VERSION = 1 if version < 3.8 else 0

_CITATION = """\
@article{liu2022transformers,
  title={Transformers learn shortcuts to automata},
  author={Liu, Bingbin and Ash, Jordan T and Goel, Surbhi and Krishnamurthy, Akshay and Zhang, Cyril},
  journal={arXiv preprint arXiv:2210.10749},
  year={2022}
}
"""

_DESCRIPTION = """\
Non-autoregressive automaton simulation datasets.
"""

_HOMEPAGE = ""

_LICENSE = ""

_URLS = {}

class AutomatonDataset(datasets.GeneratorBasedBuilder):
    """TODO: Short description of my dataset."""

    VERSION = datasets.Version("0.0.0")
    BUILDER_CONFIGS = []
    
    def __init__(self, config={}, **kwargs):
        super().__init__(**kwargs)
        
        """
        Set default configs
        """
        if 'name' not in config:
            config['name'] = 'parity'
        # if 'length' not in config: # sequence length
        #     config['length'] = 20
        if 'size' not in config: # number of sequences
            config['size'] = -1

        self.data_config = config
        self.automaton = dataset_map[config['name']](config)

    def _info(self):
        features = datasets.Features(
            {
                "input_ids": datasets.Sequence(datasets.Value("int32"), length=-1),
                "label_ids": datasets.Sequence(datasets.Value("int32"), length=-1)
            }
        )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "split": "train",
                },
            )
        ]

    def _generate_examples(self, split):
        for i in itertools.count(start=0):
            if i == self.data_config['size']:
                break
            x, y = self.automaton.sample()
            yield i, {
                "input_ids": x,
                "label_ids": y
            }

class Automaton:
    """
    This is a parent class that must be inherited.
    """
    def __init__(self, data_config):
        self.data_config = data_config

        if 'seed' in self.data_config:
            self.np_rng = np.random.default_rng(self.data_config['seed'])
        else:
            self.np_rng = np.random.default_rng()

        if 'length' not in data_config: # sequence length
            data_config['length'] = 20
        self.T = self.data_config['length']

        if 'random_length' not in data_config:
            data_config['random_length'] = 0
        self.random_length = data_config['random_length']

        self.__info__ = \
                "  - T (int): sequence length.\n" \
            + "  - random_length (int in {0, 1}): whether to randomly sample a length per sample.\n"

    def f(self, x):
        """
        Get output sequence given an input seq
        """
        raise NotImplementedError()

    def sample(self):
        raise NotImplementedError()

    def sample_length(self):
        if self.random_length:
            return self.np_rng.choice(range(1, self.T+1))
        return self.T

    def help(self):
            print(self.__info__)

class BinaryInputAutomaton(Automaton):
    """
    This is a parent class that must be inherited.
    Subclasses: ParityAutomaton, GridworldAutomaton, ABABAutomaton

    TODO: sample sequences with a given number of 1s
    """
    def __init__(self, data_config):
        super().__init__(data_config)

        if 'prob1' not in data_config:
            data_config['prob1'] = 0.5
        self.prob1 = data_config['prob1']
        self.__info__ = "  - prob1 (float in [0,1]): probability of token 1\n" \
            + self.__info__

    def f(self, x):
        raise NotImplementedError()

    def sample(self):
        T = self.sample_length()
        x = self.np_rng.binomial(1, self.prob1, size=T)
        return x, self.f(x)

class ParityAutomaton(BinaryInputAutomaton):
    def __init__(self, data_config):
        super().__init__(data_config)
        self.name = 'parity'

        self.__info__ = "Parity machine with 2 states: \n" \
            + "- Inputs: binary strings\n" \
            + "- Labels: binary strings of the partial parity\n" \
            + "- Config: \n" \
            + self.__info__

    def f(self, x):
        return np.cumsum(x) % 2

class GridworldAutomaton(BinaryInputAutomaton):
    """
    Note: gridworld currently doesn't include a no-op.
    """
    def __init__(self, data_config):
        super().__init__(data_config)

        if 'n' not in data_config:
            data_config['n'] = 9
        """
        NOTE: n is the number of states, and S is the id (0-indexing) of the rightmost state.
            i.e. the states are 0,1,2,...,S, where S=n-1.
        """
        self.n = data_config['n'] 
        self.S = self.n - 1

        if 'label_type' not in data_config:
            # Options: state, parity, boundary
            data_config['label_type'] = 'state'
        self.label_type = data_config['label_type']

        self.name = f'Grid{self.n}'

        self.__info__ = f"1d Gridworld of n={self.n} states:\n" \
            + "- Inputs: binary strings, i.e. move left(0) or right(1)\n" \
            + "- Labels: depending on 'label_type'. \n" \
            + "- Config: \n" \
            + "  - n (int): number of states; i.e. the states are 0,1,2,...,n-1.\n" \
            + "  - label_type (str): choosing from the following options:\n" \
            + "    - 'state' (default): the state id, i.e. 0 to n-1.\n" \
            + "    - 'parity': the state id mod 2.\n" \
            + "    - 'boundary': whether the current state is in {0, n-1} or not.\n" \
            + self.__info__

    def f(self, x):
        x = copy(x)
        x[x == 0] = -1
        if OLD_PY_VERSION:
            # NOTE: for Python 3.7 or below, accumulate doesn't have the 'initial' argument.
            x = np.concatenate([np.array([0]), x]).astype(np.int64)
            states = list(itertools.accumulate(x, lambda a,b: max(min(a+b, self.S), 0)))
            states = states[1:]
        else:
            states = list(itertools.accumulate(x, lambda a,b: max(min(a+b, self.S), 0), initial=0))
            states = states[1:] # remove the 1st entry with is the (meaningless) initial value 0
        return np.array(states).astype(np.int64)


class ABABAutomaton(BinaryInputAutomaton):
    def __init__(self, data_config):
        super().__init__(data_config)
        self.name = 'abab'

        if 'prob_abab_pos_sample' not in data_config:
            # The probability of having a positive sequence, i.e. 010101010101...
            data_config['prob_abab_pos_sample'] = 0.25
        if 'label_type' not in data_config:
            # Options: 'state', 'boundary'
            data_config['label_type'] = 'state'

        self.prob_abab_pos_sample = data_config['prob_abab_pos_sample']
        self.label_type = data_config['label_type']

        self.transition = np.array([
            [4, 1], # state 0
            [2, 4], # state 1
            [4, 3], # state 2
            [0, 4], # state 3
            [4, 4], # state 4
        ])

        self.__info__ = "abab: an automaton with 4 states + 1 absorbing state:\n" \
            + "- Inputs: binary strings\n" \
            + "- Labels: depending on 'label_type'.\n" \
            + "- Config:\n" \
            + "  - prob_abab_pos_sample (float in [0,1]): probability of having a 'positive' sequence, i.e. 01010101010...\n" \
            + "  - label_type (str): choosing from the following options:\n" \
            + "    - 'state' (default): the state id.\n" \
            + "    - 'boundary': whether the state is in state 3 (the states are 0,1,2,3).\n" \
            + self.__info__ 

    def f(self, x):
        labels = []
        curr_state = 3
        for each in x:
            curr_state = self.transition[curr_state, each]
            labels += curr_state,
        labels = np.array(labels).astype(np.int64)
        if self.label_type == 'boundary':
            labels = (labels == 3).astype(np.int64)
        return labels

    def sample(self):
        pos_sample = self.np_rng.random() < self.prob_abab_pos_sample
        if pos_sample:
            T = self.sample_length()
            x = [0,1,0,1] * (T//4)
            x += [0,1,0,1][:(T%4)]
            x = np.array(x)
            return x, self.f(x)
        else:
            return super().sample()


class AdderAutomaton(BinaryInputAutomaton):
    def __init__(self, data_config):
        super().__init__(data_config)
        self.name = 'addition'

        if 'n_addends' not in data_config:
            data_config['n_addends'] = 2
        self.n_addends = data_config['n_addends']
        self.addend_scales = np.array([2**i for i in range(self.n_addends)]).reshape(-1, 1)

        if 'label_type' not in data_config:
            data_config['label_type'] = 'state'
        self.label_type = data_config['label_type']

        self.__info__ = f'Adder of n={self.n_addends} binary numbers:\n' \
            +f"- Inputs: {self.n_addends} binary numbers, encoded as the int for the {self.n_addends}-bit binary number.\n" \
            + "- Labels: depending on the label_type.\n" \
            + "- Config:\n" \
            + "  - n_addends (int): number of binary numbers to be added; default as 2.\n" \
            + "  - label_type (str): choosing from the following options: \n" \
            +f"    - 'state': the state id, i.e. the int for the base-{self.n_addends} int corresponding to the number (carry, digit). \n" \
            +f"    - 'digit': the current output base-{self.n_addends} digit, without the carry. \n" \
            + "    - 'position': the current carry bit.\n" \
            + self.__info__

    def f(self, x):
        outputs, carries = [], []
        carry = 0
        T = x.shape[-1]
        for i in range(T):
            curr_sum = x[:, i].sum() + carry
            # NOTE: 'mod n_addends' makes sure the carry is binary
            output, carry = curr_sum % self.n_addends, curr_sum // self.n_addends
            outputs += output,
            carries += carry,
        outputs = np.array(outputs).astype(np.int64)
        carries = np.array(carries).astype(np.int64)

        if self.label_type == 'state':
            return outputs + self.n_addends*carries
        elif self.label_type ==  'digit':
            return outputs
        elif self.label_type == 'carry':
            return carries

    def sample_addend(self, T):
        a = self.np_rng.binomial(1, self.prob1, size=T)
        return a

    def sample(self):
        T = self.sample_length()
        x = np.stack([self.sample_addend(T) for _ in range(self.n_addends)])
        # Pad the most significant bit (rightmost position, i.e. we're reversing the number) with 0 to handle the potential carry
        pad = np.zeros((self.n_addends, 1))
        x = np.concatenate([x, pad], 1)

        x_encode = (self.addend_scales * x).sum(0)
        return x_encode, self.f(x)

class FlipFlopAutomaton(Automaton):
    def __init__(self, data_config):
        super().__init__(data_config)
        self.name = 'flipflop'

        if 'n' not in data_config:
            data_config['n'] = 2
        
        self.n_states = data_config['n'] 
        self.n_actions = self.n_states + 1
        self.transition = np.array([list(range(self.n_actions))] + [[i+1]*self.n_actions for i in range(self.n_states)]).T

        self.__info__ = f"Flipflop with n={self.n_states} states:\n" \
            +f"- Inputs: tokens are either 0 (read) or 1:{self.n} (write).\n" \
            + "- Labels: the state id.\n" \
            + "- Config:\n" \
            + "  - n (int): number of write states; i.e. the states are 1,2,...,n, plus a default start state 0.\n" \
            + self.__info__ 

    def f(self, x):
        state, states = 0, []
        for action_id in x:
            state = self.transition[state, action_id]
            states += state,
        return np.array(states)

    def sample(self):
        T = self.sample_length()
        rand = self.np_rng.uniform(size=T)
        nonzero_pos = (rand < 0.5).astype(np.int64)
        writes = self.np_rng.choice(range(1, self.n_states+1), size=T)
        x = writes * nonzero_pos
        return x, self.f(x)


class PermutationAutomaton(Automaton):
    """
    This is a parent class that must be inherited.
    Subclasses: SymmetricAutomaton, AlternatingAutomaton
    """
    def __init__(self, data_config):
        super().__init__(data_config)

        if 'n' not in data_config:
            data_config['n'] = 5
        if 'label_type' not in data_config:
            # Options: 'state', 'first_chair'
            data_config['label_type'] = 'state'
        
        self.n = data_config['n'] # the symmetric group Sn
        self.label_type = data_config['label_type']

        self.__info__ = \
            "  - label_type (str): choosing from the following options:\n" \
            + "    - 'state' (default): the state id.\n" \
            + "    - 'first_chair': the element in the first position of the permutation.\n" \
            + "          e.g. if the current permutation is [2,1,4,3], then 'first_chair' is 2.\n" \
            + self.__info__

    def get_state_label(self, state):
        enc = self.state_encode(state)
        return self.state_label_map[enc]

    def f(self, x):
        curr_state = np.arange(self.n)
        labels = []
        for action_id in x:
            curr_state = self.actions[action_id].dot(curr_state)

            if self.label_type == 'state':
                    labels += self.get_state_label(curr_state),
            elif self.label_type == 'first_chair':
                    labels += curr_state[0],
        return np.array(labels)

    def sample(self):
        T = self.sample_length()
        x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)

        return x, self.f(x)


class SymmetricAutomaton(PermutationAutomaton):
    """
    TODO: add options for labels as functions of states
    - parity (whether a state is even): this may need packages (e.g. Permutation from sympy)
    - position / toggle: for S3 ~ D6, we can add labels for substructures as in Dihedral groups.
    """
    def __init__(self, data_config):
        super().__init__(data_config)

        self.name = f'S{self.n}'

        """
        Get states
        """
        self.state_encode = lambda state: ''.join([str(int(each)) for each in state])
        self.state_label_map = {}
        for si, state in enumerate(itertools.permutations(range(self.n))):
            enc = self.state_encode(state)
            self.state_label_map[enc] = si

        """
        Get actions (3 defaults: id, shift-by-1, swap-first-two)
        """
        if 'n_actions' not in data_config:
            data_config['n_actions'] = 3
        self.n_actions = data_config['n_actions']
        self.actions = {0: np.eye(self.n)}
        # shift all elements to the right by 1
        shift_idx = list(range(1, self.n)) + [0]
        self.actions[1] = np.eye(self.n)[shift_idx]
        # swap the first 2 elements
        shift_idx = [1, 0] + list(range(2, self.n))
        self.actions[2] = np.eye(self.n)[shift_idx]

        if self.n_actions > 3:
            # add permutations in the order given by itertools.permutations
            self.all_permutations = list(itertools.permutations(range(self.n)))[1:]
            cnt = 2
            for each in self.all_permutations:
                action = np.eye(self.n)[list(each)]
                if np.linalg.norm(action - self.actions[0]) == 0:
                    continue
                elif np.linalg.norm(action - self.actions[1]) == 0:
                    continue
                self.actions[cnt] = action
                cnt += 1
                if cnt == self.n_actions: break

        self.__info__ = f"Symmetric group on n={self.n} objects:\n" \
            +f"- Inputs: tokens are either 0 (no-op), or 1:{self.n_actions} (corresponding to {self.n_actions} permutations).\n" \
            + "- Labels: depending on 'label_type'.\n" \
            + "- Config:\n" \
            + "  - n (int): number of objects, i.e. there are n! states.\n" \
            + "  - n_actions (int): number of permutations to include in the generator set;\n" \
            + "        the ordering is given by itertools.permutations, and the first 'n_actions' permutations will be included.\n" \
            + self.__info__ 


class AlternatingAutomaton(PermutationAutomaton):
    """
    TODO: other choices of generators (currently using (12x))?
    """
    def __init__(self, data_config):
        super().__init__(data_config)

        self.name = f'A{self.n}'

        """
        Get states
        """
        self.state_label_map = {}
        self.state_encode = lambda state: ''.join([str(int(each)) for each in state])
        cnt = 0
        for si, state in enumerate(itertools.permutations(range(self.n))):
            if not Permutation(state).is_even:
                    continue
            enc = self.state_encode(state)
            self.state_label_map[enc] = cnt
            cnt += 1

        """
        Get actions: all 3 cycles of the form (12x)
        """
        self.actions = {0: np.eye(self.n)}
        for idx in range(2, self.n):
            # (1, 2, idx) 
            shift_idx = list(range(self.n))
            shift_idx[0],shift_idx[1], shift_idx[idx] = shift_idx[1], shift_idx[idx], shift_idx[0]
            self.actions[idx-1] = np.eye(self.n)[shift_idx]
        self.n_actions = len(self.actions)

        self.__info__ = f"Alternating group on n={self.n} objects:\n" \
            +f"- Inputs: tokens from 0 to n-3, corresponding to all 3-cycles of the form (12x).\n" \
            + "- Labels: depending on 'label_type'.\n" \
            + "- Config:\n" \
            + "  - n (int): number of objects, i.e. there are n!/2 states.\n" \
            + self.__info__ 


class CyclicAutomaton(Automaton):
    def __init__(self, data_config):
        super().__init__(data_config)

        if 'n' not in data_config:
            data_config['n'] = 5
        self.n = data_config['n']

        """
        Get actions: shift by i positions, for i = 0 to n_actions-1
        """
        if 'n_actions' not in data_config:
            data_config['n_actions'] = 2
        self.n_actions = data_config['n_actions']
        shift_idx = list(range(1, self.n)) + [0]
        self.actions = {}
        for i in range(self.n_actions):
            shift_idx = list(range(i, self.n)) + list(range(0, i))
            self.actions[i] = np.eye(self.n)[shift_idx]

        self.__info__ = 'Cyclic group of n={self.n} states:\n' \
            +f"- Inputs: tokens from 0 to n_actions-1\n" \
            + "- Labels: the current state.\n" \
            + "- Config:\n" \
            + "  - n (int): number of states.\n" \
            + "  - n_actions (int): number of actions/generators, which are 0, 1, ..., n_actions-1.\n" \
            + self.__info__

    def f(self, x):
        return np.cumsum(x) % self.n

    def sample(self):
        T = self.sample_length()
        x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)

        return x, self.f(x)


class DihedralAutomaton(Automaton):
    def __init__(self, data_config):
        super().__init__(data_config)

        if 'n' not in data_config:
            data_config['n'] = 4
        self.n = data_config['n']

        if 'label_type' not in data_config:
            # Options: 'state', 'toggle', 'position'
            data_config['label_type'] = 'state'
        self.label_type = data_config['label_type']

        """
        2 actions: toggle, or shift by 1 position (direction determined by the toggle).
        """
        self.n_actions = 2
        self.actions = {}
        # shift all elements to the left (counter-clockwise) by 1
        shift_idx = list(range(1, self.n)) + [0]
        self.actions[0] = np.eye(self.n)[shift_idx]
        # shift all elements to the right (closewise) by 1
        shift_idx = [self.n-1] + list(range(self.n-1))
        self.actions[1] = np.eye(self.n)[shift_idx]

        self.__info__ = 'Dihedral group of order 2n, where n={self.n}:\n' \
            +f"- Inputs: binary tokens:\n" \
            + "      0 for toggle, i.e. change direction in the n-cycle;\n" \
            + "      1 for drive,  i.e. move forward 1 step on the n-cycle.\n" \
            + "- Labels: depending on the label_type.\n" \
            + "- Config:\n" \
            + "  - n (int): size of the 'cycle'; i.e. there are 2n states considering also the toggle bit.\n" \
            + "  - label_type (str): choosing from the following options: \n" \
            + "    - 'state': the state id, i.e. considering both toggle and position. \n" \
            + "    - 'toggle': the toggle bit (in {0, 1}). \n" \
            + "    - 'position': the position on the n-cycle (in [n]).\n" \
            + self.__info__

    def f_sequential(self, x):
        # sanity check: sequential solution
        position = np.arange(self.n)
        states = []
        toggle = 0 # i.e. parity
        for action in x:
            if action == 0:
                # toggle direction
                toggle = 1 - toggle
            else:
                # drive by 1
                position = self.actions[toggle].dot(position)
            states += (toggle, position[0]),
        return states

    def f(self, x):
        # Parallel solution

        # Get toggles: a parity task on the toggle bit
        toggles = (x == 0).astype(np.int64)
        toggle_status = np.cumsum(toggles) % 2

        # Get positions: a directed modular counter
        directions = (-1)**toggle_status
        directed_drives = (x != 0).astype(np.int64) * directions
        positions = np.cumsum(directed_drives) % self.n

        if self.label_type == 'state':
            labels = [self.get_state_label(each) for each in zip(toggle_status, positions)]
            return np.array(labels).astype(np.int64)
        elif self.label_type == 'toggle':
            return toggle_status
        elif self.label_type == 'positions':
            return positions

    def get_state_label(self, state):
        """
        toggle in {0,1}
        position in [k]
        """
        toggle, position = state
        label = self.n*toggle + position
        return label

    def sample(self):
        T = self.sample_length()
        x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)

        return x, self.f(x)


class QuaternionAutomaton(Automaton):
    def __init__(self, data_config):
        super().__init__(data_config)

        self.n_states = 8 # {-1, 1} x {1, i, j, k}
        self.n_actions = 4  # {1, i, j, k}
        self.transition_pos = [
            0, 1, 2, 3,
            1, 4, 3, 6,
            2, 7, 4, 1,
            3, 2, 5, 4,
        ]
        self.transition_neg = [(each+4)%8 for each in self.transition_pos]
        self.transition = np.array(self.transition_pos + self.transition_neg)
        self.transition = self.transition.reshape(-1, 4)

        self.__info__ = "Quaternion group:\n" \
            + "- Inputs: tokens in {0,1,2,3}, corresponding to 1,i,j,k.\n" \
            + "- Labels: the state id; 8 states in total: 2 signs ({-1,1}) x 4 values ({1,i,j,k}).\n" \
            + "- Config:\n" \
            + self.__info__

    def f(self, x):
        curr_state = 0
        states = []
        for action_id in x:
            curr_state = self.transition[curr_state, action_id]
            states += curr_state,
        return np.array(states).astype(np.int64)

    def sample(self):
        T = self.sample_length()
        x = self.np_rng.choice(range(self.n_actions), size=T)
        return x, self.f(x)

class PermutationResetAutomaton(Automaton):
    def __init__(self, data_config):
        super().__init__(data_config)

        self.n = data_config['n']
        self.generators = data_config['generators']
        self.perm_probs = data_config['perm_probs']
        if type(self.generators[0]) is str:
            self.generators = [ np.array(list(map(int, list(g)))) for g in self.generators ]

        self.n_states = math.factorial(self.n) # states = permutations; maybe rename
        self.n_generators = len(self.generators) # actions = generators
        self.n_actions = self.n_states + self.n_generators # 1 reset symbol per state, 1 apply symbol per generator

        self.init_state = np.arange(self.n) # identity permutation
        
        # lookup tables
        self.int2perm = list(map(np.array, itertools.permutations(range(self.n))))
        self.perm2int = {tuple(p):i for i,p in enumerate(self.int2perm)}
        
        # interval lengths
        T = self.sample_length()
        self.lags = [1]
        while self.lags[-1]*2 < T:
            self.lags.append(self.lags[-1]*2)

    def f(self, x):
        curr_state = self.init_state
        states = []
        for action_id in x:
            if action_id >= self.n_states:
                curr_state = self.generators[action_id - self.n_states][curr_state]
            else:
                curr_state = self.int2perm[action_id]
            states.append(self.perm2int[tuple(curr_state)])
        return np.array(states, dtype=np.int64)

    def sample(self):
        T = self.sample_length()
        x = self.np_rng.choice(range(self.n_generators), p=self.perm_probs, size=T) + self.n_states
        
        i = 0
        while i < T:
            x[i] = self.np_rng.choice(range(self.n_states))
            i += self.np_rng.choice(self.lags)
        
        return x, self.f(x)



dataset_map = {
    'abab': ABABAutomaton,
    'add': AdderAutomaton,
    'alternating': AlternatingAutomaton,
    'cyclic': CyclicAutomaton,
    'dihedral': DihedralAutomaton,
    'flipflop': FlipFlopAutomaton,
    'gridworld': GridworldAutomaton,
    'parity': ParityAutomaton,
    'quaternion': QuaternionAutomaton,
    'symmetric': SymmetricAutomaton,
    'permutation_reset': PermutationResetAutomaton
    # TODO: add Dyck
}