X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=maze.py;h=d5662f0500e24dad299210a69efed402e3db2339;hb=HEAD;hp=81afcd94b7e12eedb0721887b6861de4bc7982bf;hpb=c921b95d0ea5b94a893447fbd4792e5047ba6e99;p=picoclvr.git diff --git a/maze.py b/maze.py index 81afcd9..d5662f0 100755 --- a/maze.py +++ b/maze.py @@ -13,11 +13,13 @@ 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): - a, k = 0, 0 + assert h % 2 == 1 and w % 2 == 1 - while k < nb_walls: + nb_attempts, nb_added_walls = 0, 0 + + 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 @@ -27,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()), @@ -34,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()), @@ -45,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 @@ -146,8 +156,16 @@ def mark_path(walls, i, j, goal_i, goal_j, policy): assert n < nmax +def path_optimality(ref_paths, paths): + return (ref_paths == v_path).long().flatten(1).sum(1) == ( + paths == v_path + ).long().flatten(1).sum(1) + + def path_correctness(mazes, paths): - still_ok = (mazes - (paths * (paths < 4))).view(mazes.size(0), -1).abs().sum(1) == 0 + still_ok = (mazes - (paths * (paths != v_path))).view(mazes.size(0), -1).abs().sum( + 1 + ) == 0 reached = still_ok.new_zeros(still_ok.size()) current, pred_current = paths.clone(), paths.new_zeros(paths.size()) goal = (mazes == v_goal).long() @@ -211,9 +229,8 @@ def save_image( mazes, target_paths=None, predicted_paths=None, - score_paths=None, - score_truth=None, path_correct=None, + path_optimal=None, ): colors = torch.tensor( [ @@ -231,17 +248,6 @@ def save_image( colors[mazes.reshape(-1)].reshape(mazes.size() + (-1,)).permute(0, 3, 1, 2) ) - if score_truth is not None: - score_truth = score_truth.cpu() - c_score_truth = score_truth.unsqueeze(1).expand(-1, 3, -1, -1) - c_score_truth = ( - c_score_truth * colors[4].reshape(1, 3, 1, 1) - + (1 - c_score_truth) * colors[0].reshape(1, 3, 1, 1) - ).long() - c_mazes = (mazes.unsqueeze(1) != v_empty) * c_mazes + ( - mazes.unsqueeze(1) == v_empty - ) * c_score_truth - imgs = c_mazes.unsqueeze(1) if target_paths is not None: @@ -264,28 +270,28 @@ def save_image( ) imgs = torch.cat((imgs, c_predicted_paths.unsqueeze(1)), 1) - if score_paths is not None: - score_paths = score_paths.cpu() - c_score_paths = score_paths.unsqueeze(1).expand(-1, 3, -1, -1) - c_score_paths = ( - c_score_paths * colors[4].reshape(1, 3, 1, 1) - + (1 - c_score_paths) * colors[0].reshape(1, 3, 1, 1) - ).long() - c_score_paths = c_score_paths * (mazes.unsqueeze(1) == v_empty) + c_mazes * ( - mazes.unsqueeze(1) != v_empty - ) - imgs = torch.cat((imgs, c_score_paths.unsqueeze(1)), 1) + img = torch.tensor([255, 255, 0]).view(1, -1, 1, 1) # NxKxCxHxW - if path_correct is None: - path_correct = torch.zeros(imgs.size(0)) <= 1 - path_correct = path_correct.cpu().long().view(-1, 1, 1, 1) - img = torch.tensor([224, 224, 224]).view(1, -1, 1, 1) * path_correct + torch.tensor( - [255, 0, 0] - ).view(1, -1, 1, 1) * (1 - path_correct) + if path_optimal is not None: + path_optimal = path_optimal.cpu().long().view(-1, 1, 1, 1) + img = ( + img * (1 - path_optimal) + + torch.tensor([0, 255, 0]).view(1, -1, 1, 1) * path_optimal + ) + + if path_correct is not None: + path_correct = path_correct.cpu().long().view(-1, 1, 1, 1) + img = img * path_correct + torch.tensor([255, 0, 0]).view(1, -1, 1, 1) * ( + 1 - path_correct + ) + img = img.expand( -1, -1, imgs.size(3) + 2, 1 + imgs.size(1) * (1 + imgs.size(4)) ).clone() + + print(f"{img.size()=} {imgs.size()=}") + for k in range(imgs.size(1)): img[ :, @@ -303,9 +309,9 @@ def save_image( if __name__ == "__main__": device = torch.device("cuda" if torch.cuda.is_available() else "cpu") - mazes, paths = create_maze_data(8) + mazes, paths, policies = create_maze_data(8) mazes, paths = mazes.to(device), paths.to(device) - save_image("test.png", mazes, paths, paths) + save_image("test.png", mazes=mazes, target_paths=paths, predicted_paths=paths) print(path_correctness(mazes, paths)) ######################################################################