-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSizing_opt_v2.m
397 lines (339 loc) · 10.6 KB
/
Sizing_opt_v2.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
% Sizing optimization using MATLAB's inbuilt toolboxes
%% Using fminsearch - https://uk.mathworks.com/help/matlab/ref/fminsearch.html
% % But need to constrain variables to be non-negative
% x0 = [1, 1, 10]; % Initial guess for n_s, n_w, Eb_init
% x = fminsearch(@Objective_LI_DE_no_DR,x0)
%% Using fmincon (https://uk.mathworks.com/help/optim/ug/example-nonlinear-constrained-minimization.html)
% Solve a Constrained Nonlinear Problem, Solver-Based - fmincon(https://uk.mathworks.com/help/optim/ug/fmincon.html)
% options = optimoptions(@fmincon,...
% 'Display','iter','Algorithm','interior-point','StepTolerance',1e-12,'ConstraintTolerance',1e-12,...
% 'FunctionTolerance',1e-12,'MaxIterations',10000, 'MaxFunctionEvaluations',10000);
options = optimoptions(@fmincon,...
'Display','iter','Algorithm','interior-point');
[x,fval,exitflag,output] = fmincon(@Objective_LI_DE_v2_1h,[0 0 0],...
[],[],[],[],[],[],@positive,options)
% Works well to give a reasonable result at least when using 5 equal weights
% But takes a large no. of iterations to converge, might be a local minimum rather than a global one!
%% GlobalSearch (https://uk.mathworks.com/help/gads/globalsearch.html)
% No longer need to specify constraints on inputs explicitly since it's now
% a bounded constrained optimization (positive constraint is enforced by lower bound)
% Maybe remove upper bound constraint - don't need to explicitly specify this?
rng default % For reproducibility
ms = MultiStart('UseParallel',true);
gs = GlobalSearch(ms);
options = optimoptions(@fmincon, 'Display','iter','Algorithm','active-set',...
'MaxIterations',2000,'MaxFunctionEvaluations',2000,'UseParallel',true);
%'PlotFcn',{@gsplotbestf,@gsplotfunccount});
% Can't plot with GS or MS while using parallel processing
problem = createOptimProblem('fmincon','objective',@Objective_LI_DE_v2_1h,'x0',[50 3.5 50],...
'lb',[0,0,0],'ub',[100,30,200],'options',options);
tic
[x, fval] = run(gs,problem)
gs_time = toc; % Time taken for optimization
%% Multiple starting point search (https://uk.mathworks.com/help/gads/multistart.html)
rng default % For reproducibility
options = optimoptions(@fmincon,'Display','iter',...
'Algorithm','active-set','MaxIterations',2000,'MaxFunctionEvaluations',2000);
problem = createOptimProblem('fmincon','objective',...
@Objective_LI_DE_v2_1h,'x0',[50 3.5 50],...
'lb',[0,0,0],'ub',[100,30,200],'options',options);
ms = MultiStart('UseParallel',true);
tic
[x,f] = run(ms,problem,85)
ms_time = toc;
%% Direct pattern search (https://uk.mathworks.com/help/gads/patternsearch.html)
% Pattern search using several different initial/start points
rng default % For reproducibility
lb = [0,0,0];
ub = [100,30,200];
A = [];
b = [];
Aeq = [];
beq = [];
x = zeros(85,3);
fval = zeros(85,1);
exitflag = zeros(85,1);
options = optimoptions('patternsearch','UseParallel',true,'UseCompletePoll',true,...
'UseVectorized',false);
%PlotFcn',{@psplotbestf,psplotbestx,psplotmeshsize,psplotfuncount}
tic
for i = 1:1:85
x0 = lb + rand(size(lb)).*(ub - lb);
[x(i,:), fval(i), exitflag(i)] = patternsearch(@Objective_LI_DE_v2_1h,x0,A,b,Aeq,beq,lb,ub,options);
end
[min_obj, index] = min(fval)
x_opt = x(index,:)
ps_time = toc;
%% Genetic algorithm (GA)
rng default % For reproducibility
lb = [0,0,0];
ub = [100,30,200];
fitness_function = @Objective_LI_DE_v2_1h;
nvars = 3; % number of variables
options = optimoptions('ga','UseParallel', true, 'UseVectorized', false,...
'PlotFcn','gaplotgenealogy');
%@gaplotbestf,@gaplotbestindiv,@gaplotrange,
tic
[x,fval] = ga(fitness_function,nvars,[],[],[],[],lb,ub,[],options)
ga_time = toc;
%% Plotting objective
plotobjective(@Objective_LI_DE_v2_1h,[0 30; 0 10; 0 100]);
%% Genetic algorithm (GA) - vectorized
rng default % For reproducibility
lb = [0,0,0];
ub = [100,20,200];
fitness_function = @Objective_LI_DE_v2_1h_vectorized;
nvars = 3; % number of variables
options = optimoptions('ga','UseParallel', true, 'UseVectorized', true);
tic
[x,fval] = ga(fitness_function,nvars,[],[],[],[],lb,ub,[],options)
ga_time = toc;
% But not easy to implement in this case due to the manner in which the
% objective function is written, preallocate arrays for all the powers etc.
%% PSO
rng default % For reproducibility of results (needed for random algorithm)
options = optimoptions('particleswarm', 'UseParallel', true, ...
'UseVectorized', false,'PlotFcn','pswplotbestf');
nvars = 3;
lb = [0,0,0];
ub = [100,30,200];
tic
[x,fval,exitflag] = particleswarm(@Objective_LI_DE_v2_1h,nvars,lb,ub,options)
pso_time = toc;
%% Surrogate optimization
rng default
lb = [0,0,0];
ub = [100,30,200];
options = optimoptions('surrogateopt','UseParallel',true,'MaxFunctionEvaluations',...
2000,'PlotFcn','surrogateoptplot');
tic
[x,fval] = surrogateopt(@Objective_LI_DE_v2_1h,lb,ub,options);
sur_time = toc;
%% Simulated annealing
rng default;
lb = [0,0,0];
ub = [100,30,200];
x0 = [50 3.5 50]; % initial point
options = optimoptions('simulannealbnd','PlotFcns',...
{@saplotbestf,@saplotf,@saplotstopping});
tic
[x,~,exitFlag,output] = simulannealbnd(@Objective_LI_DE_v2_1h,x0,lb,ub,options)
sa_time = toc;
%% PSO - but optimizing wrt rated powers instead
rng default % For reproducibility of results (needed for random algorithm)
options = optimoptions('particleswarm', 'UseParallel', true, ...
'UseVectorized', false,'PlotFcn','pswplotbestf');
nvars = 3;
lb = [0,0,0];
ub = [25,25,200];
tic
[x,fval,exitflag] = particleswarm(@Objective_LI_DE_v8_1h_optPrated,nvars,lb,ub,options)
pso_time = toc;
%% Pareto set using gamultiobj
rng default;
lb = [0,0,0];
ub = [100,10,200]; % for n_s, n_w, Eb_init
tic
options = optimoptions('gamultiobj','UseParallel',true,'UseVectorized',false,...
'ParetoFraction',0.50);
[x_p,fval] = gamultiobj(@sizing_pareto,3,[],[],[],[],lb,ub,options);
gamultiobj_time = toc;
%% Pareto set with rated total powers as inputs instead of n_s/n_w
rng default;
options = optimoptions('gamultiobj','UseParallel',true,...
'UseVectorized',false,'ParetoFraction',0.50);
lb = [0,0,0];
ub = [25,25,200];
tic
[x_p,fval] = gamultiobj(@sizing_pareto,3,[],[],[],[],lb,ub,[],options);
gamultiobj_time = toc;
%% Pareto set/front using paretosearch (pattern search algorithm)
rng default;
options = optimoptions('paretosearch','UseParallel',true,...
'UseVectorized',false,'ParetoSetSize',200);
lb = [0,0,0];
ub = [100,10,200]; % for rated P_s, P_w, Eb_init
tic
[x_p,fval] = paretosearch(@sizing_pareto,3,[],[],[],[],lb,ub,[],options);
paretosearch_time = toc;
%% Pareto set with rated total powers as inputs instead of n_s/n_w
rng default;
options = optimoptions('paretosearch','UseParallel',true,...
'UseVectorized',false,'ParetoSetSize',200);
lb = [0,0,0];
ub = [25,25,200];
tic
[x_p,fval] = paretosearch(@sizing_pareto,3,[],[],[],[],lb,ub,[],options);
paretosearch_time = toc;
%%
w = [0.2 0.2 0.2 0.2 0.2]; % Weights of different cost terms
cost = fval * w';
%% 3-D pareto set plot in control variable (inputs) space
% 3D scatter plot
% Plotting optimal values of n_s, n_w and Eb_init
scatter3(x_p(:,1),x_p(:,2),x_p(:,3),10,'b','filled')
xlabel('No. of solar PV arrays');
ylabel('No. of wind turbines');
zlabel('Battery capacity (kWh)');
%% Interpolated surface plot
figure(1)
x = x_p(:,1);
y = x_p(:,2);
z = x_p(:,3);
F = scatteredInterpolant(x,y,z,'natural','none');
sgr = linspace(min(x),max(x));
ygr = linspace(min(y),max(y));
[XX,YY] = meshgrid(sgr,ygr);
ZZ = F(XX,YY);
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,5,'k','filled')
%scatter3(x,y,z,'k.')
%% Griddata
figure(2)
x = x_p(:,1);
y = x_p(:,2);
z = x_p(:,3);
sgr = linspace(min(x),max(x));
ygr = linspace(min(y),max(y));
[XX,YY] = meshgrid(sgr,ygr);
ZZ = griddata(x,y,z,XX,YY,'natural');
%mesh(xq,yq,zq);
surf(XX,YY,ZZ,'LineStyle','none');
hold on
plot3(x,y,z,'k.')
%% Rotated views
figure
subplot(2,2,1)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
subplot(2,2,2)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
view(-148,8)
subplot(2,2,3)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
view(-180,8)
subplot(2,2,4)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
view(-300,8)
%% Rotated separate figures
font = 15;
figure(1)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
xlabel('No. of solar PV arrays');
ylabel('No. of wind turbines');
zlabel('Battery capacity (kWh)');
set(gca,'FontSize',font);
figure(2)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
view(-148,8)
xlabel('No. of solar PV arrays');
ylabel('No. of wind turbines');
zlabel('Battery capacity (kWh)');
set(gca,'FontSize',font);
figure(3)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
view(-180,8)
xlabel('No. of solar PV arrays');
ylabel('No. of wind turbines');
zlabel('Battery capacity (kWh)');
figure(4)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
view(-300,8)
xlabel('No. of solar PV arrays');
ylabel('No. of wind turbines');
zlabel('Battery capacity (kWh)');
set(gca,'FontSize',font);
%%
xlabel('PV capacity (kW)');
ylabel('WT capacity (kW)');
zlabel('BS capacity (kWh)');
%% Rotated separate figures
figure(1)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
xlabel('PV capacity (kW)');
ylabel('WT capacity (kW)');
zlabel('BS capacity (kWh)');
figure(2)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
view(-148,8)
xlabel('PV capacity (kW)');
ylabel('WT capacity (kW)');
zlabel('BS capacity (kWh)');
figure(3)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
view(-180,8)
xlabel('PV capacity (kW)');
ylabel('WT capacity (kW)');
zlabel('BS capacity (kWh)');
figure(4)
surf(XX,YY,ZZ,'LineStyle','none')
hold on
scatter3(x,y,z,'k.');
hold off
view(-300,8)
xlabel('PV capacity (kW)');
ylabel('WT capacity (kW)');
zlabel('BS capacity (kWh)');
%% 2D Pareto front curves for inputs
n_s = x_p(:,1);
n_w = x_p(:,2);
Eb_init = x_p(:,3);
figure(1)
scatter(n_s,n_w,10,'b','filled');
xlabel('PV capacity (kW)');
ylabel('WT capacity (kW)');
figure(2)
scatter(n_s,Eb_init,10,'b','filled');
xlabel('PV capacity (kW)');
ylabel('Battery capacity (kWh)');
figure(3)
scatter(n_w,Eb_init,10,'b','filled');
xlabel('WT capacity (kW)');
ylabel('Battery capacity (kWh)');
%% Pareto set for objectives
LCOE = fval(:,1);
Emissions = fval(:,2);
DPSP = fval(:,3);
Dump = fval(:,4);
%REF = ones(size(fval,1),1) - fval(:,5);
scatter(Dump,Emissions,5,'b','filled')
xlabel('Dump energy ratio');
ylabel('Normalized emissions');
%% fsolve to obtain pareto front of inputs
Cost = @ (x) Objective_LI_DE_v2_1h(x) - 0.1367;
OPTIONS = optimoptions('fsolve','Algorithm','Levenberg-Marquardt',...
'StepTolerance',1e-10);
[X] = fsolve(Cost,[10 3 100],OPTIONS);