add alternating groups; add a parent class 'PermutationSampler' for symmetric and alternating groups
Browse files- automata.py +87 -31
automata.py
CHANGED
|
@@ -18,6 +18,7 @@ import csv
|
|
| 18 |
import json
|
| 19 |
import os
|
| 20 |
import itertools
|
|
|
|
| 21 |
|
| 22 |
import datasets
|
| 23 |
import numpy as np
|
|
@@ -29,9 +30,6 @@ major, minor = sys.version_info[:2]
|
|
| 29 |
version = major + 0.1*minor
|
| 30 |
OLD_PY_VERSION = 1 if version < 3.8 else 0
|
| 31 |
|
| 32 |
-
# Local imports
|
| 33 |
-
# from symmetric import SymmetricSampler
|
| 34 |
-
|
| 35 |
_CITATION = """\
|
| 36 |
"""
|
| 37 |
|
|
@@ -315,19 +313,16 @@ class FlipFlopSampler(AutomatonSampler):
|
|
| 315 |
return x, self.f(x)
|
| 316 |
|
| 317 |
|
| 318 |
-
|
|
|
|
| 319 |
"""
|
| 320 |
-
|
| 321 |
-
- parity (whether a state is even): this may need packages (e.g. Permutation from sympy)
|
| 322 |
-
- position / toggle: for S3 ~ D6, we can add labels for substructures as in Dihedral groups.
|
| 323 |
"""
|
| 324 |
def __init__(self, data_config):
|
| 325 |
super().__init__(data_config)
|
| 326 |
|
| 327 |
if 'n' not in data_config:
|
| 328 |
-
data_config['n'] = 5
|
| 329 |
-
if 'n_actions' not in data_config:
|
| 330 |
-
data_config['n_actions'] = 3
|
| 331 |
if 'label_type' not in data_config:
|
| 332 |
# Options: 'state', 'first_chair'
|
| 333 |
data_config['label_type'] = 'state'
|
|
@@ -335,6 +330,46 @@ class SymmetricSampler(AutomatonSampler):
|
|
| 335 |
self.n = data_config['n'] # the symmetric group Sn
|
| 336 |
self.label_type = data_config['label_type']
|
| 337 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
self.name = f'S{self.n}'
|
| 339 |
|
| 340 |
"""
|
|
@@ -349,6 +384,8 @@ class SymmetricSampler(AutomatonSampler):
|
|
| 349 |
"""
|
| 350 |
Get actions (3 defaults: id, shift-by-1, swap-first-two)
|
| 351 |
"""
|
|
|
|
|
|
|
| 352 |
self.n_actions = data_config['n_actions']
|
| 353 |
self.actions = {0: np.eye(self.n)}
|
| 354 |
# shift all elements to the right by 1
|
|
@@ -377,39 +414,58 @@ class SymmetricSampler(AutomatonSampler):
|
|
| 377 |
+ "- Labels: depending on 'label_type'.\n" \
|
| 378 |
+ "- Config:\n" \
|
| 379 |
+ " - n (int): number of objects, i.e. there are n! states.\n" \
|
| 380 |
-
+ " -
|
| 381 |
-
+ "
|
| 382 |
-
+ " - 'first_chair': the element in the first position of the permutation.\n" \
|
| 383 |
-
+ " e.g. if the current permutation is [2,3,1,4], then 'first_chair' is 2.\n" \
|
| 384 |
+ self.__info__
|
| 385 |
|
| 386 |
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
|
|
|
|
|
|
|
|
|
| 390 |
|
| 391 |
-
|
| 392 |
-
curr_state = np.arange(self.n)
|
| 393 |
-
labels = []
|
| 394 |
-
for action in x:
|
| 395 |
-
curr_state = self.actions[action].dot(curr_state)
|
| 396 |
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
|
| 402 |
-
return np.array(labels)
|
| 403 |
|
| 404 |
-
def sample(self):
|
| 405 |
-
T = self.sample_length()
|
| 406 |
-
x = np.random.choice(range(self.n_actions), replace=True, size=T)
|
| 407 |
|
| 408 |
-
return x, self.f(x)
|
| 409 |
|
| 410 |
|
| 411 |
dataset_map = {
|
| 412 |
'abab': ABABSampler,
|
|
|
|
| 413 |
'gridworld': GridworldSampler,
|
| 414 |
'flipflop': FlipFlopSampler,
|
| 415 |
'parity': ParitySampler,
|
|
|
|
| 18 |
import json
|
| 19 |
import os
|
| 20 |
import itertools
|
| 21 |
+
from sympy.combinatorics.permutations import Permutation
|
| 22 |
|
| 23 |
import datasets
|
| 24 |
import numpy as np
|
|
|
|
| 30 |
version = major + 0.1*minor
|
| 31 |
OLD_PY_VERSION = 1 if version < 3.8 else 0
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
_CITATION = """\
|
| 34 |
"""
|
| 35 |
|
|
|
|
| 313 |
return x, self.f(x)
|
| 314 |
|
| 315 |
|
| 316 |
+
|
| 317 |
+
class PermutationSampler(AutomatonSampler):
|
| 318 |
"""
|
| 319 |
+
Subclasses: SymmetricSampler, AlternatingSampler
|
|
|
|
|
|
|
| 320 |
"""
|
| 321 |
def __init__(self, data_config):
|
| 322 |
super().__init__(data_config)
|
| 323 |
|
| 324 |
if 'n' not in data_config:
|
| 325 |
+
data_config['n'] = 5
|
|
|
|
|
|
|
| 326 |
if 'label_type' not in data_config:
|
| 327 |
# Options: 'state', 'first_chair'
|
| 328 |
data_config['label_type'] = 'state'
|
|
|
|
| 330 |
self.n = data_config['n'] # the symmetric group Sn
|
| 331 |
self.label_type = data_config['label_type']
|
| 332 |
|
| 333 |
+
self.__info__ = \
|
| 334 |
+
" - label_type (str): choosing from the following options:\n" \
|
| 335 |
+
+ " - 'state' (default): the state id.\n" \
|
| 336 |
+
+ " - 'first_chair': the element in the first position of the permutation.\n" \
|
| 337 |
+
+ " e.g. if the current permutation is [2,1,4,3], then 'first_chair' is 2.\n" \
|
| 338 |
+
+ self.__info__
|
| 339 |
+
|
| 340 |
+
def get_state_label(self, state):
|
| 341 |
+
enc = self.state_encode(state)
|
| 342 |
+
return self.state_label_map[enc]
|
| 343 |
+
|
| 344 |
+
def f(self, x):
|
| 345 |
+
curr_state = np.arange(self.n)
|
| 346 |
+
labels = []
|
| 347 |
+
for action in x:
|
| 348 |
+
curr_state = self.actions[action].dot(curr_state)
|
| 349 |
+
|
| 350 |
+
if self.label_type == 'state':
|
| 351 |
+
labels += self.get_state_label(curr_state),
|
| 352 |
+
elif self.label_type == 'first_chair':
|
| 353 |
+
labels += curr_state[0],
|
| 354 |
+
|
| 355 |
+
return np.array(labels)
|
| 356 |
+
|
| 357 |
+
def sample(self):
|
| 358 |
+
T = self.sample_length()
|
| 359 |
+
x = np.random.choice(range(self.n_actions), replace=True, size=T)
|
| 360 |
+
|
| 361 |
+
return x, self.f(x)
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
class SymmetricSampler(PermutationSampler):
|
| 365 |
+
"""
|
| 366 |
+
TODO: add options for labels as functions of states
|
| 367 |
+
- parity (whether a state is even): this may need packages (e.g. Permutation from sympy)
|
| 368 |
+
- position / toggle: for S3 ~ D6, we can add labels for substructures as in Dihedral groups.
|
| 369 |
+
"""
|
| 370 |
+
def __init__(self, data_config):
|
| 371 |
+
super().__init__(data_config)
|
| 372 |
+
|
| 373 |
self.name = f'S{self.n}'
|
| 374 |
|
| 375 |
"""
|
|
|
|
| 384 |
"""
|
| 385 |
Get actions (3 defaults: id, shift-by-1, swap-first-two)
|
| 386 |
"""
|
| 387 |
+
if 'n_actions' not in data_config:
|
| 388 |
+
data_config['n_actions'] = 3
|
| 389 |
self.n_actions = data_config['n_actions']
|
| 390 |
self.actions = {0: np.eye(self.n)}
|
| 391 |
# shift all elements to the right by 1
|
|
|
|
| 414 |
+ "- Labels: depending on 'label_type'.\n" \
|
| 415 |
+ "- Config:\n" \
|
| 416 |
+ " - n (int): number of objects, i.e. there are n! states.\n" \
|
| 417 |
+
+ " - n_actions (int): number of permutations to include in the generator set;\n" \
|
| 418 |
+
+ " the ordering is given by itertools.permutations, and the first 'n_actions' permutations will be included.\n" \
|
|
|
|
|
|
|
| 419 |
+ self.__info__
|
| 420 |
|
| 421 |
|
| 422 |
+
class AlternatingSampler(PermutationSampler):
|
| 423 |
+
"""
|
| 424 |
+
TODO: other choices of generators (currently using (12x))?
|
| 425 |
+
"""
|
| 426 |
+
def __init__(self, data_config):
|
| 427 |
+
super().__init__(data_config)
|
| 428 |
|
| 429 |
+
self.name = f'A{self.n}'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
|
| 431 |
+
"""
|
| 432 |
+
Get states
|
| 433 |
+
"""
|
| 434 |
+
self.state_label_map = {}
|
| 435 |
+
self.state_encode = lambda state: ''.join([str(int(each)) for each in state])
|
| 436 |
+
cnt = 0
|
| 437 |
+
for si, state in enumerate(itertools.permutations(range(self.n))):
|
| 438 |
+
if not Permutation(state).is_even:
|
| 439 |
+
continue
|
| 440 |
+
enc = self.state_encode(state)
|
| 441 |
+
self.state_label_map[enc] = cnt
|
| 442 |
+
cnt += 1
|
| 443 |
+
|
| 444 |
+
"""
|
| 445 |
+
Get actions: all 3 cycles of the form (12x)
|
| 446 |
+
"""
|
| 447 |
+
self.actions = {0: np.eye(self.n)}
|
| 448 |
+
for idx in range(2, self.n):
|
| 449 |
+
# (1, 2, idx)
|
| 450 |
+
shift_idx = list(range(self.n))
|
| 451 |
+
shift_idx[0],shift_idx[1], shift_idx[idx] = shift_idx[1], shift_idx[idx], shift_idx[0]
|
| 452 |
+
self.actions[idx-1] = np.eye(self.n)[shift_idx]
|
| 453 |
+
self.n_actions = len(self.actions)
|
| 454 |
+
|
| 455 |
+
self.__info__ = f"Alternating group on n={self.n} objects:\n" \
|
| 456 |
+
+f"- Inputs: tokens from 0 to n-3, corresponding to all 3-cycles of the form (12x).\n" \
|
| 457 |
+
+ "- Labels: depending on 'label_type'.\n" \
|
| 458 |
+
+ "- Config:\n" \
|
| 459 |
+
+ " - n (int): number of objects, i.e. there are n!/2 states.\n" \
|
| 460 |
+
+ self.__info__
|
| 461 |
|
|
|
|
| 462 |
|
|
|
|
|
|
|
|
|
|
| 463 |
|
|
|
|
| 464 |
|
| 465 |
|
| 466 |
dataset_map = {
|
| 467 |
'abab': ABABSampler,
|
| 468 |
+
'alternating': AlternatingSampler,
|
| 469 |
'gridworld': GridworldSampler,
|
| 470 |
'flipflop': FlipFlopSampler,
|
| 471 |
'parity': ParitySampler,
|