X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=wireworld.py;h=65b12adda45e27ef20d3b4d646f092f663b37356;hb=db8c21397d370ae16fd6078858c649e2ab14fe4e;hp=aff236d799ed516a1d6e9c1027a78b8968b58f5b;hpb=29679cb42710602037fee650a5672f01a3292077;p=culture.git diff --git a/wireworld.py b/wireworld.py index aff236d..65b12ad 100755 --- a/wireworld.py +++ b/wireworld.py @@ -52,10 +52,20 @@ class Wireworld(problem.Problem): return self.token_forward, self.token_backward def generate_frame_sequences(self, nb): + result = [] + N = 100 + for _ in tqdm.tqdm( + range(0, nb + N, N), dynamic_ncols=True, desc="world generation" + ): + result.append(self.generate_frame_sequences_hard(100)) + return torch.cat(result, dim=0)[:nb] + + def generate_frame_sequences_hard(self, nb): frame_sequences = [] result = torch.full( - (nb * 4, self.nb_iterations, self.height, self.width), self.token_empty + (nb * 4, self.nb_iterations * self.speed, self.height, self.width), + self.token_empty, ) for n in range(result.size(0)): @@ -68,10 +78,45 @@ class Wireworld(problem.Problem): while True: if i < 0 or i >= self.height or j < 0 or j >= self.width: break + o = 0 + if i > 0: + o += (result[n, 0, i - 1, j] == self.token_conductor).long() + if i < self.height - 1: + o += (result[n, 0, i + 1, j] == self.token_conductor).long() + if j > 0: + o += (result[n, 0, i, j - 1] == self.token_conductor).long() + if j < self.width - 1: + o += (result[n, 0, i, j + 1] == self.token_conductor).long() + if o > 1: + break result[n, 0, i, j] = self.token_conductor i += vi j += vj - if torch.rand(1) < 0.5: + if ( + result[n, 0] == self.token_conductor + ).long().sum() > self.width and torch.rand(1) < 0.5: + break + + while True: + for _ in range(self.height * self.width): + i = torch.randint(self.height, (1,)) + j = torch.randint(self.width, (1,)) + v = torch.randint(2, (2,)) + vi = v[0] * (v[1] * 2 - 1) + vj = (1 - v[0]) * (v[1] * 2 - 1) + if ( + i + vi >= 0 + and i + vi < self.height + and j + vj >= 0 + and j + vj < self.width + and result[n, 0, i, j] == self.token_conductor + and result[n, 0, i + vi, j + vj] == self.token_conductor + ): + result[n, 0, i, j] = self.token_head + result[n, 0, i + vi, j + vj] = self.token_tail + break + + if torch.rand(1) < 0.75: break weight = torch.full((1, 1, 3, 3), 1.0) @@ -109,12 +154,15 @@ class Wireworld(problem.Problem): ) ) - i = (result[:, -1] == self.token_head).flatten(1).max(dim=1).values > 0 - result = result[ - torch.arange(self.nb_iterations, device=result.device) * self.speed + :, torch.arange(self.nb_iterations, device=result.device) * self.speed ] + i = (result[:, -1] == self.token_head).flatten(1).max(dim=1).values > 0 + result = result[i] + + # print(f"{result.size(0)=} {nb=}") + if result.size(0) < nb: # print(result.size(0)) result = torch.cat( @@ -266,7 +314,7 @@ class Wireworld(problem.Problem): if __name__ == "__main__": import time - wireworld = Wireworld(height=10, width=15, nb_iterations=2, speed=1) + wireworld = Wireworld(height=8, width=10, nb_iterations=5, speed=1) start_time = time.perf_counter() frame_sequences = wireworld.generate_frame_sequences(nb=96) @@ -275,19 +323,20 @@ if __name__ == "__main__": # print(wireworld.seq2str(seq[:4])) - # for t in range(frame_sequences.size(1)): - # img = wireworld.seq2img(frame_sequences[:, t]) - # torchvision.utils.save_image( - # img.float() / 255.0, - # f"/tmp/frame_{t:03d}.png", - # nrow=8, - # padding=6, - # pad_value=0, - # ) + for t in range(frame_sequences.size(1)): + img = wireworld.seq2img(frame_sequences[:, t]) + torchvision.utils.save_image( + img.float() / 255.0, + f"/tmp/frame_{t:03d}.png", + nrow=8, + padding=6, + pad_value=0, + ) # m = (torch.rand(seq.size()) < 0.05).long() # seq = (1 - m) * seq + m * 23 + wireworld = Wireworld(height=8, width=10, nb_iterations=2, speed=5) token_sequences = wireworld.generate_token_sequences(32) wireworld.save_quizzes(token_sequences, "/tmp", "seq") # img = wireworld.seq2img(frame_sequences[:60])