sequence.py
5.5 kB · python · 169 lines
1from mrlypy.core.state import randint, sample, bool, choice2import numpy as np3from mrlypy.core import formulas as f4from typing import Callable, List5from enums import Sequence6from models import Task78def evens_sequence(limit: int) -> List[int]:9 return [i for i in range(0, limit + 1, 2)]1011def odds_sequence(limit: int) -> List[int]:12 return [i for i in range(1, limit + 1, 2)]1314def random_sequence(limit: int) -> List[int]:15 options = list(range(0, limit + 1))16 count = randint(1, len(options))17 return sorted(sample(options, count))1819def prime_sequence(limit: int) -> List[int]:20 if limit < 2:21 return []22 return [2] + [i for i in range(3, limit + 1, 2) if all(i % j != 0 for j in range(3, int(i**0.5) + 1, 2))]2324def binary_sequence(limit: int) -> List[int]:25 sequence = []26 n = 027 while True:28 val = 2**n29 if val > limit:30 break31 sequence.append(val)32 n += 133 return sequence3435def fibonacci_sequence(limit: int) -> List[int]:36 sequence = []37 a, b = 0, 138 while a <= limit:39 sequence.append(a)40 a, b = b, a + b41 return sorted(list(set(sequence)))4243def mrly_sequence(limit: int, generator: Callable) -> List[int]:44 sequence = []45 number = 146 level = 147 while True:48 val = generator(number, level)49 if val > limit:50 break51 sequence.append(val)52 number += 253 return sorted(list(set(sequence)))5455def grid_squares_sequence(limit: int) -> List[int]:56 return mrly_sequence(limit, f.grid_squares)5758def carpet_fill_squares_sequence(limit: int) -> List[int]:59 return mrly_sequence(limit, f.carpet_fill_squares)6061def carpet_void_squares_sequence(limit: int) -> List[int]:62 return mrly_sequence(limit, f.carpet_void_squares)6364def net_fill_squares_sequence(limit: int) -> List[int]:65 return mrly_sequence(limit, f.net_fill_squares)6667def net_void_squares_sequence(limit: int) -> List[int]:68 return mrly_sequence(limit, f.net_void_squares)6970def tree_fill_squares_sequence(limit: int) -> List[int]:71 return mrly_sequence(limit, f.tree_fill_squares)7273def tree_void_squares_sequence(limit: int) -> List[int]:74 return mrly_sequence(limit, f.tree_void_squares)7576def void_fill_squares_sequence(limit: int) -> List[int]:77 return mrly_sequence(limit, f.void_fill_squares)7879def void_void_squares_sequence(limit: int) -> List[int]:80 return mrly_sequence(limit, f.void_void_squares)8182SEQUENCE_FACTORY = {83 Sequence.EVENS: evens_sequence,84 Sequence.ODDS: odds_sequence,85 Sequence.RANDOM: random_sequence,86 Sequence.PRIME: prime_sequence,87 Sequence.BINARY: binary_sequence,88 Sequence.FIBONACCI: fibonacci_sequence,89 Sequence.GRID_SQUARES: grid_squares_sequence,90 Sequence.CARPET_FILL_SQUARES: carpet_fill_squares_sequence,91 Sequence.CARPET_VOID_SQUARES: carpet_void_squares_sequence,92 Sequence.TREE_FILL_SQUARES: tree_fill_squares_sequence,93 Sequence.TREE_VOID_SQUARES: tree_void_squares_sequence,94 Sequence.NET_FILL_SQUARES: net_fill_squares_sequence,95 Sequence.NET_VOID_SQUARES: net_void_squares_sequence,96 Sequence.VOID_FILL_SQUARES: void_fill_squares_sequence,97 Sequence.VOID_VOID_SQUARES: void_void_squares_sequence,98}99100def create_sequence(task: Task) -> Task:101 print(f"Creating sequence for variation: {task.key}")102 if task.birth_counts and task.survive_counts:103 print(f"Birth counts: {task.birth_counts}")104 print(f"Survive counts: {task.survive_counts}")105 return task106 max_neighbors = int(np.sum(task.mask.cell.types))107 print(f"Max neighbors: {max_neighbors}")108 raw_birth = SEQUENCE_FACTORY[task.birth_sequence](max_neighbors)109 raw_survive = SEQUENCE_FACTORY[task.survive_sequence](max_neighbors)110 task.birth_counts = [111 x for x in raw_birth112 if (x != 0 or task.include_zeros) and (x != 1 or task.include_ones)113 ]114 print(f"Birth counts: {task.birth_counts[:10]}")115 task.survive_counts = [116 x for x in raw_survive117 if (x != 0 or task.include_zeros) and (x != 1 or task.include_ones)118 ]119 print(f"Survive counts: {task.survive_counts[:10]}")120 return task121122# PRIMARIES123124PRIMARIES = [125 Sequence.EVENS,126 Sequence.ODDS,127 Sequence.RANDOM,128 Sequence.PRIME,129 Sequence.BINARY,130 Sequence.FIBONACCI,131]132133SECONDARIES = [134 Sequence.GRID_SQUARES,135 Sequence.CARPET_FILL_SQUARES,136 Sequence.CARPET_VOID_SQUARES,137 Sequence.NET_FILL_SQUARES,138 Sequence.NET_VOID_SQUARES,139 Sequence.TREE_FILL_SQUARES,140 Sequence.TREE_VOID_SQUARES,141 Sequence.VOID_FILL_SQUARES,142 Sequence.VOID_VOID_SQUARES143]144145def setup_sequence(task: Task) -> Task:146 print(f"Setting up sequence for variation: {task.key}")147 if task.is_simple():148 sequences = PRIMARIES149 else:150 sequences = PRIMARIES + SECONDARIES151 sequences.remove(Sequence.RANDOM)152 print(f"Sequences: {[s.value for s in sequences]}")153 task.reflect = bool()154 print(f"Reflect: {task.reflect}")155 match task.reflect:156 case True:157 sequence = choice(sequences)158 print(f"Birth/Survive sequence: {sequence}")159 task.birth_sequence = sequence160 task.survive_sequence = sequence161 case False:162 task.birth_sequence, task.survive_sequence = sample(sequences, 2)163 print(f"Birth sequence: {task.birth_sequence}")164 print(f"Survive sequence: {task.survive_sequence}")165 task.include_zeros = bool()166 print(f"Include zeros: {task.include_zeros}")167 task.include_ones = bool()168 print(f"Include ones: {task.include_ones}")169 return task