X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=grids.py;h=03da16d93c46a713fe0b7f382b2407d4c5470c52;hb=719785dbea77989a54bf7592bb6919f2e8f3f6c5;hp=b40d5321aaf834adaba8c64c4c9427f5b0615651;hpb=47fcc6b14bf217e7b73e1b7f440fc6aaf862122b;p=culture.git diff --git a/grids.py b/grids.py index b40d532..03da16d 100755 --- a/grids.py +++ b/grids.py @@ -65,19 +65,6 @@ class Grids(problem.Problem): return x - def frame2img_(self, x, scale=15): - x = x.reshape(x.size(0), self.height, -1) - x = self.colors[x].permute(0, 3, 1, 2) - s = x.shape - x = x[:, :, :, None, :, None].expand(-1, -1, -1, scale, -1, scale) - x = x.reshape(s[0], s[1], s[2] * scale, s[3] * scale) - - x[:, :, :, torch.arange(0, x.size(3), scale)] = 0 - x[:, :, torch.arange(0, x.size(2), scale), :] = 0 - x = x[:, :, 1:, 1:] - - return x - def save_image( self, result_dir, @@ -594,34 +581,36 @@ class Grids(problem.Problem): def task_islands(self, A, f_A, B, f_B): for X, f_X in [(A, f_A), (B, f_B)]: + nb_on_border = 0 + for _ in range(10): + for k in torch.randperm(self.height * self.width): + i, j = k % self.height, k // self.height + border = ( + i == 0 or i == self.height - 1 or j == 0 or j == self.width - 1 + ) + no, nq, nq_diag = self.contact(X, i, j, 1) + + if ( + (nq > 0 and not border) + or (nq == 0 and border and nb_on_border < 4) + ) and nq_diag == 0: + X[i, j] = 1 + if border: + nb_on_border += 1 + while True: - i, j = torch.randint(self.height, (1,)), torch.randint(self.width, (1,)) - if ( - i == 0 - or i == self.height - 1 - or j == 0 - or j == self.width - 1 - or X[i, j] == 1 - ): - break - while True: - di, dj = torch.randint(3, (2,)) - 1 - if abs(di) + abs(dj) > 0: - break - X[i, j] = 1 - while True: - i, j = i + di, j + dj - if i < 0 or i >= self.height or j < 0 or j >= self.width: - break - b = ( - i == 0 - or i == self.height - 1 - or j == 0 - or j == self.width - 1 - or X[i, j] == 1 - ) - X[i, j] = 1 - if b: + nb_fixes = 0 + for i in range(1, self.height - 1): + for j in range(1, self.width - 1): + if ( + X[i, j] == 1 + and X[i - 1, j] + X[i + 1, j] + X[i, j - 1] + X[i, j + 1] + == 1 + ): + X[i, j] = 0 + nb_fixes += 1 + + if nb_fixes == 0: break ###################################################################### @@ -694,8 +683,8 @@ if __name__ == "__main__": grids = Grids() - for t in grids.all_tasks(): - # for t in [grids.task_islands]: + # for t in grids.all_tasks(): + for t in [grids.task_islands]: print(t.__name__) prompts, answers = grids.generate_prompts_and_answers(nb, tasks=[t]) grids.save_quizzes("/tmp", t.__name__, prompts[:nb], answers[:nb], nrow=4)