Skip to content

Commit 0e87e9f

Browse files
authored
Reorganises code and removes unused routines (#178)
* Moves some files in the project * Separates "customModelClass.m" into four "process" routines * Removes test files from "targetFunctions" directory * Tidies up "runDE" routine * Removes custom RAT Dream routines * Fixes function arguments * Addresses review comments
1 parent 18db474 commit 0e87e9f

31 files changed

+338
-4679
lines changed

minimisers/DE/deopt.m

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,16 @@
7777
% General Public License can be obtained from the
7878
% Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
7979
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80-
function [FVr_bestmem,problem] = deopt(fname,problem,problemDefLimits,problemDefCells,controls,S_struct)
80+
function [FVr_bestmem,problem] = deopt(fname,problem,problemDefCells,controls,S_struct)
8181

82-
83-
%function FVr_bestmem = rascal_deopt(fname,problem,PlotIt,controls,S_struct)
84-
85-
%[FVr_bestmem,S_bestval,I_nfeval]
8682
str = struct('I_nc',0,'FVr_ca',0,'I_no',0,'FVr_oa',0);
8783
S_val = repmat(str,S_struct.I_NP,1);
88-
%coder.varsize(S_val(:),[Inf 1],[1 0]);
8984

9085
%-----This is just for notational convenience and to keep the code uncluttered.--------
9186

92-
9387
coder.varsize('problemDef.resample',[Inf,1],[1 0]);
9488
coder.varsize('FVr_bestmem',[1 Inf],[0 1]);
9589
coder.varsize('FVr_bestmemit',[1 Inf],[0 1]);
96-
%coder.varsize('FM_pop',[S_struct.I_NP,2],[1 0]);
97-
9890

9991
stopflag = 0;
10092
I_best_index = 1;
@@ -111,9 +103,6 @@
111103
I_strategy = S_struct.I_strategy;
112104
I_refresh = S_struct.I_refresh;
113105
I_plotting = S_struct.I_plotting;
114-
%coder.varsize('FM_pop',[20,2],[0 0]);
115-
%FM_pop = zeros(I_NP,2);
116-
117106

118107
%-----Check input variables---------------------------------------------
119108
if (I_NP < 5)
@@ -141,42 +130,29 @@
141130
FM_pop(k,:) = FVr_minbound + rand(1,I_D).*(FVr_maxbound - FVr_minbound);
142131
end
143132

144-
%FM_popold = zeros(size(FM_pop)); % toggle population
145-
%FVr_bestmemit = zeros(1,2);% best population member in iteration
146133
I_nfeval = 0; % number of function evaluations
147134

148135
%------Evaluate the best member after initialization----------------------
149-
%str = struct('I_nc',0,'FVr_ca',0,'I_no',0,'FVr_oa',0);
150-
% S_MSE.FVr_ca = [];
151-
% S_MSE.I_no = [];
152-
% S_MSE.FVr_oa(1) = [];
153136

154137
str = struct('I_nc',0,'FVr_ca',0,'I_no',0,'FVr_oa',0);
155138
S_val = repmat(str,I_NP,1);
156139

157-
158-
%intrafun(p,problemDef,controls,problemDefCells,problemDefLimits);
159-
160140
coder.varsize('I_best_index',[1 1],[0 0]);
161141
I_best_index = 1; % start with first population member
162-
S_val(1) = fname(FM_pop(I_best_index,:),problem,controls,problemDefCells,problemDefLimits);
142+
S_val(1) = fname(FM_pop(I_best_index,:),problem,controls,problemDefCells);
163143
S_bestval = S_val(1); % best objective function value so far
164144
I_nfeval = I_nfeval + 1;
165145
for k=2:I_NP % check the remaining members
166-
S_val(k) = fname(FM_pop(k,:),problem,controls,problemDefCells,problemDefLimits);
146+
S_val(k) = fname(FM_pop(k,:),problem,controls,problemDefCells);
167147
I_nfeval = I_nfeval + 1;
168148
if (leftWin(S_val(k),S_bestval) == 1)
169149
I_best_index = k; % save its location
170150
S_bestval = S_val(k);
171151
end
172152
end
173-
%val = [0 0];
174153
val = FM_pop(I_best_index,:);
175154
FVr_bestmemit = val; % best member of current iteration
176155

177-
% FVr_bestmemit = FM_pop(I_best_index,:);
178-
% S_bestvalit = S_bestval; % best value of current iteration
179-
180156
FVr_bestmem = FVr_bestmemit; % best member ever
181157

182158
%------DE-Minimization---------------------------------------------
@@ -281,7 +257,7 @@
281257
FM_origin = FM_pm3;
282258
FM_ui = FM_popold.*FM_mpo + FM_ui.*FM_mui; % crossover
283259
else % either-or-algorithm
284-
if (rand < 0.5); % Pmu = 0.5
260+
if (rand < 0.5) % Pmu = 0.5
285261
FM_ui = FM_pm3 + fWeight*(FM_pm1 - FM_pm2);% differential variation
286262
FM_origin = FM_pm3;
287263
else % use F-K-Rule: K = 0.5(F+1)
@@ -308,7 +284,7 @@
308284
end
309285
%=====End boundary constraints==========================================
310286

311-
S_tempval = fname(FM_ui(k,:),problem, controls,problemDefCells,problemDefLimits); % check cost of competitor
287+
S_tempval = fname(FM_ui(k,:),problem, controls,problemDefCells); % check cost of competitor
312288
I_nfeval = I_nfeval + 1;
313289
if (leftWin(S_tempval,S_val(k)) == 1)
314290
FM_pop(k,:) = FM_ui(k,:); % replace old vector with new one (for new iteration)

minimisers/DE/runDE.m

Lines changed: 122 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,130 @@
11
function [problemDef,problem,result] = runDE(problemDef,problemDefCells,problemDefLimits,controls)
22

3+
[problemDef,~] = fitsetup(problemDef,problemDefCells,problemDefLimits,controls);
4+
F_VTR = controls.targetValue; %Value to reach
5+
I_D = length(problemDef.fitpars);
6+
7+
FVr_minbound = problemDef.fitconstr(:,1)';
8+
FVr_maxbound = problemDef.fitconstr(:,2)';
9+
I_bnd_constr = 1; %1: use bounds as bound constraints, 0: no bound constraints
10+
11+
% I_NP number of population members
12+
I_NP = controls.populationSize;
13+
14+
% I_itermax maximum number of iterations (generations)
15+
I_itermax = controls.numGenerations;
16+
17+
% fWeight DE-stepsize fWeight ex [0, 2]
18+
fWeight = controls.fWeight;
19+
20+
% F_CR crossover probability constant ex [0, 1]
21+
F_CR = controls.crossoverProbability;
22+
23+
% I_strategy 1 --> DE/rand/1:
24+
% the classical version of DE.
25+
% 2 --> DE/local-to-best/1:
26+
% a version which has been used by quite a number
27+
% of scientists. Attempts a balance between robustness
28+
% and fast convergence.
29+
% 3 --> DE/best/1 with jitter:
30+
% taylored for small population sizes and fast convergence.
31+
% Dimensionality should not be too high.
32+
% 4 --> DE/rand/1 with per-vector-dither:
33+
% Classical DE with dither to become even more robust.
34+
% 5 --> DE/rand/1 with per-generation-dither:
35+
% Classical DE with dither to become even more robust.
36+
% Choosing fWeight = 0.3 is a good start here.
37+
% 6 --> DE/rand/1 either-or-algorithm:
38+
% Alternates between differential mutation and three-point-
39+
% recombination.
40+
41+
I_strategy = 5;
42+
43+
% I_refresh intermediate output will be produced after "I_refresh"
44+
% iterations. No intermediate output will be produced
45+
% if I_refresh is < 1
46+
I_refresh = 1;
47+
48+
% I_plotting Will use plotting if set to 1. Will skip plotting otherwise.
49+
I_plotting = 0;
50+
51+
%-----Definition of tolerance scheme--------------------------------------
52+
%-----The scheme is sampled at I_lentol points----------------------------
53+
I_lentol = 50;
54+
FVr_x = linspace(-1,1,I_lentol); %ordinate running from -1 to +1
55+
FVr_lim_up = ones(1,I_lentol); %upper limit is 1
56+
FVr_lim_lo = -ones(1,I_lentol); %lower limit is -1
57+
58+
%Tell compiler abut variable sizes
59+
coder.varsize('S_struct.I_lentol', [Inf 1],[1 0]);
60+
coder.varsize('S_struct.FVr_x', [1 Inf],[0 1]);
61+
coder.varsize('S_struct.FVr_lim_up', [1 Inf],[0 1]);
62+
coder.varsize('S_struct.FVr_lim_lo', [1 Inf],[0 1]);
63+
64+
coder.varsize('S_struct.I_NP', [1 1],[0 0]);
65+
coder.varsize('S_struct.fWeight', [1 1],[0 0]);
66+
coder.varsize('S_struct.F_CR', [1 1],[0 0]);
67+
coder.varsize('S_struct.I_D', [1 1],[0 0]);
68+
coder.varsize('S_struct.FVr_minbound', [1 Inf],[0 1]);
69+
coder.varsize('S_struct.FVr_maxbound', [1 Inf],[0 1]);
70+
coder.varsize('S_struct.I_bnd_constr', [1 1],[0 0]);
71+
coder.varsize('S_struct.I_itermax', [1 1],[0 0]);
72+
coder.varsize('S_struct.F_VTR', [1 1],[0 0]);
73+
coder.varsize('S_struct.I_strategy', [1 1],[0 0]);
74+
coder.varsize('S_struct.I_refresh', [1 1],[0 0]);
75+
coder.varsize('S_struct.I_plotting', [1 1],[0 0]);
76+
coder.varsize('S_struct.FM_pop',[Inf 2],[1 0]);
77+
coder.varsize('S_struct.FVr_bestmem',[1 Inf],[0 1]);
78+
coder.varsize('FVr_bestmem',[1 Inf],[0 1]);
79+
80+
%-----tie all important values to a structure that can be passed along----
81+
S_struct.I_lentol = I_lentol;
82+
S_struct.FVr_x = FVr_x;
83+
S_struct.FVr_lim_up = FVr_lim_up;
84+
S_struct.FVr_lim_lo = FVr_lim_lo;
85+
86+
S_struct.I_NP = I_NP;
87+
S_struct.fWeight = fWeight;
88+
S_struct.F_CR = F_CR;
89+
S_struct.I_D = I_D;
90+
S_struct.FVr_minbound = FVr_minbound;
91+
S_struct.FVr_maxbound = FVr_maxbound;
92+
S_struct.I_bnd_constr = I_bnd_constr;
93+
S_struct.I_itermax = I_itermax;
94+
S_struct.F_VTR = F_VTR;
95+
S_struct.I_strategy = I_strategy;
96+
S_struct.I_refresh = I_refresh;
97+
S_struct.I_plotting = I_plotting;
98+
S_struct.FM_pop = zeros(I_NP,2);
99+
S_struct.FVr_bestmem = [0 0];
100+
101+
[res,problemDef] = deopt(@intrafun,problemDef,problemDefCells,controls,S_struct);
102+
problemDef.fitpars = res;
103+
problemDef = unpackparams(problemDef,controls);
104+
[problem,result] = reflectivityCalculation(problemDef,problemDefCells,controls);
105+
106+
if ~strcmpi(controls.display,'off')
107+
fprintf('Final chi squared is %g\n',problem.calculations.sum_chi);
108+
end
3109

4-
[problemDef,~] = fitsetup(problemDef,problemDefCells,problemDefLimits,controls);
5-
F_VTR = controls.targetValue; %Value to reach
6-
I_D = length(problemDef.fitpars);
7-
8-
FVr_minbound = problemDef.fitconstr(:,1)';
9-
FVr_maxbound = problemDef.fitconstr(:,2)';
10-
I_bnd_constr = 1; %1: use bounds as bound constraints, 0: no bound constraints
11-
12-
% I_NP number of population members
13-
I_NP = controls.populationSize;
14-
15-
% I_itermax maximum number of iterations (generations)
16-
I_itermax = controls.numGenerations;
17-
18-
% fWeight DE-stepsize fWeight ex [0, 2]
19-
fWeight = controls.fWeight;
20-
21-
% F_CR crossover probability constant ex [0, 1]
22-
F_CR = controls.crossoverProbability;
23-
24-
% I_strategy 1 --> DE/rand/1:
25-
% the classical version of DE.
26-
% 2 --> DE/local-to-best/1:
27-
% a version which has been used by quite a number
28-
% of scientists. Attempts a balance between robustness
29-
% and fast convergence.
30-
% 3 --> DE/best/1 with jitter:
31-
% taylored for small population sizes and fast convergence.
32-
% Dimensionality should not be too high.
33-
% 4 --> DE/rand/1 with per-vector-dither:
34-
% Classical DE with dither to become even more robust.
35-
% 5 --> DE/rand/1 with per-generation-dither:
36-
% Classical DE with dither to become even more robust.
37-
% Choosing fWeight = 0.3 is a good start here.
38-
% 6 --> DE/rand/1 either-or-algorithm:
39-
% Alternates between differential mutation and three-point-
40-
% recombination.
41-
42-
I_strategy = 5;
43-
44-
% I_refresh intermediate output will be produced after "I_refresh"
45-
% iterations. No intermediate output will be produced
46-
% if I_refresh is < 1
47-
I_refresh = 1;
48-
49-
% I_plotting Will use plotting if set to 1. Will skip plotting otherwise.
50-
I_plotting = 0;
51-
52-
%-----Definition of tolerance scheme--------------------------------------
53-
%-----The scheme is sampled at I_lentol points----------------------------
54-
I_lentol = 50;
55-
FVr_x = linspace(-1,1,I_lentol); %ordinate running from -1 to +1
56-
FVr_lim_up = ones(1,I_lentol); %upper limit is 1
57-
FVr_lim_lo = -ones(1,I_lentol); %lower limit is -1
58-
59-
%Tell compiler abut variable sizes
60-
coder.varsize('S_struct.I_lentol', [Inf 1],[1 0]);
61-
coder.varsize('S_struct.FVr_x', [1 Inf],[0 1]);
62-
coder.varsize('S_struct.FVr_lim_up', [1 Inf],[0 1]);
63-
coder.varsize('S_struct.FVr_lim_lo', [1 Inf],[0 1]);
64-
65-
coder.varsize('S_struct.I_NP', [1 1],[0 0]);
66-
coder.varsize('S_struct.fWeight', [1 1],[0 0]);
67-
coder.varsize('S_struct.F_CR', [1 1],[0 0]);
68-
coder.varsize('S_struct.I_D', [1 1],[0 0]);
69-
coder.varsize('S_struct.FVr_minbound', [1 Inf],[0 1]);
70-
coder.varsize('S_struct.FVr_maxbound', [1 Inf],[0 1]);
71-
coder.varsize('S_struct.I_bnd_constr', [1 1],[0 0]);
72-
coder.varsize('S_struct.I_itermax', [1 1],[0 0]);
73-
coder.varsize('S_struct.F_VTR', [1 1],[0 0]);
74-
coder.varsize('S_struct.I_strategy', [1 1],[0 0]);
75-
coder.varsize('S_struct.I_refresh', [1 1],[0 0]);
76-
coder.varsize('S_struct.I_plotting', [1 1],[0 0]);
77-
coder.varsize('S_struct.FM_pop',[Inf 2],[1 0]);
78-
coder.varsize('S_struct.FVr_bestmem',[1 Inf],[0 1]);
79-
coder.varsize('FVr_bestmem',[1 Inf],[0 1]);
80-
81-
%-----tie all important values to a structure that can be passed along----
82-
S_struct.I_lentol = I_lentol;
83-
S_struct.FVr_x = FVr_x;
84-
S_struct.FVr_lim_up = FVr_lim_up;
85-
S_struct.FVr_lim_lo = FVr_lim_lo;
86-
87-
S_struct.I_NP = I_NP;
88-
S_struct.fWeight = fWeight;
89-
S_struct.F_CR = F_CR;
90-
S_struct.I_D = I_D;
91-
S_struct.FVr_minbound = FVr_minbound;
92-
S_struct.FVr_maxbound = FVr_maxbound;
93-
S_struct.I_bnd_constr = I_bnd_constr;
94-
S_struct.I_itermax = I_itermax;
95-
S_struct.F_VTR = F_VTR;
96-
S_struct.I_strategy = I_strategy;
97-
S_struct.I_refresh = I_refresh;
98-
S_struct.I_plotting = I_plotting;
99-
S_struct.FM_pop = zeros(I_NP,2);
100-
S_struct.FVr_bestmem = [0 0];
101-
102-
%res = deopt(@intrafun,problemDef,controls,S_struct);
103-
104-
[res,problemDef] = deopt(@intrafun,problemDef,problemDefLimits,problemDefCells,controls,S_struct);
105-
problemDef.fitpars = res;
106-
problemDef = unpackparams(problemDef,controls);
107-
[problem,result] = reflectivityCalculation(problemDef,problemDefCells,controls);
108-
109-
if ~strcmpi(controls.display,'off')
110-
fprintf('Final chi squared is %g\n',problem.calculations.sum_chi);
111110
end
112111

113-
end
114-
115-
116-
function S_MSE = intrafun(p,problemDef,controls,problemDefCells,problemDefLimits)
117-
118-
% S_MSE.I_nc = [];
119-
% S_MSE.FVr_ca = [];
120-
% S_MSE.I_no = [];
121-
% S_MSE.FVr_oa(1) = [];
122-
123-
coder.varsize('S_MSE.I_nc',[1 1],[0 0]);
124-
coder.varsize('S_MSE.FVr_ca',[1 1],[0 0]);
125-
coder.varsize('S_MSE.I_no',[1 1],[0 0]);
126-
coder.varsize('S_MSE.FVr_oa',[1 1],[0 0]);
127-
128-
% data = problem.data;
129-
% x = data(:,1);
130-
% y = data(:,2);
131-
% e = data(:,3);
132-
%
133-
% line = (p(1)*x) + p(2);
134-
%
135-
% fval = sum(((y-line).^2)./e);
136-
137-
problemDef.fitpars = p;
138-
problemDef = unpackparams(problemDef,controls);
139-
[problemDef,~] = reflectivityCalculation(problemDef,problemDefCells,controls);
140-
fval = problemDef.calculations.sum_chi;
141-
142-
S_MSE.I_nc = 0;%no constraints THESE FIRST FEW VALS MAY BE WRONG
143-
S_MSE.FVr_ca = 0;%no constraint array
144-
S_MSE.I_no = 1;%number of objectives (costs)
145-
S_MSE.FVr_oa = fval;
146-
147-
end
148-
149-
150-
function PlotIt(FVr_bestmem,problem)
151-
152-
% problem.fitpars = FVr_bestmem;
153-
% setappdata(0,'problem',problem);
154-
155-
p = FVr_bestmem;
156-
157-
data = problem.data;
158-
x = data(:,1);
159-
y = data(:,2);
160-
e = data(:,3);
161-
162-
line = (p(1)*x) + p(2);
163112

164-
figure(1)
165-
clf;hold on
166-
%errorbar(x,y,e,'bo');
167-
plot(x,line);
113+
function S_MSE = intrafun(p,problemDef,controls,problemDefCells)
114+
115+
coder.varsize('S_MSE.I_nc',[1 1],[0 0]);
116+
coder.varsize('S_MSE.FVr_ca',[1 1],[0 0]);
117+
coder.varsize('S_MSE.I_no',[1 1],[0 0]);
118+
coder.varsize('S_MSE.FVr_oa',[1 1],[0 0]);
119+
120+
problemDef.fitpars = p;
121+
problemDef = unpackparams(problemDef,controls);
122+
[problemDef,~] = reflectivityCalculation(problemDef,problemDefCells,controls);
123+
fval = problemDef.calculations.sum_chi;
124+
125+
S_MSE.I_nc = 0; %no constraints THESE FIRST FEW VALS MAY BE WRONG
126+
S_MSE.FVr_ca = 0; %no constraint array
127+
S_MSE.I_no = 1; %number of objectives (costs)
128+
S_MSE.FVr_oa = fval;
168129

169130
end

0 commit comments

Comments
 (0)