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 286 287 288 289
| function varargout = ehjh(varargin)
% EHJH M-file for ehjh.fig
% EHJH, by itself, creates a new EHJH or raises the existing
% singleton*.
%
% H = EHJH returns the handle to a new EHJH or the handle to
% the existing singleton*.
%
% EHJH('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EHJH.M with the given input arguments.
%
% EHJH('Property','Value',...) creates a new EHJH or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ehjh_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ehjh_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help ehjh
% Last Modified by GUIDE v2.5 26-Sep-2010 19:22:35
global reg
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ehjh_OpeningFcn, ...
'gui_OutputFcn', @ehjh_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before ehjh is made visible.
function ehjh_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ehjh (see VARARGIN)
% Choose default command line output for ehjh
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ehjh wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ehjh_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global reg
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global reg;
[reg.refFile,reg.refPath] = uigetfile({'*.jpg';'*.png';'*.tif';'*.tiff'},'Open the Reference Image');
reg.refImg=(imread([reg.refPath '/' reg.refFile]));
axes(handles.axes1);
reg.refImgHandle=imshow(reg.refImg,[0 255]);
set(reg.refImgHandle,'HitTest','on');
grid off;
zoom off;
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global reg;
[reg.floatFile,reg.floatPath] = uigetfile({'*.jpg';'*.png';'*.tif';'*.tiff'},'Open the Floating Image');
reg.floatImg=(imread([reg.floatPath '/' reg.floatFile]));
axes(handles.axes2);
reg.floatImgHandle=imshow(reg.floatImg,[0 255]);
set(reg.floatImgHandle,'HitTest','on');
grid off;
zoom off;
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global reg;
A = reg.refImg;
B = reg.floatImg;
NP=16;
D=3;
F=0.525;
CR=0.8;
itermax=40;
strategy=2;
objFctHandle= @(x) transforma(x,A,B);
%-----Check input variables-----------------------------------------------
if (NP < 5)
fprintf(1,'Error! NP should be >= 5\n');
end
if ((CR < 0) | (CR > 1))
fprintf(1,'Error! CR should be ex [0,1]\n');
end
if (itermax < 0)
fprintf(1,'Error! itermax should be > 0\n');
end
%-----Initialize population and some arrays-------------------------------
pop = zeros(NP,D); %initialize pop to gain speed
lowbound1 = -100; % Lower bound for parameters (all parameters treated alike)
highbound1 = 100; % Upper bound for parameters (all parameters treated alike)
lowbound2 = -100; % Lower bound for parameters (all parameters treated alike)
highbound2 = 100; % Upper bound for parameters (all parameters treated alike)
lowbound3 = -100;
highbound3 = 100;
%----pop is a matrix of size NPxD. It will be initialized-------------
%----with random values between highbound and lowbound----------------
for i=1:NP
pop(i,1) = lowbound1 + rand*(highbound1 - lowbound1);
pop(i,2) = lowbound2 + rand*(highbound2 - lowbound2);
pop(i,3) = lowbound3 + rand*(highbound3 - lowbound3);
end
popold = zeros(size(pop)); % toggle population
val = zeros(1,NP); % create and reset the "cost array"
bestmem = zeros(1,D); % best population member ever
bestmemit = zeros(1,D); % best population member in iteration
nfeval = 0; % number of function evaluations
%------Evaluate the best member after initialization----------------------
%------Objective function is the Rosenbrock saddle------------------------
%------100*(x2-x1^2)^2+(1-x1)^2.------------------------------------------
ibest = 1; % start with first population member
% >>>>>>>>>>>eval<<<<<<<<<<<<<<<
val(1) = objFctHandle([pop(ibest,1) pop(ibest,2) pop(ibest,3)]);
bestval = val(1); % best objective function value so far
nfeval = nfeval + 1;
for i=2:NP % check the remaining members
% >>>>>>>>>>>eval<<<<<<<<<<<<<<<
val(i) = objFctHandle([pop(i,1) pop(i,2) pop(i,3)]);
nfeval = nfeval + 1;
if (val(i) < bestval) % if member is better
ibest = i; % save its location
bestval = val(i);
end
end
bestmemit = pop(ibest,:); % best member of current iteration
bestvalit = bestval; % best value of current iteration
bestmem = bestmemit; % best member ever
% 3D-plot function
%------DE-Minimization---------------------------------------------
%------popold is the population which has to compete. It is--------
%------static through one iteration. pop is the newly--------------
%------emerging population.----------------------------------------
pm1 = zeros(NP,D); % initialize population matrix 1
pm2 = zeros(NP,D); % initialize population matrix 2
pm3 = zeros(NP,D); % initialize population matrix 3
pm4 = zeros(NP,D); % initialize population matrix 4
pm5 = zeros(NP,D); % initialize population matrix 5
bm = zeros(NP,D); % initialize bestmember matrix
ui = zeros(NP,D); % intermediate population of perturbed vectors
mui = zeros(NP,D); % mask for intermediate population
mpo = zeros(NP,D); % mask for old population
rot = (0:1:NP-1); % rotating index array
rt = zeros(NP); % another rotating index array
a1 = zeros(NP); % index array
a2 = zeros(NP); % index array
a3 = zeros(NP); % index array
a4 = zeros(NP); % index array
a5 = zeros(NP); % index array
ind = zeros(4);
iter = 1;
while ((iter < itermax) & (bestval > -2))
popold = pop; % save the old population
ind = randperm(4); % index pointer array
a1 = randperm(NP); % shuffle locations of vectors
rt = rem(rot+ind(1),NP); % rotate indices by ind(1) positions
a2 = a1(rt+1); % rotate vector locations
rt = rem(rot+ind(2),NP);
a3 = a2(rt+1);
rt = rem(rot+ind(3),NP);
a4 = a3(rt+1);
rt = rem(rot+ind(4),NP);
a5 = a4(rt+1);
pm1 = popold(a1,:); % shuffled population 1
pm2 = popold(a2,:); % shuffled population 2
pm3 = popold(a3,:); % shuffled population 3
pm4 = popold(a4,:); % shuffled population 4
pm5 = popold(a5,:); % shuffled population 5
for i=1:NP % population filled with the best member
bm(i,:) = bestmemit; % of the last iteration
end
mui = rand(NP,D) < CR; % all random numbers < CR are 1, 0 otherwise
mpo = mui < 0.5; % inverse mask to mui
if (strategy == 1) % DE/best/1
ui = bm + F*(pm1 - pm2); % differential variation
ui = popold.*mpo + ui.*mui; % binomial crossover
elseif (strategy == 2) % DE/rand/1
ui = pm3 + F*(pm1 - pm2); % differential variation
ui = popold.*mpo + ui.*mui; % binomial crossover
elseif (strategy == 3) % DE/rand-to-best/1
ui = popold + F*(bm-popold) + F*(pm1 - pm2);
ui = popold.*mpo + ui.*mui; % binomial crossover
elseif (strategy == 4) % DE/best/2
ui = bm + F*(pm1 - pm2 + pm3 - pm4); % differential variation
ui = popold.*mpo + ui.*mui; % binomial crossover
else % DE/rand/2
ui = pm5 + F*(pm1 - pm2 + pm3 - pm4); % differential variation
ui = popold.*mpo + ui.*mui; % binomial crossover
end
%-----Select which vectors are allowed to enter the new population------------
for i=1:NP
% >>>>>>>>>>>eval<<<<<<<<<<<<<<<100*(x2-x1^2)^2+(1-x1)^2.-
tempval = objFctHandle([ui(i,1) ui(i,2) ui(i,3)]); % check cost of competitor
nfeval = nfeval + 1;
if (tempval <= val(i)) % if competitor is better than value in "cost array"
pop(i,:) = ui(i,:); % replace old vector with new one (for new iteration)
val(i) = tempval; % save value in "cost array"
%----we update bestval only in case of success to save time-----------
if (tempval < bestval) % if competitor better than the best one ever
bestval = tempval; % new best value
bestmem = ui(i,:); % new best parameter vector ever
end
end
end %---end for imember=1:NP
bestmemit = bestmem; % freeze the best member of this iteration for the coming
% iteration. This is needed for some of the strategies.
%----Output section----------------------------------------------------------
if (rem(iter,10) == 0)
fprintf(1,'Iteration: %d, Best: %f, F: %f, CR: %f, NP: %d\n',iter,bestval,F,CR,NP);
for n=1:D
fprintf(1,'best(%d) = %f\n',n,bestmem(n));
end
end
iter = iter + 1;
end %---end while ((iter < itermax) ...
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
Partager