|
1 | 1 | function [problemDef,problem,result] = runDE(problemDef,problemDefCells,problemDefLimits,controls)
|
2 | 2 |
|
| 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 |
3 | 109 |
|
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); |
111 | 110 | end
|
112 | 111 |
|
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); |
163 | 112 |
|
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; |
168 | 129 |
|
169 | 130 | end
|
0 commit comments