From 21ed4aa91d0f1ac87ec684d8808e5ced552ad457 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fran=C3=A7ois=20Fleuret?= Date: Sat, 23 Mar 2024 23:11:34 +0100 Subject: [PATCH] Update. --- evasion.py | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ maze.py | 22 ++++++---- tasks.py | 1 - world.py | 2 +- 4 files changed, 133 insertions(+), 9 deletions(-) create mode 100755 evasion.py diff --git a/evasion.py b/evasion.py new file mode 100755 index 0000000..4efa4b3 --- /dev/null +++ b/evasion.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python + +import torch + +from torch.nn import functional as F + +###################################################################### + + +def generate_sequence(nb, height=6, width=6, T=10): + rnd = torch.rand(nb, height, width) + rnd[:, 0, :] = 0 + rnd[:, -1, :] = 0 + rnd[:, :, 0] = 0 + rnd[:, :, -1] = 0 + wall = 0 + + for k in range(3): + wall = wall + ( + rnd.flatten(1).argmax(dim=1)[:, None] + == torch.arange(rnd.flatten(1).size(1))[None, :] + ).long().reshape(rnd.size()) + rnd = rnd * (1 - wall.clamp(max=1)) + + seq = wall[:, None, :, :].expand(-1, T, -1, -1).clone() + + agent = torch.zeros(seq.size(), dtype=torch.int64) + agent[:, 0, 0, 0] = 1 + agent_actions = torch.randint(5, (nb, T)) + monster = torch.zeros(seq.size(), dtype=torch.int64) + monster[:, 0, -1, -1] = 1 + monster_actions = torch.randint(5, (nb, T)) + + all_moves = agent.new(nb, 5, height, width) + for t in range(T - 1): + all_moves.zero_() + all_moves[:, 0] = agent[:, t] + all_moves[:, 1, 1:, :] = agent[:, t, :-1, :] + all_moves[:, 2, :-1, :] = agent[:, t, 1:, :] + all_moves[:, 3, :, 1:] = agent[:, t, :, :-1] + all_moves[:, 4, :, :-1] = agent[:, t, :, 1:] + a = F.one_hot(agent_actions[:, t], num_classes=5)[:, :, None, None] + after_move = (all_moves * a).sum(dim=1) + collision = ( + (after_move * (1 - wall) * (1 - monster[:, t])) + .flatten(1) + .sum(dim=1)[:, None, None] + == 0 + ).long() + agent[:, t + 1] = collision * agent[:, t] + (1 - collision) * after_move + + all_moves.zero_() + all_moves[:, 0] = monster[:, t] + all_moves[:, 1, 1:, :] = monster[:, t, :-1, :] + all_moves[:, 2, :-1, :] = monster[:, t, 1:, :] + all_moves[:, 3, :, 1:] = monster[:, t, :, :-1] + all_moves[:, 4, :, :-1] = monster[:, t, :, 1:] + a = F.one_hot(monster_actions[:, t], num_classes=5)[:, :, None, None] + after_move = (all_moves * a).sum(dim=1) + collision = ( + (after_move * (1 - wall) * (1 - agent[:, t + 1])) + .flatten(1) + .sum(dim=1)[:, None, None] + == 0 + ).long() + monster[:, t + 1] = collision * monster[:, t] + (1 - collision) * after_move + + seq += 2 * agent + 3 * monster + + return seq, agent_actions + + +###################################################################### + + +def seq2str(seq, actions=None): + # symbols=" #@$" + symbols = " █@$" + + hline = ("+" + "-" * seq.size(-1)) * seq.size(1) + "+" + "\n" + + result = hline + + for n in range(seq.size(0)): + for i in range(seq.size(2)): + result += ( + "|" + + "|".join( + ["".join([symbols[v.item()] for v in row]) for row in seq[n, :, i]] + ) + + "|" + + "\n" + ) + + result += hline + + if actions is not None: + result += ( + "|" + + "|".join( + ["INESW"[a.item()] + " " * (seq.size(-1) - 1) for a in actions[n]] + ) + + "|" + + "\n" + ) + + result += hline + + return result + + +###################################################################### + +if __name__ == "__main__": + seq, actions = generate_sequence(40, 4, 6, T=20) + + print(seq2str(seq, actions)) diff --git a/maze.py b/maze.py index 8ac9fce..d5662f0 100755 --- a/maze.py +++ b/maze.py @@ -15,11 +15,11 @@ v_empty, v_wall, v_start, v_goal, v_path = 0, 1, 2, 3, 4 def create_maze(h=11, w=17, nb_walls=8): assert h % 2 == 1 and w % 2 == 1 - a, k = 0, 0 + nb_attempts, nb_added_walls = 0, 0 - while k < nb_walls: + while nb_added_walls < nb_walls: while True: - if a == 0: + if nb_attempts == 0: m = torch.zeros(h, w, dtype=torch.int64) m[0, :] = 1 m[-1, :] = 1 @@ -29,6 +29,7 @@ def create_maze(h=11, w=17, nb_walls=8): r = torch.rand(4) if r[0] <= 0.5: + # Add a vertical wall i1, i2, j = ( int((r[1] * h).item()), int((r[2] * h).item()), @@ -36,10 +37,14 @@ def create_maze(h=11, w=17, nb_walls=8): ) i1, i2, j = i1 - i1 % 2, i2 - i2 % 2, j - j % 2 i1, i2 = min(i1, i2), max(i1, i2) + + # If this wall does not hit another one, add it if i2 - i1 > 1 and i2 - i1 <= h / 2 and m[i1 : i2 + 1, j].sum() <= 1: m[i1 : i2 + 1, j] = 1 break + else: + # Add an horizontal wall i, j1, j2 = ( int((r[1] * h).item()), int((r[2] * w).item()), @@ -47,15 +52,18 @@ def create_maze(h=11, w=17, nb_walls=8): ) i, j1, j2 = i - i % 2, j1 - j1 % 2, j2 - j2 % 2 j1, j2 = min(j1, j2), max(j1, j2) + + # If this wall does not hit another one, add it if j2 - j1 > 1 and j2 - j1 <= w / 2 and m[i, j1 : j2 + 1].sum() <= 1: m[i, j1 : j2 + 1] = 1 break - a += 1 - if a > 10 * nb_walls: - a, k = 0, 0 + nb_attempts += 1 + + if nb_attempts > 10 * nb_walls: + nb_attempts, nb_added_walls = 0, 0 - k += 1 + nb_added_walls += 1 return m diff --git a/tasks.py b/tasks.py index d21e264..d680951 100755 --- a/tasks.py +++ b/tasks.py @@ -99,7 +99,6 @@ class TaskFromFile(Task): ).to("cpu") if shuffle: - print("SHUFFLING!") i = torch.randperm(input.size(0)) input = input[i].contiguous() pred_mask = pred_mask[i].contiguous() diff --git a/world.py b/world.py index aad0bfb..d95bddb 100755 --- a/world.py +++ b/world.py @@ -464,7 +464,7 @@ if __name__ == "__main__": frame2seq, seq2frame, ) = create_data_and_processors( - 25000, + 250, 1000, nb_epochs=5, mode="first_last", -- 2.39.5