-
Notifications
You must be signed in to change notification settings - Fork 7
/
RandomSwaps.m
66 lines (51 loc) · 1.88 KB
/
RandomSwaps.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function [grid_permutation, obj_value] = RandomSwaps(image_dist_matrix, ...
grid_dist_matrix, ...
num_swaps, threshold)
if ~exist('threshold', 'var')
threshold = 0;
end
if ~exist('num_swaps', 'var')
num_swaps = 3e4;
end
n = size(image_dist_matrix, 1);
grid_permutation = 1:n;
% Calculate result for initial permutation
obj_value = EvaluateObjectiveFunc_internal(image_dist_matrix, grid_dist_matrix);
for ii = 1:num_swaps
% if score is below threshold, we terminate the optimization
if (obj_value < threshold)
break;
end
% Swap 2 indices
swap_indices = randi(n, 1, 2);
grid_dist_matrix = Swap2Indices_matrix(grid_dist_matrix, n, swap_indices);
grid_permutation = Swap2Indices_vector(grid_permutation, swap_indices);
current_result = EvaluateObjectiveFunc_internal(image_dist_matrix, grid_dist_matrix);
if (current_result > obj_value)
% Revert if result made things worst
grid_dist_matrix = Swap2Indices_matrix(grid_dist_matrix, n, swap_indices);
grid_permutation = Swap2Indices_vector(grid_permutation, swap_indices);
else
% Update best result
obj_value = current_result;
end
end
end
function [m_out] = Swap2Indices_matrix(m, num_elements, ind_pair)
indices = 1:num_elements;
indices(ind_pair(1)) = ind_pair(2);
indices(ind_pair(2)) = ind_pair(1);
m = m(indices, :);
m_out = m(:, indices);
end
function [v_out] = Swap2Indices_vector(v, ind_pair)
tmp = v(ind_pair(1));
v(ind_pair(1)) = v(ind_pair(2));
v(ind_pair(2)) = tmp;
v_out = v;
end
function [result, C] = EvaluateObjectiveFunc_internal(image_dist_matrix, grid_dist_matrix)
dist_images = squareform(image_dist_matrix);
dist_grid = squareform(grid_dist_matrix);
[result, C] = EvaluateObjectiveFunc(dist_images, dist_grid);
end