-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_sa_functionized.m
283 lines (235 loc) · 10.5 KB
/
test_sa_functionized.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
%---TEST SECTION-----------------------------------------------------------
clear; clc; close all;
fprintf('Reading files\n')
stlData = stlread('Mand-left-cut.stl');
mand = stlData.Points;
stlData1 = stlread('Pelvis-left-cut.stl');
pelvis = stlData1.Points;
fprintf('Start Simulated Anneling\n')
[new_fig, bd] = simulatedAnneling(mand,pelvis,[5,5],[50,10],[1,0.1],[1,0.1])
fprintf('Ploting')
figure
pelvis_fig = plot3(pelvis(:,1),pelvis(:,2),pelvis(:,3),'.');
hold on
plot3(new_fig(:,1),new_fig(:,2),new_fig(:,3),'.g')
title('simulated anneling')
%---FUNCTION---------------------------------------------------------------
function [end_mand, end_distances] = simulatedAnneling(mand,pelvis,number_of_restarts,startT,maxStep,maxRotation)
%Two Step Simulated Anneling with an inital rough approximation flowed by a
%fine tuning of sveral good inital results
%Parameters are: mand = smaller point colud, pelvis = biger point cloud,
%number_of_restarts = number of generated results array[1x2], startTemp = Inital
%Temperature array[1x2], maxStep = maximal Step size array [1x2], maxRotation = maximal number of
%roations array[1x2]
%% Simulated Annealing algorithm
% Initialize paramters Rotation matrix to unit matrix and translation vector
% to zero vector
alpha = 0;
beta = 0;
gamma = 0;
xt = 0;
yt = 0;
zt = 0;
parameters_best = [alpha, beta, gamma, xt, yt, zt];
parameters_current = parameters_best;
% Create matrix to remember rejected solutions to get a shorter running
% time, since the calculation of the (modified) hausdorff distance is
% relatively time consuming and add the inital parameters
% rejected = parameters_current;
%%
% Use hausdorff distance of the inital positions as initial best value
fprintf('Initial distance calculation: ')
tic
distance_best = directed_averaged_hausdorff_distance(mand, pelvis, 10);
toc
%%
% Calculate boundaries for the solution space
x_max = max(pelvis(:,1));
x_min = min(pelvis(:,1));
y_max = max(pelvis(:,2));
y_min = min(pelvis(:,2));
z_max = max(pelvis(:,3));
z_min = min(pelvis(:,3));
% Set starting temperature for the outer loop, the max stepsize and the max
% rotation
% number_of_restarts = 5;
% startT = 50;
% maxStep = 1;
% maxRotation = 1;
best_positions = [];
best_distances = [];
fprintf('Find inital Solutions\n')
%initial solution
for restarts=1:number_of_restarts(1)
fprintf(['Working on ',num2str(restarts),' restart from ',num2str(number_of_restarts(1)),'.\n'])
for T=startT(1):-1:1
for v=1:5
% randomly update parameters for rotation
parameters_current(1) = parameters_best(1) + (rand-0.5)*2*maxRotation(1)*T/startT(1);
parameters_current(2) = parameters_best(2) + (rand-0.5)*2*maxRotation(1)*T/startT(1);
parameters_current(3) = parameters_best(3) + (rand-0.5)*2*maxRotation(1)*T/startT(1);
% randomly update parameters for translation
parameters_current(4) = parameters_best(4) + (rand-0.5)*2*maxStep(1)*T/startT(1);
parameters_current(5) = parameters_best(5) + (rand-0.5)*2*maxStep(1)*T/startT(1);
parameters_current(6) = parameters_best(6) + (rand-0.5)*2*maxStep(1)*T/startT(1);
% transform the mand matrix
mand_current = transformation(parameters_current, mand);
% update the parameters as long as we are not in the solution space
% or are already in the rejected parameters
while (max(mand_current(:,1)) > x_max+10 || min(mand_current(:,1)) < x_min-10 || ...
max(mand_current(:,2)) > y_max+10 || min(mand_current(:,2)) < y_min-10 || ...
max(mand_current(:,3)) > z_max+10 || min(mand_current(:,3)) < z_min-10)
% randomly update parameters for rotation
parameters_current(1) = parameters_best(1) + (rand-0.5)*2*maxRotation(1)*T/startT(1);
parameters_current(2) = parameters_best(2) + (rand-0.5)*2*maxRotation(1)*T/startT(1);
parameters_current(3) = parameters_best(3) + (rand-0.5)*2*maxRotation(1)*T/startT(1);
% randomly update parameters for translation
parameters_current(4) = parameters_best(4) + (rand-0.5)*2*maxStep(1)*T/startT(1);
parameters_current(5) = parameters_best(5) + (rand-0.5)*2*maxStep(1)*T/startT(1);
parameters_current(6) = parameters_best(6) + (rand-0.5)*2*maxStep(1)*T/startT(1);
% transform the mand matrix
mand_current = transformation(parameters_current, mand);
% check if parameters were already rejected
%tf = ismember(parameters_current, rejected, 'rows');
end
% calculated the (modified) hausdorff distance for the transformed
% mand matrix
distance_current = directed_averaged_hausdorff_distance(mand_current, pelvis,10);
difference = distance_current - distance_best;
% if the new distance is smaller than the last distance accept the
% solution
if difference < 0
parameters_best = parameters_current;
distance_best = distance_current;
% else if the new distance is not smaller than the last distance
% accept the solution with a random probability
elseif (exp((-difference*300)/T) > rand)
parameters_best = parameters_current;
distance_best = distance_current;
fprintf('Works')
end
fprintf(['Temperature: ',num2str(T)])
end
end
best_positions = [best_positions; parameters_best];
best_distances = [best_distances; distance_best];
end
%angeben der besten distanzen ()
%%
%fine tuning
fprintf('Fine tuning for final Solutions\n')
end_mand = {};
end_distances = [];
for i=1:number_of_restarts(2)
mand_new = transformation(best_positions(i,:), mand);
% figure,
% plot3(mand_new(:,1),mand_new(:,2),mand_new(:,3),'.')
% hold on
% plot3(pelvis(:,1),pelvis(:,2),pelvis(:,3),'k.');
% title('Fine Tuning')
% Set starting temperature for the outer loop, the max stepsize and the max
% rotation
alpha = 0;
beta = 0;
gamma = 0;
xt = 0;
yt = 0;
zt = 0;
parameters_best = [alpha, beta, gamma, xt, yt, zt];
parameters_current = parameters_best;
% startT = 10;
% maxStep = 0.1;
% maxRotation = 0.1;
fprintf(['Working on ',num2str(restarts),' restart from ',num2str(number_of_restarts(2)),'.\n'])
for T=startT(2):-1:1
for v=1:5
% randomly update parameters for rotation
parameters_current(1) = parameters_best(1) + (rand-0.5)*2*maxRotation(2)*T/startT(2);
parameters_current(2) = parameters_best(2) + (rand-0.5)*2*maxRotation(2)*T/startT(2);
parameters_current(3) = parameters_best(3) + (rand-0.5)*2*maxRotation(2)*T/startT(2);
% randomly update parameters for translation
parameters_current(4) = parameters_best(4) + (rand-0.5)*2*maxStep(2)*T/startT(2);
parameters_current(5) = parameters_best(5) + (rand-0.5)*2*maxStep(2)*T/startT(2);
parameters_current(6) = parameters_best(6) + (rand-0.5)*2*maxStep(2)*T/startT(2);
% transform the mand matrix
mand_current = transformation(parameters_current, mand_new);
% update the parameters as long as we are not in the solution space
% or are already in the rejected parameters
while (max(mand_current(:,1)) > x_max || min(mand_current(:,1)) < x_min || ...
max(mand_current(:,2)) > y_max || min(mand_current(:,2)) < y_min || ...
max(mand_current(:,3)) > z_max || min(mand_current(:,3)) < z_min)
% randomly update parameters for rotation
parameters_current(1) = parameters_best(1) + (rand-0.5)*2*maxRotation(2)*T/startT(2);
parameters_current(2) = parameters_best(2) + (rand-0.5)*2*maxRotation(2)*T/startT(2);
parameters_current(3) = parameters_best(3) + (rand-0.5)*2*maxRotation(2)*T/startT(2);
% randomly update parameters for translation
parameters_current(4) = parameters_best(4) + (rand-0.5)*2*maxStep(2)*T/startT(2);
parameters_current(5) = parameters_best(5) + (rand-0.5)*2*maxStep(2)*T/startT(2);
parameters_current(6) = parameters_best(6) + (rand-0.5)*2*maxStep(2)*T/startT(2);
% transform the mand matrix
mand_current = transformation(parameters_current, mand_new);
end
% calculated the (modified) hausdorff distance for the transformed
% mand matrix
distance_current = directed_averaged_hausdorff_distance(mand_current, pelvis,5);
difference = distance_current - distance_best;
% if the new distance is smaller than the last distance accept the
% solution
if difference < 0
parameters_best = parameters_current;
distance_best = distance_current;
% else if the new distance is not smaller than the last distance
% accept the solution with a random probability
elseif (exp((-difference*300)/T) > rand)
parameters_best = parameters_current;
distance_best = distance_current;
end
end
% plot3(mand_current(:,1),mand_current(:,2),mand_current(:,3),'.')
% drawnow
fprintf(['Temperature: ',num2str(T)])
end
end_mand{i} = transformation(parameters_best, mand_new);
end_distances = [end_distances; distance_best];
end
end
%
% %--------------------NICHT HINZUFüGEN SOLLTE SCHON VORHANDEN SEIN----------
% function [X_new] = transformation(parameters, X)
% %Transform a point cloud in 3D
% alpha = parameters(1);
% beta = parameters(2);
% gamma = parameters(3);
% xt = parameters(4);
% yt = parameters(5);
% zt = parameters(6);
% r11 = cos(alpha)*cos(beta);
% r12 = cos(alpha)*sin(beta)*sin(gamma)-sin(alpha)*cos(gamma);
% r13 = cos(alpha)*sin(beta)*cos(gamma)+sin(alpha)*sin(gamma);
% r14 = xt;
%
% r21 = sin(alpha)*cos(beta);
% r22 = sin(alpha)*sin(beta)*sin(gamma)+cos(alpha)*cos(gamma);
% r23 = sin(alpha)*sin(beta)*cos(gamma)-cos(alpha)*sin(gamma);
% r24 = yt;
%
% r31 = -sin(beta);
% r32 = cos(beta)*sin(gamma);
% r33 = cos(beta)*cos(gamma);
% r34 = zt;
%
% r41 = 0;
% r42 = 0;
% r43 = 0;
% r44 = 1;
%
% T = [r11, r12, r13, r14;...
% r21, r22, r23, r24;...
% r31, r32, r33, r34;...
% r41, r42, r43, r44];
%
% extension = ones(length(X),1);
% X_ext = horzcat(X, extension);
% X_new_ext = T*X_ext';
% X_new = X_new_ext(1:3,:)';
% end