Commit
·
b0d21d5
1
Parent(s):
dbf2014
unify indentation
Browse files- automata.py +572 -580
automata.py
CHANGED
|
@@ -109,660 +109,652 @@ class AutomatonDataset(datasets.GeneratorBasedBuilder):
|
|
| 109 |
}
|
| 110 |
|
| 111 |
class Automaton:
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
if 'seed' in self.data_config:
|
| 119 |
-
self.np_rng = np.random.default_rng(self.data_config['seed'])
|
| 120 |
-
else:
|
| 121 |
-
self.np_rng = np.random.default_rng()
|
| 122 |
-
|
| 123 |
-
if 'length' not in data_config: # sequence length
|
| 124 |
-
data_config['length'] = 20
|
| 125 |
-
self.T = self.data_config['length']
|
| 126 |
-
|
| 127 |
-
if 'random_length' not in data_config:
|
| 128 |
-
data_config['random_length'] = 0
|
| 129 |
-
self.random_length = data_config['random_length']
|
| 130 |
-
|
| 131 |
-
self.__info__ = \
|
| 132 |
-
" - T (int): sequence length.\n" \
|
| 133 |
-
+ " - random_length (int in {0, 1}): whether to randomly sample a length per sample.\n"
|
| 134 |
-
|
| 135 |
-
def f(self, x):
|
| 136 |
-
"""
|
| 137 |
-
Get output sequence given an input seq
|
| 138 |
-
"""
|
| 139 |
-
raise NotImplementedError()
|
| 140 |
-
|
| 141 |
-
def sample(self):
|
| 142 |
-
raise NotImplementedError()
|
| 143 |
-
|
| 144 |
-
def sample_length(self):
|
| 145 |
-
if self.random_length:
|
| 146 |
-
return self.np_rng.choice(range(1, self.T+1))
|
| 147 |
-
return self.T
|
| 148 |
-
|
| 149 |
-
def help(self):
|
| 150 |
-
print(self.__info__)
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
super().__init__(data_config)
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
self.__info__ = " - prob1 (float in [0,1]): probability of token 1\n" \
|
| 166 |
-
+ self.__info__
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
|
|
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
super().__init__(data_config)
|
| 179 |
-
self.name = 'parity'
|
| 180 |
|
| 181 |
-
self
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
+ self.__info__
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
|
| 190 |
-
class
|
| 191 |
-
"""
|
| 192 |
-
Note: gridworld currently doesn't include a no-op.
|
| 193 |
-
"""
|
| 194 |
-
def __init__(self, data_config):
|
| 195 |
-
super().__init__(data_config)
|
| 196 |
-
|
| 197 |
-
if 'n' not in data_config:
|
| 198 |
-
data_config['n'] = 9
|
| 199 |
-
"""
|
| 200 |
-
NOTE: n is the number of states, and S is the id (0-indexing) of the rightmost state.
|
| 201 |
-
i.e. the states are 0,1,2,...,S, where S=n-1.
|
| 202 |
"""
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
if 'label_type' not in data_config:
|
| 207 |
-
# Options: state, parity, boundary
|
| 208 |
-
data_config['label_type'] = 'state'
|
| 209 |
-
self.label_type = data_config['label_type']
|
| 210 |
-
|
| 211 |
-
self.name = f'Grid{self.n}'
|
| 212 |
-
|
| 213 |
-
self.__info__ = f"1d Gridworld of n={self.n} states:\n" \
|
| 214 |
-
+ "- Inputs: binary strings, i.e. move left(0) or right(1)\n" \
|
| 215 |
-
+ "- Labels: depending on 'label_type'. \n" \
|
| 216 |
-
+ "- Config: \n" \
|
| 217 |
-
+ " - n (int): number of states; i.e. the states are 0,1,2,...,n-1.\n" \
|
| 218 |
-
+ " - label_type (str): choosing from the following options:\n" \
|
| 219 |
-
+ " - 'state' (default): the state id, i.e. 0 to n-1.\n" \
|
| 220 |
-
+ " - 'parity': the state id mod 2.\n" \
|
| 221 |
-
+ " - 'boundary': whether the current state is in {0, n-1} or not.\n" \
|
| 222 |
-
+ self.__info__
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
def f(self, x):
|
| 226 |
-
x = copy(x)
|
| 227 |
-
x[x == 0] = -1
|
| 228 |
-
if OLD_PY_VERSION:
|
| 229 |
-
# NOTE: for Python 3.7 or below, accumulate doesn't have the 'initial' argument.
|
| 230 |
-
x = np.concatenate([np.array([0]), x]).astype(np.int64)
|
| 231 |
-
states = list(itertools.accumulate(x, lambda a,b: max(min(a+b, self.S), 0)))
|
| 232 |
-
states = states[1:]
|
| 233 |
-
else:
|
| 234 |
-
states = list(itertools.accumulate(x, lambda a,b: max(min(a+b, self.S), 0), initial=0))
|
| 235 |
-
states = states[1:] # remove the 1st entry with is the (meaningless) initial value 0
|
| 236 |
-
return np.array(states).astype(np.int64)
|
| 237 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
if 'prob_abab_pos_sample' not in data_config:
|
| 245 |
-
# The probability of having a positive sequence, i.e. 010101010101...
|
| 246 |
-
data_config['prob_abab_pos_sample'] = 0.25
|
| 247 |
-
if 'label_type' not in data_config:
|
| 248 |
-
# Options: 'state', 'boundary'
|
| 249 |
-
data_config['label_type'] = 'state'
|
| 250 |
-
|
| 251 |
-
self.prob_abab_pos_sample = data_config['prob_abab_pos_sample']
|
| 252 |
-
self.label_type = data_config['label_type']
|
| 253 |
-
|
| 254 |
-
self.transition = np.array(
|
| 255 |
-
[[4, 1], # state 0
|
| 256 |
-
[2, 4], # state 1
|
| 257 |
-
[4, 3], # state 2
|
| 258 |
-
[0, 4], # state 3
|
| 259 |
-
[4, 4], # state 4
|
| 260 |
-
])
|
| 261 |
-
|
| 262 |
-
self.__info__ = "abab: an automaton with 4 states + 1 absorbing state:\n" \
|
| 263 |
-
+ "- Inputs: binary strings\n" \
|
| 264 |
-
+ "- Labels: depending on 'label_type'.\n" \
|
| 265 |
-
+ "- Config:\n" \
|
| 266 |
-
+ " - prob_abab_pos_sample (float in [0,1]): probability of having a 'positive' sequence, i.e. 01010101010...\n" \
|
| 267 |
-
+ " - label_type (str): choosing from the following options:\n" \
|
| 268 |
-
+ " - 'state' (default): the state id.\n" \
|
| 269 |
-
+ " - 'boundary': whether the state is in state 3 (the states are 0,1,2,3).\n" \
|
| 270 |
-
+ self.__info__
|
| 271 |
-
|
| 272 |
-
def f(self, x):
|
| 273 |
-
labels = []
|
| 274 |
-
curr_state = 3
|
| 275 |
-
for each in x:
|
| 276 |
-
curr_state = self.transition[curr_state, each]
|
| 277 |
-
labels += curr_state,
|
| 278 |
-
labels = np.array(labels).astype(np.int64)
|
| 279 |
-
if self.label_type == 'boundary':
|
| 280 |
-
labels = (labels == 3).astype(np.int64)
|
| 281 |
-
return labels
|
| 282 |
-
|
| 283 |
-
def sample(self):
|
| 284 |
-
pos_sample = self.np_rng.random() < self.prob_abab_pos_sample
|
| 285 |
-
if pos_sample:
|
| 286 |
-
T = self.sample_length()
|
| 287 |
-
x = [0,1,0,1] * (T//4)
|
| 288 |
-
x += [0,1,0,1][:(T%4)]
|
| 289 |
-
x = np.array(x)
|
| 290 |
-
return x, self.f(x)
|
| 291 |
-
else:
|
| 292 |
-
return super().sample()
|
| 293 |
|
|
|
|
|
|
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
if 'n_addends' not in data_config:
|
| 301 |
-
data_config['n_addends'] = 2
|
| 302 |
-
self.n_addends = data_config['n_addends']
|
| 303 |
-
self.addend_scales = np.array([2**i for i in range(self.n_addends)]).reshape(-1, 1)
|
| 304 |
-
|
| 305 |
-
if 'label_type' not in data_config:
|
| 306 |
-
data_config['label_type'] = 'state'
|
| 307 |
-
self.label_type = data_config['label_type']
|
| 308 |
-
|
| 309 |
-
self.__info__ = f'Adder of n={self.n_addends} binary numbers:\n' \
|
| 310 |
-
+f"- Inputs: {self.n_addends} binary numbers, encoded as the int for the {self.n_addends}-bit binary number.\n" \
|
| 311 |
-
+ "- Labels: depending on the label_type.\n" \
|
| 312 |
-
+ "- Config:\n" \
|
| 313 |
-
+ " - n_addends (int): number of binary numbers to be added; default as 2.\n" \
|
| 314 |
-
+ " - label_type (str): choosing from the following options: \n" \
|
| 315 |
-
+f" - 'state': the state id, i.e. the int for the base-{self.n_addends} int corresponding to the number (carry, digit). \n" \
|
| 316 |
-
+f" - 'digit': the current output base-{self.n_addends} digit, without the carry. \n" \
|
| 317 |
-
+ " - 'position': the current carry bit.\n" \
|
| 318 |
-
+ self.__info__
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
def f(self, x):
|
| 322 |
-
outputs, carries = [], []
|
| 323 |
-
carry = 0
|
| 324 |
-
T = x.shape[-1]
|
| 325 |
-
for i in range(T):
|
| 326 |
-
curr_sum = x[:, i].sum() + carry
|
| 327 |
-
# NOTE: 'mod n_addends' makes sure the carry is binary
|
| 328 |
-
output, carry = curr_sum % self.n_addends, curr_sum // self.n_addends
|
| 329 |
-
outputs += output,
|
| 330 |
-
carries += carry,
|
| 331 |
-
outputs = np.array(outputs).astype(np.int64)
|
| 332 |
-
carries = np.array(carries).astype(np.int64)
|
| 333 |
-
|
| 334 |
-
if self.label_type == 'state':
|
| 335 |
-
return outputs + self.n_addends*carries
|
| 336 |
-
elif self.label_type == 'digit':
|
| 337 |
-
return outputs
|
| 338 |
-
elif self.label_type == 'carry':
|
| 339 |
-
return carries
|
| 340 |
-
|
| 341 |
-
def sample_addend(self, T):
|
| 342 |
-
a = self.np_rng.binomial(1, self.prob1, size=T)
|
| 343 |
-
return a
|
| 344 |
-
|
| 345 |
-
def sample(self):
|
| 346 |
-
T = self.sample_length()
|
| 347 |
-
x = np.stack([self.sample_addend(T) for _ in range(self.n_addends)])
|
| 348 |
-
# Pad the most significant bit (rightmost position, i.e. we're reversing the number) with 0 to handle the potential carry
|
| 349 |
-
pad = np.zeros((self.n_addends, 1))
|
| 350 |
-
x = np.concatenate([x, pad], 1)
|
| 351 |
-
|
| 352 |
-
x_encode = (self.addend_scales * x).sum(0)
|
| 353 |
-
return x_encode, self.f(x)
|
| 354 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
if 'n' not in data_config:
|
| 362 |
-
data_config['n'] = 2
|
| 363 |
-
|
| 364 |
-
self.n_states = data_config['n']
|
| 365 |
-
self.n_actions = self.n_states + 1
|
| 366 |
-
self.transition = np.array([list(range(self.n_actions))] + [[i+1]*self.n_actions for i in range(self.n_states)]).T
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
+ "- Labels: the state id.\n" \
|
| 371 |
-
+ "- Config:\n" \
|
| 372 |
-
+ " - n (int): number of write states; i.e. the states are 1,2,...,n, plus a default start state 0.\n" \
|
| 373 |
-
+ self.__info__
|
| 374 |
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
|
| 390 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
|
| 392 |
|
| 393 |
-
class
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
+ " e.g. if the current permutation is [2,1,4,3], then 'first_chair' is 2.\n" \
|
| 415 |
-
+ self.__info__
|
| 416 |
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
|
| 421 |
-
def f(self, x):
|
| 422 |
-
curr_state = np.arange(self.n)
|
| 423 |
-
labels = []
|
| 424 |
-
for action_id in x:
|
| 425 |
-
curr_state = self.actions[action_id].dot(curr_state)
|
| 426 |
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
- parity (whether a state is even): this may need packages (e.g. Permutation from sympy)
|
| 445 |
-
- position / toggle: for S3 ~ D6, we can add labels for substructures as in Dihedral groups.
|
| 446 |
-
"""
|
| 447 |
-
def __init__(self, data_config):
|
| 448 |
-
super().__init__(data_config)
|
| 449 |
|
| 450 |
-
|
| 451 |
|
| 452 |
-
"""
|
| 453 |
-
Get states
|
| 454 |
-
"""
|
| 455 |
-
self.state_encode = lambda state: ''.join([str(int(each)) for each in state])
|
| 456 |
-
self.state_label_map = {}
|
| 457 |
-
for si, state in enumerate(itertools.permutations(range(self.n))):
|
| 458 |
-
enc = self.state_encode(state)
|
| 459 |
-
self.state_label_map[enc] = si
|
| 460 |
|
|
|
|
| 461 |
"""
|
| 462 |
-
|
|
|
|
|
|
|
| 463 |
"""
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
self.n_actions = data_config['n_actions']
|
| 467 |
-
self.actions = {0: np.eye(self.n)}
|
| 468 |
-
# shift all elements to the right by 1
|
| 469 |
-
shift_idx = list(range(1, self.n)) + [0]
|
| 470 |
-
self.actions[1] = np.eye(self.n)[shift_idx]
|
| 471 |
-
# swap the first 2 elements
|
| 472 |
-
shift_idx = [1, 0] + list(range(2, self.n))
|
| 473 |
-
self.actions[2] = np.eye(self.n)[shift_idx]
|
| 474 |
-
|
| 475 |
-
if self.n_actions > 3:
|
| 476 |
-
# add permutations in the order given by itertools.permutations
|
| 477 |
-
self.all_permutations = list(itertools.permutations(range(self.n)))[1:]
|
| 478 |
-
cnt = 2
|
| 479 |
-
for each in self.all_permutations:
|
| 480 |
-
action = np.eye(self.n)[list(each)]
|
| 481 |
-
if np.linalg.norm(action - self.actions[0]) == 0:
|
| 482 |
-
continue
|
| 483 |
-
elif np.linalg.norm(action - self.actions[1]) == 0:
|
| 484 |
-
continue
|
| 485 |
-
self.actions[cnt] = action
|
| 486 |
-
cnt += 1
|
| 487 |
-
if cnt == self.n_actions: break
|
| 488 |
-
|
| 489 |
-
self.__info__ = f"Symmetric group on n={self.n} objects:\n" \
|
| 490 |
-
+f"- Inputs: tokens are either 0 (no-op), or 1:{self.n_actions} (corresponding to {self.n_actions} permutations).\n" \
|
| 491 |
-
+ "- Labels: depending on 'label_type'.\n" \
|
| 492 |
-
+ "- Config:\n" \
|
| 493 |
-
+ " - n (int): number of objects, i.e. there are n! states.\n" \
|
| 494 |
-
+ " - n_actions (int): number of permutations to include in the generator set;\n" \
|
| 495 |
-
+ " the ordering is given by itertools.permutations, and the first 'n_actions' permutations will be included.\n" \
|
| 496 |
-
+ self.__info__
|
| 497 |
|
|
|
|
| 498 |
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
|
|
|
|
|
|
| 505 |
|
| 506 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
|
| 508 |
-
"""
|
| 509 |
-
Get states
|
| 510 |
-
"""
|
| 511 |
-
self.state_label_map = {}
|
| 512 |
-
self.state_encode = lambda state: ''.join([str(int(each)) for each in state])
|
| 513 |
-
cnt = 0
|
| 514 |
-
for si, state in enumerate(itertools.permutations(range(self.n))):
|
| 515 |
-
if not Permutation(state).is_even:
|
| 516 |
-
continue
|
| 517 |
-
enc = self.state_encode(state)
|
| 518 |
-
self.state_label_map[enc] = cnt
|
| 519 |
-
cnt += 1
|
| 520 |
|
|
|
|
| 521 |
"""
|
| 522 |
-
|
| 523 |
"""
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
# (1, 2, idx)
|
| 527 |
-
shift_idx = list(range(self.n))
|
| 528 |
-
shift_idx[0],shift_idx[1], shift_idx[idx] = shift_idx[1], shift_idx[idx], shift_idx[0]
|
| 529 |
-
self.actions[idx-1] = np.eye(self.n)[shift_idx]
|
| 530 |
-
self.n_actions = len(self.actions)
|
| 531 |
|
| 532 |
-
|
| 533 |
-
+f"- Inputs: tokens from 0 to n-3, corresponding to all 3-cycles of the form (12x).\n" \
|
| 534 |
-
+ "- Labels: depending on 'label_type'.\n" \
|
| 535 |
-
+ "- Config:\n" \
|
| 536 |
-
+ " - n (int): number of objects, i.e. there are n!/2 states.\n" \
|
| 537 |
-
+ self.__info__
|
| 538 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
|
| 541 |
|
| 542 |
class CyclicAutomaton(Automaton):
|
| 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 |
class DihedralAutomaton(Automaton):
|
| 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 |
-
def sample(self):
|
| 663 |
-
T = self.sample_length()
|
| 664 |
-
x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)
|
| 665 |
|
| 666 |
-
|
|
|
|
|
|
|
| 667 |
|
|
|
|
| 668 |
|
| 669 |
|
| 670 |
class QuaternionAutomaton(Automaton):
|
| 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 |
class PermutationResetAutomaton(Automaton):
|
| 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 |
dataset_map = {
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
|
|
|
| 109 |
}
|
| 110 |
|
| 111 |
class Automaton:
|
| 112 |
+
"""
|
| 113 |
+
This is a parent class that must be inherited.
|
| 114 |
+
"""
|
| 115 |
+
def __init__(self, data_config):
|
| 116 |
+
self.data_config = data_config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
if 'seed' in self.data_config:
|
| 119 |
+
self.np_rng = np.random.default_rng(self.data_config['seed'])
|
| 120 |
+
else:
|
| 121 |
+
self.np_rng = np.random.default_rng()
|
| 122 |
|
| 123 |
+
if 'length' not in data_config: # sequence length
|
| 124 |
+
data_config['length'] = 20
|
| 125 |
+
self.T = self.data_config['length']
|
|
|
|
| 126 |
|
| 127 |
+
if 'random_length' not in data_config:
|
| 128 |
+
data_config['random_length'] = 0
|
| 129 |
+
self.random_length = data_config['random_length']
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
self.__info__ = \
|
| 132 |
+
" - T (int): sequence length.\n" \
|
| 133 |
+
+ " - random_length (int in {0, 1}): whether to randomly sample a length per sample.\n"
|
| 134 |
|
| 135 |
+
def f(self, x):
|
| 136 |
+
"""
|
| 137 |
+
Get output sequence given an input seq
|
| 138 |
+
"""
|
| 139 |
+
raise NotImplementedError()
|
| 140 |
|
| 141 |
+
def sample(self):
|
| 142 |
+
raise NotImplementedError()
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
def sample_length(self):
|
| 145 |
+
if self.random_length:
|
| 146 |
+
return self.np_rng.choice(range(1, self.T+1))
|
| 147 |
+
return self.T
|
|
|
|
| 148 |
|
| 149 |
+
def help(self):
|
| 150 |
+
print(self.__info__)
|
| 151 |
|
| 152 |
+
class BinaryInputAutomaton(Automaton):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
"""
|
| 154 |
+
This is a parent class that must be inherited.
|
| 155 |
+
Subclasses: ParityAutomaton, GridworldAutomaton, ABABAutomaton
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
+
TODO: sample sequences with a given number of 1s
|
| 158 |
+
"""
|
| 159 |
+
def __init__(self, data_config):
|
| 160 |
+
super().__init__(data_config)
|
| 161 |
|
| 162 |
+
if 'prob1' not in data_config:
|
| 163 |
+
data_config['prob1'] = 0.5
|
| 164 |
+
self.prob1 = data_config['prob1']
|
| 165 |
+
self.__info__ = " - prob1 (float in [0,1]): probability of token 1\n" \
|
| 166 |
+
+ self.__info__
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
def f(self, x):
|
| 169 |
+
raise NotImplementedError()
|
| 170 |
|
| 171 |
+
def sample(self):
|
| 172 |
+
T = self.sample_length()
|
| 173 |
+
x = self.np_rng.binomial(1, self.prob1, size=T)
|
| 174 |
+
return x, self.f(x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
class ParityAutomaton(BinaryInputAutomaton):
|
| 177 |
+
def __init__(self, data_config):
|
| 178 |
+
super().__init__(data_config)
|
| 179 |
+
self.name = 'parity'
|
| 180 |
|
| 181 |
+
self.__info__ = "Parity machine with 2 states: \n" \
|
| 182 |
+
+ "- Inputs: binary strings\n" \
|
| 183 |
+
+ "- Labels: binary strings of the partial parity\n" \
|
| 184 |
+
+ "- Config: \n" \
|
| 185 |
+
+ self.__info__
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
+
def f(self, x):
|
| 188 |
+
return np.cumsum(x) % 2
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
class GridworldAutomaton(BinaryInputAutomaton):
|
| 191 |
+
"""
|
| 192 |
+
Note: gridworld currently doesn't include a no-op.
|
| 193 |
+
"""
|
| 194 |
+
def __init__(self, data_config):
|
| 195 |
+
super().__init__(data_config)
|
| 196 |
|
| 197 |
+
if 'n' not in data_config:
|
| 198 |
+
data_config['n'] = 9
|
| 199 |
+
"""
|
| 200 |
+
NOTE: n is the number of states, and S is the id (0-indexing) of the rightmost state.
|
| 201 |
+
i.e. the states are 0,1,2,...,S, where S=n-1.
|
| 202 |
+
"""
|
| 203 |
+
self.n = data_config['n']
|
| 204 |
+
self.S = self.n - 1
|
| 205 |
+
|
| 206 |
+
if 'label_type' not in data_config:
|
| 207 |
+
# Options: state, parity, boundary
|
| 208 |
+
data_config['label_type'] = 'state'
|
| 209 |
+
self.label_type = data_config['label_type']
|
| 210 |
+
|
| 211 |
+
self.name = f'Grid{self.n}'
|
| 212 |
+
|
| 213 |
+
self.__info__ = f"1d Gridworld of n={self.n} states:\n" \
|
| 214 |
+
+ "- Inputs: binary strings, i.e. move left(0) or right(1)\n" \
|
| 215 |
+
+ "- Labels: depending on 'label_type'. \n" \
|
| 216 |
+
+ "- Config: \n" \
|
| 217 |
+
+ " - n (int): number of states; i.e. the states are 0,1,2,...,n-1.\n" \
|
| 218 |
+
+ " - label_type (str): choosing from the following options:\n" \
|
| 219 |
+
+ " - 'state' (default): the state id, i.e. 0 to n-1.\n" \
|
| 220 |
+
+ " - 'parity': the state id mod 2.\n" \
|
| 221 |
+
+ " - 'boundary': whether the current state is in {0, n-1} or not.\n" \
|
| 222 |
+
+ self.__info__
|
| 223 |
+
|
| 224 |
+
def f(self, x):
|
| 225 |
+
x = copy(x)
|
| 226 |
+
x[x == 0] = -1
|
| 227 |
+
if OLD_PY_VERSION:
|
| 228 |
+
# NOTE: for Python 3.7 or below, accumulate doesn't have the 'initial' argument.
|
| 229 |
+
x = np.concatenate([np.array([0]), x]).astype(np.int64)
|
| 230 |
+
states = list(itertools.accumulate(x, lambda a,b: max(min(a+b, self.S), 0)))
|
| 231 |
+
states = states[1:]
|
| 232 |
+
else:
|
| 233 |
+
states = list(itertools.accumulate(x, lambda a,b: max(min(a+b, self.S), 0), initial=0))
|
| 234 |
+
states = states[1:] # remove the 1st entry with is the (meaningless) initial value 0
|
| 235 |
+
return np.array(states).astype(np.int64)
|
| 236 |
|
| 237 |
|
| 238 |
+
class ABABAutomaton(BinaryInputAutomaton):
|
| 239 |
+
def __init__(self, data_config):
|
| 240 |
+
super().__init__(data_config)
|
| 241 |
+
self.name = 'abab'
|
| 242 |
+
|
| 243 |
+
if 'prob_abab_pos_sample' not in data_config:
|
| 244 |
+
# The probability of having a positive sequence, i.e. 010101010101...
|
| 245 |
+
data_config['prob_abab_pos_sample'] = 0.25
|
| 246 |
+
if 'label_type' not in data_config:
|
| 247 |
+
# Options: 'state', 'boundary'
|
| 248 |
+
data_config['label_type'] = 'state'
|
| 249 |
+
|
| 250 |
+
self.prob_abab_pos_sample = data_config['prob_abab_pos_sample']
|
| 251 |
+
self.label_type = data_config['label_type']
|
| 252 |
+
|
| 253 |
+
self.transition = np.array(
|
| 254 |
+
[[4, 1], # state 0
|
| 255 |
+
[2, 4], # state 1
|
| 256 |
+
[4, 3], # state 2
|
| 257 |
+
[0, 4], # state 3
|
| 258 |
+
[4, 4], # state 4
|
| 259 |
+
])
|
| 260 |
+
|
| 261 |
+
self.__info__ = "abab: an automaton with 4 states + 1 absorbing state:\n" \
|
| 262 |
+
+ "- Inputs: binary strings\n" \
|
| 263 |
+
+ "- Labels: depending on 'label_type'.\n" \
|
| 264 |
+
+ "- Config:\n" \
|
| 265 |
+
+ " - prob_abab_pos_sample (float in [0,1]): probability of having a 'positive' sequence, i.e. 01010101010...\n" \
|
| 266 |
+
+ " - label_type (str): choosing from the following options:\n" \
|
| 267 |
+
+ " - 'state' (default): the state id.\n" \
|
| 268 |
+
+ " - 'boundary': whether the state is in state 3 (the states are 0,1,2,3).\n" \
|
| 269 |
+
+ self.__info__
|
| 270 |
+
|
| 271 |
+
def f(self, x):
|
| 272 |
+
labels = []
|
| 273 |
+
curr_state = 3
|
| 274 |
+
for each in x:
|
| 275 |
+
curr_state = self.transition[curr_state, each]
|
| 276 |
+
labels += curr_state,
|
| 277 |
+
labels = np.array(labels).astype(np.int64)
|
| 278 |
+
if self.label_type == 'boundary':
|
| 279 |
+
labels = (labels == 3).astype(np.int64)
|
| 280 |
+
return labels
|
| 281 |
+
|
| 282 |
+
def sample(self):
|
| 283 |
+
pos_sample = self.np_rng.random() < self.prob_abab_pos_sample
|
| 284 |
+
if pos_sample:
|
| 285 |
+
T = self.sample_length()
|
| 286 |
+
x = [0,1,0,1] * (T//4)
|
| 287 |
+
x += [0,1,0,1][:(T%4)]
|
| 288 |
+
x = np.array(x)
|
| 289 |
+
return x, self.f(x)
|
| 290 |
+
else:
|
| 291 |
+
return super().sample()
|
| 292 |
|
| 293 |
|
| 294 |
+
class AdderAutomaton(BinaryInputAutomaton):
|
| 295 |
+
def __init__(self, data_config):
|
| 296 |
+
super().__init__(data_config)
|
| 297 |
+
self.name = 'addition'
|
| 298 |
+
|
| 299 |
+
if 'n_addends' not in data_config:
|
| 300 |
+
data_config['n_addends'] = 2
|
| 301 |
+
self.n_addends = data_config['n_addends']
|
| 302 |
+
self.addend_scales = np.array([2**i for i in range(self.n_addends)]).reshape(-1, 1)
|
| 303 |
+
|
| 304 |
+
if 'label_type' not in data_config:
|
| 305 |
+
data_config['label_type'] = 'state'
|
| 306 |
+
self.label_type = data_config['label_type']
|
| 307 |
+
|
| 308 |
+
self.__info__ = f'Adder of n={self.n_addends} binary numbers:\n' \
|
| 309 |
+
+f"- Inputs: {self.n_addends} binary numbers, encoded as the int for the {self.n_addends}-bit binary number.\n" \
|
| 310 |
+
+ "- Labels: depending on the label_type.\n" \
|
| 311 |
+
+ "- Config:\n" \
|
| 312 |
+
+ " - n_addends (int): number of binary numbers to be added; default as 2.\n" \
|
| 313 |
+
+ " - label_type (str): choosing from the following options: \n" \
|
| 314 |
+
+f" - 'state': the state id, i.e. the int for the base-{self.n_addends} int corresponding to the number (carry, digit). \n" \
|
| 315 |
+
+f" - 'digit': the current output base-{self.n_addends} digit, without the carry. \n" \
|
| 316 |
+
+ " - 'position': the current carry bit.\n" \
|
| 317 |
+
+ self.__info__
|
| 318 |
+
|
| 319 |
+
def f(self, x):
|
| 320 |
+
outputs, carries = [], []
|
| 321 |
+
carry = 0
|
| 322 |
+
T = x.shape[-1]
|
| 323 |
+
for i in range(T):
|
| 324 |
+
curr_sum = x[:, i].sum() + carry
|
| 325 |
+
# NOTE: 'mod n_addends' makes sure the carry is binary
|
| 326 |
+
output, carry = curr_sum % self.n_addends, curr_sum // self.n_addends
|
| 327 |
+
outputs += output,
|
| 328 |
+
carries += carry,
|
| 329 |
+
outputs = np.array(outputs).astype(np.int64)
|
| 330 |
+
carries = np.array(carries).astype(np.int64)
|
| 331 |
+
|
| 332 |
+
if self.label_type == 'state':
|
| 333 |
+
return outputs + self.n_addends*carries
|
| 334 |
+
elif self.label_type == 'digit':
|
| 335 |
+
return outputs
|
| 336 |
+
elif self.label_type == 'carry':
|
| 337 |
+
return carries
|
| 338 |
+
|
| 339 |
+
def sample_addend(self, T):
|
| 340 |
+
a = self.np_rng.binomial(1, self.prob1, size=T)
|
| 341 |
+
return a
|
| 342 |
+
|
| 343 |
+
def sample(self):
|
| 344 |
+
T = self.sample_length()
|
| 345 |
+
x = np.stack([self.sample_addend(T) for _ in range(self.n_addends)])
|
| 346 |
+
# Pad the most significant bit (rightmost position, i.e. we're reversing the number) with 0 to handle the potential carry
|
| 347 |
+
pad = np.zeros((self.n_addends, 1))
|
| 348 |
+
x = np.concatenate([x, pad], 1)
|
| 349 |
+
|
| 350 |
+
x_encode = (self.addend_scales * x).sum(0)
|
| 351 |
+
return x_encode, self.f(x)
|
| 352 |
|
| 353 |
+
class FlipFlopAutomaton(Automaton):
|
| 354 |
+
def __init__(self, data_config):
|
| 355 |
+
super().__init__(data_config)
|
| 356 |
+
self.name = 'flipflop'
|
|
|
|
|
|
|
| 357 |
|
| 358 |
+
if 'n' not in data_config:
|
| 359 |
+
data_config['n'] = 2
|
| 360 |
+
|
| 361 |
+
self.n_states = data_config['n']
|
| 362 |
+
self.n_actions = self.n_states + 1
|
| 363 |
+
self.transition = np.array([list(range(self.n_actions))] + [[i+1]*self.n_actions for i in range(self.n_states)]).T
|
| 364 |
+
|
| 365 |
+
self.__info__ = f"Flipflop with n={self.n_states} states:\n" \
|
| 366 |
+
+f"- Inputs: tokens are either 0 (read) or 1:{self.n} (write).\n" \
|
| 367 |
+
+ "- Labels: the state id.\n" \
|
| 368 |
+
+ "- Config:\n" \
|
| 369 |
+
+ " - n (int): number of write states; i.e. the states are 1,2,...,n, plus a default start state 0.\n" \
|
| 370 |
+
+ self.__info__
|
| 371 |
+
|
| 372 |
+
def f(self, x):
|
| 373 |
+
state, states = 0, []
|
| 374 |
+
for action_id in x:
|
| 375 |
+
state = self.transition[state, action_id]
|
| 376 |
+
states += state,
|
| 377 |
+
return np.array(states)
|
| 378 |
+
|
| 379 |
+
def sample(self):
|
| 380 |
+
T = self.sample_length()
|
| 381 |
+
rand = self.np_rng.uniform(size=T)
|
| 382 |
+
nonzero_pos = (rand < 0.5).astype(np.int64)
|
| 383 |
+
writes = self.np_rng.choice(range(1, self.n_states+1), size=T)
|
| 384 |
+
x = writes * nonzero_pos
|
| 385 |
+
return x, self.f(x)
|
| 386 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
|
| 388 |
+
class PermutationAutomaton(Automaton):
|
| 389 |
+
"""
|
| 390 |
+
This is a parent class that must be inherited.
|
| 391 |
+
Subclasses: SymmetricAutomaton, AlternatingAutomaton
|
| 392 |
+
"""
|
| 393 |
+
def __init__(self, data_config):
|
| 394 |
+
super().__init__(data_config)
|
| 395 |
+
|
| 396 |
+
if 'n' not in data_config:
|
| 397 |
+
data_config['n'] = 5
|
| 398 |
+
if 'label_type' not in data_config:
|
| 399 |
+
# Options: 'state', 'first_chair'
|
| 400 |
+
data_config['label_type'] = 'state'
|
| 401 |
+
|
| 402 |
+
self.n = data_config['n'] # the symmetric group Sn
|
| 403 |
+
self.label_type = data_config['label_type']
|
| 404 |
|
| 405 |
+
self.__info__ = \
|
| 406 |
+
" - label_type (str): choosing from the following options:\n" \
|
| 407 |
+
+ " - 'state' (default): the state id.\n" \
|
| 408 |
+
+ " - 'first_chair': the element in the first position of the permutation.\n" \
|
| 409 |
+
+ " e.g. if the current permutation is [2,1,4,3], then 'first_chair' is 2.\n" \
|
| 410 |
+
+ self.__info__
|
| 411 |
|
| 412 |
+
def get_state_label(self, state):
|
| 413 |
+
enc = self.state_encode(state)
|
| 414 |
+
return self.state_label_map[enc]
|
| 415 |
|
| 416 |
+
def f(self, x):
|
| 417 |
+
curr_state = np.arange(self.n)
|
| 418 |
+
labels = []
|
| 419 |
+
for action_id in x:
|
| 420 |
+
curr_state = self.actions[action_id].dot(curr_state)
|
| 421 |
|
| 422 |
+
if self.label_type == 'state':
|
| 423 |
+
labels += self.get_state_label(curr_state),
|
| 424 |
+
elif self.label_type == 'first_chair':
|
| 425 |
+
labels += curr_state[0],
|
| 426 |
+
return np.array(labels)
|
| 427 |
|
| 428 |
+
def sample(self):
|
| 429 |
+
T = self.sample_length()
|
| 430 |
+
x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
+
return x, self.f(x)
|
| 433 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
|
| 435 |
+
class SymmetricAutomaton(PermutationAutomaton):
|
| 436 |
"""
|
| 437 |
+
TODO: add options for labels as functions of states
|
| 438 |
+
- parity (whether a state is even): this may need packages (e.g. Permutation from sympy)
|
| 439 |
+
- position / toggle: for S3 ~ D6, we can add labels for substructures as in Dihedral groups.
|
| 440 |
"""
|
| 441 |
+
def __init__(self, data_config):
|
| 442 |
+
super().__init__(data_config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
|
| 444 |
+
self.name = f'S{self.n}'
|
| 445 |
|
| 446 |
+
"""
|
| 447 |
+
Get states
|
| 448 |
+
"""
|
| 449 |
+
self.state_encode = lambda state: ''.join([str(int(each)) for each in state])
|
| 450 |
+
self.state_label_map = {}
|
| 451 |
+
for si, state in enumerate(itertools.permutations(range(self.n))):
|
| 452 |
+
enc = self.state_encode(state)
|
| 453 |
+
self.state_label_map[enc] = si
|
| 454 |
|
| 455 |
+
"""
|
| 456 |
+
Get actions (3 defaults: id, shift-by-1, swap-first-two)
|
| 457 |
+
"""
|
| 458 |
+
if 'n_actions' not in data_config:
|
| 459 |
+
data_config['n_actions'] = 3
|
| 460 |
+
self.n_actions = data_config['n_actions']
|
| 461 |
+
self.actions = {0: np.eye(self.n)}
|
| 462 |
+
# shift all elements to the right by 1
|
| 463 |
+
shift_idx = list(range(1, self.n)) + [0]
|
| 464 |
+
self.actions[1] = np.eye(self.n)[shift_idx]
|
| 465 |
+
# swap the first 2 elements
|
| 466 |
+
shift_idx = [1, 0] + list(range(2, self.n))
|
| 467 |
+
self.actions[2] = np.eye(self.n)[shift_idx]
|
| 468 |
+
|
| 469 |
+
if self.n_actions > 3:
|
| 470 |
+
# add permutations in the order given by itertools.permutations
|
| 471 |
+
self.all_permutations = list(itertools.permutations(range(self.n)))[1:]
|
| 472 |
+
cnt = 2
|
| 473 |
+
for each in self.all_permutations:
|
| 474 |
+
action = np.eye(self.n)[list(each)]
|
| 475 |
+
if np.linalg.norm(action - self.actions[0]) == 0:
|
| 476 |
+
continue
|
| 477 |
+
elif np.linalg.norm(action - self.actions[1]) == 0:
|
| 478 |
+
continue
|
| 479 |
+
self.actions[cnt] = action
|
| 480 |
+
cnt += 1
|
| 481 |
+
if cnt == self.n_actions: break
|
| 482 |
+
|
| 483 |
+
self.__info__ = f"Symmetric group on n={self.n} objects:\n" \
|
| 484 |
+
+f"- Inputs: tokens are either 0 (no-op), or 1:{self.n_actions} (corresponding to {self.n_actions} permutations).\n" \
|
| 485 |
+
+ "- Labels: depending on 'label_type'.\n" \
|
| 486 |
+
+ "- Config:\n" \
|
| 487 |
+
+ " - n (int): number of objects, i.e. there are n! states.\n" \
|
| 488 |
+
+ " - n_actions (int): number of permutations to include in the generator set;\n" \
|
| 489 |
+
+ " the ordering is given by itertools.permutations, and the first 'n_actions' permutations will be included.\n" \
|
| 490 |
+
+ self.__info__
|
| 491 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
|
| 493 |
+
class AlternatingAutomaton(PermutationAutomaton):
|
| 494 |
"""
|
| 495 |
+
TODO: other choices of generators (currently using (12x))?
|
| 496 |
"""
|
| 497 |
+
def __init__(self, data_config):
|
| 498 |
+
super().__init__(data_config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
|
| 500 |
+
self.name = f'A{self.n}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
|
| 502 |
+
"""
|
| 503 |
+
Get states
|
| 504 |
+
"""
|
| 505 |
+
self.state_label_map = {}
|
| 506 |
+
self.state_encode = lambda state: ''.join([str(int(each)) for each in state])
|
| 507 |
+
cnt = 0
|
| 508 |
+
for si, state in enumerate(itertools.permutations(range(self.n))):
|
| 509 |
+
if not Permutation(state).is_even:
|
| 510 |
+
continue
|
| 511 |
+
enc = self.state_encode(state)
|
| 512 |
+
self.state_label_map[enc] = cnt
|
| 513 |
+
cnt += 1
|
| 514 |
|
| 515 |
+
"""
|
| 516 |
+
Get actions: all 3 cycles of the form (12x)
|
| 517 |
+
"""
|
| 518 |
+
self.actions = {0: np.eye(self.n)}
|
| 519 |
+
for idx in range(2, self.n):
|
| 520 |
+
# (1, 2, idx)
|
| 521 |
+
shift_idx = list(range(self.n))
|
| 522 |
+
shift_idx[0],shift_idx[1], shift_idx[idx] = shift_idx[1], shift_idx[idx], shift_idx[0]
|
| 523 |
+
self.actions[idx-1] = np.eye(self.n)[shift_idx]
|
| 524 |
+
self.n_actions = len(self.actions)
|
| 525 |
+
|
| 526 |
+
self.__info__ = f"Alternating group on n={self.n} objects:\n" \
|
| 527 |
+
+f"- Inputs: tokens from 0 to n-3, corresponding to all 3-cycles of the form (12x).\n" \
|
| 528 |
+
+ "- Labels: depending on 'label_type'.\n" \
|
| 529 |
+
+ "- Config:\n" \
|
| 530 |
+
+ " - n (int): number of objects, i.e. there are n!/2 states.\n" \
|
| 531 |
+
+ self.__info__
|
| 532 |
|
| 533 |
|
| 534 |
class CyclicAutomaton(Automaton):
|
| 535 |
+
def __init__(self, data_config):
|
| 536 |
+
super().__init__(data_config)
|
| 537 |
|
| 538 |
+
if 'n' not in data_config:
|
| 539 |
+
data_config['n'] = 5
|
| 540 |
+
self.n = data_config['n']
|
| 541 |
|
| 542 |
+
"""
|
| 543 |
+
Get actions: shift by i positions, for i = 0 to n_actions-1
|
| 544 |
+
"""
|
| 545 |
+
if 'n_actions' not in data_config:
|
| 546 |
+
data_config['n_actions'] = 2
|
| 547 |
+
self.n_actions = data_config['n_actions']
|
| 548 |
+
shift_idx = list(range(1, self.n)) + [0]
|
| 549 |
+
self.actions = {}
|
| 550 |
+
for i in range(self.n_actions):
|
| 551 |
+
shift_idx = list(range(i, self.n)) + list(range(0, i))
|
| 552 |
+
self.actions[i] = np.eye(self.n)[shift_idx]
|
| 553 |
|
| 554 |
+
self.__info__ = 'Cyclic group of n={self.n} states:\n' \
|
| 555 |
+
+f"- Inputs: tokens from 0 to n_actions-1\n" \
|
| 556 |
+
+ "- Labels: the current state.\n" \
|
| 557 |
+
+ "- Config:\n" \
|
| 558 |
+
+ " - n (int): number of states.\n" \
|
| 559 |
+
+ " - n_actions (int): number of actions/generators, which are 0, 1, ..., n_actions-1.\n" \
|
| 560 |
+
+ self.__info__
|
| 561 |
|
| 562 |
+
def f(self, x):
|
| 563 |
+
return np.cumsum(x) % self.n
|
| 564 |
|
| 565 |
+
def sample(self):
|
| 566 |
+
T = self.sample_length()
|
| 567 |
+
x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)
|
| 568 |
|
| 569 |
+
return x, self.f(x)
|
| 570 |
|
| 571 |
|
| 572 |
class DihedralAutomaton(Automaton):
|
| 573 |
+
def __init__(self, data_config):
|
| 574 |
+
super().__init__(data_config)
|
| 575 |
|
| 576 |
+
if 'n' not in data_config:
|
| 577 |
+
data_config['n'] = 4
|
| 578 |
+
self.n = data_config['n']
|
| 579 |
|
| 580 |
+
if 'label_type' not in data_config:
|
| 581 |
+
# Options: 'state', 'toggle', 'position'
|
| 582 |
+
data_config['label_type'] = 'state'
|
| 583 |
+
self.label_type = data_config['label_type']
|
| 584 |
|
| 585 |
+
"""
|
| 586 |
+
2 actions: toggle, or shift by 1 position (direction determined by the toggle).
|
| 587 |
+
"""
|
| 588 |
+
self.n_actions = 2
|
| 589 |
+
self.actions = {}
|
| 590 |
+
# shift all elements to the left (counter-clockwise) by 1
|
| 591 |
+
shift_idx = list(range(1, self.n)) + [0]
|
| 592 |
+
self.actions[0] = np.eye(self.n)[shift_idx]
|
| 593 |
+
# shift all elements to the right (closewise) by 1
|
| 594 |
+
shift_idx = [self.n-1] + list(range(self.n-1))
|
| 595 |
+
self.actions[1] = np.eye(self.n)[shift_idx]
|
| 596 |
+
|
| 597 |
+
self.__info__ = 'Dihedral group of order 2n, where n={self.n}:\n' \
|
| 598 |
+
+f"- Inputs: binary tokens:\n" \
|
| 599 |
+
+ " 0 for toggle, i.e. change direction in the n-cycle;\n" \
|
| 600 |
+
+ " 1 for drive, i.e. move forward 1 step on the n-cycle.\n" \
|
| 601 |
+
+ "- Labels: depending on the label_type.\n" \
|
| 602 |
+
+ "- Config:\n" \
|
| 603 |
+
+ " - n (int): size of the 'cycle'; i.e. there are 2n states considering also the toggle bit.\n" \
|
| 604 |
+
+ " - label_type (str): choosing from the following options: \n" \
|
| 605 |
+
+ " - 'state': the state id, i.e. considering both toggle and position. \n" \
|
| 606 |
+
+ " - 'toggle': the toggle bit (in {0, 1}). \n" \
|
| 607 |
+
+ " - 'position': the position on the n-cycle (in [n]).\n" \
|
| 608 |
+
+ self.__info__
|
| 609 |
+
|
| 610 |
+
def f_sequential(self, x):
|
| 611 |
+
# sanity check: sequential solution
|
| 612 |
+
position = np.arange(self.n)
|
| 613 |
+
states = []
|
| 614 |
+
toggle = 0 # i.e. parity
|
| 615 |
+
for action in x:
|
| 616 |
+
if action == 0:
|
| 617 |
+
# toggle direction
|
| 618 |
+
toggle = 1 - toggle
|
| 619 |
+
else:
|
| 620 |
+
# drive by 1
|
| 621 |
+
position = self.actions[toggle].dot(position)
|
| 622 |
+
states += (toggle, position[0]),
|
| 623 |
+
return states
|
| 624 |
+
|
| 625 |
+
def f(self, x):
|
| 626 |
+
# Parallel solution
|
| 627 |
+
|
| 628 |
+
# Get toggles: a parity task on the toggle bit
|
| 629 |
+
toggles = (x == 0).astype(np.int64)
|
| 630 |
+
toggle_status = np.cumsum(toggles) % 2
|
| 631 |
+
|
| 632 |
+
# Get positions: a directed modular counter
|
| 633 |
+
directions = (-1)**toggle_status
|
| 634 |
+
directed_drives = (x != 0).astype(np.int64) * directions
|
| 635 |
+
positions = np.cumsum(directed_drives) % self.n
|
| 636 |
+
|
| 637 |
+
if self.label_type == 'state':
|
| 638 |
+
labels = [self.get_state_label(each) for each in zip(toggle_status, positions)]
|
| 639 |
+
return np.array(labels).astype(np.int64)
|
| 640 |
+
elif self.label_type == 'toggle':
|
| 641 |
+
return toggle_status
|
| 642 |
+
elif self.label_type == 'positions':
|
| 643 |
+
return positions
|
| 644 |
+
|
| 645 |
+
def get_state_label(self, state):
|
| 646 |
+
"""
|
| 647 |
+
toggle in {0,1}
|
| 648 |
+
position in [k]
|
| 649 |
+
"""
|
| 650 |
+
toggle, position = state
|
| 651 |
+
label = self.n*toggle + position
|
| 652 |
+
return label
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
|
| 654 |
+
def sample(self):
|
| 655 |
+
T = self.sample_length()
|
| 656 |
+
x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)
|
| 657 |
|
| 658 |
+
return x, self.f(x)
|
| 659 |
|
| 660 |
|
| 661 |
class QuaternionAutomaton(Automaton):
|
| 662 |
+
def __init__(self, data_config):
|
| 663 |
+
super().__init__(data_config)
|
| 664 |
+
|
| 665 |
+
self.vocab_size = 8 # {-1, 1} x {1, i, j, k}
|
| 666 |
+
self.n_actions = 4 # {1, i, j, k}
|
| 667 |
+
self.transition_pos = [
|
| 668 |
+
0, 1, 2, 3,
|
| 669 |
+
1, 4, 3, 6,
|
| 670 |
+
2, 7, 4, 1,
|
| 671 |
+
3, 2, 5, 4,
|
| 672 |
+
]
|
| 673 |
+
self.transition_neg = [(each+4)%8 for each in self.transition_pos]
|
| 674 |
+
self.transition = np.array(self.transition_pos + self.transition_neg)
|
| 675 |
+
self.transition = self.transition.reshape(-1, 4)
|
| 676 |
+
|
| 677 |
+
self.__info__ = "Quaternion group:\n" \
|
| 678 |
+
+ "- Inputs: tokens in {0,1,2,3}, corresponding to 1,i,j,k.\n" \
|
| 679 |
+
+ "- Labels: the state id; 8 states in total: 2 signs ({-1,1}) x 4 values ({1,i,j,k}).\n" \
|
| 680 |
+
+ "- Config:\n" \
|
| 681 |
+
+ self.__info__
|
| 682 |
+
|
| 683 |
+
def f(self, x):
|
| 684 |
+
curr_state = 0
|
| 685 |
+
states = []
|
| 686 |
+
for action_id in x:
|
| 687 |
+
curr_state = self.transition[curr_state, action_id]
|
| 688 |
+
states += curr_state,
|
| 689 |
+
return np.array(states).astype(np.int64)
|
| 690 |
+
|
| 691 |
+
def sample(self):
|
| 692 |
+
T = self.sample_length()
|
| 693 |
+
x = self.np_rng.choice(range(self.n_actions), size=T)
|
| 694 |
+
return x, self.f(x)
|
| 695 |
|
| 696 |
|
| 697 |
class PermutationResetAutomaton(Automaton):
|
| 698 |
+
def __init__(self, data_config):
|
| 699 |
+
super().__init__(data_config)
|
| 700 |
|
| 701 |
+
self.n = data_config['n']
|
| 702 |
+
self.generators = data_config['generators']
|
| 703 |
+
self.perm_probs = data_config['perm_probs']
|
| 704 |
+
if type(self.generators[0]) is str:
|
| 705 |
+
self.generators = [ np.array(list(map(int, list(g)))) for g in self.generators ]
|
| 706 |
|
| 707 |
+
self.vocab_size = math.factorial(self.n) # states = permutations; maybe rename
|
| 708 |
+
self.n_generators = len(self.generators) # actions = generators
|
| 709 |
+
self.n_actions = self.vocab_size + self.n_generators # 1 reset symbol per state, 1 apply symbol per generator
|
| 710 |
|
| 711 |
+
self.init_state = np.arange(self.n) # identity permutation
|
| 712 |
+
|
| 713 |
+
# lookup tables
|
| 714 |
+
self.int2perm = list(map(np.array, itertools.permutations(range(self.n))))
|
| 715 |
+
self.perm2int = {tuple(p):i for i,p in enumerate(self.int2perm)}
|
| 716 |
+
|
| 717 |
+
# interval lengths
|
| 718 |
+
T = self.sample_length()
|
| 719 |
+
self.lags = [1]
|
| 720 |
+
while self.lags[-1]*2 < T:
|
| 721 |
+
self.lags.append(self.lags[-1]*2)
|
| 722 |
+
|
| 723 |
+
def f(self, x):
|
| 724 |
+
curr_state = self.init_state
|
| 725 |
+
states = []
|
| 726 |
+
for action_id in x:
|
| 727 |
+
if action_id >= self.vocab_size:
|
| 728 |
+
curr_state = self.generators[action_id - self.vocab_size][curr_state]
|
| 729 |
+
else:
|
| 730 |
+
curr_state = self.int2perm[action_id]
|
| 731 |
+
states.append(self.perm2int[tuple(curr_state)])
|
| 732 |
+
return np.array(states, dtype=np.int64)
|
| 733 |
+
|
| 734 |
+
def sample(self):
|
| 735 |
+
T = self.sample_length()
|
| 736 |
+
x = self.np_rng.choice(range(self.n_generators), p=self.perm_probs, size=T) + self.vocab_size
|
| 737 |
+
|
| 738 |
+
i = 0
|
| 739 |
+
while i < T:
|
| 740 |
+
x[i] = self.np_rng.choice(range(self.vocab_size))
|
| 741 |
+
i += self.np_rng.choice(self.lags)
|
| 742 |
+
|
| 743 |
+
return x, self.f(x)
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
dataset_map = {
|
| 748 |
+
'abab': ABABAutomaton,
|
| 749 |
+
'add': AdderAutomaton,
|
| 750 |
+
'alternating': AlternatingAutomaton,
|
| 751 |
+
'cyclic': CyclicAutomaton,
|
| 752 |
+
'dihedral': DihedralAutomaton,
|
| 753 |
+
'flipflop': FlipFlopAutomaton,
|
| 754 |
+
'gridworld': GridworldAutomaton,
|
| 755 |
+
'parity': ParityAutomaton,
|
| 756 |
+
'quaternion': QuaternionAutomaton,
|
| 757 |
+
'symmetric': SymmetricAutomaton,
|
| 758 |
+
'permutation_reset': PermutationResetAutomaton
|
| 759 |
+
# TODO: add Dyck
|
| 760 |
+
}
|