X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=maze.py;h=36eef25540b5b52a9b5688c12573b1bcd03bf6f2;hb=29cd6ffe24dfbc5720efe8b123ec1973d868881a;hp=d11ab6ef177fbef75dcd354e38da45e4df4f717f;hpb=39e24a2f9076db2d512791e723e7f2dc0275d99c;p=beaver.git diff --git a/maze.py b/maze.py index d11ab6e..36eef25 100755 --- a/maze.py +++ b/maze.py @@ -61,11 +61,11 @@ def create_maze(h=11, w=17, nb_walls=8): ###################################################################### -def compute_distance(walls, i, j): +def compute_distance(walls, goal_i, goal_j): max_length = walls.numel() dist = torch.full_like(walls, max_length) - dist[i, j] = 0 + dist[goal_i, goal_j] = 0 pred_dist = torch.empty_like(dist) while True: @@ -93,8 +93,8 @@ def compute_distance(walls, i, j): ###################################################################### -def compute_policy(walls, i, j): - distance = compute_distance(walls, i, j) +def compute_policy(walls, goal_i, goal_j): + distance = compute_distance(walls, goal_i, goal_j) distance = distance + walls.numel() * walls value = distance.new_full((4,) + distance.size(), walls.numel()) @@ -110,6 +110,21 @@ def compute_policy(walls, i, j): return proba +def stationary_density(policy, start_i, start_j): + probas = policy.new_zeros(policy.size()[:-1]) + pred_probas = probas.clone() + probas[start_i, start_j] = 1.0 + + while not pred_probas.equal(probas): + pred_probas.copy_(probas) + probas.zero_() + probas[1:, :] = pred_probas[:-1, :] * policy[0, :-1, :] + probas[:-1, :] = pred_probas[1:, :] * policy[1, 1:, :] + probas[:, 1:] = pred_probas[:, :-1] * policy[2, :, :-1] + probas[:, :-1] = pred_probas[:, 1:] * policy[3, :, 1:] + probas[start_i, start_j] = 1.0 + + ###################################################################### @@ -158,11 +173,11 @@ def create_maze_data( ): mazes = torch.empty(nb, height, width, dtype=torch.int64) paths = torch.empty(nb, height, width, dtype=torch.int64) - policies = torch.empty(nb, 4, height, width, dtype=torch.int64) + policies = torch.empty(nb, 4, height, width) for n in progress_bar(range(nb)): maze = create_maze(height, width, nb_walls) - i = (1 - maze).nonzero() + i = (maze == v_empty).nonzero() while True: start, goal = i[torch.randperm(i.size(0))[:2]] if (start - goal).abs().sum() >= dist_min: @@ -187,35 +202,63 @@ def create_maze_data( ###################################################################### -def save_image(name, mazes, target_paths, predicted_paths=None, path_correct=None): - mazes, target_paths = mazes.cpu(), target_paths.cpu() - +def save_image( + name, + mazes, + target_paths=None, + predicted_paths=None, + score_paths=None, + path_correct=None, +): colors = torch.tensor( [ [255, 255, 255], # empty [0, 0, 0], # wall [0, 255, 0], # start - [0, 0, 255], # goal + [127, 127, 255], # goal [255, 0, 0], # path ] ) - mazes = colors[mazes.reshape(-1)].reshape(mazes.size() + (-1,)).permute(0, 3, 1, 2) - target_paths = ( - colors[target_paths.reshape(-1)] - .reshape(target_paths.size() + (-1,)) - .permute(0, 3, 1, 2) + mazes = mazes.cpu() + + c_mazes = ( + colors[mazes.reshape(-1)].reshape(mazes.size() + (-1,)).permute(0, 3, 1, 2) ) - imgs = torch.cat((mazes.unsqueeze(1), target_paths.unsqueeze(1)), 1) + + imgs = c_mazes.unsqueeze(1) + + if target_paths is not None: + target_paths = target_paths.cpu() + + c_target_paths = ( + colors[target_paths.reshape(-1)] + .reshape(target_paths.size() + (-1,)) + .permute(0, 3, 1, 2) + ) + + imgs = torch.cat((imgs, c_target_paths.unsqueeze(1)), 1) if predicted_paths is not None: predicted_paths = predicted_paths.cpu() - predicted_paths = ( + c_predicted_paths = ( colors[predicted_paths.reshape(-1)] .reshape(predicted_paths.size() + (-1,)) .permute(0, 3, 1, 2) ) - imgs = torch.cat((imgs, predicted_paths.unsqueeze(1)), 1) + 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) # NxKxCxHxW if path_correct is None: