-
Notifications
You must be signed in to change notification settings - Fork 3
/
two_sept_simulated_anneling.m
285 lines (236 loc) · 12 KB
/
two_sept_simulated_anneling.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
284
285
%% Setup reading files and creating point clouds
clear;clc;close all;
% reading stl files
stlData = stlread('Mand-left-cut.stl');
mand = stlData.Points;
stlData1 = stlread('Pelvis-left-cut.stl');
pelvis = stlData1.Points;
% Initial overview 3D plots of both stl objects
figure
mand_fig = plot3(mand(:,1),mand(:,2),mand(:,3),'.');
title('mand')
figure
pelvis_fig = plot3(pelvis(:,1),pelvis(:,2),pelvis(:,3),'.');
title('pelvis')
% updating mand position, the mand point cloud is moved to the center of
% gravity of the pelvis point cloud
mand = move(mand,pelvis);
%plot of both in one figure
figure
% 3d plot of both parts intially
plot3(mand(:,1),mand(:,2),mand(:,3),'m.')
hold on
plot3(pelvis(:,1),pelvis(:,2),pelvis(:,3),'k.');
% xlabel('x')
% ylabel('y')
% zlabel('z')
title('Trial')
drawnow
%% 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 Alignment: ')
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
startT = 100;
maxStep = 5;
maxRotation = 1;
store_attempts = 5;
%initialization of later potential attempts
potential_attempts = rand(store_attempts,7)*1000; % 1 = distance, 2-7 = parameters of transitionmatrix
while distance_best > 5*10^(-1)
tic
fprintf('Start Simulated Anneling: Quick Part\n')
for T=startT:-1:1
for v=1:5
% randomly update parameters for rotation
parameters_current(1) = parameters_best(1) + (rand-0.5)*2*maxRotation*T/startT;
parameters_current(2) = parameters_best(2) + (rand-0.5)*2*maxRotation*T/startT;
parameters_current(3) = parameters_best(3) + (rand-0.5)*2*maxRotation*T/startT;
% randomly update parameters for translation
parameters_current(4) = parameters_best(4) + (rand-0.5)*2*maxStep*T/startT;
parameters_current(5) = parameters_best(5) + (rand-0.5)*2*maxStep*T/startT;
parameters_current(6) = parameters_best(6) + (rand-0.5)*2*maxStep*T/startT;
% transform the mand matrix
mand_current = transformation(parameters_current, mand);
% check if parameters were already rejected
tf = ismember(parameters_current, rejected, 'rows');
% 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+5 || min(mand_current(:,1)) < x_min-5 || ...
max(mand_current(:,2)) > y_max+5 || min(mand_current(:,2)) < y_min-5 || ...
max(mand_current(:,3)) > z_max+5 || min(mand_current(:,3)) < z_min-5 || ...
tf)
% record rejected parameters
rejected = [rejected; parameters_current];
% randomly update parameters for rotation
parameters_current(1) = parameters_best(1) + (rand-0.5)*2*maxRotation*T/startT;
parameters_current(2) = parameters_best(2) + (rand-0.5)*2*maxRotation*T/startT;
parameters_current(3) = parameters_best(3) + (rand-0.5)*2*maxRotation*T/startT;
% randomly update parameters for translation
parameters_current(4) = parameters_best(4) + (rand-0.5)*2*maxStep*T/startT;
parameters_current(5) = parameters_best(5) + (rand-0.5)*2*maxStep*T/startT;
parameters_current(6) = parameters_best(6) + (rand-0.5)*2*maxStep*T/startT;
% 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*50)/T) > rand)
p = exp((-difference*50)/T)
parameters_best = parameters_current;
distance_best = distance_current;
end
rejected = [rejected; parameters_current];
end
plot3(mand_current(:,1),mand_current(:,2),mand_current(:,3),'.')
T
distance_best
if distance_current < min(potential_attempts(:,1))
%maximum anpasse in matrix
idx = potential_attempts == max(potential_attempts(:,1)); % get index of max distance and replace row afterwards
index = find(idx);
potential_attempts(index,1) = distance_current;
potential_attempts(index,2) = parameters_current(1);
potential_attempts(index,3) = parameters_current(2);
potential_attempts(index,4) = parameters_current(3);
potential_attempts(index,5) = parameters_current(4);
potential_attempts(index,6) = parameters_current(5);
potential_attempts(index,7) = parameters_current(6);
end
% parameters_best
% parameters_current
drawnow
end
toc
% fprintf(num2str(store_attempts)+' best where estimated\n\n')
end
%% Ploting n best Results after initial plot
fprintf('Ploting Inital Potential Results\n\n')
figure
plot3(pelvis(:,1),pelvis(:,2),pelvis(:,3),'k.');
hold on
title('Fit after initial rough optimization')
for i = 1:size(potential_attempts,1)
if potential_attempts(i,1) < 1
mand_current = transformation(potential_attempts(i,2:7), mand);
plot3(mand_current(:,1),mand_current(:,2),mand_current(:,3),'.')
drawnow
end
end
%% refinement of the preselected attempts
figure
plot3(pelvis(:,1),pelvis(:,2),pelvis(:,3),'k.');
hold on
title('Refinement')
for i = 1:size(potential_attempts,1)
parameters_current = potential_attempts(i,2:7)
if potential_attempts(i,1) < 1
while distance_best > 5*10^(-3)
tic
fprintf('Start Simulated Anneling: Quick Part\n')
for T=startT:-1:1
for v=1:5
% randomly update parameters for rotation
parameters_current(1) = potential_attempts(i,2) + (rand-0.5)*2*maxRotation*T/startT;
parameters_current(2) = potential_attempts(i,3) + (rand-0.5)*2*maxRotation*T/startT;
parameters_current(3) = potential_attempts(i,4) + (rand-0.5)*2*maxRotation*T/startT;
% % randomly update parameters for translation
% parameters_current(4) = parameters_best(4) + (rand-0.5)*2*maxStep*T/startT;
% parameters_current(5) = parameters_best(5) + (rand-0.5)*2*maxStep*T/startT;
% parameters_current(6) = parameters_best(6) + (rand-0.5)*2*maxStep*T/startT;
% transform the mand matrix
mand_current = transformation(parameters_current, mand);
% check if parameters were already rejected
tf = ismember(parameters_current, rejected, 'rows');
% 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+5 || min(mand_current(:,1)) < x_min-5 || ...
max(mand_current(:,2)) > y_max+5 || min(mand_current(:,2)) < y_min-5 || ...
max(mand_current(:,3)) > z_max+5 || min(mand_current(:,3)) < z_min-5 || ...
tf) %maxStep einsetzen?
% record rejected parameters
rejected = [rejected; parameters_current];
% randomly update parameters for rotation
parameters_current(1) = parameters_best(1) + (rand-0.5)*2*maxRotation*T/startT;
parameters_current(2) = parameters_best(2) + (rand-0.5)*2*maxRotation*T/startT;
parameters_current(3) = parameters_best(3) + (rand-0.5)*2*maxRotation*T/startT;
% randomly update parameters for translation
parameters_current(4) = parameters_best(4) + (rand-0.5)*2*maxStep*T/startT;
parameters_current(5) = parameters_best(5) + (rand-0.5)*2*maxStep*T/startT;
parameters_current(6) = parameters_best(6) + (rand-0.5)*2*maxStep*T/startT;
% 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*50)/T) > rand) %tmax anstelle von 50?
p = exp((-difference*50)/T) %tmax anstelle von 50?
parameters_best = parameters_current;
distance_best = distance_current;
end
rejected = [rejected; parameters_current];
end
plot3(mand_current(:,1),mand_current(:,2),mand_current(:,3),'.')
T
distance_best
% parameters_best
% parameters_current
drawnow
end
toc
% fprintf(num2str(store_attempts)+' best where estimated\n\n')
end
end
end
%plot best solution
figure
plot3(pelvis(:,1),pelvis(:,2),pelvis(:,3),'k.');
hold on
title('Best Solution')
plot3(mand_current(:,1),mand_current(:,2),mand_current(:,3),'.')