X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=grids.py;h=eea8c6c7440c0a6513b20d74259877ed0f207800;hb=5f5c6c079c2751a76887444c211c5c464e875ed0;hp=aa21543f849a927c41871c34897090693e9eeb32;hpb=a2e623102ecd20491a8ba89bd119bffa0b34da1f;p=culture.git diff --git a/grids.py b/grids.py index aa21543..eea8c6c 100755 --- a/grids.py +++ b/grids.py @@ -143,7 +143,7 @@ class Grids(problem.Problem): self.task_scale, self.task_symbols, self.task_isometry, - # self.task_path, + # self.task_islands, ] if tasks is None: @@ -628,8 +628,8 @@ class Grids(problem.Problem): 1000, self.height, self.width, - nb_seeds=self.height * self.width // 9, - nb_iterations=self.height * self.width // 20, + nb_seeds=self.height * self.width // 8, + nb_iterations=self.height * self.width // 10, ) ) @@ -877,7 +877,7 @@ class Grids(problem.Problem): ): break - def compute_distance(self, walls, goal_i, goal_j, start_i, start_j): + def compute_distance(self, walls, goal_i, goal_j): max_length = walls.numel() dist = torch.full_like(walls, max_length) @@ -886,9 +886,10 @@ class Grids(problem.Problem): while True: pred_dist.copy_(dist) - d = ( + dist[1:-1, 1:-1] = ( torch.cat( ( + dist[None, 1:-1, 1:-1], dist[None, 1:-1, 0:-2], dist[None, 2:, 1:-1], dist[None, 1:-1, 2:], @@ -899,16 +900,16 @@ class Grids(problem.Problem): + 1 ) - dist[1:-1, 1:-1].minimum_(d) # = torch.min(dist[1:-1, 1:-1], d) dist = walls * max_length + (1 - walls) * dist - if dist[start_i, start_j] < max_length or dist.equal(pred_dist): + if dist.equal(pred_dist): return dist * (1 - walls) # @torch.compile - def task_path(self, A, f_A, B, f_B): + def task_distance(self, A, f_A, B, f_B): c = torch.randperm(len(self.colors) - 1)[:3] + 1 - dist = torch.empty(self.height + 2, self.width + 2) + dist0 = torch.empty(self.height + 2, self.width + 2) + dist1 = torch.empty(self.height + 2, self.width + 2) for X, f_X in [(A, f_A), (B, f_B)]: nb_rec = torch.randint(3, (1,)).item() + 1 while True: @@ -933,43 +934,31 @@ class Grids(problem.Problem): ) if X[i1, j1] == 0: break - dist[...] = 1 - dist[1:-1, 1:-1] = (X != 0).long() - dist[...] = self.compute_distance(dist, i1 + 1, j1 + 1, i0 + 1, j0 + 1) - if dist[i0 + 1, j0 + 1] >= 1 and dist[i0 + 1, j0 + 1] < self.height * 4: + dist1[...] = 1 + dist1[1:-1, 1:-1] = (X != 0).long() + dist1[...] = self.compute_distance(dist1, i1 + 1, j1 + 1) + if ( + dist1[i0 + 1, j0 + 1] >= 1 + and dist1[i0 + 1, j0 + 1] < self.height * 4 + ): break - dist[1:-1, 1:-1] += (X != 0).long() * self.height * self.width - dist[0, :] = self.height * self.width - dist[-1, :] = self.height * self.width - dist[:, 0] = self.height * self.width - dist[:, -1] = self.height * self.width - # dist += torch.rand(dist.size()) - - i, j = i0 + 1, j0 + 1 - while i != i1 + 1 or j != j1 + 1: - f_X[i - 1, j - 1] = c[2] - r, s, t, u = ( - dist[i - 1, j], - dist[i, j - 1], - dist[i + 1, j], - dist[i, j + 1], - ) - m = min(r, s, t, u) - if r == m: - i = i - 1 - elif t == m: - i = i + 1 - elif s == m: - j = j - 1 - else: - j = j + 1 + dist0[...] = 1 + dist0[1:-1, 1:-1] = (X != 0).long() + dist0[...] = self.compute_distance(dist0, i0 + 1, j0 + 1) - X[i0, j0] = c[2] - # f_X[i0, j0] = c[1] + dist0 = dist0[1:-1, 1:-1] + dist1 = dist1[1:-1, 1:-1] + + D = dist1[i0, j0] + for d in range(1, D): + M = (dist0 == d) & (dist1 == D - d) + f_X[...] = (1 - M) * f_X + M * c[1] - X[i1, j1] = c[1] - f_X[i1, j1] = c[1] + X[i0, j0] = c[2] + f_X[i0, j0] = c[2] + X[i1, j1] = c[2] + f_X[i1, j1] = c[2] # for X, f_X in [(A, f_A), (B, f_B)]: # n = torch.arange(self.height * self.width).reshape(self.height, self.width) @@ -1051,65 +1040,30 @@ class Grids(problem.Problem): def task_islands(self, A, f_A, B, f_B): c = torch.randperm(len(self.colors) - 1)[:2] + 1 for X, f_X in [(A, f_A), (B, f_B)]: - while True: - k = torch.randperm(self.height * self.width) - Z = torch.zeros(self.height + 2, self.width + 2) - - i0, j0 = ( - torch.randint(self.height, (1,)).item() + 1, - torch.randint(self.width, (1,)).item() + 1, + if not hasattr(self, "cache_islands") or len(self.cache_islands) == 0: + self.cache_islands = list( + grow_islands( + 1000, + self.height, + self.width, + nb_seeds=self.height * self.width // 20, + nb_iterations=self.height * self.width // 2, + ) ) - Z[i0 - 1 : i0 + 2, j0 - 1 : j0 + 2] = 1 - - nb = 9 - - for q in k: - i, j = q % self.height + 1, q // self.height + 1 - - if Z[i, j] == 0: - r, s, t, u, v, w, x, y = ( - Z[i - 1, j], - Z[i - 1, j + 1], - Z[i, j + 1], - Z[i + 1, j + 1], - Z[i + 1, j], - Z[i + 1, j - 1], - Z[i, j - 1], - Z[i - 1, j - 1], - ) + A = self.cache_islands.pop() - if ( - (nb < 16 or r + s + t + u + v + w + x + y > 0) - and (s == 0 or r + t > 0) - and (u == 0 or t + v > 0) - and (w == 0 or x + v > 0) - and (y == 0 or x + r > 0) - ): - # if r+s+t+u+v+w+x+y==0: - Z[i, j] = 1 - nb += 1 - - if nb == self.height * self.width // 2: - break - - if nb == self.height * self.width // 2: - break - - M = Z.clone() - Z[i0, j0] = 2 - X[...] = (Z[1:-1, 1:-1] == 1) * c[0] + (Z[1:-1, 1:-1] == 2) * c[1] - - for _ in range(self.height + self.width): - Z[1:-1, 1:-1] = Z[1:-1, 1:-1].maximum( - torch.maximum( - torch.maximum(Z[0:-2, 1:-1], Z[2:, 1:-1]), - torch.maximum(Z[1:-1, 0:-2], Z[1:-1, 2:]), - ) + while True: + i, j = ( + torch.randint(self.height // 2, (1,)).item(), + torch.randint(self.width // 2, (1,)).item(), ) - Z *= M + if A[i, j] > 0: + break - f_X[...] = (Z[1:-1, 1:-1] == 1) * c[0] + (Z[1:-1, 1:-1] == 2) * c[1] + X[...] = (A > 0) * c[0] + X[i, j] = c[1] + f_X[...] = (A == A[i, j]) * c[1] + ((A > 0) & (A != A[i, j])) * c[0] ###################################################################### @@ -1201,7 +1155,7 @@ if __name__ == "__main__": # nb, nrow = 8, 2 # for t in grids.all_tasks: - for t in [grids.task_count]: + for t in [grids.task_distance]: print(t.__name__) prompts, answers = grids.generate_prompts_and_answers_(nb, tasks=[t]) grids.save_quiz_illustrations( @@ -1213,7 +1167,7 @@ if __name__ == "__main__": nb = 1000 # for t in grids.all_tasks: - for t in [grids.task_count]: + for t in [grids.task_distance]: start_time = time.perf_counter() prompts, answers = grids.generate_prompts_and_answers_(nb, tasks=[t]) delay = time.perf_counter() - start_time